water.vertex.fx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. precision highp float;
  2. // Attributes
  3. attribute vec3 position;
  4. #ifdef NORMAL
  5. attribute vec3 normal;
  6. #endif
  7. #ifdef UV1
  8. attribute vec2 uv;
  9. #endif
  10. #ifdef UV2
  11. attribute vec2 uv2;
  12. #endif
  13. #ifdef VERTEXCOLOR
  14. attribute vec4 color;
  15. #endif
  16. #include<bonesDeclaration>
  17. // Uniforms
  18. #include<instancesDeclaration>
  19. uniform mat4 view;
  20. uniform mat4 viewProjection;
  21. #ifdef BUMP
  22. varying vec2 vNormalUV;
  23. #ifdef BUMPSUPERIMPOSE
  24. varying vec2 vNormalUV2;
  25. #endif
  26. uniform mat4 normalMatrix;
  27. uniform vec2 vNormalInfos;
  28. #endif
  29. #ifdef POINTSIZE
  30. uniform float pointSize;
  31. #endif
  32. // Output
  33. varying vec3 vPositionW;
  34. #ifdef NORMAL
  35. varying vec3 vNormalW;
  36. #endif
  37. #ifdef VERTEXCOLOR
  38. varying vec4 vColor;
  39. #endif
  40. #include<clipPlaneVertexDeclaration>
  41. #include<fogVertexDeclaration>
  42. #include<__decl__lightFragment>[0..maxSimultaneousLights]
  43. #include<logDepthDeclaration>
  44. // Water uniforms
  45. uniform mat4 worldReflectionViewProjection;
  46. uniform vec2 windDirection;
  47. uniform float waveLength;
  48. uniform float time;
  49. uniform float windForce;
  50. uniform float waveHeight;
  51. uniform float waveSpeed;
  52. // Water varyings
  53. varying vec3 vPosition;
  54. varying vec3 vRefractionMapTexCoord;
  55. varying vec3 vReflectionMapTexCoord;
  56. void main(void) {
  57. #include<instancesVertex>
  58. #include<bonesVertex>
  59. vec4 worldPos = finalWorld * vec4(position, 1.0);
  60. vPositionW = vec3(worldPos);
  61. #ifdef NORMAL
  62. vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));
  63. #endif
  64. // Texture coordinates
  65. #ifndef UV1
  66. vec2 uv = vec2(0., 0.);
  67. #endif
  68. #ifndef UV2
  69. vec2 uv2 = vec2(0., 0.);
  70. #endif
  71. #ifdef BUMP
  72. if (vNormalInfos.x == 0.)
  73. {
  74. vNormalUV = vec2(normalMatrix * vec4((uv * 1.0) / waveLength + time * windForce * windDirection, 1.0, 0.0));
  75. #ifdef BUMPSUPERIMPOSE
  76. vNormalUV2 = vec2(normalMatrix * vec4((uv * 0.721) / waveLength + time * 1.2 * windForce * windDirection, 1.0, 0.0));
  77. #endif
  78. }
  79. else
  80. {
  81. vNormalUV = vec2(normalMatrix * vec4((uv2 * 1.0) / waveLength + time * windForce * windDirection , 1.0, 0.0));
  82. #ifdef BUMPSUPERIMPOSE
  83. vNormalUV2 = vec2(normalMatrix * vec4((uv2 * 0.721) / waveLength + time * 1.2 * windForce * windDirection , 1.0, 0.0));
  84. #endif
  85. }
  86. #endif
  87. // Clip plane
  88. #include<clipPlaneVertex>
  89. // Fog
  90. #include<fogVertex>
  91. // Shadows
  92. #include<shadowsVertex>[0..maxSimultaneousLights]
  93. // Vertex color
  94. #ifdef VERTEXCOLOR
  95. vColor = color;
  96. #endif
  97. // Point size
  98. #ifdef POINTSIZE
  99. gl_PointSize = pointSize;
  100. #endif
  101. vec3 p = position;
  102. float newY = (sin(((p.x / 0.05) + time * waveSpeed)) * waveHeight * windDirection.x * 5.0)
  103. + (cos(((p.z / 0.05) + time * waveSpeed)) * waveHeight * windDirection.y * 5.0);
  104. p.y += abs(newY);
  105. gl_Position = viewProjection * finalWorld * vec4(p, 1.0);
  106. #ifdef REFLECTION
  107. worldPos = viewProjection * finalWorld * vec4(p, 1.0);
  108. // Water
  109. vPosition = position;
  110. vRefractionMapTexCoord.x = 0.5 * (worldPos.w + worldPos.x);
  111. vRefractionMapTexCoord.y = 0.5 * (worldPos.w + worldPos.y);
  112. vRefractionMapTexCoord.z = worldPos.w;
  113. worldPos = worldReflectionViewProjection * vec4(position, 1.0);
  114. vReflectionMapTexCoord.x = 0.5 * (worldPos.w + worldPos.x);
  115. vReflectionMapTexCoord.y = 0.5 * (worldPos.w + worldPos.y);
  116. vReflectionMapTexCoord.z = worldPos.w;
  117. #endif
  118. #include<logDepthVertex>
  119. }