babylon.subMesh.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.SubMesh = function (materialIndex, verticesStart, verticesCount, indexStart, indexCount, mesh) {
  4. this._mesh = mesh;
  5. mesh.subMeshes.push(this);
  6. this.materialIndex = materialIndex;
  7. this.verticesStart = verticesStart;
  8. this.verticesCount = verticesCount;
  9. this.indexStart = indexStart;
  10. this.indexCount = indexCount;
  11. this.refreshBoundingInfo();
  12. };
  13. //Properties
  14. BABYLON.SubMesh.prototype.getBoundingInfo = function () {
  15. return this._boundingInfo;
  16. };
  17. BABYLON.SubMesh.prototype.getMesh = function () {
  18. return this._mesh;
  19. };
  20. BABYLON.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._scene.defaultMaterial;
  27. }
  28. return rootMaterial;
  29. };
  30. // Methods
  31. BABYLON.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. BABYLON.SubMesh.prototype._checkCollision = function (collider) {
  40. return this._boundingInfo._checkCollision(collider);
  41. };
  42. BABYLON.SubMesh.prototype.updateBoundingInfo = function (world, scale) {
  43. this._boundingInfo._update(world, scale);
  44. };
  45. BABYLON.SubMesh.prototype.isInFrustrum = function (frustumPlanes) {
  46. return this._boundingInfo.isInFrustrum(frustumPlanes);
  47. };
  48. BABYLON.SubMesh.prototype.render = function () {
  49. this._mesh.render(this);
  50. };
  51. BABYLON.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],
  56. indices[index + 1], indices[index + 2],
  57. indices[index + 2], indices[index]);
  58. }
  59. this._linesIndexBuffer = engine.createIndexBuffer(linesIndices);
  60. this.linesIndexCount = linesIndices.length;
  61. }
  62. return this._linesIndexBuffer;
  63. };
  64. BABYLON.SubMesh.prototype.canIntersects = function (ray) {
  65. return ray.intersectsBox(this._boundingInfo.boundingBox);
  66. };
  67. BABYLON.SubMesh.prototype.intersects = function (ray, positions, indices, fastCheck) {
  68. var distance = Number.MAX_VALUE;
  69. // Triangles test
  70. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  71. var p0 = positions[indices[index]];
  72. var p1 = positions[indices[index + 1]];
  73. var p2 = positions[indices[index + 2]];
  74. var currentDistance = ray.intersectsTriangle(p0, p1, p2);
  75. if (currentDistance > 0) {
  76. if (fastCheck || currentDistance < distance) {
  77. distance = currentDistance;
  78. if (fastCheck) {
  79. break;
  80. }
  81. }
  82. }
  83. }
  84. if (distance > 0 && distance < Number.MAX_VALUE)
  85. return distance;
  86. return 0;
  87. };
  88. // Clone
  89. BABYLON.SubMesh.prototype.clone = function (newMesh) {
  90. return new BABYLON.SubMesh(this.materialIndex, this.verticesStart, this.verticesCount, this.indexStart, this.indexCount, newMesh);
  91. };
  92. // Statics
  93. BABYLON.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. })();