terrain.fragment.fx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. precision highp float;
  2. // Constants
  3. uniform vec3 vEyePosition;
  4. uniform vec4 vDiffuseColor;
  5. #ifdef SPECULARTERM
  6. uniform vec4 vSpecularColor;
  7. #endif
  8. // Input
  9. varying vec3 vPositionW;
  10. #ifdef NORMAL
  11. varying vec3 vNormalW;
  12. #endif
  13. #ifdef VERTEXCOLOR
  14. varying vec4 vColor;
  15. #endif
  16. // Helper functions
  17. #include<helperFunctions>
  18. // Lights
  19. #include<__decl__lightFragment>[0..maxSimultaneousLights]
  20. // Samplers
  21. #ifdef DIFFUSE
  22. varying vec2 vTextureUV;
  23. uniform sampler2D textureSampler;
  24. uniform vec2 vTextureInfos;
  25. uniform sampler2D diffuse1Sampler;
  26. uniform sampler2D diffuse2Sampler;
  27. uniform sampler2D diffuse3Sampler;
  28. uniform vec2 diffuse1Infos;
  29. uniform vec2 diffuse2Infos;
  30. uniform vec2 diffuse3Infos;
  31. #endif
  32. #ifdef BUMP
  33. uniform sampler2D bump1Sampler;
  34. uniform sampler2D bump2Sampler;
  35. uniform sampler2D bump3Sampler;
  36. #endif
  37. // Shadows
  38. #include<lightsFragmentFunctions>
  39. #include<shadowsFragmentFunctions>
  40. #include<clipPlaneFragmentDeclaration>
  41. // Fog
  42. #include<fogFragmentDeclaration>
  43. // Bump
  44. #ifdef BUMP
  45. #extension GL_OES_standard_derivatives : enable
  46. // Thanks to http://www.thetenthplanet.de/archives/1180
  47. mat3 cotangent_frame(vec3 normal, vec3 p, vec2 uv)
  48. {
  49. // get edge vectors of the pixel triangle
  50. vec3 dp1 = dFdx(p);
  51. vec3 dp2 = dFdy(p);
  52. vec2 duv1 = dFdx(uv);
  53. vec2 duv2 = dFdy(uv);
  54. // solve the linear system
  55. vec3 dp2perp = cross(dp2, normal);
  56. vec3 dp1perp = cross(normal, dp1);
  57. vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x;
  58. vec3 binormal = dp2perp * duv1.y + dp1perp * duv2.y;
  59. // construct a scale-invariant frame
  60. float invmax = inversesqrt(max(dot(tangent, tangent), dot(binormal, binormal)));
  61. return mat3(tangent * invmax, binormal * invmax, normal);
  62. }
  63. vec3 perturbNormal(vec3 viewDir, vec3 mixColor)
  64. {
  65. vec3 bump1Color = texture2D(bump1Sampler, vTextureUV * diffuse1Infos).xyz;
  66. vec3 bump2Color = texture2D(bump2Sampler, vTextureUV * diffuse2Infos).xyz;
  67. vec3 bump3Color = texture2D(bump3Sampler, vTextureUV * diffuse3Infos).xyz;
  68. bump1Color.rgb *= mixColor.r;
  69. bump2Color.rgb = mix(bump1Color.rgb, bump2Color.rgb, mixColor.g);
  70. vec3 map = mix(bump2Color.rgb, bump3Color.rgb, mixColor.b);
  71. map = map * 255. / 127. - 128. / 127.;
  72. mat3 TBN = cotangent_frame(vNormalW * vTextureInfos.y, -viewDir, vTextureUV);
  73. return normalize(TBN * map);
  74. }
  75. #endif
  76. void main(void) {
  77. // Clip plane
  78. #ifdef CLIPPLANE
  79. if (fClipDistance > 0.0)
  80. discard;
  81. #endif
  82. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  83. // Base color
  84. vec4 baseColor = vec4(1., 1., 1., 1.);
  85. vec3 diffuseColor = vDiffuseColor.rgb;
  86. #ifdef SPECULARTERM
  87. float glossiness = vSpecularColor.a;
  88. vec3 specularColor = vSpecularColor.rgb;
  89. #else
  90. float glossiness = 0.;
  91. #endif
  92. // Alpha
  93. float alpha = vDiffuseColor.a;
  94. // Bump
  95. #ifdef NORMAL
  96. vec3 normalW = normalize(vNormalW);
  97. #else
  98. vec3 normalW = vec3(1.0, 1.0, 1.0);
  99. #endif
  100. #ifdef DIFFUSE
  101. baseColor = texture2D(textureSampler, vTextureUV);
  102. #if defined(BUMP) && defined(DIFFUSE)
  103. normalW = perturbNormal(viewDirectionW, baseColor.rgb);
  104. #endif
  105. #ifdef ALPHATEST
  106. if (baseColor.a < 0.4)
  107. discard;
  108. #endif
  109. #include<depthPrePass>
  110. baseColor.rgb *= vTextureInfos.y;
  111. vec4 diffuse1Color = texture2D(diffuse1Sampler, vTextureUV * diffuse1Infos);
  112. vec4 diffuse2Color = texture2D(diffuse2Sampler, vTextureUV * diffuse2Infos);
  113. vec4 diffuse3Color = texture2D(diffuse3Sampler, vTextureUV * diffuse3Infos);
  114. diffuse1Color.rgb *= baseColor.r;
  115. diffuse2Color.rgb = mix(diffuse1Color.rgb, diffuse2Color.rgb, baseColor.g);
  116. baseColor.rgb = mix(diffuse2Color.rgb, diffuse3Color.rgb, baseColor.b);
  117. #endif
  118. #ifdef VERTEXCOLOR
  119. baseColor.rgb *= vColor.rgb;
  120. #endif
  121. // Lighting
  122. vec3 diffuseBase = vec3(0., 0., 0.);
  123. lightingInfo info;
  124. float shadow = 1.;
  125. #ifdef SPECULARTERM
  126. vec3 specularBase = vec3(0., 0., 0.);
  127. #endif
  128. #include<lightFragment>[0..maxSimultaneousLights]
  129. #ifdef VERTEXALPHA
  130. alpha *= vColor.a;
  131. #endif
  132. #ifdef SPECULARTERM
  133. vec3 finalSpecular = specularBase * specularColor;
  134. #else
  135. vec3 finalSpecular = vec3(0.0);
  136. #endif
  137. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor * baseColor.rgb, 0.0, 1.0);
  138. // Composition
  139. vec4 color = vec4(finalDiffuse + finalSpecular, alpha);
  140. #include<fogFragment>
  141. gl_FragColor = color;
  142. }