particles.vertex.fx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #ifdef BILLBOARDSTRETCHED
  13. attribute vec3 direction;
  14. #endif
  15. #ifdef RAMPGRADIENT
  16. attribute vec4 remapData;
  17. #endif
  18. attribute vec2 offset;
  19. // Uniforms
  20. uniform mat4 view;
  21. uniform mat4 projection;
  22. uniform vec2 translationPivot;
  23. #ifdef ANIMATESHEET
  24. uniform vec3 particlesInfos; // x (number of rows) y(number of columns) z(rowSize)
  25. #endif
  26. // Output
  27. varying vec2 vUV;
  28. varying vec4 vColor;
  29. #ifdef RAMPGRADIENT
  30. varying vec4 remapRanges;
  31. #endif
  32. #if defined(CLIPPLANE) || defined(CLIPPLANE2) || defined(CLIPPLANE3) || defined(CLIPPLANE4)
  33. uniform mat4 invView;
  34. #endif
  35. #include<clipPlaneVertexDeclaration>
  36. #ifdef BILLBOARD
  37. uniform vec3 eyePosition;
  38. #endif
  39. vec3 rotate(vec3 yaxis, vec3 rotatedCorner) {
  40. vec3 xaxis = normalize(cross(vec3(0., 1.0, 0.), yaxis));
  41. vec3 zaxis = normalize(cross(yaxis, xaxis));
  42. vec3 row0 = vec3(xaxis.x, xaxis.y, xaxis.z);
  43. vec3 row1 = vec3(yaxis.x, yaxis.y, yaxis.z);
  44. vec3 row2 = vec3(zaxis.x, zaxis.y, zaxis.z);
  45. mat3 rotMatrix = mat3(row0, row1, row2);
  46. vec3 alignedCorner = rotMatrix * rotatedCorner;
  47. return position + alignedCorner;
  48. }
  49. #ifdef BILLBOARDSTRETCHED
  50. vec3 rotateAlign(vec3 toCamera, vec3 rotatedCorner) {
  51. vec3 normalizedToCamera = normalize(toCamera);
  52. vec3 normalizedCrossDirToCamera = normalize(cross(normalize(direction), normalizedToCamera));
  53. vec3 crossProduct = normalize(cross(normalizedToCamera, normalizedCrossDirToCamera));
  54. vec3 row0 = vec3(normalizedCrossDirToCamera.x, normalizedCrossDirToCamera.y, normalizedCrossDirToCamera.z);
  55. vec3 row1 = vec3(crossProduct.x, crossProduct.y, crossProduct.z);
  56. vec3 row2 = vec3(normalizedToCamera.x, normalizedToCamera.y, normalizedToCamera.z);
  57. mat3 rotMatrix = mat3(row0, row1, row2);
  58. vec3 alignedCorner = rotMatrix * rotatedCorner;
  59. return position + alignedCorner;
  60. }
  61. #endif
  62. void main(void) {
  63. vec2 cornerPos;
  64. cornerPos = (vec2(offset.x - 0.5, offset.y - 0.5) - translationPivot) * size + translationPivot;
  65. #ifdef BILLBOARD
  66. // Rotate
  67. vec3 rotatedCorner;
  68. #ifdef BILLBOARDY
  69. rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
  70. rotatedCorner.z = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
  71. rotatedCorner.y = 0.;
  72. vec3 yaxis = position - eyePosition;
  73. yaxis.y = 0.;
  74. vec3 worldPos = rotate(normalize(yaxis), rotatedCorner);
  75. vec3 viewPos = (view * vec4(worldPos, 1.0)).xyz;
  76. #elif defined(BILLBOARDSTRETCHED)
  77. rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
  78. rotatedCorner.y = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
  79. rotatedCorner.z = 0.;
  80. vec3 toCamera = position - eyePosition;
  81. vec3 worldPos = rotateAlign(toCamera, rotatedCorner);
  82. vec3 viewPos = (view * vec4(worldPos, 1.0)).xyz;
  83. #else
  84. rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
  85. rotatedCorner.y = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
  86. rotatedCorner.z = 0.;
  87. vec3 viewPos = (view * vec4(position, 1.0)).xyz + rotatedCorner;
  88. #endif
  89. #ifdef RAMPGRADIENT
  90. remapRanges = remapData;
  91. #endif
  92. // Position
  93. gl_Position = projection * vec4(viewPos, 1.0);
  94. #else
  95. // Rotate
  96. vec3 rotatedCorner;
  97. rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
  98. rotatedCorner.z = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
  99. rotatedCorner.y = 0.;
  100. vec3 yaxis = normalize(direction);
  101. vec3 worldPos = rotate(yaxis, rotatedCorner);
  102. gl_Position = projection * view * vec4(worldPos, 1.0);
  103. #endif
  104. vColor = color;
  105. #ifdef ANIMATESHEET
  106. float rowOffset = floor(cellIndex / particlesInfos.z);
  107. float columnOffset = cellIndex - rowOffset * particlesInfos.z;
  108. vec2 uvScale = particlesInfos.xy;
  109. vec2 uvOffset = vec2(offset.x , 1.0 - offset.y);
  110. vUV = (uvOffset + vec2(columnOffset, rowOffset)) * uvScale;
  111. #else
  112. vUV = offset;
  113. #endif
  114. // Clip plane
  115. #if defined(CLIPPLANE) || defined(CLIPPLANE2) || defined(CLIPPLANE3) || defined(CLIPPLANE4)
  116. vec4 worldPos = invView * vec4(viewPos, 1.0);
  117. #endif
  118. #include<clipPlaneVertex>
  119. }