fur.vertex.fx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. precision highp float;
  2. // Attributes
  3. attribute vec3 position;
  4. attribute vec3 normal;
  5. #ifdef UV1
  6. attribute vec2 uv;
  7. #endif
  8. #ifdef UV2
  9. attribute vec2 uv2;
  10. #endif
  11. #ifdef VERTEXCOLOR
  12. attribute vec4 color;
  13. #endif
  14. #ifdef BONES
  15. attribute vec4 matricesIndices;
  16. attribute vec4 matricesWeights;
  17. #endif
  18. // Uniforms
  19. uniform float furLength;
  20. uniform float furAngle;
  21. #ifdef HIGHLEVEL
  22. uniform float furOffset;
  23. uniform vec3 furGravity;
  24. uniform float furTime;
  25. uniform float furSpacing;
  26. #endif
  27. #ifdef HEIGHTMAP
  28. uniform sampler2D heightTexture;
  29. #endif
  30. #ifdef HIGHLEVEL
  31. varying vec2 vFurUV;
  32. #endif
  33. #ifdef INSTANCES
  34. attribute vec4 world0;
  35. attribute vec4 world1;
  36. attribute vec4 world2;
  37. attribute vec4 world3;
  38. #else
  39. uniform mat4 world;
  40. #endif
  41. uniform mat4 view;
  42. uniform mat4 viewProjection;
  43. #ifdef DIFFUSE
  44. varying vec2 vDiffuseUV;
  45. uniform mat4 diffuseMatrix;
  46. uniform vec2 vDiffuseInfos;
  47. #endif
  48. #ifdef BONES
  49. uniform mat4 mBones[BonesPerMesh];
  50. #endif
  51. #ifdef POINTSIZE
  52. uniform float pointSize;
  53. #endif
  54. // Output
  55. varying vec3 vPositionW;
  56. #ifdef NORMAL
  57. varying vec3 vNormalW;
  58. #endif
  59. varying float vfur_length;
  60. #ifdef VERTEXCOLOR
  61. varying vec4 vColor;
  62. #endif
  63. #ifdef CLIPPLANE
  64. uniform vec4 vClipPlane;
  65. varying float fClipDistance;
  66. #endif
  67. #ifdef FOG
  68. varying float fFogDistance;
  69. #endif
  70. #ifdef SHADOWS
  71. #if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
  72. uniform mat4 lightMatrix0;
  73. varying vec4 vPositionFromLight0;
  74. #endif
  75. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  76. uniform mat4 lightMatrix1;
  77. varying vec4 vPositionFromLight1;
  78. #endif
  79. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  80. uniform mat4 lightMatrix2;
  81. varying vec4 vPositionFromLight2;
  82. #endif
  83. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  84. uniform mat4 lightMatrix3;
  85. varying vec4 vPositionFromLight3;
  86. #endif
  87. #endif
  88. float Rand(vec3 rv) {
  89. float x = dot(rv, vec3(12.9898,78.233, 24.65487));
  90. return fract(sin(x) * 43758.5453);
  91. }
  92. void main(void) {
  93. mat4 finalWorld;
  94. #ifdef INSTANCES
  95. finalWorld = mat4(world0, world1, world2, world3);
  96. #else
  97. finalWorld = world;
  98. #endif
  99. #ifdef BONES
  100. mat4 m0 = mBones[int(matricesIndices.x)] * matricesWeights.x;
  101. mat4 m1 = mBones[int(matricesIndices.y)] * matricesWeights.y;
  102. mat4 m2 = mBones[int(matricesIndices.z)] * matricesWeights.z;
  103. #ifdef BONES4
  104. mat4 m3 = mBones[int(matricesIndices.w)] * matricesWeights.w;
  105. finalWorld = finalWorld * (m0 + m1 + m2 + m3);
  106. #else
  107. finalWorld = finalWorld * (m0 + m1 + m2);
  108. #endif
  109. #endif
  110. //FUR
  111. float r = Rand(position);
  112. #ifdef HEIGHTMAP
  113. vfur_length = furLength * texture2D(heightTexture, uv).rgb.x;
  114. #else
  115. vfur_length = (furLength * r);
  116. #endif
  117. vec3 tangent1 = vec3(normal.y, -normal.x, 0);
  118. vec3 tangent2 = vec3(-normal.z, 0, normal.x);
  119. r = Rand(tangent1 * r);
  120. float J = (2.0 + 4.0 * r);
  121. r = Rand(tangent2*r);
  122. float K = (2.0 + 2.0 * r);
  123. tangent1 = tangent1*J + tangent2 * K;
  124. tangent1 = normalize(tangent1);
  125. vec3 newPosition = position + normal * vfur_length*cos(furAngle) + tangent1 * vfur_length * sin(furAngle);
  126. #ifdef HIGHLEVEL
  127. // Compute fur data passed to the pixel shader
  128. vec3 forceDirection = vec3(0.0, 0.0, 0.0);
  129. forceDirection.x = sin(furTime + position.x * 0.05) * 0.2;
  130. forceDirection.y = cos(furTime * 0.7 + position.y * 0.04) * 0.2;
  131. forceDirection.z = sin(furTime * 0.7 + position.z * 0.04) * 0.2;
  132. vec3 displacement = vec3(0.0, 0.0, 0.0);
  133. displacement = furGravity + forceDirection;
  134. float displacementFactor = pow(furOffset, 3.0);
  135. vec3 aNormal = normal;
  136. aNormal.xyz += displacement * displacementFactor;
  137. newPosition = vec3(newPosition.x, newPosition.y, newPosition.z) + (normalize(aNormal) * furOffset * furSpacing);
  138. #endif
  139. #ifdef NORMAL
  140. #ifdef HIGHLEVEL
  141. vNormalW = normalize(normal * aNormal);
  142. #else
  143. vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));
  144. #endif
  145. #endif
  146. //END FUR
  147. gl_Position = viewProjection * finalWorld * vec4(newPosition, 1.0);
  148. vec4 worldPos = finalWorld * vec4(newPosition, 1.0);
  149. vPositionW = vec3(worldPos);
  150. // Texture coordinates
  151. #ifndef UV1
  152. vec2 uv = vec2(0., 0.);
  153. #endif
  154. #ifndef UV2
  155. vec2 uv2 = vec2(0., 0.);
  156. #endif
  157. #ifdef HIGHLEVEL
  158. vFurUV = uv * 20.0;
  159. #endif
  160. #ifdef DIFFUSE
  161. if (vDiffuseInfos.x == 0.)
  162. {
  163. vDiffuseUV = vec2(diffuseMatrix * vec4(uv, 1.0, 0.0));
  164. }
  165. else
  166. {
  167. vDiffuseUV = vec2(diffuseMatrix * vec4(uv2, 1.0, 0.0));
  168. }
  169. #endif
  170. // Clip plane
  171. #ifdef CLIPPLANE
  172. fClipDistance = dot(worldPos, vClipPlane);
  173. #endif
  174. // Fog
  175. #ifdef FOG
  176. fFogDistance = (view * worldPos).z;
  177. #endif
  178. // Shadows
  179. #ifdef SHADOWS
  180. #if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
  181. vPositionFromLight0 = lightMatrix0 * worldPos;
  182. #endif
  183. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  184. vPositionFromLight1 = lightMatrix1 * worldPos;
  185. #endif
  186. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  187. vPositionFromLight2 = lightMatrix2 * worldPos;
  188. #endif
  189. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  190. vPositionFromLight3 = lightMatrix3 * worldPos;
  191. #endif
  192. #endif
  193. // Vertex color
  194. #ifdef VERTEXCOLOR
  195. vColor = color;
  196. #endif
  197. // Point size
  198. #ifdef POINTSIZE
  199. gl_PointSize = pointSize;
  200. #endif
  201. }