terrain.fragment.fx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // Lights
  17. #include<lightFragmentDeclaration>[0..maxSimultaneousLights]
  18. // Samplers
  19. #ifdef DIFFUSE
  20. varying vec2 vTextureUV;
  21. uniform sampler2D textureSampler;
  22. uniform vec2 vTextureInfos;
  23. uniform sampler2D diffuse1Sampler;
  24. uniform sampler2D diffuse2Sampler;
  25. uniform sampler2D diffuse3Sampler;
  26. uniform vec2 diffuse1Infos;
  27. uniform vec2 diffuse2Infos;
  28. uniform vec2 diffuse3Infos;
  29. #endif
  30. #ifdef BUMP
  31. uniform sampler2D bump1Sampler;
  32. uniform sampler2D bump2Sampler;
  33. uniform sampler2D bump3Sampler;
  34. #endif
  35. // Shadows
  36. #include<lightsFragmentFunctions>
  37. #include<shadowsFragmentFunctions>
  38. #include<clipPlaneFragmentDeclaration>
  39. // Fog
  40. #include<fogFragmentDeclaration>
  41. // Bump
  42. #ifdef BUMP
  43. #extension GL_OES_standard_derivatives : enable
  44. // Thanks to http://www.thetenthplanet.de/archives/1180
  45. mat3 cotangent_frame(vec3 normal, vec3 p, vec2 uv)
  46. {
  47. // get edge vectors of the pixel triangle
  48. vec3 dp1 = dFdx(p);
  49. vec3 dp2 = dFdy(p);
  50. vec2 duv1 = dFdx(uv);
  51. vec2 duv2 = dFdy(uv);
  52. // solve the linear system
  53. vec3 dp2perp = cross(dp2, normal);
  54. vec3 dp1perp = cross(normal, dp1);
  55. vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x;
  56. vec3 binormal = dp2perp * duv1.y + dp1perp * duv2.y;
  57. // construct a scale-invariant frame
  58. float invmax = inversesqrt(max(dot(tangent, tangent), dot(binormal, binormal)));
  59. return mat3(tangent * invmax, binormal * invmax, normal);
  60. }
  61. vec3 perturbNormal(vec3 viewDir, vec3 mixColor)
  62. {
  63. vec3 bump1Color = texture2D(bump1Sampler, vTextureUV * diffuse1Infos).xyz;
  64. vec3 bump2Color = texture2D(bump2Sampler, vTextureUV * diffuse2Infos).xyz;
  65. vec3 bump3Color = texture2D(bump3Sampler, vTextureUV * diffuse3Infos).xyz;
  66. bump1Color.rgb *= mixColor.r;
  67. bump2Color.rgb = mix(bump1Color.rgb, bump2Color.rgb, mixColor.g);
  68. vec3 map = mix(bump2Color.rgb, bump3Color.rgb, mixColor.b);
  69. map = map * 255. / 127. - 128. / 127.;
  70. mat3 TBN = cotangent_frame(vNormalW * vTextureInfos.y, -viewDir, vTextureUV);
  71. return normalize(TBN * map);
  72. }
  73. #endif
  74. void main(void) {
  75. // Clip plane
  76. #ifdef CLIPPLANE
  77. if (fClipDistance > 0.0)
  78. discard;
  79. #endif
  80. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  81. // Base color
  82. vec4 baseColor = vec4(1., 1., 1., 1.);
  83. vec3 diffuseColor = vDiffuseColor.rgb;
  84. #ifdef SPECULARTERM
  85. float glossiness = vSpecularColor.a;
  86. vec3 specularColor = vSpecularColor.rgb;
  87. #else
  88. float glossiness = 0.;
  89. #endif
  90. // Alpha
  91. float alpha = vDiffuseColor.a;
  92. // Bump
  93. #ifdef NORMAL
  94. vec3 normalW = normalize(vNormalW);
  95. #else
  96. vec3 normalW = vec3(1.0, 1.0, 1.0);
  97. #endif
  98. #ifdef DIFFUSE
  99. baseColor = texture2D(textureSampler, vTextureUV);
  100. #if defined(BUMP) && defined(DIFFUSE)
  101. normalW = perturbNormal(viewDirectionW, baseColor.rgb);
  102. #endif
  103. #ifdef ALPHATEST
  104. if (baseColor.a < 0.4)
  105. discard;
  106. #endif
  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. }