babylon.subMesh.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. module BABYLON {
  2. export class SubMesh {
  3. public linesIndexCount: number;
  4. private _mesh: Mesh;
  5. private _boundingInfo: BoundingInfo;
  6. private _linesIndexBuffer: WebGLBuffer;
  7. public _lastColliderWorldVertices: Vector3[];
  8. public _trianglePlanes: Plane[];
  9. public _lastColliderTransformMatrix: Matrix;
  10. constructor(public materialIndex: number, public verticesStart: number, public verticesCount: number, public indexStart, public indexCount: number, mesh: Mesh) {
  11. this._mesh = mesh;
  12. mesh.subMeshes.push(this);
  13. this.refreshBoundingInfo();
  14. }
  15. public getBoundingInfo(): BoundingInfo {
  16. return this._boundingInfo;
  17. }
  18. public getMesh(): Mesh {
  19. return this._mesh;
  20. }
  21. public getMaterial(): Material {
  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. public refreshBoundingInfo(): void {
  33. var data = this._mesh.getVerticesData(VertexBuffer.PositionKind);
  34. if (!data) {
  35. this._boundingInfo = this._mesh._boundingInfo;
  36. return;
  37. }
  38. var extend = BABYLON.Tools.ExtractMinAndMax(data, this.verticesStart, this.verticesCount);
  39. this._boundingInfo = new BoundingInfo(extend.minimum, extend.maximum);
  40. }
  41. public _checkCollision(collider: Collider): boolean {
  42. return this._boundingInfo._checkCollision(collider);
  43. }
  44. public updateBoundingInfo(world: Matrix): void {
  45. if (!this._boundingInfo) {
  46. this.refreshBoundingInfo();
  47. }
  48. this._boundingInfo._update(world);
  49. }
  50. public isInFrustum(frustumPlanes: Plane[]): boolean {
  51. return this._boundingInfo.isInFrustum(frustumPlanes);
  52. }
  53. public render(): void {
  54. this._mesh.render(this);
  55. }
  56. public getLinesIndexBuffer(indices: number[], engine): WebGLBuffer {
  57. if (!this._linesIndexBuffer) {
  58. var linesIndices = [];
  59. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  60. linesIndices.push(indices[index], indices[index + 1],
  61. indices[index + 1], indices[index + 2],
  62. indices[index + 2], indices[index]);
  63. }
  64. this._linesIndexBuffer = engine.createIndexBuffer(linesIndices);
  65. this.linesIndexCount = linesIndices.length;
  66. }
  67. return this._linesIndexBuffer;
  68. }
  69. public canIntersects(ray: Ray): boolean {
  70. return ray.intersectsBox(this._boundingInfo.boundingBox);
  71. }
  72. public intersects(ray: Ray, positions: Vector3[], indices: number[], fastCheck?: boolean): IntersectionInfo {
  73. var intersectInfo: IntersectionInfo = null;
  74. // Triangles test
  75. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  76. var p0 = positions[indices[index]];
  77. var p1 = positions[indices[index + 1]];
  78. var p2 = positions[indices[index + 2]];
  79. var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);
  80. if (currentIntersectInfo) {
  81. if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
  82. intersectInfo = currentIntersectInfo;
  83. intersectInfo.faceId = index / 3;
  84. if (fastCheck) {
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. return intersectInfo;
  91. }
  92. // Clone
  93. public clone(newMesh: Mesh): SubMesh {
  94. return new SubMesh(this.materialIndex, this.verticesStart, this.verticesCount, this.indexStart, this.indexCount, newMesh);
  95. }
  96. // Statics
  97. public static CreateFromIndices(materialIndex: number, startIndex: number, indexCount: number, mesh: Mesh): SubMesh {
  98. var minVertexIndex = Number.MAX_VALUE;
  99. var maxVertexIndex = -Number.MAX_VALUE;
  100. var indices = mesh.getIndices();
  101. for (var index = startIndex; index < startIndex + indexCount; index++) {
  102. var vertexIndex = indices[index];
  103. if (vertexIndex < minVertexIndex)
  104. minVertexIndex = vertexIndex;
  105. else if (vertexIndex > maxVertexIndex)
  106. maxVertexIndex = vertexIndex;
  107. }
  108. return new BABYLON.SubMesh(materialIndex, minVertexIndex, maxVertexIndex - minVertexIndex, startIndex, indexCount, mesh);
  109. }
  110. }
  111. }