gpuUpdateParticles.vertex.fx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #version 300 es
  2. #define PI 3.14159
  3. uniform float currentCount;
  4. uniform float timeDelta;
  5. uniform float stopFactor;
  6. uniform mat4 emitterWM;
  7. uniform vec2 lifeTime;
  8. uniform vec2 emitPower;
  9. uniform vec2 sizeRange;
  10. uniform vec4 scaleRange;
  11. #ifndef COLORGRADIENTS
  12. uniform vec4 color1;
  13. uniform vec4 color2;
  14. #endif
  15. uniform vec3 gravity;
  16. uniform sampler2D randomSampler;
  17. uniform sampler2D randomSampler2;
  18. uniform vec4 angleRange;
  19. #ifdef BOXEMITTER
  20. uniform vec3 direction1;
  21. uniform vec3 direction2;
  22. uniform vec3 minEmitBox;
  23. uniform vec3 maxEmitBox;
  24. #endif
  25. #ifdef SPHEREEMITTER
  26. uniform float radius;
  27. uniform float radiusRange;
  28. #ifdef DIRECTEDSPHEREEMITTER
  29. uniform vec3 direction1;
  30. uniform vec3 direction2;
  31. #else
  32. uniform float directionRandomizer;
  33. #endif
  34. #endif
  35. #ifdef CONEEMITTER
  36. uniform vec2 radius;
  37. uniform float coneAngle;
  38. uniform vec2 height;
  39. uniform float directionRandomizer;
  40. #endif
  41. // Particles state
  42. in vec3 position;
  43. in float age;
  44. in float life;
  45. in vec4 seed;
  46. in vec3 size;
  47. #ifndef COLORGRADIENTS
  48. in vec4 color;
  49. #endif
  50. in vec3 direction;
  51. #ifndef BILLBOARD
  52. in vec3 initialDirection;
  53. #endif
  54. #ifdef ANGULARSPEEDGRADIENTS
  55. in float angle;
  56. #else
  57. in vec2 angle;
  58. #endif
  59. #ifdef ANIMATESHEET
  60. in float cellIndex;
  61. #endif
  62. // Output
  63. out vec3 outPosition;
  64. out float outAge;
  65. out float outLife;
  66. out vec4 outSeed;
  67. out vec3 outSize;
  68. #ifndef COLORGRADIENTS
  69. out vec4 outColor;
  70. #endif
  71. out vec3 outDirection;
  72. #ifndef BILLBOARD
  73. out vec3 outInitialDirection;
  74. #endif
  75. #ifdef ANGULARSPEEDGRADIENTS
  76. out float outAngle;
  77. #else
  78. out vec2 outAngle;
  79. #endif
  80. #ifdef ANIMATESHEET
  81. out float outCellIndex;
  82. #endif
  83. #ifdef SIZEGRADIENTS
  84. uniform sampler2D sizeGradientSampler;
  85. #endif
  86. #ifdef ANGULARSPEEDGRADIENTS
  87. uniform sampler2D angularSpeedGradientSampler;
  88. #endif
  89. #ifdef VELOCITYGRADIENTS
  90. uniform sampler2D velocityGradientSampler;
  91. #endif
  92. #ifdef NOISE
  93. uniform vec3 noiseStrength;
  94. uniform sampler2D noiseSampler;
  95. #endif
  96. #ifdef ANIMATESHEET
  97. uniform vec3 cellInfos;
  98. #endif
  99. vec3 getRandomVec3(float offset) {
  100. return texture(randomSampler2, vec2(float(gl_VertexID) * offset / currentCount, 0)).rgb;
  101. }
  102. vec4 getRandomVec4(float offset) {
  103. return texture(randomSampler, vec2(float(gl_VertexID) * offset / currentCount, 0));
  104. }
  105. void main() {
  106. if (age >= life) {
  107. if (stopFactor == 0.) {
  108. outPosition = position;
  109. outAge = life;
  110. outLife = life;
  111. outSeed = seed;
  112. #ifndef COLORGRADIENTS
  113. outColor = vec4(0.,0.,0.,0.);
  114. #endif
  115. outSize = vec3(0., 0., 0.);
  116. #ifndef BILLBOARD
  117. outInitialDirection = initialDirection;
  118. #endif
  119. outDirection = direction;
  120. outAngle = angle;
  121. #ifdef ANIMATESHEET
  122. outCellIndex = cellIndex;
  123. #endif
  124. return;
  125. }
  126. vec3 position;
  127. vec3 direction;
  128. // Let's get some random values
  129. vec4 randoms = getRandomVec4(seed.x);
  130. // Age and life
  131. outAge = 0.0;
  132. outLife = lifeTime.x + (lifeTime.y - lifeTime.x) * randoms.r;
  133. // Seed
  134. outSeed = seed;
  135. // Size
  136. #ifdef SIZEGRADIENTS
  137. outSize.x = texture(sizeGradientSampler, vec2(0, 0)).r;
  138. #else
  139. outSize.x = sizeRange.x + (sizeRange.y - sizeRange.x) * randoms.g;
  140. #endif
  141. outSize.y = scaleRange.x + (scaleRange.y - scaleRange.x) * randoms.b;
  142. outSize.z = scaleRange.z + (scaleRange.w - scaleRange.z) * randoms.a;
  143. #ifndef COLORGRADIENTS
  144. // Color
  145. outColor = color1 + (color2 - color1) * randoms.b;
  146. #endif
  147. // Angular speed
  148. #ifndef ANGULARSPEEDGRADIENTS
  149. outAngle.y = angleRange.x + (angleRange.y - angleRange.x) * randoms.a;
  150. outAngle.x = angleRange.z + (angleRange.w - angleRange.z) * randoms.r;
  151. #else
  152. outAngle = angleRange.z + (angleRange.w - angleRange.z) * randoms.r;
  153. #endif
  154. // Position / Direction (based on emitter type)
  155. #ifdef BOXEMITTER
  156. vec3 randoms2 = getRandomVec3(seed.y);
  157. vec3 randoms3 = getRandomVec3(seed.z);
  158. position = minEmitBox + (maxEmitBox - minEmitBox) * randoms2;
  159. direction = direction1 + (direction2 - direction1) * randoms3;
  160. #elif defined(SPHEREEMITTER)
  161. vec3 randoms2 = getRandomVec3(seed.y);
  162. vec3 randoms3 = getRandomVec3(seed.z);
  163. // Position on the sphere surface
  164. float phi = 2.0 * PI * randoms2.x;
  165. float theta = acos(2.0 * randoms2.y - 1.0);
  166. float randX = cos(phi) * sin(theta);
  167. float randY = cos(theta);
  168. float randZ = sin(phi) * sin(theta);
  169. position = (radius - (radius * radiusRange * randoms2.z)) * vec3(randX, randY, randZ);
  170. #ifdef DIRECTEDSPHEREEMITTER
  171. direction = direction1 + (direction2 - direction1) * randoms3;
  172. #else
  173. // Direction
  174. direction = position + directionRandomizer * randoms3;
  175. #endif
  176. #elif defined(CONEEMITTER)
  177. vec3 randoms2 = getRandomVec3(seed.y);
  178. float s = 2.0 * PI * randoms2.x;
  179. float h = randoms2.y * height.y;
  180. // Better distribution in a cone at normal angles.
  181. h = 1. - h * h;
  182. float lRadius = radius.x - radius.x * randoms2.z * radius.y;
  183. lRadius = lRadius * h;
  184. float randX = lRadius * sin(s);
  185. float randZ = lRadius * cos(s);
  186. float randY = h * height.x;
  187. position = vec3(randX, randY, randZ);
  188. // Direction
  189. if (abs(cos(coneAngle)) == 1.0) {
  190. direction = vec3(0., 1.0, 0.);
  191. } else {
  192. vec3 randoms3 = getRandomVec3(seed.z);
  193. direction = position + directionRandomizer * randoms3;
  194. }
  195. #else
  196. // Create the particle at origin
  197. position = vec3(0., 0., 0.);
  198. // Spread in all directions
  199. direction = 2.0 * (getRandomVec3(seed.w) - vec3(0.5, 0.5, 0.5));
  200. #endif
  201. float power = emitPower.x + (emitPower.y - emitPower.x) * randoms.a;
  202. outPosition = (emitterWM * vec4(position, 1.)).xyz;
  203. vec3 initial = (emitterWM * vec4(normalize(direction), 0.)).xyz;
  204. outDirection = initial * power;
  205. #ifndef BILLBOARD
  206. outInitialDirection = initial;
  207. #endif
  208. #ifdef ANIMATESHEET
  209. outCellIndex = cellInfos.x;
  210. #endif
  211. } else {
  212. float directionScale = timeDelta;
  213. float ageGradient = age / life;
  214. #ifdef VELOCITYGRADIENTS
  215. directionScale *= texture(velocityGradientSampler, vec2(ageGradient, 0)).r;
  216. #endif
  217. outPosition = position + direction * directionScale;
  218. outAge = age + timeDelta;
  219. outLife = life;
  220. outSeed = seed;
  221. #ifndef COLORGRADIENTS
  222. outColor = color;
  223. #endif
  224. #ifdef SIZEGRADIENTS
  225. outSize.x = texture(sizeGradientSampler, vec2(ageGradient, 0)).r;
  226. outSize.yz = size.yz;
  227. #else
  228. outSize = size;
  229. #endif
  230. #ifndef BILLBOARD
  231. outInitialDirection = initialDirection;
  232. #endif
  233. outDirection = direction + gravity * timeDelta;
  234. #ifdef NOISE
  235. vec3 localPosition = outPosition - emitterWM[3].xyz;
  236. float fetchedR = texture(noiseSampler, vec2(localPosition.y, localPosition.z) * vec2(0.5) + vec2(0.5)).r;
  237. float fetchedG = texture(noiseSampler, vec2(localPosition.x + 0.33, localPosition.z + 0.33) * vec2(0.5) + vec2(0.5)).r;
  238. float fetchedB = texture(noiseSampler, vec2(localPosition.z - 0.33, localPosition.y - 0.33) * vec2(0.5) + vec2(0.5)).r;
  239. vec3 force = vec3(2. * fetchedR - 1., 2. * fetchedG - 1., 2. * fetchedB - 1.) * noiseStrength;
  240. outDirection = outDirection + force * timeDelta;
  241. #endif
  242. #ifdef ANGULARSPEEDGRADIENTS
  243. float angularSpeed = texture(angularSpeedGradientSampler, vec2(ageGradient, 0)).r;
  244. outAngle = angle + angularSpeed * timeDelta;
  245. #else
  246. outAngle = vec2(angle.x + angle.y * timeDelta, angle.y);
  247. #endif
  248. #ifdef ANIMATESHEET
  249. float dist = cellInfos.y - cellInfos.x;
  250. float ratio = clamp(mod(outAge * cellInfos.z, life) / life, 0., 1.0);
  251. outCellIndex = float(int(cellInfos.x + ratio * dist));
  252. #endif
  253. }
  254. }