gpuRenderParticles.vertex.fx 4.9 KB

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