gpuUpdateParticles.vertex.fx 9.3 KB

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