water.vertex.fx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #ifdef BONES
  17. attribute vec4 matricesIndices;
  18. attribute vec4 matricesWeights;
  19. #endif
  20. // Uniforms
  21. #ifdef INSTANCES
  22. attribute vec4 world0;
  23. attribute vec4 world1;
  24. attribute vec4 world2;
  25. attribute vec4 world3;
  26. #else
  27. uniform mat4 world;
  28. #endif
  29. uniform mat4 view;
  30. uniform mat4 viewProjection;
  31. #ifdef BUMP
  32. varying vec2 vNormalUV;
  33. uniform mat4 normalMatrix;
  34. uniform vec2 vNormalInfos;
  35. #endif
  36. #ifdef BONES
  37. uniform mat4 mBones[BonesPerMesh];
  38. #endif
  39. #ifdef POINTSIZE
  40. uniform float pointSize;
  41. #endif
  42. // Output
  43. varying vec3 vPositionW;
  44. #ifdef NORMAL
  45. varying vec3 vNormalW;
  46. #endif
  47. #ifdef VERTEXCOLOR
  48. varying vec4 vColor;
  49. #endif
  50. #ifdef CLIPPLANE
  51. uniform vec4 vClipPlane;
  52. varying float fClipDistance;
  53. #endif
  54. #ifdef FOG
  55. varying float fFogDistance;
  56. #endif
  57. #ifdef SHADOWS
  58. #if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
  59. uniform mat4 lightMatrix0;
  60. varying vec4 vPositionFromLight0;
  61. #endif
  62. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  63. uniform mat4 lightMatrix1;
  64. varying vec4 vPositionFromLight1;
  65. #endif
  66. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  67. uniform mat4 lightMatrix2;
  68. varying vec4 vPositionFromLight2;
  69. #endif
  70. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  71. uniform mat4 lightMatrix3;
  72. varying vec4 vPositionFromLight3;
  73. #endif
  74. #endif
  75. // Water uniforms
  76. uniform mat4 worldReflectionViewProjection;
  77. uniform vec2 windDirection;
  78. uniform float waveLength;
  79. uniform float time;
  80. uniform float windForce;
  81. uniform float waveHeight;
  82. uniform float waveSpeed;
  83. // Water varyings
  84. varying vec3 vPosition;
  85. varying vec3 vRefractionMapTexCoord;
  86. varying vec3 vReflectionMapTexCoord;
  87. void main(void) {
  88. mat4 finalWorld;
  89. #ifdef INSTANCES
  90. finalWorld = mat4(world0, world1, world2, world3);
  91. #else
  92. finalWorld = world;
  93. #endif
  94. #ifdef BONES
  95. mat4 m0 = mBones[int(matricesIndices.x)] * matricesWeights.x;
  96. mat4 m1 = mBones[int(matricesIndices.y)] * matricesWeights.y;
  97. mat4 m2 = mBones[int(matricesIndices.z)] * matricesWeights.z;
  98. #ifdef BONES4
  99. mat4 m3 = mBones[int(matricesIndices.w)] * matricesWeights.w;
  100. finalWorld = finalWorld * (m0 + m1 + m2 + m3);
  101. #else
  102. finalWorld = finalWorld * (m0 + m1 + m2);
  103. #endif
  104. #endif
  105. vec4 worldPos = finalWorld * vec4(position, 1.0);
  106. vPositionW = vec3(worldPos);
  107. #ifdef NORMAL
  108. vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));
  109. #endif
  110. // Texture coordinates
  111. #ifndef UV1
  112. vec2 uv = vec2(0., 0.);
  113. #endif
  114. #ifndef UV2
  115. vec2 uv2 = vec2(0., 0.);
  116. #endif
  117. #ifdef BUMP
  118. if (vNormalInfos.x == 0.)
  119. {
  120. vNormalUV = vec2(normalMatrix * vec4((uv * 1.0) / waveLength + time * windForce * windDirection, 1.0, 0.0));
  121. }
  122. else
  123. {
  124. vNormalUV = vec2(normalMatrix * vec4((uv2 * 1.0) / waveLength + time * windForce * windDirection, 1.0, 0.0));
  125. }
  126. #endif
  127. // Clip plane
  128. #ifdef CLIPPLANE
  129. fClipDistance = dot(worldPos, vClipPlane);
  130. #endif
  131. // Fog
  132. #ifdef FOG
  133. fFogDistance = (view * worldPos).z;
  134. #endif
  135. // Shadows
  136. #ifdef SHADOWS
  137. #if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
  138. vPositionFromLight0 = lightMatrix0 * worldPos;
  139. #endif
  140. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  141. vPositionFromLight1 = lightMatrix1 * worldPos;
  142. #endif
  143. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  144. vPositionFromLight2 = lightMatrix2 * worldPos;
  145. #endif
  146. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  147. vPositionFromLight3 = lightMatrix3 * worldPos;
  148. #endif
  149. #endif
  150. // Vertex color
  151. #ifdef VERTEXCOLOR
  152. vColor = color;
  153. #endif
  154. // Point size
  155. #ifdef POINTSIZE
  156. gl_PointSize = pointSize;
  157. #endif
  158. vec3 p = position;
  159. float newY = (sin(((p.x / 0.05) + time * waveSpeed)) * waveHeight * windDirection.x * 5.0)
  160. + (cos(((p.z / 0.05) + time * waveSpeed)) * waveHeight * windDirection.y * 5.0);
  161. p.y += abs(newY);
  162. gl_Position = viewProjection * finalWorld * vec4(p, 1.0);
  163. #ifdef REFLECTION
  164. worldPos = viewProjection * finalWorld * vec4(p, 1.0);
  165. // Water
  166. vPosition = position;
  167. vRefractionMapTexCoord.x = 0.5 * (worldPos.w + worldPos.x);
  168. vRefractionMapTexCoord.y = 0.5 * (worldPos.w + worldPos.y);
  169. vRefractionMapTexCoord.z = worldPos.w;
  170. worldPos = worldReflectionViewProjection * vec4(position, 1.0);
  171. vReflectionMapTexCoord.x = 0.5 * (worldPos.w + worldPos.x);
  172. vReflectionMapTexCoord.y = 0.5 * (worldPos.w + worldPos.y);
  173. vReflectionMapTexCoord.z = worldPos.w;
  174. #endif
  175. }