babylon.subMesh.ts 6.5 KB

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