terrain.fragment.fx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include<clipPlaneFragment>
  79. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  80. // Base color
  81. vec4 baseColor = vec4(1., 1., 1., 1.);
  82. vec3 diffuseColor = vDiffuseColor.rgb;
  83. #ifdef SPECULARTERM
  84. float glossiness = vSpecularColor.a;
  85. vec3 specularColor = vSpecularColor.rgb;
  86. #else
  87. float glossiness = 0.;
  88. #endif
  89. // Alpha
  90. float alpha = vDiffuseColor.a;
  91. // Bump
  92. #ifdef NORMAL
  93. vec3 normalW = normalize(vNormalW);
  94. #else
  95. vec3 normalW = vec3(1.0, 1.0, 1.0);
  96. #endif
  97. #ifdef DIFFUSE
  98. baseColor = texture2D(textureSampler, vTextureUV);
  99. #if defined(BUMP) && defined(DIFFUSE)
  100. normalW = perturbNormal(viewDirectionW, baseColor.rgb);
  101. #endif
  102. #ifdef ALPHATEST
  103. if (baseColor.a < 0.4)
  104. discard;
  105. #endif
  106. #include<depthPrePass>
  107. baseColor.rgb *= vTextureInfos.y;
  108. vec4 diffuse1Color = texture2D(diffuse1Sampler, vTextureUV * diffuse1Infos);
  109. vec4 diffuse2Color = texture2D(diffuse2Sampler, vTextureUV * diffuse2Infos);
  110. vec4 diffuse3Color = texture2D(diffuse3Sampler, vTextureUV * diffuse3Infos);
  111. diffuse1Color.rgb *= baseColor.r;
  112. diffuse2Color.rgb = mix(diffuse1Color.rgb, diffuse2Color.rgb, baseColor.g);
  113. baseColor.rgb = mix(diffuse2Color.rgb, diffuse3Color.rgb, baseColor.b);
  114. #endif
  115. #ifdef VERTEXCOLOR
  116. baseColor.rgb *= vColor.rgb;
  117. #endif
  118. // Lighting
  119. vec3 diffuseBase = vec3(0., 0., 0.);
  120. lightingInfo info;
  121. float shadow = 1.;
  122. #ifdef SPECULARTERM
  123. vec3 specularBase = vec3(0., 0., 0.);
  124. #endif
  125. #include<lightFragment>[0..maxSimultaneousLights]
  126. #ifdef VERTEXALPHA
  127. alpha *= vColor.a;
  128. #endif
  129. #ifdef SPECULARTERM
  130. vec3 finalSpecular = specularBase * specularColor;
  131. #else
  132. vec3 finalSpecular = vec3(0.0);
  133. #endif
  134. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor * baseColor.rgb, 0.0, 1.0);
  135. // Composition
  136. vec4 color = vec4(finalDiffuse + finalSpecular, alpha);
  137. #include<fogFragment>
  138. gl_FragColor = color;
  139. }