babylon.subMesh.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. module BABYLON {
  2. export class SubMesh {
  3. public linesIndexCount: number;
  4. private _mesh: AbstractMesh;
  5. private _renderingMesh: Mesh;
  6. private _boundingInfo: BoundingInfo;
  7. private _linesIndexBuffer: WebGLBuffer;
  8. public _lastColliderWorldVertices: Vector3[];
  9. public _trianglePlanes: Plane[];
  10. public _lastColliderTransformMatrix: Matrix;
  11. public _renderId = 0;
  12. public _distanceToCamera: number;
  13. public _id: number;
  14. constructor(public materialIndex: number, public verticesStart: number, public verticesCount: number, public indexStart, public indexCount: number, mesh: AbstractMesh, renderingMesh?: Mesh, createBoundingBox: boolean = true) {
  15. this._mesh = mesh;
  16. this._renderingMesh = renderingMesh || <Mesh>mesh;
  17. mesh.subMeshes.push(this);
  18. this._id = mesh.subMeshes.length - 1;
  19. if (createBoundingBox) {
  20. this.refreshBoundingInfo();
  21. }
  22. }
  23. public getBoundingInfo(): BoundingInfo {
  24. return this._boundingInfo;
  25. }
  26. public getMesh(): AbstractMesh {
  27. return this._mesh;
  28. }
  29. public getRenderingMesh(): Mesh {
  30. return this._renderingMesh;
  31. }
  32. public getMaterial(): Material {
  33. var rootMaterial = this._renderingMesh.material;
  34. if (rootMaterial && rootMaterial instanceof MultiMaterial) {
  35. var multiMaterial = <MultiMaterial>rootMaterial;
  36. return multiMaterial.getSubMaterial(this.materialIndex);
  37. }
  38. if (!rootMaterial) {
  39. return this._mesh.getScene().defaultMaterial;
  40. }
  41. return rootMaterial;
  42. }
  43. // Methods
  44. public refreshBoundingInfo(): void {
  45. var data = this._renderingMesh.getVerticesData(VertexBuffer.PositionKind);
  46. if (!data) {
  47. this._boundingInfo = this._mesh._boundingInfo;
  48. return;
  49. }
  50. var indices = this._renderingMesh.getIndices();
  51. var extend;
  52. if (this.indexStart === 0 && this.indexCount === indices.length) {
  53. extend = BABYLON.Tools.ExtractMinAndMax(data, this.verticesStart, this.verticesCount);
  54. } else {
  55. extend = BABYLON.Tools.ExtractMinAndMaxIndexed(data, indices, this.indexStart, this.indexCount);
  56. }
  57. this._boundingInfo = new BoundingInfo(extend.minimum, extend.maximum);
  58. }
  59. public _checkCollision(collider: Collider): boolean {
  60. return this._boundingInfo._checkCollision(collider);
  61. }
  62. public updateBoundingInfo(world: Matrix): void {
  63. if (!this._boundingInfo) {
  64. this.refreshBoundingInfo();
  65. }
  66. this._boundingInfo._update(world);
  67. }
  68. public isInFrustum(frustumPlanes: Plane[]): boolean {
  69. return this._boundingInfo.isInFrustum(frustumPlanes);
  70. }
  71. public render(): void {
  72. this._renderingMesh.render(this);
  73. }
  74. public getLinesIndexBuffer(indices: number[], engine): WebGLBuffer {
  75. if (!this._linesIndexBuffer) {
  76. var linesIndices = [];
  77. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  78. linesIndices.push(indices[index], indices[index + 1],
  79. indices[index + 1], indices[index + 2],
  80. indices[index + 2], indices[index]);
  81. }
  82. this._linesIndexBuffer = engine.createIndexBuffer(linesIndices);
  83. this.linesIndexCount = linesIndices.length;
  84. }
  85. return this._linesIndexBuffer;
  86. }
  87. public canIntersects(ray: Ray): boolean {
  88. return ray.intersectsBox(this._boundingInfo.boundingBox);
  89. }
  90. public intersects(ray: Ray, positions: Vector3[], indices: number[], fastCheck?: boolean): IntersectionInfo {
  91. var intersectInfo: IntersectionInfo = null;
  92. // Triangles test
  93. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  94. var p0 = positions[indices[index]];
  95. var p1 = positions[indices[index + 1]];
  96. var p2 = positions[indices[index + 2]];
  97. var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);
  98. if (currentIntersectInfo) {
  99. if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
  100. intersectInfo = currentIntersectInfo;
  101. intersectInfo.faceId = index / 3;
  102. if (fastCheck) {
  103. break;
  104. }
  105. }
  106. }
  107. }
  108. return intersectInfo;
  109. }
  110. // Clone
  111. public clone(newMesh: AbstractMesh, newRenderingMesh?: Mesh): SubMesh {
  112. var result = new SubMesh(this.materialIndex, this.verticesStart, this.verticesCount, this.indexStart, this.indexCount, newMesh, newRenderingMesh, false);
  113. result._boundingInfo = new BoundingInfo(this._boundingInfo.minimum, this._boundingInfo.maximum);
  114. return result;
  115. }
  116. // Dispose
  117. public dispose() {
  118. if (this._linesIndexBuffer) {
  119. this._mesh.getScene().getEngine()._releaseBuffer(this._linesIndexBuffer);
  120. this._linesIndexBuffer = null;
  121. }
  122. // Remove from mesh
  123. var index = this._mesh.subMeshes.indexOf(this);
  124. this._mesh.subMeshes.splice(index, 1);
  125. }
  126. // Statics
  127. public static CreateFromIndices(materialIndex: number, startIndex: number, indexCount: number, mesh: AbstractMesh, renderingMesh?: Mesh): SubMesh {
  128. var minVertexIndex = Number.MAX_VALUE;
  129. var maxVertexIndex = -Number.MAX_VALUE;
  130. renderingMesh = renderingMesh || <Mesh>mesh;
  131. var indices = renderingMesh.getIndices();
  132. for (var index = startIndex; index < startIndex + indexCount; index++) {
  133. var vertexIndex = indices[index];
  134. if (vertexIndex < minVertexIndex)
  135. minVertexIndex = vertexIndex;
  136. if (vertexIndex > maxVertexIndex)
  137. maxVertexIndex = vertexIndex;
  138. }
  139. return new BABYLON.SubMesh(materialIndex, minVertexIndex, maxVertexIndex - minVertexIndex + 1, startIndex, indexCount, mesh, renderingMesh);
  140. }
  141. }
  142. }