customParticleEmitter.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { DeepCopier } from "../../Misc/deepCopier";
  2. import { Vector3, Matrix, TmpVectors } from "../../Maths/math.vector";
  3. import { Effect } from "../../Materials/effect";
  4. import { Particle } from "../particle";
  5. import { IParticleEmitterType } from "./IParticleEmitterType";
  6. import { Nullable } from '../../types';
  7. /**
  8. * Particle emitter emitting particles from a custom list of positions.
  9. */
  10. export class CustomParticleEmitter implements IParticleEmitterType {
  11. /**
  12. * Gets or sets the position generator that will create the inital position of each particle.
  13. * Index will be provided when used with GPU particle. Particle will be provided when used with CPU particles
  14. */
  15. public particlePositionGenerator: (index: number, particle: Nullable<Particle>, outPosition: Vector3) => void = () => {};
  16. /**
  17. * Gets or sets the destination generator that will create the final destination of each particle.
  18. * * Index will be provided when used with GPU particle. Particle will be provided when used with CPU particles
  19. */
  20. public particleDestinationGenerator: (index: number, particle: Nullable<Particle>, outDestination: Vector3) => void = () => {};
  21. /**
  22. * Creates a new instance CustomParticleEmitter
  23. */
  24. constructor() {
  25. }
  26. /**
  27. * Called by the particle System when the direction is computed for the created particle.
  28. * @param worldMatrix is the world matrix of the particle system
  29. * @param directionToUpdate is the direction vector to update with the result
  30. * @param particle is the particle we are computed the direction for
  31. */
  32. public startDirectionFunction(worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle): void {
  33. let tmpVector = TmpVectors.Vector3[0];
  34. if (this.particleDestinationGenerator) {
  35. this.particleDestinationGenerator(-1, particle, tmpVector);
  36. // Get direction
  37. let diffVector = TmpVectors.Vector3[1];
  38. tmpVector.subtractToRef(particle.position, diffVector);
  39. diffVector.scaleToRef(1 / particle.lifeTime, tmpVector);
  40. } else {
  41. tmpVector.set(0, 0, 0);
  42. }
  43. Vector3.TransformNormalToRef(tmpVector, worldMatrix, directionToUpdate);
  44. }
  45. /**
  46. * Called by the particle System when the position is computed for the created particle.
  47. * @param worldMatrix is the world matrix of the particle system
  48. * @param positionToUpdate is the position vector to update with the result
  49. * @param particle is the particle we are computed the position for
  50. */
  51. public startPositionFunction(worldMatrix: Matrix, positionToUpdate: Vector3, particle: Particle): void {
  52. let tmpVector = TmpVectors.Vector3[0];
  53. if (this.particlePositionGenerator) {
  54. this.particlePositionGenerator(-1, particle, tmpVector);
  55. } else {
  56. tmpVector.set(0, 0, 0);
  57. }
  58. Vector3.TransformCoordinatesToRef(tmpVector, worldMatrix, positionToUpdate);
  59. }
  60. /**
  61. * Clones the current emitter and returns a copy of it
  62. * @returns the new emitter
  63. */
  64. public clone(): CustomParticleEmitter {
  65. let newOne = new CustomParticleEmitter();
  66. DeepCopier.DeepCopy(this, newOne);
  67. return newOne;
  68. }
  69. /**
  70. * Called by the GPUParticleSystem to setup the update shader
  71. * @param effect defines the update shader
  72. */
  73. public applyToShader(effect: Effect): void {
  74. }
  75. /**
  76. * Returns a string to use to update the GPU particles update shader
  77. * @returns a string containng the defines string
  78. */
  79. public getEffectDefines(): string {
  80. return "#define CUSTOMEMITTER";
  81. }
  82. /**
  83. * Returns the string "PointParticleEmitter"
  84. * @returns a string containing the class name
  85. */
  86. public getClassName(): string {
  87. return "CustomParticleEmitter";
  88. }
  89. /**
  90. * Serializes the particle system to a JSON object.
  91. * @returns the JSON object
  92. */
  93. public serialize(): any {
  94. var serializationObject: any = {};
  95. serializationObject.type = this.getClassName();
  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. }
  104. }