babylon.sphereParticleEmitter.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. module BABYLON {
  2. /**
  3. * Particle emitter emitting particles from the inside of a sphere.
  4. * It emits the particles alongside the sphere radius. The emission direction might be randomized.
  5. */
  6. export class SphereParticleEmitter implements IParticleEmitterType {
  7. /**
  8. * Creates a new instance SphereParticleEmitter
  9. * @param radius the radius of the emission sphere (1 by default)
  10. * @param radiusRange the range of the emission sphere [0-1] 0 Surface only, 1 Entire Radius (1 by default)
  11. * @param directionRandomizer defines how much to randomize the particle direction [0-1]
  12. */
  13. constructor(
  14. /**
  15. * The radius of the emission sphere.
  16. */
  17. public radius = 1,
  18. /**
  19. * The range of emission [0-1] 0 Surface only, 1 Entire Radius.
  20. */
  21. public radiusRange = 1,
  22. /**
  23. * How much to randomize the particle direction [0-1].
  24. */
  25. public directionRandomizer = 0) {
  26. }
  27. /**
  28. * Called by the particle System when the direction is computed for the created particle.
  29. * @param worldMatrix is the world matrix of the particle system
  30. * @param directionToUpdate is the direction vector to update with the result
  31. * @param particle is the particle we are computed the direction for
  32. */
  33. public startDirectionFunction(worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle): void {
  34. var direction = particle.position.subtract(worldMatrix.getTranslation()).normalize();
  35. var randX = Scalar.RandomRange(0, this.directionRandomizer);
  36. var randY = Scalar.RandomRange(0, this.directionRandomizer);
  37. var randZ = Scalar.RandomRange(0, this.directionRandomizer);
  38. direction.x += randX;
  39. direction.y += randY;
  40. direction.z += randZ;
  41. direction.normalize();
  42. Vector3.TransformNormalFromFloatsToRef(direction.x, direction.y, direction.z, worldMatrix, directionToUpdate);
  43. }
  44. /**
  45. * Called by the particle System when the position is computed for the created particle.
  46. * @param worldMatrix is the world matrix of the particle system
  47. * @param positionToUpdate is the position vector to update with the result
  48. * @param particle is the particle we are computed the position for
  49. */
  50. public startPositionFunction(worldMatrix: Matrix, positionToUpdate: Vector3, particle: Particle): void {
  51. var randRadius = this.radius - Scalar.RandomRange(0, this.radius * this.radiusRange);
  52. var v = Scalar.RandomRange(0, 1.0);
  53. var phi = Scalar.RandomRange(0, 2 * Math.PI);
  54. var theta = Math.acos(2 * v - 1);
  55. var randX = randRadius * Math.cos(phi) * Math.sin(theta);
  56. var randY = randRadius * Math.cos(theta);
  57. var randZ = randRadius * Math.sin(phi) * Math.sin(theta);
  58. Vector3.TransformCoordinatesFromFloatsToRef(randX, randY, randZ, worldMatrix, positionToUpdate);
  59. }
  60. /**
  61. * Clones the current emitter and returns a copy of it
  62. * @returns the new emitter
  63. */
  64. public clone(): SphereParticleEmitter {
  65. let newOne = new SphereParticleEmitter(this.radius, this.directionRandomizer);
  66. Tools.DeepCopy(this, newOne);
  67. return newOne;
  68. }
  69. /**
  70. * Called by the {BABYLON.GPUParticleSystem} to setup the update shader
  71. * @param effect defines the update shader
  72. */
  73. public applyToShader(effect: Effect): void {
  74. effect.setFloat("radius", this.radius);
  75. effect.setFloat("radiusRange", this.radiusRange);
  76. effect.setFloat("directionRandomizer", this.directionRandomizer);
  77. }
  78. /**
  79. * Returns a string to use to update the GPU particles update shader
  80. * @returns a string containng the defines string
  81. */
  82. public getEffectDefines(): string {
  83. return "#define SPHEREEMITTER"
  84. }
  85. /**
  86. * Returns the string "SphereParticleEmitter"
  87. * @returns a string containing the class name
  88. */
  89. public getClassName(): string {
  90. return "SphereParticleEmitter";
  91. }
  92. /**
  93. * Serializes the particle system to a JSON object.
  94. * @returns the JSON object
  95. */
  96. public serialize(): any {
  97. var serializationObject: any = {};
  98. serializationObject.type = this.getClassName();
  99. serializationObject.radius = this.radius;
  100. serializationObject.radiusRange = this.radiusRange;
  101. serializationObject.directionRandomizer = this.directionRandomizer;
  102. return serializationObject;
  103. }
  104. /**
  105. * Parse properties from a JSON object
  106. * @param serializationObject defines the JSON object
  107. */
  108. public parse(serializationObject: any): void {
  109. this.radius = serializationObject.radius;
  110. this.radiusRange = serializationObject.radiusRange;
  111. this.directionRandomizer = serializationObject.directionRandomizer;
  112. }
  113. }
  114. /**
  115. * Particle emitter emitting particles from the inside of a sphere.
  116. * It emits the particles randomly between two vectors.
  117. */
  118. export class SphereDirectedParticleEmitter extends SphereParticleEmitter {
  119. /**
  120. * Creates a new instance SphereDirectedParticleEmitter
  121. * @param radius the radius of the emission sphere (1 by default)
  122. * @param direction1 the min limit of the emission direction (up vector by default)
  123. * @param direction2 the max limit of the emission direction (up vector by default)
  124. */
  125. constructor(radius = 1,
  126. /**
  127. * The min limit of the emission direction.
  128. */
  129. public direction1 = new Vector3(0, 1, 0),
  130. /**
  131. * The max limit of the emission direction.
  132. */
  133. public direction2 = new Vector3(0, 1, 0)) {
  134. super(radius);
  135. }
  136. /**
  137. * Called by the particle System when the direction is computed for the created particle.
  138. * @param worldMatrix is the world matrix of the particle system
  139. * @param directionToUpdate is the direction vector to update with the result
  140. * @param particle is the particle we are computed the direction for
  141. */
  142. public startDirectionFunction(worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle): void {
  143. var randX = Scalar.RandomRange(this.direction1.x, this.direction2.x);
  144. var randY = Scalar.RandomRange(this.direction1.y, this.direction2.y);
  145. var randZ = Scalar.RandomRange(this.direction1.z, this.direction2.z);
  146. Vector3.TransformNormalFromFloatsToRef(randX, randY, randZ, worldMatrix, directionToUpdate);
  147. }
  148. /**
  149. * Clones the current emitter and returns a copy of it
  150. * @returns the new emitter
  151. */
  152. public clone(): SphereDirectedParticleEmitter {
  153. let newOne = new SphereDirectedParticleEmitter(this.radius, this.direction1, this.direction2);
  154. Tools.DeepCopy(this, newOne);
  155. return newOne;
  156. }
  157. /**
  158. * Called by the GPUParticleSystem to setup the update shader
  159. * @param effect defines the update shader
  160. */
  161. public applyToShader(effect: Effect): void {
  162. effect.setFloat("radius", this.radius);
  163. effect.setFloat("radiusRange", this.radiusRange);
  164. effect.setVector3("direction1", this.direction1);
  165. effect.setVector3("direction2", this.direction2);
  166. }
  167. /**
  168. * Returns a string to use to update the GPU particles update shader
  169. * @returns a string containng the defines string
  170. */
  171. public getEffectDefines(): string {
  172. return "#define SPHEREEMITTER\n#define DIRECTEDSPHEREEMITTER"
  173. }
  174. /**
  175. * Returns the string "SphereDirectedParticleEmitter"
  176. * @returns a string containing the class name
  177. */
  178. public getClassName(): string {
  179. return "SphereDirectedParticleEmitter";
  180. }
  181. /**
  182. * Serializes the particle system to a JSON object.
  183. * @returns the JSON object
  184. */
  185. public serialize(): any {
  186. var serializationObject = super.serialize();
  187. serializationObject.direction1 = this.direction1.asArray();
  188. serializationObject.direction2 = this.direction2.asArray();
  189. return serializationObject;
  190. }
  191. /**
  192. * Parse properties from a JSON object
  193. * @param serializationObject defines the JSON object
  194. */
  195. public parse(serializationObject: any): void {
  196. super.parse(serializationObject);
  197. this.direction1.copyFrom(serializationObject.direction1);
  198. this.direction2.copyFrom(serializationObject.direction2);
  199. }
  200. }
  201. }