babylon.subMesh.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. module BABYLON {
  2. export class SubMesh implements ICullable {
  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. public _materialDefines: MaterialDefines;
  16. public _materialEffect: Effect;
  17. private _currentMaterial: Material;
  18. public get effect(): Effect {
  19. return this._materialEffect;
  20. }
  21. public setEffect(effect: Effect, defines?: MaterialDefines) {
  22. if (this._materialEffect === effect) {
  23. return;
  24. }
  25. this._materialDefines = defines;
  26. this._materialEffect = effect;
  27. }
  28. constructor(public materialIndex: number, public verticesStart: number, public verticesCount: number, public indexStart, public indexCount: number, mesh: AbstractMesh, renderingMesh?: Mesh, createBoundingBox: boolean = true) {
  29. this._mesh = mesh;
  30. this._renderingMesh = renderingMesh || <Mesh>mesh;
  31. mesh.subMeshes.push(this);
  32. this._trianglePlanes = [];
  33. this._id = mesh.subMeshes.length - 1;
  34. if (createBoundingBox) {
  35. this.refreshBoundingInfo();
  36. mesh.computeWorldMatrix(true);
  37. }
  38. }
  39. public get IsGlobal(): boolean {
  40. return (this.verticesStart === 0 && this.verticesCount == this._mesh.getTotalVertices());
  41. }
  42. /**
  43. * Returns the submesh BoudingInfo object.
  44. */
  45. public getBoundingInfo(): BoundingInfo {
  46. if (this.IsGlobal) {
  47. return this._mesh.getBoundingInfo();
  48. }
  49. return this._boundingInfo;
  50. }
  51. /**
  52. * Sets the submesh BoundingInfo.
  53. * Return the SubMesh.
  54. */
  55. public setBoundingInfo(boundingInfo: BoundingInfo): SubMesh {
  56. this._boundingInfo = boundingInfo;
  57. return this;
  58. }
  59. /**
  60. * Returns the mesh of the current submesh.
  61. */
  62. public getMesh(): AbstractMesh {
  63. return this._mesh;
  64. }
  65. /**
  66. * Returns the rendering mesh of the submesh.
  67. */
  68. public getRenderingMesh(): Mesh {
  69. return this._renderingMesh;
  70. }
  71. /**
  72. * Returns the submesh material.
  73. */
  74. public getMaterial(): Material {
  75. var rootMaterial = this._renderingMesh.material;
  76. if (rootMaterial && (<MultiMaterial>rootMaterial).getSubMaterial) {
  77. var multiMaterial = <MultiMaterial>rootMaterial;
  78. var effectiveMaterial = multiMaterial.getSubMaterial(this.materialIndex);
  79. if (this._currentMaterial !== effectiveMaterial) {
  80. this._currentMaterial = effectiveMaterial;
  81. if (this._materialDefines) {
  82. this._materialDefines.markAllAsDirty();
  83. }
  84. }
  85. return effectiveMaterial;
  86. }
  87. if (!rootMaterial) {
  88. return this._mesh.getScene().defaultMaterial;
  89. }
  90. return rootMaterial;
  91. }
  92. // Methods
  93. /**
  94. * Sets a new updated BoundingInfo object to the submesh.
  95. * Returns the SubMesh.
  96. */
  97. public refreshBoundingInfo(): SubMesh {
  98. this._lastColliderWorldVertices = null;
  99. if (this.IsGlobal) {
  100. return;
  101. }
  102. var data = this._renderingMesh.getVerticesData(VertexBuffer.PositionKind);
  103. if (!data) {
  104. this._boundingInfo = this._mesh._boundingInfo;
  105. return;
  106. }
  107. var indices = this._renderingMesh.getIndices();
  108. var extend: { minimum: Vector3, maximum: Vector3 };
  109. //is this the only submesh?
  110. if (this.indexStart === 0 && this.indexCount === indices.length) {
  111. //the rendering mesh's bounding info can be used, it is the standard submesh for all indices.
  112. extend = { minimum: this._renderingMesh.getBoundingInfo().minimum.clone(), maximum: this._renderingMesh.getBoundingInfo().maximum.clone() };
  113. } else {
  114. extend = Tools.ExtractMinAndMaxIndexed(data, indices, this.indexStart, this.indexCount, this._renderingMesh.geometry.boundingBias);
  115. }
  116. this._boundingInfo = new BoundingInfo(extend.minimum, extend.maximum);
  117. return this;
  118. }
  119. public _checkCollision(collider: Collider): boolean {
  120. return this.getBoundingInfo()._checkCollision(collider);
  121. }
  122. /**
  123. * Updates the submesh BoundingInfo.
  124. * Returns the Submesh.
  125. */
  126. public updateBoundingInfo(world: Matrix): SubMesh {
  127. if (!this.getBoundingInfo()) {
  128. this.refreshBoundingInfo();
  129. }
  130. this.getBoundingInfo().update(world);
  131. return this;
  132. }
  133. /**
  134. * True is the submesh bounding box intersects the frustum defined by the passed array of planes.
  135. * Boolean returned.
  136. */
  137. public isInFrustum(frustumPlanes: Plane[]): boolean {
  138. return this.getBoundingInfo().isInFrustum(frustumPlanes);
  139. }
  140. /**
  141. * True is the submesh bounding box is completely inside the frustum defined by the passed array of planes.
  142. * Boolean returned.
  143. */
  144. public isCompletelyInFrustum(frustumPlanes: Plane[]): boolean {
  145. return this.getBoundingInfo().isCompletelyInFrustum(frustumPlanes);
  146. }
  147. /**
  148. * Renders the submesh.
  149. * Returns it.
  150. */
  151. public render(enableAlphaMode: boolean): SubMesh {
  152. this._renderingMesh.render(this, enableAlphaMode);
  153. return this;
  154. }
  155. /**
  156. * Returns a new Index Buffer.
  157. * Type returned : WebGLBuffer.
  158. */
  159. public getLinesIndexBuffer(indices: IndicesArray, engine: Engine): WebGLBuffer {
  160. if (!this._linesIndexBuffer) {
  161. var linesIndices = [];
  162. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  163. linesIndices.push(indices[index], indices[index + 1],
  164. indices[index + 1], indices[index + 2],
  165. indices[index + 2], indices[index]);
  166. }
  167. this._linesIndexBuffer = engine.createIndexBuffer(linesIndices);
  168. this.linesIndexCount = linesIndices.length;
  169. }
  170. return this._linesIndexBuffer;
  171. }
  172. /**
  173. * True is the passed Ray intersects the submesh bounding box.
  174. * Boolean returned.
  175. */
  176. public canIntersects(ray: Ray): boolean {
  177. return ray.intersectsBox(this.getBoundingInfo().boundingBox);
  178. }
  179. /**
  180. * Returns an object IntersectionInfo.
  181. */
  182. public intersects(ray: Ray, positions: Vector3[], indices: IndicesArray, fastCheck?: boolean): IntersectionInfo {
  183. var intersectInfo: IntersectionInfo = null;
  184. // LineMesh first as it's also a Mesh...
  185. if (this._mesh instanceof LinesMesh) {
  186. var lineMesh = <LinesMesh>this._mesh;
  187. // Line test
  188. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 2) {
  189. var p0 = positions[indices[index]];
  190. var p1 = positions[indices[index + 1]];
  191. var length = ray.intersectionSegment(p0, p1, lineMesh.intersectionThreshold);
  192. if (length < 0) {
  193. continue;
  194. }
  195. if (fastCheck || !intersectInfo || length < intersectInfo.distance) {
  196. intersectInfo = new IntersectionInfo(null, null, length);
  197. if (fastCheck) {
  198. break;
  199. }
  200. }
  201. }
  202. }
  203. else {
  204. // Triangles test
  205. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  206. var p0 = positions[indices[index]];
  207. var p1 = positions[indices[index + 1]];
  208. var p2 = positions[indices[index + 2]];
  209. var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);
  210. if (currentIntersectInfo) {
  211. if (currentIntersectInfo.distance < 0) {
  212. continue;
  213. }
  214. if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
  215. intersectInfo = currentIntersectInfo;
  216. intersectInfo.faceId = index / 3;
  217. if (fastCheck) {
  218. break;
  219. }
  220. }
  221. }
  222. }
  223. }
  224. return intersectInfo;
  225. }
  226. // Clone
  227. /**
  228. * Creates a new Submesh from the passed Mesh.
  229. */
  230. public clone(newMesh: AbstractMesh, newRenderingMesh?: Mesh): SubMesh {
  231. var result = new SubMesh(this.materialIndex, this.verticesStart, this.verticesCount, this.indexStart, this.indexCount, newMesh, newRenderingMesh, false);
  232. if (!this.IsGlobal) {
  233. result._boundingInfo = new BoundingInfo(this.getBoundingInfo().minimum, this.getBoundingInfo().maximum);
  234. }
  235. return result;
  236. }
  237. // Dispose
  238. /**
  239. * Disposes the Submesh.
  240. * Returns nothing.
  241. */
  242. public dispose(): void {
  243. if (this._linesIndexBuffer) {
  244. this._mesh.getScene().getEngine()._releaseBuffer(this._linesIndexBuffer);
  245. this._linesIndexBuffer = null;
  246. }
  247. // Remove from mesh
  248. var index = this._mesh.subMeshes.indexOf(this);
  249. this._mesh.subMeshes.splice(index, 1);
  250. }
  251. // Statics
  252. /**
  253. * Creates a new Submesh from the passed parameters :
  254. * - materialIndex (integer) : the index of the main mesh material.
  255. * - startIndex (integer) : the index where to start the copy in the mesh indices array.
  256. * - indexCount (integer) : the number of indices to copy then from the startIndex.
  257. * - mesh (Mesh) : the main mesh to create the submesh from.
  258. * - renderingMesh (optional Mesh) : rendering mesh.
  259. */
  260. public static CreateFromIndices(materialIndex: number, startIndex: number, indexCount: number, mesh: AbstractMesh, renderingMesh?: Mesh): SubMesh {
  261. var minVertexIndex = Number.MAX_VALUE;
  262. var maxVertexIndex = -Number.MAX_VALUE;
  263. renderingMesh = renderingMesh || <Mesh>mesh;
  264. var indices = renderingMesh.getIndices();
  265. for (var index = startIndex; index < startIndex + indexCount; index++) {
  266. var vertexIndex = indices[index];
  267. if (vertexIndex < minVertexIndex)
  268. minVertexIndex = vertexIndex;
  269. if (vertexIndex > maxVertexIndex)
  270. maxVertexIndex = vertexIndex;
  271. }
  272. return new SubMesh(materialIndex, minVertexIndex, maxVertexIndex - minVertexIndex + 1, startIndex, indexCount, mesh, renderingMesh);
  273. }
  274. }
  275. }