gpuUpdateParticles.vertex.fx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. #version 300 es
  2. #define PI 3.14159
  3. uniform float currentCount;
  4. uniform float timeDelta;
  5. uniform float stopFactor;
  6. #ifndef LOCAL
  7. uniform mat4 emitterWM;
  8. #endif
  9. uniform vec2 lifeTime;
  10. uniform vec2 emitPower;
  11. uniform vec2 sizeRange;
  12. uniform vec4 scaleRange;
  13. #ifndef COLORGRADIENTS
  14. uniform vec4 color1;
  15. uniform vec4 color2;
  16. #endif
  17. uniform vec3 gravity;
  18. uniform sampler2D randomSampler;
  19. uniform sampler2D randomSampler2;
  20. uniform vec4 angleRange;
  21. #ifdef BOXEMITTER
  22. uniform vec3 direction1;
  23. uniform vec3 direction2;
  24. uniform vec3 minEmitBox;
  25. uniform vec3 maxEmitBox;
  26. #endif
  27. #ifdef POINTEMITTER
  28. uniform vec3 direction1;
  29. uniform vec3 direction2;
  30. #endif
  31. #ifdef HEMISPHERICEMITTER
  32. uniform float radius;
  33. uniform float radiusRange;
  34. uniform float directionRandomizer;
  35. #endif
  36. #ifdef SPHEREEMITTER
  37. uniform float radius;
  38. uniform float radiusRange;
  39. #ifdef DIRECTEDSPHEREEMITTER
  40. uniform vec3 direction1;
  41. uniform vec3 direction2;
  42. #else
  43. uniform float directionRandomizer;
  44. #endif
  45. #endif
  46. #ifdef CYLINDEREMITTER
  47. uniform float radius;
  48. uniform float height;
  49. uniform float radiusRange;
  50. #ifdef DIRECTEDCYLINDEREMITTER
  51. uniform vec3 direction1;
  52. uniform vec3 direction2;
  53. #else
  54. uniform float directionRandomizer;
  55. #endif
  56. #endif
  57. #ifdef CONEEMITTER
  58. uniform vec2 radius;
  59. uniform float coneAngle;
  60. uniform vec2 height;
  61. uniform float directionRandomizer;
  62. #endif
  63. // Particles state
  64. in vec3 position;
  65. #ifdef CUSTOMEMITTER
  66. in vec3 initialPosition;
  67. #endif
  68. in float age;
  69. in float life;
  70. in vec4 seed;
  71. in vec3 size;
  72. #ifndef COLORGRADIENTS
  73. in vec4 color;
  74. #endif
  75. in vec3 direction;
  76. #ifndef BILLBOARD
  77. in vec3 initialDirection;
  78. #endif
  79. #ifdef ANGULARSPEEDGRADIENTS
  80. in float angle;
  81. #else
  82. in vec2 angle;
  83. #endif
  84. #ifdef ANIMATESHEET
  85. in float cellIndex;
  86. #ifdef ANIMATESHEETRANDOMSTART
  87. in float cellStartOffset;
  88. #endif
  89. #endif
  90. #ifdef NOISE
  91. in vec3 noiseCoordinates1;
  92. in vec3 noiseCoordinates2;
  93. #endif
  94. // Output
  95. out vec3 outPosition;
  96. #ifdef CUSTOMEMITTER
  97. out vec3 outInitialPosition;
  98. #endif
  99. out float outAge;
  100. out float outLife;
  101. out vec4 outSeed;
  102. out vec3 outSize;
  103. #ifndef COLORGRADIENTS
  104. out vec4 outColor;
  105. #endif
  106. out vec3 outDirection;
  107. #ifndef BILLBOARD
  108. out vec3 outInitialDirection;
  109. #endif
  110. #ifdef ANGULARSPEEDGRADIENTS
  111. out float outAngle;
  112. #else
  113. out vec2 outAngle;
  114. #endif
  115. #ifdef ANIMATESHEET
  116. out float outCellIndex;
  117. #ifdef ANIMATESHEETRANDOMSTART
  118. out float outCellStartOffset;
  119. #endif
  120. #endif
  121. #ifdef NOISE
  122. out vec3 outNoiseCoordinates1;
  123. out vec3 outNoiseCoordinates2;
  124. #endif
  125. #ifdef SIZEGRADIENTS
  126. uniform sampler2D sizeGradientSampler;
  127. #endif
  128. #ifdef ANGULARSPEEDGRADIENTS
  129. uniform sampler2D angularSpeedGradientSampler;
  130. #endif
  131. #ifdef VELOCITYGRADIENTS
  132. uniform sampler2D velocityGradientSampler;
  133. #endif
  134. #ifdef LIMITVELOCITYGRADIENTS
  135. uniform sampler2D limitVelocityGradientSampler;
  136. uniform float limitVelocityDamping;
  137. #endif
  138. #ifdef DRAGGRADIENTS
  139. uniform sampler2D dragGradientSampler;
  140. #endif
  141. #ifdef NOISE
  142. uniform vec3 noiseStrength;
  143. uniform sampler2D noiseSampler;
  144. #endif
  145. #ifdef ANIMATESHEET
  146. uniform vec3 cellInfos;
  147. #endif
  148. vec3 getRandomVec3(float offset) {
  149. return texture(randomSampler2, vec2(float(gl_VertexID) * offset / currentCount, 0)).rgb;
  150. }
  151. vec4 getRandomVec4(float offset) {
  152. return texture(randomSampler, vec2(float(gl_VertexID) * offset / currentCount, 0));
  153. }
  154. void main() {
  155. float newAge = age + timeDelta;
  156. // If particle is dead and system is not stopped, spawn as new particle
  157. if (newAge >= life && stopFactor != 0.) {
  158. vec3 newPosition;
  159. vec3 newDirection;
  160. // Let's get some random values
  161. vec4 randoms = getRandomVec4(seed.x);
  162. // Age and life
  163. outLife = lifeTime.x + (lifeTime.y - lifeTime.x) * randoms.r;
  164. outAge = newAge - life;
  165. // Seed
  166. outSeed = seed;
  167. // Size
  168. #ifdef SIZEGRADIENTS
  169. outSize.x = texture(sizeGradientSampler, vec2(0, 0)).r;
  170. #else
  171. outSize.x = sizeRange.x + (sizeRange.y - sizeRange.x) * randoms.g;
  172. #endif
  173. outSize.y = scaleRange.x + (scaleRange.y - scaleRange.x) * randoms.b;
  174. outSize.z = scaleRange.z + (scaleRange.w - scaleRange.z) * randoms.a;
  175. #ifndef COLORGRADIENTS
  176. // Color
  177. outColor = color1 + (color2 - color1) * randoms.b;
  178. #endif
  179. // Angular speed
  180. #ifndef ANGULARSPEEDGRADIENTS
  181. outAngle.y = angleRange.x + (angleRange.y - angleRange.x) * randoms.a;
  182. outAngle.x = angleRange.z + (angleRange.w - angleRange.z) * randoms.r;
  183. #else
  184. outAngle = angleRange.z + (angleRange.w - angleRange.z) * randoms.r;
  185. #endif
  186. // Position / Direction (based on emitter type)
  187. #ifdef POINTEMITTER
  188. vec3 randoms2 = getRandomVec3(seed.y);
  189. vec3 randoms3 = getRandomVec3(seed.z);
  190. newPosition = vec3(0, 0, 0);
  191. newDirection = direction1 + (direction2 - direction1) * randoms3;
  192. #elif defined(BOXEMITTER)
  193. vec3 randoms2 = getRandomVec3(seed.y);
  194. vec3 randoms3 = getRandomVec3(seed.z);
  195. newPosition = minEmitBox + (maxEmitBox - minEmitBox) * randoms2;
  196. newDirection = direction1 + (direction2 - direction1) * randoms3;
  197. #elif defined(HEMISPHERICEMITTER)
  198. vec3 randoms2 = getRandomVec3(seed.y);
  199. vec3 randoms3 = getRandomVec3(seed.z);
  200. // Position on the sphere surface
  201. float phi = 2.0 * PI * randoms2.x;
  202. float theta = acos(2.0 * randoms2.y - 1.0);
  203. float randX = cos(phi) * sin(theta);
  204. float randY = cos(theta);
  205. float randZ = sin(phi) * sin(theta);
  206. newPosition = (radius - (radius * radiusRange * randoms2.z)) * vec3(randX, abs(randY), randZ);
  207. newDirection = newPosition + directionRandomizer * randoms3;
  208. #elif defined(SPHEREEMITTER)
  209. vec3 randoms2 = getRandomVec3(seed.y);
  210. vec3 randoms3 = getRandomVec3(seed.z);
  211. // Position on the sphere surface
  212. float phi = 2.0 * PI * randoms2.x;
  213. float theta = acos(2.0 * randoms2.y - 1.0);
  214. float randX = cos(phi) * sin(theta);
  215. float randY = cos(theta);
  216. float randZ = sin(phi) * sin(theta);
  217. newPosition = (radius - (radius * radiusRange * randoms2.z)) * vec3(randX, randY, randZ);
  218. #ifdef DIRECTEDSPHEREEMITTER
  219. newDirection = direction1 + (direction2 - direction1) * randoms3;
  220. #else
  221. // Direction
  222. newDirection = newPosition + directionRandomizer * randoms3;
  223. #endif
  224. #elif defined(CYLINDEREMITTER)
  225. vec3 randoms2 = getRandomVec3(seed.y);
  226. vec3 randoms3 = getRandomVec3(seed.z);
  227. // Position on the cylinder
  228. float yPos = (randoms2.x - 0.5)*height;
  229. float angle = randoms2.y * PI * 2.;
  230. float inverseRadiusRangeSquared = ((1.-radiusRange) * (1.-radiusRange));
  231. float positionRadius = radius*sqrt(inverseRadiusRangeSquared + (randoms2.z * (1.-inverseRadiusRangeSquared)));
  232. float xPos = positionRadius * cos(angle);
  233. float zPos = positionRadius * sin(angle);
  234. newPosition = vec3(xPos, yPos, zPos);
  235. #ifdef DIRECTEDCYLINDEREMITTER
  236. newDirection = direction1 + (direction2 - direction1) * randoms3;
  237. #else
  238. // Direction
  239. angle = angle + ((randoms3.x-0.5) * PI);
  240. newDirection = vec3(cos(angle), randoms3.y-0.5, sin(angle));
  241. newDirection = normalize(newDirection);
  242. #endif
  243. #elif defined(CONEEMITTER)
  244. vec3 randoms2 = getRandomVec3(seed.y);
  245. float s = 2.0 * PI * randoms2.x;
  246. #ifdef CONEEMITTERSPAWNPOINT
  247. float h = 0.0001;
  248. #else
  249. float h = randoms2.y * height.y;
  250. // Better distribution in a cone at normal angles.
  251. h = 1. - h * h;
  252. #endif
  253. float lRadius = radius.x - radius.x * randoms2.z * radius.y;
  254. lRadius = lRadius * h;
  255. float randX = lRadius * sin(s);
  256. float randZ = lRadius * cos(s);
  257. float randY = h * height.x;
  258. newPosition = vec3(randX, randY, randZ);
  259. // Direction
  260. if (abs(cos(coneAngle)) == 1.0) {
  261. newDirection = vec3(0., 1.0, 0.);
  262. } else {
  263. vec3 randoms3 = getRandomVec3(seed.z);
  264. newDirection = normalize(newPosition + directionRandomizer * randoms3);
  265. }
  266. #elif defined(CUSTOMEMITTER)
  267. newPosition = initialPosition;
  268. outInitialPosition = initialPosition;
  269. #else
  270. // Create the particle at origin
  271. newPosition = vec3(0., 0., 0.);
  272. // Spread in all directions
  273. newDirection = 2.0 * (getRandomVec3(seed.w) - vec3(0.5, 0.5, 0.5));
  274. #endif
  275. float power = emitPower.x + (emitPower.y - emitPower.x) * randoms.a;
  276. #ifdef LOCAL
  277. outPosition = newPosition;
  278. #else
  279. outPosition = (emitterWM * vec4(newPosition, 1.)).xyz;
  280. #endif
  281. #ifdef CUSTOMEMITTER
  282. outDirection = direction;
  283. #ifndef BILLBOARD
  284. outInitialDirection = direction;
  285. #endif
  286. #else
  287. #ifdef LOCAL
  288. vec3 initial = newDirection;
  289. #else
  290. vec3 initial = (emitterWM * vec4(newDirection, 0.)).xyz;
  291. #endif
  292. outDirection = initial * power;
  293. #ifndef BILLBOARD
  294. outInitialDirection = initial;
  295. #endif
  296. #endif
  297. #ifdef ANIMATESHEET
  298. outCellIndex = cellInfos.x;
  299. #ifdef ANIMATESHEETRANDOMSTART
  300. outCellStartOffset = randoms.a * outLife;
  301. #endif
  302. #endif
  303. #ifdef NOISE
  304. outNoiseCoordinates1 = noiseCoordinates1;
  305. outNoiseCoordinates2 = noiseCoordinates2;
  306. #endif
  307. } else {
  308. float directionScale = timeDelta;
  309. outAge = newAge;
  310. float ageGradient = newAge / life;
  311. #ifdef VELOCITYGRADIENTS
  312. directionScale *= texture(velocityGradientSampler, vec2(ageGradient, 0)).r;
  313. #endif
  314. #ifdef DRAGGRADIENTS
  315. directionScale *= 1.0 - texture(dragGradientSampler, vec2(ageGradient, 0)).r;
  316. #endif
  317. #if defined(CUSTOMEMITTER)
  318. outPosition = position + (direction - position) * ageGradient;
  319. outInitialPosition = initialPosition;
  320. #else
  321. outPosition = position + direction * directionScale;
  322. #endif
  323. outLife = life;
  324. outSeed = seed;
  325. #ifndef COLORGRADIENTS
  326. outColor = color;
  327. #endif
  328. #ifdef SIZEGRADIENTS
  329. outSize.x = texture(sizeGradientSampler, vec2(ageGradient, 0)).r;
  330. outSize.yz = size.yz;
  331. #else
  332. outSize = size;
  333. #endif
  334. #ifndef BILLBOARD
  335. outInitialDirection = initialDirection;
  336. #endif
  337. #ifdef CUSTOMEMITTER
  338. outDirection = direction;
  339. #else
  340. vec3 updatedDirection = direction + gravity * timeDelta;
  341. #ifdef LIMITVELOCITYGRADIENTS
  342. float limitVelocity = texture(limitVelocityGradientSampler, vec2(ageGradient, 0)).r;
  343. float currentVelocity = length(updatedDirection);
  344. if (currentVelocity > limitVelocity) {
  345. updatedDirection = updatedDirection * limitVelocityDamping;
  346. }
  347. #endif
  348. outDirection = updatedDirection;
  349. #ifdef NOISE
  350. float fetchedR = texture(noiseSampler, vec2(noiseCoordinates1.x, noiseCoordinates1.y) * vec2(0.5) + vec2(0.5)).r;
  351. float fetchedG = texture(noiseSampler, vec2(noiseCoordinates1.z, noiseCoordinates2.x) * vec2(0.5) + vec2(0.5)).r;
  352. float fetchedB = texture(noiseSampler, vec2(noiseCoordinates2.y, noiseCoordinates2.z) * vec2(0.5) + vec2(0.5)).r;
  353. vec3 force = vec3(2. * fetchedR - 1., 2. * fetchedG - 1., 2. * fetchedB - 1.) * noiseStrength;
  354. outDirection = outDirection + force * timeDelta;
  355. outNoiseCoordinates1 = noiseCoordinates1;
  356. outNoiseCoordinates2 = noiseCoordinates2;
  357. #endif
  358. #endif
  359. #ifdef ANGULARSPEEDGRADIENTS
  360. float angularSpeed = texture(angularSpeedGradientSampler, vec2(ageGradient, 0)).r;
  361. outAngle = angle + angularSpeed * timeDelta;
  362. #else
  363. outAngle = vec2(angle.x + angle.y * timeDelta, angle.y);
  364. #endif
  365. #ifdef ANIMATESHEET
  366. float offsetAge = outAge;
  367. float dist = cellInfos.y - cellInfos.x;
  368. #ifdef ANIMATESHEETRANDOMSTART
  369. outCellStartOffset = cellStartOffset;
  370. offsetAge += cellStartOffset;
  371. #else
  372. float cellStartOffset = 0.;
  373. #endif
  374. float ratio = clamp(mod(cellStartOffset + cellInfos.z * offsetAge, life) / life, 0., 1.0);
  375. outCellIndex = float(int(cellInfos.x + ratio * dist));
  376. #endif
  377. }
  378. }