VertShader.vsh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. attribute highp vec4 inVertex;
  2. attribute highp vec3 inNormal;
  3. attribute highp vec2 inTexCoord;
  4. uniform highp mat4 MVPMatrix;
  5. uniform highp mat4 ModelViewMatrix;
  6. uniform highp vec3 LightDirection;
  7. // fog uniforms
  8. uniform lowp int iFogMode;
  9. uniform highp float FogDensity;
  10. uniform highp float FogEnd;
  11. uniform highp float FogRcpEndStartDiff;
  12. varying mediump vec2 TexCoord;
  13. varying lowp vec3 DiffuseLight;
  14. varying lowp vec3 FogIntensity;
  15. void main()
  16. {
  17. // transform position to view space as we need the distance to the eye for fog
  18. highp vec3 viewPos = vec3(ModelViewMatrix * inVertex);
  19. highp float eyeDist = length(viewPos);
  20. // transform vertex position
  21. gl_Position = MVPMatrix * inVertex;
  22. // texcoords pass through
  23. TexCoord = inTexCoord;
  24. // calculate lighting
  25. // We use a directional light with direction given in model space
  26. lowp float DiffuseIntensity = dot(inNormal, normalize(LightDirection));
  27. // clamp negative values and add some ambient light
  28. DiffuseLight = vec3(max(DiffuseIntensity, 0.0) * 0.5 + 0.5);
  29. // select fog function. 1 is linear, 2 is exponential, 3 is exponential squared, 0 is no fog.
  30. highp float fogIntensity = 1.0;
  31. if(iFogMode == 1)
  32. {
  33. fogIntensity = (FogEnd - eyeDist) * FogRcpEndStartDiff;
  34. }
  35. else if(iFogMode >= 2)
  36. {
  37. highp float scaledDist = eyeDist * FogDensity;
  38. if (iFogMode == 3)
  39. {
  40. scaledDist *= scaledDist;
  41. }
  42. fogIntensity = exp2(-scaledDist);
  43. }
  44. // clamp the intensity within a valid range
  45. FogIntensity = vec3(clamp(fogIntensity, 0.0, 1.0));
  46. }