fur.vertex.fx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. vfur_length = furLength * texture2D(heightTexture, uv).rgb.x;
  65. #else
  66. vfur_length = (furLength * r);
  67. #endif
  68. vec3 tangent1 = vec3(normal.y, -normal.x, 0);
  69. vec3 tangent2 = vec3(-normal.z, 0, normal.x);
  70. r = Rand(tangent1 * r);
  71. float J = (2.0 + 4.0 * r);
  72. r = Rand(tangent2*r);
  73. float K = (2.0 + 2.0 * r);
  74. tangent1 = tangent1*J + tangent2 * K;
  75. tangent1 = normalize(tangent1);
  76. vec3 newPosition = position + normal * vfur_length*cos(furAngle) + tangent1 * vfur_length * sin(furAngle);
  77. #ifdef HIGHLEVEL
  78. // Compute fur data passed to the pixel shader
  79. vec3 forceDirection = vec3(0.0, 0.0, 0.0);
  80. forceDirection.x = sin(furTime + position.x * 0.05) * 0.2;
  81. forceDirection.y = cos(furTime * 0.7 + position.y * 0.04) * 0.2;
  82. forceDirection.z = sin(furTime * 0.7 + position.z * 0.04) * 0.2;
  83. vec3 displacement = vec3(0.0, 0.0, 0.0);
  84. displacement = furGravity + forceDirection;
  85. float displacementFactor = pow(furOffset, 3.0);
  86. vec3 aNormal = normal;
  87. aNormal.xyz += displacement * displacementFactor;
  88. newPosition = vec3(newPosition.x, newPosition.y, newPosition.z) + (normalize(aNormal) * furOffset * furSpacing);
  89. #endif
  90. #ifdef NORMAL
  91. #ifdef HIGHLEVEL
  92. vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)) * aNormal);
  93. #else
  94. vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));
  95. #endif
  96. #endif
  97. //END FUR
  98. gl_Position = viewProjection * finalWorld * vec4(newPosition, 1.0);
  99. vec4 worldPos = finalWorld * vec4(newPosition, 1.0);
  100. vPositionW = vec3(worldPos);
  101. // Texture coordinates
  102. #ifndef UV1
  103. vec2 uv = vec2(0., 0.);
  104. #endif
  105. #ifndef UV2
  106. vec2 uv2 = vec2(0., 0.);
  107. #endif
  108. #ifdef DIFFUSE
  109. if (vDiffuseInfos.x == 0.)
  110. {
  111. vDiffuseUV = vec2(diffuseMatrix * vec4(uv, 1.0, 0.0));
  112. }
  113. else
  114. {
  115. vDiffuseUV = vec2(diffuseMatrix * vec4(uv2, 1.0, 0.0));
  116. }
  117. #ifdef HIGHLEVEL
  118. vFurUV = vDiffuseUV * furDensity;
  119. #endif
  120. #else
  121. #ifdef HIGHLEVEL
  122. vFurUV = uv * furDensity;
  123. #endif
  124. #endif
  125. // Clip plane
  126. #include<clipPlaneVertex>
  127. // Fog
  128. #include<fogVertex>
  129. // Shadows
  130. #include<shadowsVertex>[0..maxSimultaneousLights]
  131. // Vertex color
  132. #ifdef VERTEXCOLOR
  133. vColor = color;
  134. #endif
  135. // Point size
  136. #ifdef POINTSIZE
  137. gl_PointSize = pointSize;
  138. #endif
  139. }