particles.vertex.fx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Attributes
  2. attribute vec3 position;
  3. attribute vec4 color;
  4. attribute float angle;
  5. attribute vec2 offset;
  6. attribute vec2 size;
  7. #ifdef ANIMATESHEET
  8. attribute float cellIndex;
  9. #endif
  10. #ifndef BILLBOARD
  11. attribute vec3 direction;
  12. #endif
  13. // Uniforms
  14. uniform mat4 view;
  15. uniform mat4 projection;
  16. #ifdef ANIMATESHEET
  17. uniform vec3 particlesInfos; // x (number of rows) y(number of columns) z(rowSize)
  18. #endif
  19. // Output
  20. varying vec2 vUV;
  21. varying vec4 vColor;
  22. #ifdef CLIPPLANE
  23. uniform vec4 vClipPlane;
  24. uniform mat4 invView;
  25. varying float fClipDistance;
  26. #endif
  27. void main(void) {
  28. vec2 cornerPos;
  29. cornerPos = vec2(offset.x - 0.5, offset.y - 0.5) * size;
  30. #ifdef BILLBOARD
  31. // Rotate
  32. vec3 rotatedCorner;
  33. rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
  34. rotatedCorner.y = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
  35. rotatedCorner.z = 0.;
  36. vec3 viewPos = (view * vec4(position, 1.0)).xyz + rotatedCorner;
  37. // Position
  38. gl_Position = projection * vec4(viewPos, 1.0);
  39. #else
  40. // Rotate
  41. vec3 rotatedCorner;
  42. rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
  43. rotatedCorner.z = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
  44. rotatedCorner.y = 0.;
  45. vec3 worldPos = position + rotatedCorner;
  46. vec3 yaxis = normalize(direction);
  47. vec3 xaxis = normalize(cross(vec3(0., 1.0, 0.), yaxis));
  48. vec3 zaxis = normalize(cross(yaxis, xaxis));
  49. vec4 row0 = vec4(xaxis.x, xaxis.y, xaxis.z, 0.);
  50. vec4 row1 = vec4(yaxis.x, yaxis.y, yaxis.z, 0.);
  51. vec4 row2 = vec4(zaxis.x, zaxis.y, zaxis.z, 0.);
  52. // vec4 row0 = vec4(1., 0., 0., 0.);
  53. // vec4 row1 = vec4(0., 1., 0., 0.);
  54. // vec4 row2 = vec4(0., 0., 1., 0.);
  55. vec4 row3 = vec4(0., 0., 0., 1.0);
  56. mat4 rotMatrix = mat4(row0, row1, row2, row3);
  57. vec4 alignedWorld = rotMatrix * vec4(worldPos, 0.0);
  58. gl_Position = projection * view * vec4(alignedWorld.xyz, 1.0);
  59. #endif
  60. vColor = color;
  61. #ifdef ANIMATESHEET
  62. float rowOffset = floor(cellIndex / particlesInfos.z);
  63. float columnOffset = cellIndex - rowOffset * particlesInfos.z;
  64. vec2 uvScale = particlesInfos.xy;
  65. vec2 uvOffset = vec2(offset.x , 1.0 - offset.y);
  66. vUV = (uvOffset + vec2(columnOffset, rowOffset)) * uvScale;
  67. #else
  68. vUV = offset;
  69. #endif
  70. // Clip plane
  71. #ifdef CLIPPLANE
  72. vec4 worldPos = invView * vec4(viewPos, 1.0);
  73. fClipDistance = dot(worldPos, vClipPlane);
  74. #endif
  75. }