water.fragment.fx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #ifdef LOGARITHMICDEPTH
  2. #extension GL_EXT_frag_depth : enable
  3. #endif
  4. precision highp float;
  5. // Constants
  6. uniform vec3 vEyePosition;
  7. uniform vec4 vDiffuseColor;
  8. #ifdef SPECULARTERM
  9. uniform vec4 vSpecularColor;
  10. #endif
  11. // Input
  12. varying vec3 vPositionW;
  13. #ifdef NORMAL
  14. varying vec3 vNormalW;
  15. #endif
  16. #ifdef VERTEXCOLOR
  17. varying vec4 vColor;
  18. #endif
  19. // Helper functions
  20. #include<helperFunctions>
  21. // Lights
  22. #include<__decl__lightFragment>[0..maxSimultaneousLights]
  23. #include<lightsFragmentFunctions>
  24. #include<shadowsFragmentFunctions>
  25. // Samplers
  26. #ifdef BUMP
  27. varying vec2 vNormalUV;
  28. varying vec2 vNormalUV2;
  29. uniform sampler2D normalSampler;
  30. uniform vec2 vNormalInfos;
  31. #endif
  32. uniform sampler2D refractionSampler;
  33. uniform sampler2D reflectionSampler;
  34. // Water uniforms
  35. const float LOG2 = 1.442695;
  36. uniform vec3 cameraPosition;
  37. uniform vec4 waterColor;
  38. uniform float colorBlendFactor;
  39. uniform vec4 waterColor2;
  40. uniform float colorBlendFactor2;
  41. uniform float bumpHeight;
  42. uniform float time;
  43. // Water varyings
  44. varying vec3 vRefractionMapTexCoord;
  45. varying vec3 vReflectionMapTexCoord;
  46. varying vec3 vPosition;
  47. #include<clipPlaneFragmentDeclaration>
  48. #include<logDepthDeclaration>
  49. // Fog
  50. #include<fogFragmentDeclaration>
  51. void main(void) {
  52. // Clip plane
  53. #include<clipPlaneFragment>
  54. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  55. // Base color
  56. vec4 baseColor = vec4(1., 1., 1., 1.);
  57. vec3 diffuseColor = vDiffuseColor.rgb;
  58. // Alpha
  59. float alpha = vDiffuseColor.a;
  60. #ifdef BUMP
  61. #ifdef BUMPSUPERIMPOSE
  62. baseColor = 0.6 * texture2D(normalSampler, vNormalUV) + 0.4 * texture2D(normalSampler,vec2(vNormalUV2.x,vNormalUV2.y));
  63. #else
  64. baseColor = texture2D(normalSampler, vNormalUV);
  65. #endif
  66. vec3 bumpColor = baseColor.rgb;
  67. #ifdef ALPHATEST
  68. if (baseColor.a < 0.4)
  69. discard;
  70. #endif
  71. baseColor.rgb *= vNormalInfos.y;
  72. #else
  73. vec3 bumpColor = vec3(1.0);
  74. #endif
  75. #ifdef VERTEXCOLOR
  76. baseColor.rgb *= vColor.rgb;
  77. #endif
  78. // Bump
  79. #ifdef NORMAL
  80. vec2 perturbation = bumpHeight * (baseColor.rg - 0.5);
  81. #ifdef BUMPAFFECTSREFLECTION
  82. vec3 normalW = normalize(vNormalW + vec3(perturbation.x*8.0,0.0,perturbation.y*8.0));
  83. if (normalW.y<0.0) {
  84. normalW.y = -normalW.y;
  85. }
  86. #else
  87. vec3 normalW = normalize(vNormalW);
  88. #endif
  89. #else
  90. vec3 normalW = vec3(1.0, 1.0, 1.0);
  91. vec2 perturbation = bumpHeight * (vec2(1.0, 1.0) - 0.5);
  92. #endif
  93. #ifdef FRESNELSEPARATE
  94. #ifdef REFLECTION
  95. // Water
  96. vec3 eyeVector = normalize(vEyePosition - vPosition);
  97. vec2 projectedRefractionTexCoords = clamp(vRefractionMapTexCoord.xy / vRefractionMapTexCoord.z + perturbation*0.5, 0.0, 1.0);
  98. vec4 refractiveColor = texture2D(refractionSampler, projectedRefractionTexCoords);
  99. vec2 projectedReflectionTexCoords = clamp(vec2(
  100. vReflectionMapTexCoord.x / vReflectionMapTexCoord.z + perturbation.x * 0.3,
  101. vReflectionMapTexCoord.y / vReflectionMapTexCoord.z + perturbation.y
  102. ),0.0, 1.0);
  103. vec4 reflectiveColor = texture2D(reflectionSampler, projectedReflectionTexCoords);
  104. vec3 upVector = vec3(0.0, 1.0, 0.0);
  105. float fresnelTerm = clamp(abs(pow(dot(eyeVector, upVector),3.0)),0.05,0.65);
  106. float IfresnelTerm = 1.0 - fresnelTerm;
  107. refractiveColor = colorBlendFactor*waterColor + (1.0-colorBlendFactor)*refractiveColor;
  108. reflectiveColor = IfresnelTerm*colorBlendFactor2*waterColor + (1.0-colorBlendFactor2*IfresnelTerm)*reflectiveColor;
  109. vec4 combinedColor = refractiveColor * fresnelTerm + reflectiveColor * IfresnelTerm;
  110. baseColor = combinedColor;
  111. #endif
  112. // Lighting
  113. vec3 diffuseBase = vec3(0., 0., 0.);
  114. lightingInfo info;
  115. float shadow = 1.;
  116. #ifdef SPECULARTERM
  117. float glossiness = vSpecularColor.a;
  118. vec3 specularBase = vec3(0., 0., 0.);
  119. vec3 specularColor = vSpecularColor.rgb;
  120. #else
  121. float glossiness = 0.;
  122. #endif
  123. #include<lightFragment>[0..maxSimultaneousLights]
  124. vec3 finalDiffuse = clamp(baseColor.rgb, 0.0, 1.0);
  125. #ifdef VERTEXALPHA
  126. alpha *= vColor.a;
  127. #endif
  128. #ifdef SPECULARTERM
  129. vec3 finalSpecular = specularBase * specularColor;
  130. #else
  131. vec3 finalSpecular = vec3(0.0);
  132. #endif
  133. #else // !FRESNELSEPARATE
  134. #ifdef REFLECTION
  135. // Water
  136. vec3 eyeVector = normalize(vEyePosition - vPosition);
  137. vec2 projectedRefractionTexCoords = clamp(vRefractionMapTexCoord.xy / vRefractionMapTexCoord.z + perturbation, 0.0, 1.0);
  138. vec4 refractiveColor = texture2D(refractionSampler, projectedRefractionTexCoords);
  139. vec2 projectedReflectionTexCoords = clamp(vReflectionMapTexCoord.xy / vReflectionMapTexCoord.z + perturbation, 0.0, 1.0);
  140. vec4 reflectiveColor = texture2D(reflectionSampler, projectedReflectionTexCoords);
  141. vec3 upVector = vec3(0.0, 1.0, 0.0);
  142. float fresnelTerm = max(dot(eyeVector, upVector), 0.0);
  143. vec4 combinedColor = refractiveColor * fresnelTerm + reflectiveColor * (1.0 - fresnelTerm);
  144. baseColor = colorBlendFactor * waterColor + (1.0 - colorBlendFactor) * combinedColor;
  145. #endif
  146. // Lighting
  147. vec3 diffuseBase = vec3(0., 0., 0.);
  148. lightingInfo info;
  149. float shadow = 1.;
  150. #ifdef SPECULARTERM
  151. float glossiness = vSpecularColor.a;
  152. vec3 specularBase = vec3(0., 0., 0.);
  153. vec3 specularColor = vSpecularColor.rgb;
  154. #else
  155. float glossiness = 0.;
  156. #endif
  157. #include<lightFragment>[0..maxSimultaneousLights]
  158. vec3 finalDiffuse = clamp(baseColor.rgb, 0.0, 1.0);
  159. #ifdef VERTEXALPHA
  160. alpha *= vColor.a;
  161. #endif
  162. #ifdef SPECULARTERM
  163. vec3 finalSpecular = specularBase * specularColor;
  164. #else
  165. vec3 finalSpecular = vec3(0.0);
  166. #endif
  167. #endif
  168. // Composition
  169. vec4 color = vec4(finalDiffuse + finalSpecular, alpha);
  170. #include<logDepthFragment>
  171. #include<fogFragment>
  172. gl_FragColor = color;
  173. }