babylon.subMesh.js 6.8 KB

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