water.vertex.fx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #ifdef LIGHT0
  59. uniform mat4 lightMatrix0;
  60. varying vec4 vPositionFromLight0;
  61. #endif
  62. #ifdef LIGHT1
  63. uniform mat4 lightMatrix1;
  64. varying vec4 vPositionFromLight1;
  65. #endif
  66. #ifdef LIGHT2
  67. uniform mat4 lightMatrix2;
  68. varying vec4 vPositionFromLight2;
  69. #endif
  70. #ifdef LIGHT3
  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 bumpHeight;
  82. uniform float waveHeight;
  83. // Water varyings
  84. varying vec3 vPosition;
  85. varying vec3 vRefractionMapTexCoord;
  86. varying vec3 vReflectionMapTexCoord;
  87. varying float vWaveHeight;
  88. void main(void) {
  89. mat4 finalWorld;
  90. #ifdef INSTANCES
  91. finalWorld = mat4(world0, world1, world2, world3);
  92. #else
  93. finalWorld = world;
  94. #endif
  95. #ifdef BONES
  96. mat4 m0 = mBones[int(matricesIndices.x)] * matricesWeights.x;
  97. mat4 m1 = mBones[int(matricesIndices.y)] * matricesWeights.y;
  98. mat4 m2 = mBones[int(matricesIndices.z)] * matricesWeights.z;
  99. #ifdef BONES4
  100. mat4 m3 = mBones[int(matricesIndices.w)] * matricesWeights.w;
  101. finalWorld = finalWorld * (m0 + m1 + m2 + m3);
  102. #else
  103. finalWorld = finalWorld * (m0 + m1 + m2);
  104. #endif
  105. #endif
  106. vec4 worldPos = finalWorld * vec4(position, 1.0);
  107. vPositionW = vec3(worldPos);
  108. #ifdef NORMAL
  109. vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));
  110. #endif
  111. // Texture coordinates
  112. #ifndef UV1
  113. vec2 uv = vec2(0., 0.);
  114. #endif
  115. #ifndef UV2
  116. vec2 uv2 = vec2(0., 0.);
  117. #endif
  118. #ifdef BUMP
  119. if (vNormalInfos.x == 0.)
  120. {
  121. vNormalUV = vec2(normalMatrix * vec4((uv * 1.0) / waveLength + time * windForce * windDirection, 1.0, 0.0));
  122. }
  123. else
  124. {
  125. vNormalUV = vec2(normalMatrix * vec4((uv2 * 1.0) / waveLength + time * windForce * windDirection, 1.0, 0.0));
  126. }
  127. #endif
  128. // Clip plane
  129. #ifdef CLIPPLANE
  130. fClipDistance = dot(worldPos, vClipPlane);
  131. #endif
  132. // Fog
  133. #ifdef FOG
  134. fFogDistance = (view * worldPos).z;
  135. #endif
  136. // Shadows
  137. #ifdef SHADOWS
  138. #ifdef LIGHT0
  139. vPositionFromLight0 = lightMatrix0 * worldPos;
  140. #endif
  141. #ifdef LIGHT1
  142. vPositionFromLight1 = lightMatrix1 * worldPos;
  143. #endif
  144. #ifdef LIGHT2
  145. vPositionFromLight2 = lightMatrix2 * worldPos;
  146. #endif
  147. #ifdef LIGHT3
  148. vPositionFromLight3 = lightMatrix3 * worldPos;
  149. #endif
  150. #endif
  151. // Vertex color
  152. #ifdef VERTEXCOLOR
  153. vColor = color;
  154. #endif
  155. // Point size
  156. #ifdef POINTSIZE
  157. gl_PointSize = pointSize;
  158. #endif
  159. vec3 p = position;
  160. p.y += (sin(((p.x / 0.05) + time * 100.0)) * waveHeight * 5.0) + (cos(((p.z / 0.05) + time * 100.0)) * waveHeight * 5.0);
  161. gl_Position = viewProjection * finalWorld * vec4(p, 1.0);;
  162. worldPos = viewProjection * finalWorld * vec4(position, 1.0);
  163. // Water
  164. vWaveHeight = bumpHeight;
  165. vPosition = position;
  166. vRefractionMapTexCoord.x = 0.5 * (worldPos.w + worldPos.x);
  167. vRefractionMapTexCoord.y = 0.5 * (worldPos.w + worldPos.y);
  168. vRefractionMapTexCoord.z = worldPos.w;
  169. worldPos = worldReflectionViewProjection * vec4(position, 1.0);
  170. vReflectionMapTexCoord.x = 0.5 * (worldPos.w + worldPos.x);
  171. vReflectionMapTexCoord.y = 0.5 * (worldPos.w + worldPos.y);
  172. vReflectionMapTexCoord.z = worldPos.w;
  173. }