lava.vertex.fx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. precision highp float;
  2. // Inputs
  3. uniform float time;
  4. uniform float speed;
  5. // Varying
  6. varying float noise;
  7. // Attributes
  8. attribute vec3 position;
  9. #ifdef NORMAL
  10. attribute vec3 normal;
  11. #endif
  12. #ifdef UV1
  13. attribute vec2 uv;
  14. #endif
  15. #ifdef UV2
  16. attribute vec2 uv2;
  17. #endif
  18. #ifdef VERTEXCOLOR
  19. attribute vec4 color;
  20. #endif
  21. #ifdef BONES
  22. attribute vec4 matricesIndices;
  23. attribute vec4 matricesWeights;
  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 DIFFUSE
  37. varying vec2 vDiffuseUV;
  38. uniform mat4 diffuseMatrix;
  39. uniform vec2 vDiffuseInfos;
  40. #endif
  41. #ifdef BONES
  42. uniform mat4 mBones[BonesPerMesh];
  43. #endif
  44. #ifdef POINTSIZE
  45. uniform float pointSize;
  46. #endif
  47. // Output
  48. varying vec3 vPositionW;
  49. #ifdef NORMAL
  50. varying vec3 vNormalW;
  51. #endif
  52. #ifdef VERTEXCOLOR
  53. varying vec4 vColor;
  54. #endif
  55. #ifdef CLIPPLANE
  56. uniform vec4 vClipPlane;
  57. varying float fClipDistance;
  58. #endif
  59. #ifdef FOG
  60. varying float fFogDistance;
  61. #endif
  62. #ifdef SHADOWS
  63. #if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
  64. uniform mat4 lightMatrix0;
  65. varying vec4 vPositionFromLight0;
  66. #endif
  67. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  68. uniform mat4 lightMatrix1;
  69. varying vec4 vPositionFromLight1;
  70. #endif
  71. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  72. uniform mat4 lightMatrix2;
  73. varying vec4 vPositionFromLight2;
  74. #endif
  75. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  76. uniform mat4 lightMatrix3;
  77. varying vec4 vPositionFromLight3;
  78. #endif
  79. #endif
  80. /* NOISE FUNCTIONS */
  81. ////// ASHIMA webgl noise
  82. ////// https://github.com/ashima/webgl-noise/blob/master/src/classicnoise3D.glsl
  83. vec3 mod289(vec3 x)
  84. {
  85. return x - floor(x * (1.0 / 289.0)) * 289.0;
  86. }
  87. vec4 mod289(vec4 x)
  88. {
  89. return x - floor(x * (1.0 / 289.0)) * 289.0;
  90. }
  91. vec4 permute(vec4 x)
  92. {
  93. return mod289(((x*34.0)+1.0)*x);
  94. }
  95. vec4 taylorInvSqrt(vec4 r)
  96. {
  97. return 1.79284291400159 - 0.85373472095314 * r;
  98. }
  99. vec3 fade(vec3 t) {
  100. return t*t*t*(t*(t*6.0-15.0)+10.0);
  101. }
  102. // Classic Perlin noise, periodic variant
  103. float pnoise(vec3 P, vec3 rep)
  104. {
  105. vec3 Pi0 = mod(floor(P), rep); // Integer part, modulo period
  106. vec3 Pi1 = mod(Pi0 + vec3(1.0), rep); // Integer part + 1, mod period
  107. Pi0 = mod289(Pi0);
  108. Pi1 = mod289(Pi1);
  109. vec3 Pf0 = fract(P); // Fractional part for interpolation
  110. vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0
  111. vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  112. vec4 iy = vec4(Pi0.yy, Pi1.yy);
  113. vec4 iz0 = Pi0.zzzz;
  114. vec4 iz1 = Pi1.zzzz;
  115. vec4 ixy = permute(permute(ix) + iy);
  116. vec4 ixy0 = permute(ixy + iz0);
  117. vec4 ixy1 = permute(ixy + iz1);
  118. vec4 gx0 = ixy0 * (1.0 / 7.0);
  119. vec4 gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5;
  120. gx0 = fract(gx0);
  121. vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0);
  122. vec4 sz0 = step(gz0, vec4(0.0));
  123. gx0 -= sz0 * (step(0.0, gx0) - 0.5);
  124. gy0 -= sz0 * (step(0.0, gy0) - 0.5);
  125. vec4 gx1 = ixy1 * (1.0 / 7.0);
  126. vec4 gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5;
  127. gx1 = fract(gx1);
  128. vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1);
  129. vec4 sz1 = step(gz1, vec4(0.0));
  130. gx1 -= sz1 * (step(0.0, gx1) - 0.5);
  131. gy1 -= sz1 * (step(0.0, gy1) - 0.5);
  132. vec3 g000 = vec3(gx0.x,gy0.x,gz0.x);
  133. vec3 g100 = vec3(gx0.y,gy0.y,gz0.y);
  134. vec3 g010 = vec3(gx0.z,gy0.z,gz0.z);
  135. vec3 g110 = vec3(gx0.w,gy0.w,gz0.w);
  136. vec3 g001 = vec3(gx1.x,gy1.x,gz1.x);
  137. vec3 g101 = vec3(gx1.y,gy1.y,gz1.y);
  138. vec3 g011 = vec3(gx1.z,gy1.z,gz1.z);
  139. vec3 g111 = vec3(gx1.w,gy1.w,gz1.w);
  140. vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
  141. g000 *= norm0.x;
  142. g010 *= norm0.y;
  143. g100 *= norm0.z;
  144. g110 *= norm0.w;
  145. vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
  146. g001 *= norm1.x;
  147. g011 *= norm1.y;
  148. g101 *= norm1.z;
  149. g111 *= norm1.w;
  150. float n000 = dot(g000, Pf0);
  151. float n100 = dot(g100, vec3(Pf1.x, Pf0.yz));
  152. float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z));
  153. float n110 = dot(g110, vec3(Pf1.xy, Pf0.z));
  154. float n001 = dot(g001, vec3(Pf0.xy, Pf1.z));
  155. float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z));
  156. float n011 = dot(g011, vec3(Pf0.x, Pf1.yz));
  157. float n111 = dot(g111, Pf1);
  158. vec3 fade_xyz = fade(Pf0);
  159. vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z);
  160. vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y);
  161. float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);
  162. return 2.2 * n_xyz;
  163. }
  164. /* END FUNCTION */
  165. float turbulence( vec3 p ) {
  166. float w = 100.0;
  167. float t = -.5;
  168. for (float f = 1.0 ; f <= 10.0 ; f++ ){
  169. float power = pow( 2.0, f );
  170. t += abs( pnoise( vec3( power * p ), vec3( 10.0, 10.0, 10.0 ) ) / power );
  171. }
  172. return t;
  173. }
  174. void main(void) {
  175. mat4 finalWorld;
  176. #ifdef INSTANCES
  177. finalWorld = mat4(world0, world1, world2, world3);
  178. #else
  179. finalWorld = world;
  180. #endif
  181. #ifdef BONES
  182. mat4 m0 = mBones[int(matricesIndices.x)] * matricesWeights.x;
  183. mat4 m1 = mBones[int(matricesIndices.y)] * matricesWeights.y;
  184. mat4 m2 = mBones[int(matricesIndices.z)] * matricesWeights.z;
  185. #ifdef BONES4
  186. mat4 m3 = mBones[int(matricesIndices.w)] * matricesWeights.w;
  187. finalWorld = finalWorld * (m0 + m1 + m2 + m3);
  188. #else
  189. finalWorld = finalWorld * (m0 + m1 + m2);
  190. #endif
  191. #endif
  192. // get a turbulent 3d noise using the normal, normal to high freq
  193. noise = 10.0 * -.10 * turbulence( .5 * normal + time*1.15 );
  194. // get a 3d noise using the position, low frequency
  195. float b = 5.0 * pnoise( 0.05 * position +vec3(time*1.025), vec3( 100.0 ) );
  196. // compose both noises
  197. float displacement = - 1.5 * noise + b;
  198. // move the position along the normal and transform it
  199. vec3 newPosition = position + normal * displacement;
  200. gl_Position = viewProjection * finalWorld * vec4( newPosition, 1.0 );
  201. vec4 worldPos = finalWorld * vec4(newPosition, 1.0);
  202. vPositionW = vec3(worldPos);
  203. #ifdef NORMAL
  204. vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));
  205. #endif
  206. // Texture coordinates
  207. #ifndef UV1
  208. vec2 uv = vec2(0., 0.);
  209. #endif
  210. #ifndef UV2
  211. vec2 uv2 = vec2(0., 0.);
  212. #endif
  213. #ifdef DIFFUSE
  214. if (vDiffuseInfos.x == 0.)
  215. {
  216. vDiffuseUV = vec2(diffuseMatrix * vec4(uv, 1.0, 0.0));
  217. }
  218. else
  219. {
  220. vDiffuseUV = vec2(diffuseMatrix * vec4(uv2, 1.0, 0.0));
  221. }
  222. #endif
  223. // Clip plane
  224. #ifdef CLIPPLANE
  225. fClipDistance = dot(worldPos, vClipPlane);
  226. #endif
  227. // Fog
  228. #ifdef FOG
  229. fFogDistance = (view * worldPos).z;
  230. #endif
  231. // Shadows
  232. #ifdef SHADOWS
  233. #if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
  234. vPositionFromLight0 = lightMatrix0 * worldPos;
  235. #endif
  236. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  237. vPositionFromLight1 = lightMatrix1 * worldPos;
  238. #endif
  239. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  240. vPositionFromLight2 = lightMatrix2 * worldPos;
  241. #endif
  242. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  243. vPositionFromLight3 = lightMatrix3 * worldPos;
  244. #endif
  245. #endif
  246. // Vertex color
  247. #ifdef VERTEXCOLOR
  248. vColor = color;
  249. #endif
  250. // Point size
  251. #ifdef POINTSIZE
  252. gl_PointSize = pointSize;
  253. #endif
  254. }