babylon.subMesh.js 6.6 KB

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