babylon.subMesh.ts 6.8 KB

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