babylon.subMesh.ts 6.8 KB

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