babylon.subMesh.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var SubMesh = (function () {
  4. function SubMesh(materialIndex, verticesStart, verticesCount, indexStart, indexCount, mesh, renderingMesh, createBoundingBox) {
  5. if (createBoundingBox === void 0) { createBoundingBox = true; }
  6. this.materialIndex = materialIndex;
  7. this.verticesStart = verticesStart;
  8. this.verticesCount = verticesCount;
  9. this.indexStart = indexStart;
  10. this.indexCount = indexCount;
  11. this._renderId = 0;
  12. this._mesh = mesh;
  13. this._renderingMesh = renderingMesh || mesh;
  14. mesh.subMeshes.push(this);
  15. this._id = mesh.subMeshes.length - 1;
  16. if (createBoundingBox) {
  17. this.refreshBoundingInfo();
  18. mesh.computeWorldMatrix(true);
  19. }
  20. }
  21. SubMesh.prototype.getBoundingInfo = function () {
  22. return this._boundingInfo;
  23. };
  24. SubMesh.prototype.getMesh = function () {
  25. return this._mesh;
  26. };
  27. SubMesh.prototype.getRenderingMesh = function () {
  28. return this._renderingMesh;
  29. };
  30. SubMesh.prototype.getMaterial = function () {
  31. var rootMaterial = this._renderingMesh.material;
  32. if (rootMaterial && rootMaterial instanceof BABYLON.MultiMaterial) {
  33. var multiMaterial = rootMaterial;
  34. return multiMaterial.getSubMaterial(this.materialIndex);
  35. }
  36. if (!rootMaterial) {
  37. return this._mesh.getScene().defaultMaterial;
  38. }
  39. return rootMaterial;
  40. };
  41. // Methods
  42. SubMesh.prototype.refreshBoundingInfo = function () {
  43. var data = this._renderingMesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  44. if (!data) {
  45. this._boundingInfo = this._mesh._boundingInfo;
  46. return;
  47. }
  48. var indices = this._renderingMesh.getIndices();
  49. var extend;
  50. if (this.indexStart === 0 && this.indexCount === indices.length) {
  51. extend = BABYLON.Tools.ExtractMinAndMax(data, this.verticesStart, this.verticesCount);
  52. }
  53. else {
  54. extend = BABYLON.Tools.ExtractMinAndMaxIndexed(data, indices, this.indexStart, this.indexCount);
  55. }
  56. this._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  57. };
  58. SubMesh.prototype._checkCollision = function (collider) {
  59. return this._boundingInfo._checkCollision(collider);
  60. };
  61. SubMesh.prototype.updateBoundingInfo = function (world) {
  62. if (!this._boundingInfo) {
  63. this.refreshBoundingInfo();
  64. }
  65. this._boundingInfo._update(world);
  66. };
  67. SubMesh.prototype.isInFrustum = function (frustumPlanes) {
  68. return this._boundingInfo.isInFrustum(frustumPlanes);
  69. };
  70. SubMesh.prototype.render = function () {
  71. this._renderingMesh.render(this);
  72. };
  73. SubMesh.prototype.getLinesIndexBuffer = function (indices, engine) {
  74. if (!this._linesIndexBuffer) {
  75. var linesIndices = [];
  76. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  77. linesIndices.push(indices[index], indices[index + 1], indices[index + 1], indices[index + 2], indices[index + 2], indices[index]);
  78. }
  79. this._linesIndexBuffer = engine.createIndexBuffer(linesIndices);
  80. this.linesIndexCount = linesIndices.length;
  81. }
  82. return this._linesIndexBuffer;
  83. };
  84. SubMesh.prototype.canIntersects = function (ray) {
  85. return ray.intersectsBox(this._boundingInfo.boundingBox);
  86. };
  87. SubMesh.prototype.intersects = function (ray, positions, indices, fastCheck) {
  88. var intersectInfo = null;
  89. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  90. var p0 = positions[indices[index]];
  91. var p1 = positions[indices[index + 1]];
  92. var p2 = positions[indices[index + 2]];
  93. var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);
  94. if (currentIntersectInfo) {
  95. if (currentIntersectInfo.distance < 0) {
  96. continue;
  97. }
  98. if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
  99. intersectInfo = currentIntersectInfo;
  100. intersectInfo.faceId = index / 3;
  101. if (fastCheck) {
  102. break;
  103. }
  104. }
  105. }
  106. }
  107. return intersectInfo;
  108. };
  109. // Clone
  110. SubMesh.prototype.clone = function (newMesh, newRenderingMesh) {
  111. var result = new SubMesh(this.materialIndex, this.verticesStart, this.verticesCount, this.indexStart, this.indexCount, newMesh, newRenderingMesh, false);
  112. result._boundingInfo = new BABYLON.BoundingInfo(this._boundingInfo.minimum, this._boundingInfo.maximum);
  113. return result;
  114. };
  115. // Dispose
  116. SubMesh.prototype.dispose = function () {
  117. if (this._linesIndexBuffer) {
  118. this._mesh.getScene().getEngine()._releaseBuffer(this._linesIndexBuffer);
  119. this._linesIndexBuffer = null;
  120. }
  121. // Remove from mesh
  122. var index = this._mesh.subMeshes.indexOf(this);
  123. this._mesh.subMeshes.splice(index, 1);
  124. };
  125. // Statics
  126. SubMesh.CreateFromIndices = function (materialIndex, startIndex, indexCount, mesh, renderingMesh) {
  127. var minVertexIndex = Number.MAX_VALUE;
  128. var maxVertexIndex = -Number.MAX_VALUE;
  129. renderingMesh = renderingMesh || mesh;
  130. var indices = renderingMesh.getIndices();
  131. for (var index = startIndex; index < startIndex + indexCount; index++) {
  132. var vertexIndex = indices[index];
  133. if (vertexIndex < minVertexIndex)
  134. minVertexIndex = vertexIndex;
  135. if (vertexIndex > maxVertexIndex)
  136. maxVertexIndex = vertexIndex;
  137. }
  138. return new BABYLON.SubMesh(materialIndex, minVertexIndex, maxVertexIndex - minVertexIndex + 1, startIndex, indexCount, mesh, renderingMesh);
  139. };
  140. return SubMesh;
  141. })();
  142. BABYLON.SubMesh = SubMesh;
  143. })(BABYLON || (BABYLON = {}));
  144. //# sourceMappingURL=babylon.subMesh.js.map