babylon.subMesh.ts 6.7 KB

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