babylon.subMesh.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 get IsGlobal(): boolean {
  27. return (this.verticesStart === 0 && this.verticesCount == this._mesh.getTotalVertices());
  28. }
  29. public getBoundingInfo(): BoundingInfo {
  30. if (this.IsGlobal) {
  31. return this._mesh.getBoundingInfo();
  32. }
  33. return this._boundingInfo;
  34. }
  35. public getMesh(): AbstractMesh {
  36. return this._mesh;
  37. }
  38. public getRenderingMesh(): Mesh {
  39. return this._renderingMesh;
  40. }
  41. public getMaterial(): Material {
  42. var rootMaterial = this._renderingMesh.material;
  43. if (rootMaterial && rootMaterial instanceof MultiMaterial) {
  44. var multiMaterial = <MultiMaterial>rootMaterial;
  45. return multiMaterial.getSubMaterial(this.materialIndex);
  46. }
  47. if (!rootMaterial) {
  48. return this._mesh.getScene().defaultMaterial;
  49. }
  50. return rootMaterial;
  51. }
  52. // Methods
  53. public refreshBoundingInfo(): void {
  54. this._lastColliderWorldVertices = null;
  55. if (this.IsGlobal) {
  56. return;
  57. }
  58. var data = this._renderingMesh.getVerticesData(VertexBuffer.PositionKind);
  59. if (!data) {
  60. this._boundingInfo = this._mesh._boundingInfo;
  61. return;
  62. }
  63. var indices = this._renderingMesh.getIndices();
  64. var extend: { minimum: Vector3, maximum: Vector3 };
  65. //is this the only submesh?
  66. if (this.indexStart === 0 && this.indexCount === indices.length) {
  67. //the rendering mesh's bounding info can be used, it is the standard submesh for all indices.
  68. extend = { minimum: this._renderingMesh.getBoundingInfo().minimum.clone(), maximum: this._renderingMesh.getBoundingInfo().maximum.clone() };
  69. } else {
  70. extend = Tools.ExtractMinAndMaxIndexed(data, indices, this.indexStart, this.indexCount, this._renderingMesh.geometry.boundingBias);
  71. }
  72. this._boundingInfo = new BoundingInfo(extend.minimum, extend.maximum);
  73. }
  74. public _checkCollision(collider: Collider): boolean {
  75. return this.getBoundingInfo()._checkCollision(collider);
  76. }
  77. public updateBoundingInfo(world: Matrix): void {
  78. if (!this.getBoundingInfo()) {
  79. this.refreshBoundingInfo();
  80. }
  81. this.getBoundingInfo().update(world);
  82. }
  83. public isInFrustum(frustumPlanes: Plane[]): boolean {
  84. return this.getBoundingInfo().isInFrustum(frustumPlanes);
  85. }
  86. public render(enableAlphaMode: boolean): void {
  87. this._renderingMesh.render(this, enableAlphaMode);
  88. }
  89. public getLinesIndexBuffer(indices: number[] | Int32Array, engine): WebGLBuffer {
  90. if (!this._linesIndexBuffer) {
  91. var linesIndices = [];
  92. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  93. linesIndices.push(indices[index], indices[index + 1],
  94. indices[index + 1], indices[index + 2],
  95. indices[index + 2], indices[index]);
  96. }
  97. this._linesIndexBuffer = engine.createIndexBuffer(linesIndices);
  98. this.linesIndexCount = linesIndices.length;
  99. }
  100. return this._linesIndexBuffer;
  101. }
  102. public canIntersects(ray: Ray): boolean {
  103. return ray.intersectsBox(this.getBoundingInfo().boundingBox);
  104. }
  105. public intersects(ray: Ray, positions: Vector3[], indices: number[] | Int32Array, fastCheck?: boolean): IntersectionInfo {
  106. var intersectInfo: IntersectionInfo = null;
  107. // LineMesh first as it's also a Mesh...
  108. if (this._mesh instanceof LinesMesh) {
  109. var lineMesh = <LinesMesh>this._mesh;
  110. // Line test
  111. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 2) {
  112. var p0 = positions[indices[index]];
  113. var p1 = positions[indices[index + 1]];
  114. var length = ray.intersectionSegment(p0, p1, lineMesh.intersectionThreshold);
  115. if (length < 0) {
  116. continue;
  117. }
  118. if (fastCheck || !intersectInfo || length < intersectInfo.distance) {
  119. intersectInfo = new IntersectionInfo(null, null, length);
  120. if (fastCheck) {
  121. break;
  122. }
  123. }
  124. }
  125. }
  126. else {
  127. // Triangles test
  128. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  129. var p0 = positions[indices[index]];
  130. var p1 = positions[indices[index + 1]];
  131. var p2 = positions[indices[index + 2]];
  132. var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);
  133. if (currentIntersectInfo) {
  134. if (currentIntersectInfo.distance < 0) {
  135. continue;
  136. }
  137. if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
  138. intersectInfo = currentIntersectInfo;
  139. intersectInfo.faceId = index / 3;
  140. if (fastCheck) {
  141. break;
  142. }
  143. }
  144. }
  145. }
  146. }
  147. return intersectInfo;
  148. }
  149. // Clone
  150. public clone(newMesh: AbstractMesh, newRenderingMesh?: Mesh): SubMesh {
  151. var result = new SubMesh(this.materialIndex, this.verticesStart, this.verticesCount, this.indexStart, this.indexCount, newMesh, newRenderingMesh, false);
  152. if (!this.IsGlobal) {
  153. result._boundingInfo = new BoundingInfo(this.getBoundingInfo().minimum, this.getBoundingInfo().maximum);
  154. }
  155. return result;
  156. }
  157. // Dispose
  158. public dispose() {
  159. if (this._linesIndexBuffer) {
  160. this._mesh.getScene().getEngine()._releaseBuffer(this._linesIndexBuffer);
  161. this._linesIndexBuffer = null;
  162. }
  163. // Remove from mesh
  164. var index = this._mesh.subMeshes.indexOf(this);
  165. this._mesh.subMeshes.splice(index, 1);
  166. }
  167. // Statics
  168. public static CreateFromIndices(materialIndex: number, startIndex: number, indexCount: number, mesh: AbstractMesh, renderingMesh?: Mesh): SubMesh {
  169. var minVertexIndex = Number.MAX_VALUE;
  170. var maxVertexIndex = -Number.MAX_VALUE;
  171. renderingMesh = renderingMesh || <Mesh>mesh;
  172. var indices = renderingMesh.getIndices();
  173. for (var index = startIndex; index < startIndex + indexCount; index++) {
  174. var vertexIndex = indices[index];
  175. if (vertexIndex < minVertexIndex)
  176. minVertexIndex = vertexIndex;
  177. if (vertexIndex > maxVertexIndex)
  178. maxVertexIndex = vertexIndex;
  179. }
  180. return new SubMesh(materialIndex, minVertexIndex, maxVertexIndex - minVertexIndex + 1, startIndex, indexCount, mesh, renderingMesh);
  181. }
  182. }
  183. }