babylon.subMesh.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var SubMesh = (function () {
  4. function SubMesh(materialIndex, verticesStart, verticesCount, indexStart, indexCount, mesh) {
  5. this.materialIndex = materialIndex;
  6. this.verticesStart = verticesStart;
  7. this.verticesCount = verticesCount;
  8. this.indexStart = indexStart;
  9. this.indexCount = indexCount;
  10. this._mesh = mesh;
  11. mesh.subMeshes.push(this);
  12. this.refreshBoundingInfo();
  13. }
  14. SubMesh.prototype.getBoundingInfo = function () {
  15. return this._boundingInfo;
  16. };
  17. SubMesh.prototype.getMesh = function () {
  18. return this._mesh;
  19. };
  20. SubMesh.prototype.getMaterial = function () {
  21. var rootMaterial = this._mesh.material;
  22. if (rootMaterial && rootMaterial.getSubMaterial) {
  23. return rootMaterial.getSubMaterial(this.materialIndex);
  24. }
  25. if (!rootMaterial) {
  26. return this._mesh.getScene().defaultMaterial;
  27. }
  28. return rootMaterial;
  29. };
  30. // Methods
  31. SubMesh.prototype.refreshBoundingInfo = function () {
  32. var data = this._mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  33. if (!data) {
  34. return;
  35. }
  36. var extend = BABYLON.Tools.ExtractMinAndMax(data, this.verticesStart, this.verticesCount);
  37. this._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  38. };
  39. SubMesh.prototype._checkCollision = function (collider) {
  40. return this._boundingInfo._checkCollision(collider);
  41. };
  42. SubMesh.prototype.updateBoundingInfo = function (world) {
  43. this._boundingInfo._update(world);
  44. };
  45. SubMesh.prototype.isInFrustum = function (frustumPlanes) {
  46. return this._boundingInfo.isInFrustum(frustumPlanes);
  47. };
  48. SubMesh.prototype.render = function () {
  49. this._mesh.render(this);
  50. };
  51. SubMesh.prototype.getLinesIndexBuffer = function (indices, engine) {
  52. if (!this._linesIndexBuffer) {
  53. var linesIndices = [];
  54. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  55. linesIndices.push(indices[index], indices[index + 1], indices[index + 1], indices[index + 2], indices[index + 2], indices[index]);
  56. }
  57. this._linesIndexBuffer = engine.createIndexBuffer(linesIndices);
  58. this.linesIndexCount = linesIndices.length;
  59. }
  60. return this._linesIndexBuffer;
  61. };
  62. SubMesh.prototype.canIntersects = function (ray) {
  63. return ray.intersectsBox(this._boundingInfo.boundingBox);
  64. };
  65. SubMesh.prototype.intersects = function (ray, positions, indices, fastCheck) {
  66. var intersectInfo = null;
  67. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  68. var p0 = positions[indices[index]];
  69. var p1 = positions[indices[index + 1]];
  70. var p2 = positions[indices[index + 2]];
  71. var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);
  72. if (currentIntersectInfo) {
  73. if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
  74. intersectInfo = currentIntersectInfo;
  75. intersectInfo.faceId = index / 3;
  76. if (fastCheck) {
  77. break;
  78. }
  79. }
  80. }
  81. }
  82. return intersectInfo;
  83. };
  84. // Clone
  85. SubMesh.prototype.clone = function (newMesh) {
  86. return new SubMesh(this.materialIndex, this.verticesStart, this.verticesCount, this.indexStart, this.indexCount, newMesh);
  87. };
  88. // Statics
  89. SubMesh.CreateFromIndices = function (materialIndex, startIndex, indexCount, mesh) {
  90. var minVertexIndex = Number.MAX_VALUE;
  91. var maxVertexIndex = -Number.MAX_VALUE;
  92. var indices = mesh.getIndices();
  93. for (var index = startIndex; index < startIndex + indexCount; index++) {
  94. var vertexIndex = indices[index];
  95. if (vertexIndex < minVertexIndex)
  96. minVertexIndex = vertexIndex;
  97. else if (vertexIndex > maxVertexIndex)
  98. maxVertexIndex = vertexIndex;
  99. }
  100. return new BABYLON.SubMesh(materialIndex, minVertexIndex, maxVertexIndex - minVertexIndex, startIndex, indexCount, mesh);
  101. };
  102. return SubMesh;
  103. })();
  104. BABYLON.SubMesh = SubMesh;
  105. })(BABYLON || (BABYLON = {}));
  106. //# sourceMappingURL=babylon.subMesh.js.map