water.vertex.fx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. uniform mat4 normalMatrix;
  24. uniform vec2 vNormalInfos;
  25. #endif
  26. #ifdef POINTSIZE
  27. uniform float pointSize;
  28. #endif
  29. // Output
  30. varying vec3 vPositionW;
  31. #ifdef NORMAL
  32. varying vec3 vNormalW;
  33. #endif
  34. #ifdef VERTEXCOLOR
  35. varying vec4 vColor;
  36. #endif
  37. #include<clipPlaneVertexDeclaration>
  38. #include<fogVertexDeclaration>
  39. #include<shadowsVertexDeclaration>[0..maxSimultaneousLights]
  40. // Water uniforms
  41. uniform mat4 worldReflectionViewProjection;
  42. uniform vec2 windDirection;
  43. uniform float waveLength;
  44. uniform float time;
  45. uniform float windForce;
  46. uniform float waveHeight;
  47. uniform float waveSpeed;
  48. // Water varyings
  49. varying vec3 vPosition;
  50. varying vec3 vRefractionMapTexCoord;
  51. varying vec3 vReflectionMapTexCoord;
  52. void main(void) {
  53. #include<instancesVertex>
  54. #include<bonesVertex>
  55. vec4 worldPos = finalWorld * vec4(position, 1.0);
  56. vPositionW = vec3(worldPos);
  57. #ifdef NORMAL
  58. vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));
  59. #endif
  60. // Texture coordinates
  61. #ifndef UV1
  62. vec2 uv = vec2(0., 0.);
  63. #endif
  64. #ifndef UV2
  65. vec2 uv2 = vec2(0., 0.);
  66. #endif
  67. #ifdef BUMP
  68. if (vNormalInfos.x == 0.)
  69. {
  70. vNormalUV = vec2(normalMatrix * vec4((uv * 1.0) / waveLength + time * windForce * windDirection, 1.0, 0.0));
  71. }
  72. else
  73. {
  74. vNormalUV = vec2(normalMatrix * vec4((uv2 * 1.0) / waveLength + time * windForce * windDirection, 1.0, 0.0));
  75. }
  76. #endif
  77. // Clip plane
  78. #include<clipPlaneVertex>
  79. // Fog
  80. #include<fogVertex>
  81. // Shadows
  82. #include<shadowsVertex>[0..maxSimultaneousLights]
  83. // Vertex color
  84. #ifdef VERTEXCOLOR
  85. vColor = color;
  86. #endif
  87. // Point size
  88. #ifdef POINTSIZE
  89. gl_PointSize = pointSize;
  90. #endif
  91. vec3 p = position;
  92. float newY = (sin(((p.x / 0.05) + time * waveSpeed)) * waveHeight * windDirection.x * 5.0)
  93. + (cos(((p.z / 0.05) + time * waveSpeed)) * waveHeight * windDirection.y * 5.0);
  94. p.y += abs(newY);
  95. gl_Position = viewProjection * finalWorld * vec4(p, 1.0);
  96. #ifdef REFLECTION
  97. worldPos = viewProjection * finalWorld * vec4(p, 1.0);
  98. // Water
  99. vPosition = position;
  100. vRefractionMapTexCoord.x = 0.5 * (worldPos.w + worldPos.x);
  101. vRefractionMapTexCoord.y = 0.5 * (worldPos.w + worldPos.y);
  102. vRefractionMapTexCoord.z = worldPos.w;
  103. worldPos = worldReflectionViewProjection * vec4(position, 1.0);
  104. vReflectionMapTexCoord.x = 0.5 * (worldPos.w + worldPos.x);
  105. vReflectionMapTexCoord.y = 0.5 * (worldPos.w + worldPos.y);
  106. vReflectionMapTexCoord.z = worldPos.w;
  107. #endif
  108. }