fur.vertex.fx 5.0 KB

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