babylon.instancedMesh.ts 4.9 KB

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