fur.vertex.fx 3.9 KB

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