babylon.InstancedMesh.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. for (var index = 0; index < this._sourceMesh.subMeshes.length; index++) {
  112. this._sourceMesh.subMeshes[index].clone(this, this._sourceMesh);
  113. }
  114. };
  115. InstancedMesh.prototype._generatePointsArray = function () {
  116. return this._sourceMesh._generatePointsArray();
  117. };
  118. // Clone
  119. InstancedMesh.prototype.clone = function (name, newParent, doNotCloneChildren) {
  120. var result = this._sourceMesh.createInstance(name);
  121. // Deep copy
  122. BABYLON.Tools.DeepCopy(this, result, ["name"], []);
  123. // Bounding info
  124. this.refreshBoundingInfo();
  125. // Parent
  126. if (newParent) {
  127. result.parent = newParent;
  128. }
  129. if (!doNotCloneChildren) {
  130. for (var index = 0; index < this.getScene().meshes.length; index++) {
  131. var mesh = this.getScene().meshes[index];
  132. if (mesh.parent == this) {
  133. mesh.clone(mesh.name, result);
  134. }
  135. }
  136. }
  137. result.computeWorldMatrix(true);
  138. return result;
  139. };
  140. // Dispoe
  141. InstancedMesh.prototype.dispose = function (doNotRecurse) {
  142. // Remove from mesh
  143. var index = this._sourceMesh.instances.indexOf(this);
  144. this._sourceMesh.instances.splice(index, 1);
  145. _super.prototype.dispose.call(this, doNotRecurse);
  146. };
  147. return InstancedMesh;
  148. })(BABYLON.AbstractMesh);
  149. BABYLON.InstancedMesh = InstancedMesh;
  150. })(BABYLON || (BABYLON = {}));
  151. //# sourceMappingURL=babylon.InstancedMesh.js.map