fur.vertex.fx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #include<bonesDeclaration>
  15. // Uniforms
  16. uniform float furLength;
  17. uniform float furAngle;
  18. #ifdef HIGHLEVEL
  19. uniform float furOffset;
  20. uniform vec3 furGravity;
  21. uniform float furTime;
  22. uniform float furSpacing;
  23. uniform float furDensity;
  24. #endif
  25. #ifdef HEIGHTMAP
  26. uniform sampler2D heightTexture;
  27. #endif
  28. #ifdef HIGHLEVEL
  29. varying vec2 vFurUV;
  30. #endif
  31. #include<instancesDeclaration>
  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 POINTSIZE
  40. uniform float pointSize;
  41. #endif
  42. // Output
  43. varying vec3 vPositionW;
  44. #ifdef NORMAL
  45. varying vec3 vNormalW;
  46. #endif
  47. varying float vfur_length;
  48. #ifdef VERTEXCOLOR
  49. varying vec4 vColor;
  50. #endif
  51. #include<clipPlaneVertexDeclaration>
  52. #include<fogVertexDeclaration>
  53. #include<shadowsVertexDeclaration>[0..maxSimultaneousLights]
  54. float Rand(vec3 rv) {
  55. float x = dot(rv, vec3(12.9898,78.233, 24.65487));
  56. return fract(sin(x) * 43758.5453);
  57. }
  58. void main(void) {
  59. #include<instancesVertex>
  60. #include<bonesVertex>
  61. //FUR
  62. float r = Rand(position);
  63. #ifdef HEIGHTMAP
  64. #if __VERSION__ > 100
  65. vfur_length = furLength * texture(heightTexture, uv).x;
  66. #else
  67. vfur_length = furLength * texture2D(heightTexture, uv).r;
  68. #endif
  69. #else
  70. vfur_length = (furLength * r);
  71. #endif
  72. vec3 tangent1 = vec3(normal.y, -normal.x, 0);
  73. vec3 tangent2 = vec3(-normal.z, 0, normal.x);
  74. r = Rand(tangent1 * r);
  75. float J = (2.0 + 4.0 * r);
  76. r = Rand(tangent2*r);
  77. float K = (2.0 + 2.0 * r);
  78. tangent1 = tangent1*J + tangent2 * K;
  79. tangent1 = normalize(tangent1);
  80. vec3 newPosition = position + normal * vfur_length*cos(furAngle) + tangent1 * vfur_length * sin(furAngle);
  81. #ifdef HIGHLEVEL
  82. // Compute fur data passed to the pixel shader
  83. vec3 forceDirection = vec3(0.0, 0.0, 0.0);
  84. forceDirection.x = sin(furTime + position.x * 0.05) * 0.2;
  85. forceDirection.y = cos(furTime * 0.7 + position.y * 0.04) * 0.2;
  86. forceDirection.z = sin(furTime * 0.7 + position.z * 0.04) * 0.2;
  87. vec3 displacement = vec3(0.0, 0.0, 0.0);
  88. displacement = furGravity + forceDirection;
  89. float displacementFactor = pow(furOffset, 3.0);
  90. vec3 aNormal = normal;
  91. aNormal.xyz += displacement * displacementFactor;
  92. newPosition = vec3(newPosition.x, newPosition.y, newPosition.z) + (normalize(aNormal) * furOffset * furSpacing);
  93. #endif
  94. #ifdef NORMAL
  95. #ifdef HIGHLEVEL
  96. vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)) * aNormal);
  97. #else
  98. vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));
  99. #endif
  100. #endif
  101. //END FUR
  102. gl_Position = viewProjection * finalWorld * vec4(newPosition, 1.0);
  103. vec4 worldPos = finalWorld * vec4(newPosition, 1.0);
  104. vPositionW = vec3(worldPos);
  105. // Texture coordinates
  106. #ifndef UV1
  107. vec2 uv = vec2(0., 0.);
  108. #endif
  109. #ifndef UV2
  110. vec2 uv2 = vec2(0., 0.);
  111. #endif
  112. #ifdef DIFFUSE
  113. if (vDiffuseInfos.x == 0.)
  114. {
  115. vDiffuseUV = vec2(diffuseMatrix * vec4(uv, 1.0, 0.0));
  116. }
  117. else
  118. {
  119. vDiffuseUV = vec2(diffuseMatrix * vec4(uv2, 1.0, 0.0));
  120. }
  121. #ifdef HIGHLEVEL
  122. vFurUV = vDiffuseUV * furDensity;
  123. #endif
  124. #else
  125. #ifdef HIGHLEVEL
  126. vFurUV = uv * furDensity;
  127. #endif
  128. #endif
  129. // Clip plane
  130. #include<clipPlaneVertex>
  131. // Fog
  132. #include<fogVertex>
  133. // Shadows
  134. #include<shadowsVertex>[0..maxSimultaneousLights]
  135. // Vertex color
  136. #ifdef VERTEXCOLOR
  137. vColor = color;
  138. #endif
  139. // Point size
  140. #ifdef POINTSIZE
  141. gl_PointSize = pointSize;
  142. #endif
  143. }