babylon.InstancedMesh.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. module BABYLON {
  2. export class InstancedMesh extends AbstractMesh {
  3. private _sourceMesh: Mesh;
  4. constructor(name: string, source: Mesh) {
  5. super(name, source.getScene());
  6. source.instances.push(this);
  7. this._sourceMesh = source;
  8. this.position.copyFrom(source.position);
  9. this.rotation.copyFrom(source.rotation);
  10. this.scaling.copyFrom(source.scaling);
  11. if (source.rotationQuaternion) {
  12. this.rotationQuaternion = source.rotationQuaternion.clone();
  13. }
  14. this.infiniteDistance = source.infiniteDistance;
  15. this.setPivotMatrix(source.getPivotMatrix());
  16. this.refreshBoundingInfo();
  17. this._syncSubMeshes();
  18. }
  19. // Methods
  20. public get receiveShadows(): boolean {
  21. return this._sourceMesh.receiveShadows;
  22. }
  23. public get material(): Material {
  24. return this._sourceMesh.material;
  25. }
  26. public get visibility(): number {
  27. return this._sourceMesh.visibility;
  28. }
  29. public get skeleton(): Skeleton {
  30. return this._sourceMesh.skeleton;
  31. }
  32. public getTotalVertices(): number {
  33. return this._sourceMesh.getTotalVertices();
  34. }
  35. public get sourceMesh(): Mesh {
  36. return this._sourceMesh;
  37. }
  38. public getVerticesData(kind: string): number[] {
  39. return this._sourceMesh.getVerticesData(kind);
  40. }
  41. public isVerticesDataPresent(kind: string): boolean {
  42. return this._sourceMesh.isVerticesDataPresent(kind);
  43. }
  44. public getIndices(): number[] {
  45. return this._sourceMesh.getIndices();
  46. }
  47. public get _positions(): Vector3[] {
  48. return this._sourceMesh._positions;
  49. }
  50. public refreshBoundingInfo(): void {
  51. var data = this._sourceMesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  52. if (data) {
  53. var extend = BABYLON.Tools.ExtractMinAndMax(data, 0, this._sourceMesh.getTotalVertices());
  54. this._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  55. }
  56. this._updateBoundingInfo();
  57. }
  58. public _preActivate(): void {
  59. this.sourceMesh._preActivate();
  60. }
  61. public _activate(renderId: number): void {
  62. this.sourceMesh._registerInstanceForRenderId(this, renderId);
  63. }
  64. public _syncSubMeshes(): void {
  65. this.releaseSubMeshes();
  66. for (var index = 0; index < this._sourceMesh.subMeshes.length; index++) {
  67. this._sourceMesh.subMeshes[index].clone(this, this._sourceMesh);
  68. }
  69. }
  70. public _generatePointsArray(): boolean {
  71. return this._sourceMesh._generatePointsArray();
  72. }
  73. // Clone
  74. public clone(name: string, newParent: Node, doNotCloneChildren?: boolean): InstancedMesh {
  75. var result = this._sourceMesh.createInstance(name);
  76. // Deep copy
  77. BABYLON.Tools.DeepCopy(this, result, ["name"], []);
  78. // Bounding info
  79. this.refreshBoundingInfo();
  80. // Parent
  81. if (newParent) {
  82. result.parent = newParent;
  83. }
  84. if (!doNotCloneChildren) {
  85. // Children
  86. for (var index = 0; index < this.getScene().meshes.length; index++) {
  87. var mesh = this.getScene().meshes[index];
  88. if (mesh.parent == this) {
  89. mesh.clone(mesh.name, result);
  90. }
  91. }
  92. }
  93. result.computeWorldMatrix(true);
  94. return result;
  95. }
  96. // Dispoe
  97. public dispose(doNotRecurse?: boolean): void {
  98. // Remove from mesh
  99. var index = this._sourceMesh.instances.indexOf(this);
  100. this._sourceMesh.instances.splice(index, 1);
  101. super.dispose(doNotRecurse);
  102. }
  103. }
  104. }