pointParticleEmitter.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 a point.
  9. * It emits the particles randomly between 2 given directions.
  10. */
  11. export class PointParticleEmitter 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. * Creates a new instance PointParticleEmitter
  22. */
  23. constructor() {
  24. }
  25. /**
  26. * Called by the particle System when the direction is computed for the created particle.
  27. * @param worldMatrix is the world matrix of the particle system
  28. * @param directionToUpdate is the direction vector to update with the result
  29. * @param particle is the particle we are computed the direction for
  30. * @param isLocal defines if the direction should be set in local space
  31. */
  32. public startDirectionFunction(worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle, isLocal: boolean): void {
  33. var randX = Scalar.RandomRange(this.direction1.x, this.direction2.x);
  34. var randY = Scalar.RandomRange(this.direction1.y, this.direction2.y);
  35. var randZ = Scalar.RandomRange(this.direction1.z, this.direction2.z);
  36. if (isLocal) {
  37. directionToUpdate.copyFromFloats(randX, randY, randZ);
  38. return;
  39. }
  40. Vector3.TransformNormalFromFloatsToRef(randX, randY, randZ, worldMatrix, directionToUpdate);
  41. }
  42. /**
  43. * Called by the particle System when the position is computed for the created particle.
  44. * @param worldMatrix is the world matrix of the particle system
  45. * @param positionToUpdate is the position vector to update with the result
  46. * @param particle is the particle we are computed the position for
  47. * @param isLocal defines if the position should be set in local space
  48. */
  49. public startPositionFunction(worldMatrix: Matrix, positionToUpdate: Vector3, particle: Particle, isLocal: boolean): void {
  50. if (isLocal) {
  51. positionToUpdate.copyFromFloats(0, 0, 0);
  52. return;
  53. }
  54. Vector3.TransformCoordinatesFromFloatsToRef(0, 0, 0, worldMatrix, positionToUpdate);
  55. }
  56. /**
  57. * Clones the current emitter and returns a copy of it
  58. * @returns the new emitter
  59. */
  60. public clone(): PointParticleEmitter {
  61. let newOne = new PointParticleEmitter();
  62. DeepCopier.DeepCopy(this, newOne);
  63. return newOne;
  64. }
  65. /**
  66. * Called by the GPUParticleSystem to setup the update shader
  67. * @param effect defines the update shader
  68. */
  69. public applyToShader(effect: Effect): void {
  70. effect.setVector3("direction1", this.direction1);
  71. effect.setVector3("direction2", this.direction2);
  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 POINTEMITTER";
  79. }
  80. /**
  81. * Returns the string "PointParticleEmitter"
  82. * @returns a string containing the class name
  83. */
  84. public getClassName(): string {
  85. return "PointParticleEmitter";
  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. return serializationObject;
  97. }
  98. /**
  99. * Parse properties from a JSON object
  100. * @param serializationObject defines the JSON object
  101. */
  102. public parse(serializationObject: any): void {
  103. Vector3.FromArrayToRef(serializationObject.direction1, 0, this.direction1);
  104. Vector3.FromArrayToRef(serializationObject.direction2, 0, this.direction2);
  105. }
  106. }