babylon.subMesh.js 6.7 KB

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