water.fragment.fx 6.0 KB

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