customParticleEmitter.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. * @param isLocal defines if the direction should be set in local space
  32. */
  33. public startDirectionFunction(worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle, isLocal: boolean): void {
  34. let tmpVector = TmpVectors.Vector3[0];
  35. if (this.particleDestinationGenerator) {
  36. this.particleDestinationGenerator(-1, particle, tmpVector);
  37. // Get direction
  38. let diffVector = TmpVectors.Vector3[1];
  39. tmpVector.subtractToRef(particle.position, diffVector);
  40. diffVector.scaleToRef(1 / particle.lifeTime, tmpVector);
  41. } else {
  42. tmpVector.set(0, 0, 0);
  43. }
  44. if (isLocal) {
  45. directionToUpdate.copyFrom(tmpVector);
  46. return;
  47. }
  48. Vector3.TransformNormalToRef(tmpVector, worldMatrix, directionToUpdate);
  49. }
  50. /**
  51. * Called by the particle System when the position is computed for the created particle.
  52. * @param worldMatrix is the world matrix of the particle system
  53. * @param positionToUpdate is the position vector to update with the result
  54. * @param particle is the particle we are computed the position for
  55. * @param isLocal defines if the position should be set in local space
  56. */
  57. public startPositionFunction(worldMatrix: Matrix, positionToUpdate: Vector3, particle: Particle, isLocal: boolean): void {
  58. let tmpVector = TmpVectors.Vector3[0];
  59. if (this.particlePositionGenerator) {
  60. this.particlePositionGenerator(-1, particle, tmpVector);
  61. } else {
  62. tmpVector.set(0, 0, 0);
  63. }
  64. if (isLocal) {
  65. positionToUpdate.copyFrom(tmpVector);
  66. return;
  67. }
  68. Vector3.TransformCoordinatesToRef(tmpVector, worldMatrix, positionToUpdate);
  69. }
  70. /**
  71. * Clones the current emitter and returns a copy of it
  72. * @returns the new emitter
  73. */
  74. public clone(): CustomParticleEmitter {
  75. let newOne = new CustomParticleEmitter();
  76. DeepCopier.DeepCopy(this, newOne);
  77. return newOne;
  78. }
  79. /**
  80. * Called by the GPUParticleSystem to setup the update shader
  81. * @param effect defines the update shader
  82. */
  83. public applyToShader(effect: Effect): void {
  84. }
  85. /**
  86. * Returns a string to use to update the GPU particles update shader
  87. * @returns a string containng the defines string
  88. */
  89. public getEffectDefines(): string {
  90. return "#define CUSTOMEMITTER";
  91. }
  92. /**
  93. * Returns the string "PointParticleEmitter"
  94. * @returns a string containing the class name
  95. */
  96. public getClassName(): string {
  97. return "CustomParticleEmitter";
  98. }
  99. /**
  100. * Serializes the particle system to a JSON object.
  101. * @returns the JSON object
  102. */
  103. public serialize(): any {
  104. var serializationObject: any = {};
  105. serializationObject.type = this.getClassName();
  106. return serializationObject;
  107. }
  108. /**
  109. * Parse properties from a JSON object
  110. * @param serializationObject defines the JSON object
  111. */
  112. public parse(serializationObject: any): void {
  113. }
  114. }