babylon.instancedMesh.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var BABYLON;
  7. (function (BABYLON) {
  8. /**
  9. * Creates an instance based on a source mesh.
  10. */
  11. var InstancedMesh = (function (_super) {
  12. __extends(InstancedMesh, _super);
  13. function InstancedMesh(name, source) {
  14. _super.call(this, name, source.getScene());
  15. source.instances.push(this);
  16. this._sourceMesh = source;
  17. this.position.copyFrom(source.position);
  18. this.rotation.copyFrom(source.rotation);
  19. this.scaling.copyFrom(source.scaling);
  20. if (source.rotationQuaternion) {
  21. this.rotationQuaternion = source.rotationQuaternion.clone();
  22. }
  23. this.infiniteDistance = source.infiniteDistance;
  24. this.setPivotMatrix(source.getPivotMatrix());
  25. this.refreshBoundingInfo();
  26. this._syncSubMeshes();
  27. }
  28. Object.defineProperty(InstancedMesh.prototype, "receiveShadows", {
  29. // Methods
  30. get: function () {
  31. return this._sourceMesh.receiveShadows;
  32. },
  33. enumerable: true,
  34. configurable: true
  35. });
  36. Object.defineProperty(InstancedMesh.prototype, "material", {
  37. get: function () {
  38. return this._sourceMesh.material;
  39. },
  40. enumerable: true,
  41. configurable: true
  42. });
  43. Object.defineProperty(InstancedMesh.prototype, "visibility", {
  44. get: function () {
  45. return this._sourceMesh.visibility;
  46. },
  47. enumerable: true,
  48. configurable: true
  49. });
  50. Object.defineProperty(InstancedMesh.prototype, "skeleton", {
  51. get: function () {
  52. return this._sourceMesh.skeleton;
  53. },
  54. enumerable: true,
  55. configurable: true
  56. });
  57. Object.defineProperty(InstancedMesh.prototype, "renderingGroupId", {
  58. get: function () {
  59. return this._sourceMesh.renderingGroupId;
  60. },
  61. enumerable: true,
  62. configurable: true
  63. });
  64. InstancedMesh.prototype.getTotalVertices = function () {
  65. return this._sourceMesh.getTotalVertices();
  66. };
  67. Object.defineProperty(InstancedMesh.prototype, "sourceMesh", {
  68. get: function () {
  69. return this._sourceMesh;
  70. },
  71. enumerable: true,
  72. configurable: true
  73. });
  74. InstancedMesh.prototype.getVerticesData = function (kind, copyWhenShared) {
  75. return this._sourceMesh.getVerticesData(kind, copyWhenShared);
  76. };
  77. InstancedMesh.prototype.isVerticesDataPresent = function (kind) {
  78. return this._sourceMesh.isVerticesDataPresent(kind);
  79. };
  80. InstancedMesh.prototype.getIndices = function () {
  81. return this._sourceMesh.getIndices();
  82. };
  83. Object.defineProperty(InstancedMesh.prototype, "_positions", {
  84. get: function () {
  85. return this._sourceMesh._positions;
  86. },
  87. enumerable: true,
  88. configurable: true
  89. });
  90. InstancedMesh.prototype.refreshBoundingInfo = function () {
  91. var meshBB = this._sourceMesh.getBoundingInfo();
  92. this._boundingInfo = new BABYLON.BoundingInfo(meshBB.minimum.clone(), meshBB.maximum.clone());
  93. this._updateBoundingInfo();
  94. };
  95. InstancedMesh.prototype._preActivate = function () {
  96. if (this._currentLOD) {
  97. this._currentLOD._preActivate();
  98. }
  99. };
  100. InstancedMesh.prototype._activate = function (renderId) {
  101. if (this._currentLOD) {
  102. this._currentLOD._registerInstanceForRenderId(this, renderId);
  103. }
  104. };
  105. InstancedMesh.prototype.getLOD = function (camera) {
  106. this._currentLOD = this.sourceMesh.getLOD(this.getScene().activeCamera, this.getBoundingInfo().boundingSphere);
  107. if (this._currentLOD === this.sourceMesh) {
  108. return this;
  109. }
  110. return this._currentLOD;
  111. };
  112. InstancedMesh.prototype._syncSubMeshes = function () {
  113. this.releaseSubMeshes();
  114. if (this._sourceMesh.subMeshes) {
  115. for (var index = 0; index < this._sourceMesh.subMeshes.length; index++) {
  116. this._sourceMesh.subMeshes[index].clone(this, this._sourceMesh);
  117. }
  118. }
  119. };
  120. InstancedMesh.prototype._generatePointsArray = function () {
  121. return this._sourceMesh._generatePointsArray();
  122. };
  123. // Clone
  124. InstancedMesh.prototype.clone = function (name, newParent, doNotCloneChildren) {
  125. var result = this._sourceMesh.createInstance(name);
  126. // Deep copy
  127. BABYLON.Tools.DeepCopy(this, result, ["name"], []);
  128. // Bounding info
  129. this.refreshBoundingInfo();
  130. // Parent
  131. if (newParent) {
  132. result.parent = newParent;
  133. }
  134. if (!doNotCloneChildren) {
  135. // Children
  136. for (var index = 0; index < this.getScene().meshes.length; index++) {
  137. var mesh = this.getScene().meshes[index];
  138. if (mesh.parent === this) {
  139. mesh.clone(mesh.name, result);
  140. }
  141. }
  142. }
  143. result.computeWorldMatrix(true);
  144. return result;
  145. };
  146. // Dispoe
  147. InstancedMesh.prototype.dispose = function (doNotRecurse) {
  148. // Remove from mesh
  149. var index = this._sourceMesh.instances.indexOf(this);
  150. this._sourceMesh.instances.splice(index, 1);
  151. _super.prototype.dispose.call(this, doNotRecurse);
  152. };
  153. return InstancedMesh;
  154. }(BABYLON.AbstractMesh));
  155. BABYLON.InstancedMesh = InstancedMesh;
  156. })(BABYLON || (BABYLON = {}));