boxParticleEmitter.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { DeepCopier } from "../../Misc/deepCopier";
  2. import { Vector3, Matrix } from "../../Maths/math.vector";
  3. import { Scalar } from "../../Maths/math.scalar";
  4. import { Effect } from "../../Materials/effect";
  5. import { Particle } from "../../Particles/particle";
  6. import { IParticleEmitterType } from "./IParticleEmitterType";
  7. /**
  8. * Particle emitter emitting particles from the inside of a box.
  9. * It emits the particles randomly between 2 given directions.
  10. */
  11. export class BoxParticleEmitter implements IParticleEmitterType {
  12. /**
  13. * Random direction of each particle after it has been emitted, between direction1 and direction2 vectors.
  14. */
  15. public direction1 = new Vector3(0, 1.0, 0);
  16. /**
  17. * Random direction of each particle after it has been emitted, between direction1 and direction2 vectors.
  18. */
  19. public direction2 = new Vector3(0, 1.0, 0);
  20. /**
  21. * 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.
  22. */
  23. public minEmitBox = new Vector3(-0.5, -0.5, -0.5);
  24. /**
  25. * 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.
  26. */
  27. public maxEmitBox = new Vector3(0.5, 0.5, 0.5);
  28. /**
  29. * Creates a new instance BoxParticleEmitter
  30. */
  31. constructor() {
  32. }
  33. /**
  34. * Called by the particle System when the direction is computed for the created particle.
  35. * @param worldMatrix is the world matrix of the particle system
  36. * @param directionToUpdate is the direction vector to update with the result
  37. * @param particle is the particle we are computed the direction for
  38. * @param isLocal defines if the direction should be set in local space
  39. */
  40. public startDirectionFunction(worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle, isLocal: boolean): void {
  41. var randX = Scalar.RandomRange(this.direction1.x, this.direction2.x);
  42. var randY = Scalar.RandomRange(this.direction1.y, this.direction2.y);
  43. var randZ = Scalar.RandomRange(this.direction1.z, this.direction2.z);
  44. if (isLocal) {
  45. directionToUpdate.x = randX;
  46. directionToUpdate.y = randY;
  47. directionToUpdate.z = randZ;
  48. return;
  49. }
  50. Vector3.TransformNormalFromFloatsToRef(randX, randY, randZ, worldMatrix, directionToUpdate);
  51. }
  52. /**
  53. * Called by the particle System when the position is computed for the created particle.
  54. * @param worldMatrix is the world matrix of the particle system
  55. * @param positionToUpdate is the position vector to update with the result
  56. * @param particle is the particle we are computed the position for
  57. * @param isLocal defines if the position should be set in local space
  58. */
  59. public startPositionFunction(worldMatrix: Matrix, positionToUpdate: Vector3, particle: Particle, isLocal: boolean): void {
  60. var randX = Scalar.RandomRange(this.minEmitBox.x, this.maxEmitBox.x);
  61. var randY = Scalar.RandomRange(this.minEmitBox.y, this.maxEmitBox.y);
  62. var randZ = Scalar.RandomRange(this.minEmitBox.z, this.maxEmitBox.z);
  63. if (isLocal) {
  64. positionToUpdate.x = randX;
  65. positionToUpdate.y = randY;
  66. positionToUpdate.z = randZ;
  67. return;
  68. }
  69. Vector3.TransformCoordinatesFromFloatsToRef(randX, randY, randZ, worldMatrix, positionToUpdate);
  70. }
  71. /**
  72. * Clones the current emitter and returns a copy of it
  73. * @returns the new emitter
  74. */
  75. public clone(): BoxParticleEmitter {
  76. let newOne = new BoxParticleEmitter();
  77. DeepCopier.DeepCopy(this, newOne);
  78. return newOne;
  79. }
  80. /**
  81. * Called by the GPUParticleSystem to setup the update shader
  82. * @param effect defines the update shader
  83. */
  84. public applyToShader(effect: Effect): void {
  85. effect.setVector3("direction1", this.direction1);
  86. effect.setVector3("direction2", this.direction2);
  87. effect.setVector3("minEmitBox", this.minEmitBox);
  88. effect.setVector3("maxEmitBox", this.maxEmitBox);
  89. }
  90. /**
  91. * Returns a string to use to update the GPU particles update shader
  92. * @returns a string containng the defines string
  93. */
  94. public getEffectDefines(): string {
  95. return "#define BOXEMITTER";
  96. }
  97. /**
  98. * Returns the string "BoxParticleEmitter"
  99. * @returns a string containing the class name
  100. */
  101. public getClassName(): string {
  102. return "BoxParticleEmitter";
  103. }
  104. /**
  105. * Serializes the particle system to a JSON object.
  106. * @returns the JSON object
  107. */
  108. public serialize(): any {
  109. var serializationObject: any = {};
  110. serializationObject.type = this.getClassName();
  111. serializationObject.direction1 = this.direction1.asArray();
  112. serializationObject.direction2 = this.direction2.asArray();
  113. serializationObject.minEmitBox = this.minEmitBox.asArray();
  114. serializationObject.maxEmitBox = this.maxEmitBox.asArray();
  115. return serializationObject;
  116. }
  117. /**
  118. * Parse properties from a JSON object
  119. * @param serializationObject defines the JSON object
  120. */
  121. public parse(serializationObject: any): void {
  122. Vector3.FromArrayToRef(serializationObject.direction1, 0, this.direction1);
  123. Vector3.FromArrayToRef(serializationObject.direction2, 0, this.direction2);
  124. Vector3.FromArrayToRef(serializationObject.minEmitBox, 0, this.minEmitBox);
  125. Vector3.FromArrayToRef(serializationObject.maxEmitBox, 0, this.maxEmitBox);
  126. }
  127. }