babylon.InstancedMesh.ts 4.6 KB

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