babylon.subMesh.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. //ANY
  21. SubMesh.prototype.getMaterial = function () {
  22. var rootMaterial = this._mesh.material;
  23. if (rootMaterial && rootMaterial.getSubMaterial) {
  24. return rootMaterial.getSubMaterial(this.materialIndex);
  25. }
  26. if (!rootMaterial) {
  27. return this._mesh.getScene().defaultMaterial;
  28. }
  29. return rootMaterial;
  30. };
  31. // Methods
  32. SubMesh.prototype.refreshBoundingInfo = function () {
  33. var data = this._mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  34. if (!data) {
  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, scale) {
  44. if (typeof scale === "undefined") { scale = 1.0; }
  45. this._boundingInfo._update(world, scale);
  46. };
  47. SubMesh.prototype.isInFrustum = function (frustumPlanes) {
  48. return this._boundingInfo.isInFrustum(frustumPlanes);
  49. };
  50. SubMesh.prototype.render = function () {
  51. this._mesh.render(this);
  52. };
  53. //ANY
  54. SubMesh.prototype.getLinesIndexBuffer = function (indices, engine) {
  55. if (!this._linesIndexBuffer) {
  56. var linesIndices = [];
  57. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  58. linesIndices.push(indices[index], indices[index + 1], indices[index + 1], indices[index + 2], indices[index + 2], indices[index]);
  59. }
  60. this._linesIndexBuffer = engine.createIndexBuffer(linesIndices);
  61. this.linesIndexCount = linesIndices.length;
  62. }
  63. return this._linesIndexBuffer;
  64. };
  65. SubMesh.prototype.canIntersects = function (ray) {
  66. return ray.intersectsBox(this._boundingInfo.boundingBox);
  67. };
  68. //ANY create intersectinfo class
  69. SubMesh.prototype.intersects = function (ray, positions, indices, fastCheck) {
  70. var intersectInfo = null;
  71. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  72. var p0 = positions[indices[index]];
  73. var p1 = positions[indices[index + 1]];
  74. var p2 = positions[indices[index + 2]];
  75. var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);
  76. if (currentIntersectInfo) {
  77. if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
  78. intersectInfo = currentIntersectInfo;
  79. intersectInfo.faceId = index / 3;
  80. if (fastCheck) {
  81. break;
  82. }
  83. }
  84. }
  85. }
  86. return intersectInfo;
  87. };
  88. // Clone
  89. SubMesh.prototype.clone = function (newMesh) {
  90. return new SubMesh(this.materialIndex, this.verticesStart, this.verticesCount, this.indexStart, this.indexCount, newMesh);
  91. };
  92. // Statics
  93. SubMesh.CreateFromIndices = function (materialIndex, startIndex, indexCount, mesh) {
  94. var minVertexIndex = Number.MAX_VALUE;
  95. var maxVertexIndex = -Number.MAX_VALUE;
  96. var indices = mesh.getIndices();
  97. for (var index = startIndex; index < startIndex + indexCount; index++) {
  98. var vertexIndex = indices[index];
  99. if (vertexIndex < minVertexIndex)
  100. minVertexIndex = vertexIndex;
  101. else if (vertexIndex > maxVertexIndex)
  102. maxVertexIndex = vertexIndex;
  103. }
  104. return new BABYLON.SubMesh(materialIndex, minVertexIndex, maxVertexIndex - minVertexIndex, startIndex, indexCount, mesh);
  105. };
  106. return SubMesh;
  107. })();
  108. BABYLON.SubMesh = SubMesh;
  109. })(BABYLON || (BABYLON = {}));
  110. //# sourceMappingURL=babylon.subMesh.js.map