water.vertex.fx 5.0 KB

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