babylon.solidParticle.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. module BABYLON {
  2. export class SolidParticle {
  3. public idx: number = 0; // particle global index
  4. public color = new Color4(1.0, 1.0, 1.0, 1.0); // color
  5. public position = Vector3.Zero(); // position
  6. public rotation = Vector3.Zero(); // rotation
  7. public rotationQuaternion: Quaternion; // quaternion, will overwrite rotation
  8. public scaling = Vector3.One(); // scaling
  9. public uvs = new Vector4(0.0, 0.0, 1.0, 1.0); // uvs
  10. public velocity = Vector3.Zero(); // velocity
  11. public alive = true; // alive
  12. public isVisible = true; // visibility
  13. public _pos: number = 0; // index of this particle in the global "positions" array
  14. public _ind: number = 0; // index of this particle in the global "indices" array
  15. public _model: ModelShape; // model shape reference
  16. public shapeId: number = 0; // model shape id
  17. public idxInShape: number = 0; // index of the particle in its shape id
  18. public _modelBoundingInfo: BoundingInfo; // reference to the shape model BoundingInfo object
  19. public _boundingInfo: BoundingInfo; // particle BoundingInfo
  20. public _sps: SolidParticleSystem; // reference to the SPS what the particle belongs to
  21. public _stillInvisible: boolean = false; // still set as invisible in order to skip useless computations
  22. /**
  23. * Creates a Solid Particle object.
  24. * Don't create particles manually, use instead the Solid Particle System internal tools like _addParticle()
  25. * `particleIndex` (integer) is the particle index in the Solid Particle System pool. It's also the particle identifier.
  26. * `positionIndex` (integer) is the starting index of the particle vertices in the SPS "positions" array.
  27. * `indiceIndex` (integer) is the starting index of the particle indices in the SPS "indices" array.
  28. * `model` (ModelShape) is a reference to the model shape on what the particle is designed.
  29. * `shapeId` (integer) is the model shape identifier in the SPS.
  30. * `idxInShape` (integer) is the index of the particle in the current model (ex: the 10th box of addShape(box, 30))
  31. * `modelBoundingInfo` is the reference to the model BoundingInfo used for intersection computations.
  32. */
  33. constructor(particleIndex: number, positionIndex: number, indiceIndex: number, model: ModelShape, shapeId: number, idxInShape: number, sps: SolidParticleSystem, modelBoundingInfo?: BoundingInfo) {
  34. this.idx = particleIndex;
  35. this._pos = positionIndex;
  36. this._ind = indiceIndex;
  37. this._model = model;
  38. this.shapeId = shapeId;
  39. this.idxInShape = idxInShape;
  40. this._sps = sps;
  41. if (modelBoundingInfo) {
  42. this._modelBoundingInfo = modelBoundingInfo;
  43. this._boundingInfo = new BoundingInfo(modelBoundingInfo.minimum, modelBoundingInfo.maximum);
  44. }
  45. }
  46. /**
  47. * legacy support, changed scale to scaling
  48. */
  49. public get scale(): Vector3 {
  50. return this.scaling;
  51. }
  52. public set scale(scale: Vector3) {
  53. this.scaling = scale;
  54. }
  55. /**
  56. * legacy support, changed quaternion to rotationQuaternion
  57. */
  58. public get quaternion(): Quaternion {
  59. return this.rotationQuaternion;
  60. }
  61. public set quaternion(q: Quaternion) {
  62. this.rotationQuaternion = q;
  63. }
  64. /**
  65. * Returns a boolean. True if the particle intersects another particle or another mesh, else false.
  66. * The intersection is computed on the particle bounding sphere and Axis Aligned Bounding Box (AABB)
  67. * `target` is the object (solid particle or mesh) what the intersection is computed against.
  68. */
  69. public intersectsMesh(target: Mesh | SolidParticle): boolean {
  70. if (!this._boundingInfo || !target._boundingInfo) {
  71. return false;
  72. }
  73. if (this._sps._bSphereOnly) {
  74. return BoundingSphere.Intersects(this._boundingInfo.boundingSphere, target._boundingInfo.boundingSphere);
  75. }
  76. return this._boundingInfo.intersects(target._boundingInfo, false);
  77. }
  78. }
  79. export class ModelShape {
  80. public shapeID: number;
  81. public _shape: Vector3[]; // flat array of model positions
  82. public _shapeUV: number[]; // flat array of model UVs
  83. public _indicesLength: number = 0; // length of the shape in the model indices array
  84. public _positionFunction: (particle: SolidParticle, i: number, s: number) => void;
  85. public _vertexFunction: (particle: SolidParticle, vertex: Vector3, i: number) => void;
  86. /**
  87. * Creates a ModelShape object. This is an internal simplified reference to a mesh used as for a model to replicate particles from by the SPS.
  88. * SPS internal tool, don't use it manually.
  89. */
  90. constructor(id: number, shape: Vector3[], indicesLength: number, shapeUV: number[], posFunction: (particle: SolidParticle, i: number, s: number) => void, vtxFunction: (particle: SolidParticle, vertex: Vector3, i: number) => void) {
  91. this.shapeID = id;
  92. this._shape = shape;
  93. this._indicesLength = indicesLength;
  94. this._shapeUV = shapeUV;
  95. this._positionFunction = posFunction;
  96. this._vertexFunction = vtxFunction;
  97. }
  98. }
  99. export class DepthSortedParticle {
  100. public ind: number = 0; // index of the particle in the "indices" array
  101. public indicesLength: number = 0; // length of the particle shape in the "indices" array
  102. public sqDistance: number = 0.0; // squared distance from the particle to the camera
  103. }
  104. }