babylon.sphereParticleEmitter.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. module BABYLON {
  2. /**
  3. * Particle emitter emitting particles from the inside of a sphere.
  4. * It emits the particles alongside the sphere radius. The emission direction might be randomized.
  5. */
  6. export class SphereParticleEmitter implements IParticleEmitterType {
  7. /**
  8. * Creates a new instance of @see SphereParticleEmitter
  9. * @param radius the radius of the emission sphere
  10. * @param directionRandomizer defines how much to randomize the particle direction [0-1]
  11. */
  12. constructor(
  13. /**
  14. * The radius of the emission sphere.
  15. */
  16. public radius: number,
  17. /**
  18. * How much to randomize the particle direction [0-1].
  19. */
  20. public directionRandomizer = 0) {
  21. }
  22. /**
  23. * Called by the particle System when the direction is computed for the created particle.
  24. * @param emitPower is the power of the particle (speed)
  25. * @param worldMatrix is the world matrix of the particle system
  26. * @param directionToUpdate is the direction vector to update with the result
  27. * @param particle is the particle we are computed the direction for
  28. */
  29. public startDirectionFunction(emitPower: number, worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle): void {
  30. var direction = particle.position.subtract(worldMatrix.getTranslation()).normalize();
  31. var randX = Scalar.RandomRange(0, this.directionRandomizer);
  32. var randY = Scalar.RandomRange(0, this.directionRandomizer);
  33. var randZ = Scalar.RandomRange(0, this.directionRandomizer);
  34. direction.x += randX;
  35. direction.y += randY;
  36. direction.z += randZ;
  37. direction.normalize();
  38. Vector3.TransformNormalFromFloatsToRef(direction.x * emitPower, direction.y * emitPower, direction.z * emitPower, worldMatrix, directionToUpdate);
  39. }
  40. /**
  41. * Called by the particle System when the position is computed for the created particle.
  42. * @param worldMatrix is the world matrix of the particle system
  43. * @param positionToUpdate is the position vector to update with the result
  44. * @param particle is the particle we are computed the position for
  45. */
  46. public startPositionFunction(worldMatrix: Matrix, positionToUpdate: Vector3, particle: Particle): void {
  47. var phi = Scalar.RandomRange(0, 2 * Math.PI);
  48. var theta = Scalar.RandomRange(0, Math.PI);
  49. var randX = this.radius * Math.cos(phi) * Math.sin(theta);
  50. var randY = this.radius * Math.cos(theta);
  51. var randZ = this.radius * Math.sin(phi) * Math.sin(theta);
  52. Vector3.TransformCoordinatesFromFloatsToRef(randX, randY, randZ, worldMatrix, positionToUpdate);
  53. }
  54. /**
  55. * Clones the current emitter and returns a copy of it
  56. * @returns the new emitter
  57. */
  58. public clone(): SphereParticleEmitter {
  59. let newOne = new SphereParticleEmitter(this.radius, this.directionRandomizer);
  60. Tools.DeepCopy(this, newOne);
  61. return newOne;
  62. }
  63. /**
  64. * Called by the {BABYLON.GPUParticleSystem} to setup the update shader
  65. * @param effect defines the update shader
  66. */
  67. public applyToShader(effect: Effect): void {
  68. effect.setFloat("radius", this.radius);
  69. effect.setFloat("directionRandomizer", this.directionRandomizer);
  70. }
  71. /**
  72. * Returns a string to use to update the GPU particles update shader
  73. */
  74. public getEffectDefines(): string {
  75. return "#define SPHEREEMITTER"
  76. }
  77. }
  78. /**
  79. * Particle emitter emitting particles from the inside of a sphere.
  80. * It emits the particles randomly between two vectors.
  81. */
  82. export class SphereDirectedParticleEmitter extends SphereParticleEmitter {
  83. /**
  84. * Creates a new instance of @see SphereDirectedParticleEmitter
  85. * @param radius the radius of the emission sphere
  86. * @param direction1 the min limit of the emission direction
  87. * @param direction2 the max limit of the emission direction
  88. */
  89. constructor(radius: number,
  90. /**
  91. * The min limit of the emission direction.
  92. */
  93. public direction1: Vector3,
  94. /**
  95. * The max limit of the emission direction.
  96. */
  97. public direction2: Vector3) {
  98. super(radius);
  99. }
  100. /**
  101. * Called by the particle System when the direction is computed for the created particle.
  102. * @param emitPower is the power of the particle (speed)
  103. * @param worldMatrix is the world matrix of the particle system
  104. * @param directionToUpdate is the direction vector to update with the result
  105. * @param particle is the particle we are computed the direction for
  106. */
  107. public startDirectionFunction(emitPower: number, worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle): void {
  108. var randX = Scalar.RandomRange(this.direction1.x, this.direction2.x);
  109. var randY = Scalar.RandomRange(this.direction1.y, this.direction2.y);
  110. var randZ = Scalar.RandomRange(this.direction1.z, this.direction2.z);
  111. Vector3.TransformNormalFromFloatsToRef(randX * emitPower, randY * emitPower, randZ * emitPower, worldMatrix, directionToUpdate);
  112. }
  113. /**
  114. * Clones the current emitter and returns a copy of it
  115. * @returns the new emitter
  116. */
  117. public clone(): SphereDirectedParticleEmitter {
  118. let newOne = new SphereDirectedParticleEmitter(this.radius, this.direction1, this.direction2);
  119. Tools.DeepCopy(this, newOne);
  120. return newOne;
  121. }
  122. /**
  123. * Called by the {BABYLON.GPUParticleSystem} to setup the update shader
  124. * @param effect defines the update shader
  125. */
  126. public applyToShader(effect: Effect): void {
  127. effect.setFloat("radius", this.radius);
  128. effect.setVector3("direction1", this.direction1);
  129. effect.setVector3("direction2", this.direction2);
  130. }
  131. /**
  132. * Returns a string to use to update the GPU particles update shader
  133. */
  134. public getEffectDefines(): string {
  135. return "#define SPHEREEMITTER\n#define DIRECTEDSPHEREEMITTER"
  136. }
  137. }
  138. }