babylon.sphereParticleEmitter.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. 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. 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. /**
  56. * Particle emitter emitting particles from the inside of a sphere.
  57. * It emits the particles randomly between two vectors.
  58. */
  59. export class SphereDirectedParticleEmitter extends SphereParticleEmitter {
  60. /**
  61. * Creates a new instance of @see SphereDirectedParticleEmitter
  62. * @param radius the radius of the emission sphere
  63. * @param direction1 the min limit of the emission direction
  64. * @param direction2 the max limit of the emission direction
  65. */
  66. constructor(radius: number,
  67. /**
  68. * The min limit of the emission direction.
  69. */
  70. public direction1: Vector3,
  71. /**
  72. * The max limit of the emission direction.
  73. */
  74. public direction2: Vector3) {
  75. super(radius);
  76. }
  77. /**
  78. * Called by the particle System when the direction is computed for the created particle.
  79. * @param emitPower is the power of the particle (speed)
  80. * @param worldMatrix is the world matrix of the particle system
  81. * @param directionToUpdate is the direction vector to update with the result
  82. * @param particle is the particle we are computed the direction for
  83. */
  84. startDirectionFunction(emitPower: number, worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle): void {
  85. var randX = Scalar.RandomRange(this.direction1.x, this.direction2.x);
  86. var randY = Scalar.RandomRange(this.direction1.y, this.direction2.y);
  87. var randZ = Scalar.RandomRange(this.direction1.z, this.direction2.z);
  88. Vector3.TransformNormalFromFloatsToRef(randX * emitPower, randY * emitPower, randZ * emitPower, worldMatrix, directionToUpdate);
  89. }
  90. }
  91. }