babylon.subMesh.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. var extend = BABYLON.Tools.ExtractMinAndMax(this._mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind), verticesStart, verticesCount);
  12. this._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  13. };
  14. //Properties
  15. BABYLON.SubMesh.prototype.getBoundingInfo = function() {
  16. return this._boundingInfo;
  17. };
  18. BABYLON.SubMesh.prototype.getMesh = function () {
  19. return this._mesh;
  20. };
  21. BABYLON.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._scene.defaultMaterial;
  28. }
  29. return rootMaterial;
  30. };
  31. // Methods
  32. BABYLON.SubMesh.prototype._checkCollision = function (collider) {
  33. return this._boundingInfo._checkCollision(collider);
  34. };
  35. BABYLON.SubMesh.prototype.updateBoundingInfo = function(world, scale) {
  36. this._boundingInfo._update(world, scale);
  37. };
  38. BABYLON.SubMesh.prototype.isInFrustrum = function (frustumPlanes) {
  39. return this._boundingInfo.isInFrustrum(frustumPlanes);
  40. };
  41. BABYLON.SubMesh.prototype.render = function() {
  42. this._mesh.render(this);
  43. };
  44. BABYLON.SubMesh.prototype.getLinesIndexBuffer = function(indices, engine) {
  45. if (!this._linesIndexBuffer) {
  46. var linesIndices = [];
  47. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  48. linesIndices.push( indices[index], indices[index + 1],
  49. indices[index + 1], indices[index + 2],
  50. indices[index + 2], indices[index]);
  51. }
  52. this._linesIndexBuffer = engine.createIndexBuffer(linesIndices);
  53. this.linesIndexCount = linesIndices.length;
  54. }
  55. return this._linesIndexBuffer;
  56. };
  57. BABYLON.SubMesh.prototype.canIntersects = function(ray) {
  58. return ray.intersectsBox(this._boundingInfo.boundingBox);
  59. };
  60. BABYLON.SubMesh.prototype.intersects = function (ray, positions, indices) {
  61. var distance = Number.MAX_VALUE;
  62. // Triangles test
  63. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  64. var p0 = positions[indices[index]];
  65. var p1 = positions[indices[index + 1]];
  66. var p2 = positions[indices[index + 2]];
  67. var result = ray.intersectsTriangle(p0, p1, p2);
  68. if (result.hit) {
  69. if (result.distance < distance && result.distance >= 0) {
  70. distance = result.distance;
  71. }
  72. }
  73. }
  74. if (distance >= 0)
  75. return { hit: true, distance: distance };
  76. return { hit: false, distance: 0 };
  77. };
  78. // Clone
  79. BABYLON.SubMesh.prototype.clone = function(newMesh) {
  80. return new BABYLON.SubMesh(this.materialIndex, this.verticesStart, this.verticesCount, this.indexStart, this.indexCount, newMesh);
  81. };
  82. // Statics
  83. BABYLON.SubMesh.CreateFromIndices = function(materialIndex, startIndex, indexCount, mesh) {
  84. var minVertexIndex = Number.MAX_VALUE;
  85. var maxVertexIndex = -Number.MAX_VALUE;
  86. var indices = mesh.getIndices();
  87. for (var index = startIndex; index < startIndex + indexCount; index++) {
  88. var vertexIndex = indices[index];
  89. if (vertexIndex < minVertexIndex)
  90. minVertexIndex = vertexIndex;
  91. else if (vertexIndex > maxVertexIndex)
  92. maxVertexIndex = vertexIndex;
  93. }
  94. return new BABYLON.SubMesh(materialIndex, minVertexIndex, maxVertexIndex - minVertexIndex, startIndex, indexCount, mesh);
  95. };
  96. })();