particles.vertex.fx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Attributes
  2. attribute vec3 position;
  3. attribute vec4 color;
  4. attribute float angle;
  5. attribute vec2 size;
  6. #ifdef ANIMATESHEET
  7. attribute float cellIndex;
  8. #endif
  9. #ifndef BILLBOARD
  10. attribute vec3 direction;
  11. #endif
  12. attribute vec2 offset;
  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 yaxis = normalize(direction);
  46. vec3 xaxis = normalize(cross(vec3(0., 1.0, 0.), yaxis));
  47. vec3 zaxis = normalize(cross(yaxis, xaxis));
  48. vec3 row0 = vec3(xaxis.x, xaxis.y, xaxis.z);
  49. vec3 row1 = vec3(yaxis.x, yaxis.y, yaxis.z);
  50. vec3 row2 = vec3(zaxis.x, zaxis.y, zaxis.z);
  51. mat3 rotMatrix = mat3(row0, row1, row2);
  52. vec3 alignedCorner = rotMatrix * rotatedCorner;
  53. vec3 worldPos = position + alignedCorner;
  54. gl_Position = projection * view * vec4(worldPos, 1.0);
  55. #endif
  56. vColor = color;
  57. #ifdef ANIMATESHEET
  58. float rowOffset = floor(cellIndex / particlesInfos.z);
  59. float columnOffset = cellIndex - rowOffset * particlesInfos.z;
  60. vec2 uvScale = particlesInfos.xy;
  61. vec2 uvOffset = vec2(offset.x , 1.0 - offset.y);
  62. vUV = (uvOffset + vec2(columnOffset, rowOffset)) * uvScale;
  63. #else
  64. vUV = offset;
  65. #endif
  66. // Clip plane
  67. #ifdef CLIPPLANE
  68. vec4 worldPos = invView * vec4(viewPos, 1.0);
  69. fClipDistance = dot(worldPos, vClipPlane);
  70. #endif
  71. }