babylon.InstancedMesh.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 _activate(renderId: number): void {
  59. this.sourceMesh._registerInstanceForRenderId(this, renderId);
  60. }
  61. public _syncSubMeshes(): void {
  62. this.releaseSubMeshes();
  63. for (var index = 0; index < this._sourceMesh.subMeshes.length; index++) {
  64. this._sourceMesh.subMeshes[index].clone(this, this._sourceMesh);
  65. }
  66. }
  67. public _generatePointsArray(): boolean {
  68. return this._sourceMesh._generatePointsArray();
  69. }
  70. // Clone
  71. public clone(name: string, newParent: Node, doNotCloneChildren?: boolean): InstancedMesh {
  72. var result = this._sourceMesh.createInstance(name);
  73. // Deep copy
  74. BABYLON.Tools.DeepCopy(this, result, ["name"], []);
  75. // Bounding info
  76. this.refreshBoundingInfo();
  77. // Parent
  78. if (newParent) {
  79. result.parent = newParent;
  80. }
  81. if (!doNotCloneChildren) {
  82. // Children
  83. for (var index = 0; index < this.getScene().meshes.length; index++) {
  84. var mesh = this.getScene().meshes[index];
  85. if (mesh.parent == this) {
  86. mesh.clone(mesh.name, result);
  87. }
  88. }
  89. }
  90. result.computeWorldMatrix(true);
  91. return result;
  92. }
  93. // Dispoe
  94. public dispose(doNotRecurse?: boolean): void {
  95. // Remove from mesh
  96. var index = this._sourceMesh.instances.indexOf(this);
  97. this._sourceMesh.instances.splice(index, 1);
  98. super.dispose(doNotRecurse);
  99. }
  100. }
  101. }