gpuUpdateParticles.vertex.fx 11 KB

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