babylon.instancedMesh.ts 4.6 KB

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