babylon.InstancedMesh.js 6.0 KB

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