babylon.boxParticleEmitter.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. module BABYLON {
  2. /**
  3. * Particle emitter emitting particles from the inside of a box.
  4. * It emits the particles randomly between 2 given directions.
  5. */
  6. export class BoxParticleEmitter implements IParticleEmitterType {
  7. /**
  8. * Random direction of each particle after it has been emitted, between direction1 and direction2 vectors.
  9. */
  10. public direction1 = new Vector3(0, 1.0, 0);
  11. /**
  12. * Random direction of each particle after it has been emitted, between direction1 and direction2 vectors.
  13. */
  14. public direction2 = new Vector3(0, 1.0, 0);
  15. /**
  16. * Minimum box point around our emitter. Our emitter is the center of particles source, but if you want your particles to emit from more than one point, then you can tell it to do so.
  17. */
  18. public minEmitBox = new Vector3(-0.5, -0.5, -0.5);
  19. /**
  20. * Maximum box point around our emitter. Our emitter is the center of particles source, but if you want your particles to emit from more than one point, then you can tell it to do so.
  21. */
  22. public maxEmitBox = new Vector3(0.5, 0.5, 0.5);
  23. /**
  24. * Creates a new instance BoxParticleEmitter
  25. */
  26. constructor() {
  27. }
  28. /**
  29. * Called by the particle System when the direction is computed for the created particle.
  30. * @param emitPower is the power of the particle (speed)
  31. * @param worldMatrix is the world matrix of the particle system
  32. * @param directionToUpdate is the direction vector to update with the result
  33. * @param particle is the particle we are computed the direction for
  34. */
  35. public startDirectionFunction(emitPower: number, worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle): void {
  36. var randX = Scalar.RandomRange(this.direction1.x, this.direction2.x);
  37. var randY = Scalar.RandomRange(this.direction1.y, this.direction2.y);
  38. var randZ = Scalar.RandomRange(this.direction1.z, this.direction2.z);
  39. Vector3.TransformNormalFromFloatsToRef(randX * emitPower, randY * emitPower, randZ * emitPower, worldMatrix, directionToUpdate);
  40. }
  41. /**
  42. * Called by the particle System when the position is computed for the created particle.
  43. * @param worldMatrix is the world matrix of the particle system
  44. * @param positionToUpdate is the position vector to update with the result
  45. * @param particle is the particle we are computed the position for
  46. */
  47. public startPositionFunction(worldMatrix: Matrix, positionToUpdate: Vector3, particle: Particle): void {
  48. var randX = Scalar.RandomRange(this.minEmitBox.x, this.maxEmitBox.x);
  49. var randY = Scalar.RandomRange(this.minEmitBox.y, this.maxEmitBox.y);
  50. var randZ = Scalar.RandomRange(this.minEmitBox.z, this.maxEmitBox.z);
  51. Vector3.TransformCoordinatesFromFloatsToRef(randX, randY, randZ, worldMatrix, positionToUpdate);
  52. }
  53. /**
  54. * Clones the current emitter and returns a copy of it
  55. * @returns the new emitter
  56. */
  57. public clone(): BoxParticleEmitter
  58. {
  59. let newOne = new BoxParticleEmitter();
  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.setVector3("direction1", this.direction1);
  69. effect.setVector3("direction2", this.direction2);
  70. effect.setVector3("minEmitBox", this.minEmitBox);
  71. effect.setVector3("maxEmitBox", this.maxEmitBox);
  72. }
  73. /**
  74. * Returns a string to use to update the GPU particles update shader
  75. * @returns a string containng the defines string
  76. */
  77. public getEffectDefines(): string {
  78. return "#define BOXEMITTER"
  79. }
  80. /**
  81. * Returns the string "BoxEmitter"
  82. * @returns a string containing the class name
  83. */
  84. public getClassName(): string {
  85. return "BoxEmitter";
  86. }
  87. /**
  88. * Serializes the particle system to a JSON object.
  89. * @returns the JSON object
  90. */
  91. public serialize(): any {
  92. var serializationObject: any = {};
  93. serializationObject.type = this.getClassName();
  94. serializationObject.direction1 = this.direction1.asArray();
  95. serializationObject.direction2 = this.direction2.asArray();
  96. serializationObject.minEmitBox = this.minEmitBox.asArray();
  97. serializationObject.maxEmitBox = this.maxEmitBox.asArray();
  98. return serializationObject;
  99. }
  100. /**
  101. * Parse properties from a JSON object
  102. * @param serializationObject defines the JSON object
  103. */
  104. public parse(serializationObject: any): void {
  105. this.direction1.copyFrom(serializationObject.direction1);
  106. this.direction2.copyFrom(serializationObject.direction2);
  107. this.minEmitBox.copyFrom(serializationObject.minEmitBox);
  108. this.maxEmitBox.copyFrom(serializationObject.maxEmitBox);
  109. }
  110. }
  111. }