gpuRenderParticles.vertex.fx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #version 300 es
  2. uniform mat4 view;
  3. uniform mat4 projection;
  4. uniform vec2 translationPivot;
  5. uniform vec3 worldOffset;
  6. // Particles state
  7. in vec3 position;
  8. in float age;
  9. in float life;
  10. in vec3 size;
  11. #ifndef BILLBOARD
  12. in vec3 initialDirection;
  13. #endif
  14. #ifdef BILLBOARDSTRETCHED
  15. in vec3 direction;
  16. #endif
  17. in float angle;
  18. #ifdef ANIMATESHEET
  19. in float cellIndex;
  20. #endif
  21. in vec2 offset;
  22. in vec2 uv;
  23. out vec2 vUV;
  24. out vec4 vColor;
  25. #if defined(CLIPPLANE) || defined(CLIPPLANE2) || defined(CLIPPLANE3) || defined(CLIPPLANE4)
  26. uniform mat4 invView;
  27. #endif
  28. #include<clipPlaneVertexDeclaration2>
  29. #ifdef COLORGRADIENTS
  30. uniform sampler2D colorGradientSampler;
  31. #else
  32. uniform vec4 colorDead;
  33. in vec4 color;
  34. #endif
  35. #ifdef ANIMATESHEET
  36. uniform vec3 sheetInfos;
  37. #endif
  38. #ifdef BILLBOARD
  39. uniform vec3 eyePosition;
  40. #endif
  41. vec3 rotate(vec3 yaxis, vec3 rotatedCorner) {
  42. vec3 xaxis = normalize(cross(vec3(0., 1.0, 0.), yaxis));
  43. vec3 zaxis = normalize(cross(yaxis, xaxis));
  44. vec3 row0 = vec3(xaxis.x, xaxis.y, xaxis.z);
  45. vec3 row1 = vec3(yaxis.x, yaxis.y, yaxis.z);
  46. vec3 row2 = vec3(zaxis.x, zaxis.y, zaxis.z);
  47. mat3 rotMatrix = mat3(row0, row1, row2);
  48. vec3 alignedCorner = rotMatrix * rotatedCorner;
  49. return (position + worldOffset) + alignedCorner;
  50. }
  51. #ifdef BILLBOARDSTRETCHED
  52. vec3 rotateAlign(vec3 toCamera, vec3 rotatedCorner) {
  53. vec3 normalizedToCamera = normalize(toCamera);
  54. vec3 normalizedCrossDirToCamera = normalize(cross(normalize(direction), normalizedToCamera));
  55. vec3 crossProduct = normalize(cross(normalizedToCamera, normalizedCrossDirToCamera));
  56. vec3 row0 = vec3(normalizedCrossDirToCamera.x, normalizedCrossDirToCamera.y, normalizedCrossDirToCamera.z);
  57. vec3 row1 = vec3(crossProduct.x, crossProduct.y, crossProduct.z);
  58. vec3 row2 = vec3(normalizedToCamera.x, normalizedToCamera.y, normalizedToCamera.z);
  59. mat3 rotMatrix = mat3(row0, row1, row2);
  60. vec3 alignedCorner = rotMatrix * rotatedCorner;
  61. return (position + worldOffset) + alignedCorner;
  62. }
  63. #endif
  64. void main() {
  65. #ifdef ANIMATESHEET
  66. float rowOffset = floor(cellIndex / sheetInfos.z);
  67. float columnOffset = cellIndex - rowOffset * sheetInfos.z;
  68. vec2 uvScale = sheetInfos.xy;
  69. vec2 uvOffset = vec2(uv.x , 1.0 - uv.y);
  70. vUV = (uvOffset + vec2(columnOffset, rowOffset)) * uvScale;
  71. #else
  72. vUV = uv;
  73. #endif
  74. float ratio = age / life;
  75. #ifdef COLORGRADIENTS
  76. vColor = texture(colorGradientSampler, vec2(ratio, 0));
  77. #else
  78. vColor = color * vec4(1.0 - ratio) + colorDead * vec4(ratio);
  79. #endif
  80. vec2 cornerPos = (offset - translationPivot) * size.yz * size.x + translationPivot;
  81. #ifdef BILLBOARD
  82. vec4 rotatedCorner;
  83. rotatedCorner.w = 0.;
  84. #ifdef BILLBOARDY
  85. rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
  86. rotatedCorner.z = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
  87. rotatedCorner.y = 0.;
  88. vec3 yaxis = (position + worldOffset) - eyePosition;
  89. yaxis.y = 0.;
  90. vec3 worldPos = rotate(normalize(yaxis), rotatedCorner.xyz);
  91. vec4 viewPosition = (view * vec4(worldPos, 1.0));
  92. #elif defined(BILLBOARDSTRETCHED)
  93. rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
  94. rotatedCorner.y = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
  95. rotatedCorner.z = 0.;
  96. vec3 toCamera = (position + worldOffset) - eyePosition;
  97. vec3 worldPos = rotateAlign(toCamera, rotatedCorner.xyz);
  98. vec4 viewPosition = (view * vec4(worldPos, 1.0));
  99. #else
  100. // Rotate
  101. rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
  102. rotatedCorner.y = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
  103. rotatedCorner.z = 0.;
  104. // Expand position
  105. vec4 viewPosition = view * vec4((position + worldOffset), 1.0) + rotatedCorner;
  106. #endif
  107. #else
  108. // Rotate
  109. vec3 rotatedCorner;
  110. rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
  111. rotatedCorner.y = 0.;
  112. rotatedCorner.z = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
  113. vec3 yaxis = normalize(initialDirection);
  114. vec3 worldPos = rotate(yaxis, rotatedCorner);
  115. // Expand position
  116. vec4 viewPosition = view * vec4(worldPos, 1.0);
  117. #endif
  118. gl_Position = projection * viewPosition;
  119. // Clip plane
  120. #if defined(CLIPPLANE) || defined(CLIPPLANE2) || defined(CLIPPLANE3) || defined(CLIPPLANE4)
  121. vec4 worldPos = invView * viewPosition;
  122. #endif
  123. #include<clipPlaneVertex>
  124. }