babylon.subMesh.ts 14 KB

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