babylon.subMesh.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. this._boundingInfo = this._mesh._boundingInfo;
  35. return;
  36. }
  37. var extend = BABYLON.Tools.ExtractMinAndMax(data, this.verticesStart, this.verticesCount);
  38. this._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  39. };
  40. SubMesh.prototype._checkCollision = function (collider) {
  41. return this._boundingInfo._checkCollision(collider);
  42. };
  43. SubMesh.prototype.updateBoundingInfo = function (world) {
  44. this._boundingInfo._update(world);
  45. };
  46. SubMesh.prototype.isInFrustum = function (frustumPlanes) {
  47. return this._boundingInfo.isInFrustum(frustumPlanes);
  48. };
  49. SubMesh.prototype.render = function () {
  50. this._mesh.render(this);
  51. };
  52. SubMesh.prototype.getLinesIndexBuffer = function (indices, engine) {
  53. if (!this._linesIndexBuffer) {
  54. var linesIndices = [];
  55. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  56. linesIndices.push(indices[index], indices[index + 1], indices[index + 1], indices[index + 2], indices[index + 2], indices[index]);
  57. }
  58. this._linesIndexBuffer = engine.createIndexBuffer(linesIndices);
  59. this.linesIndexCount = linesIndices.length;
  60. }
  61. return this._linesIndexBuffer;
  62. };
  63. SubMesh.prototype.canIntersects = function (ray) {
  64. return ray.intersectsBox(this._boundingInfo.boundingBox);
  65. };
  66. SubMesh.prototype.intersects = function (ray, positions, indices, fastCheck) {
  67. var intersectInfo = null;
  68. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  69. var p0 = positions[indices[index]];
  70. var p1 = positions[indices[index + 1]];
  71. var p2 = positions[indices[index + 2]];
  72. var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);
  73. if (currentIntersectInfo) {
  74. if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
  75. intersectInfo = currentIntersectInfo;
  76. intersectInfo.faceId = index / 3;
  77. if (fastCheck) {
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. return intersectInfo;
  84. };
  85. // Clone
  86. SubMesh.prototype.clone = function (newMesh) {
  87. return new SubMesh(this.materialIndex, this.verticesStart, this.verticesCount, this.indexStart, this.indexCount, newMesh);
  88. };
  89. // Statics
  90. SubMesh.CreateFromIndices = function (materialIndex, startIndex, indexCount, mesh) {
  91. var minVertexIndex = Number.MAX_VALUE;
  92. var maxVertexIndex = -Number.MAX_VALUE;
  93. var indices = mesh.getIndices();
  94. for (var index = startIndex; index < startIndex + indexCount; index++) {
  95. var vertexIndex = indices[index];
  96. if (vertexIndex < minVertexIndex)
  97. minVertexIndex = vertexIndex;
  98. else if (vertexIndex > maxVertexIndex)
  99. maxVertexIndex = vertexIndex;
  100. }
  101. return new BABYLON.SubMesh(materialIndex, minVertexIndex, maxVertexIndex - minVertexIndex, startIndex, indexCount, mesh);
  102. };
  103. return SubMesh;
  104. })();
  105. BABYLON.SubMesh = SubMesh;
  106. })(BABYLON || (BABYLON = {}));
  107. //# sourceMappingURL=babylon.subMesh.js.map