babylon.subMesh.js 4.3 KB

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