babylon.subMesh.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. module BABYLON {
  2. /**
  3. * Base class for submeshes
  4. */
  5. export class BaseSubMesh {
  6. /** @hidden */
  7. public _materialDefines: Nullable<MaterialDefines>;
  8. /** @hidden */
  9. public _materialEffect: Nullable<Effect>;
  10. /**
  11. * Gets associated effect
  12. */
  13. public get effect(): Nullable<Effect> {
  14. return this._materialEffect;
  15. }
  16. /**
  17. * Sets associated effect (effect used to render this submesh)
  18. * @param effect defines the effect to associate with
  19. * @param defines defines the set of defines used to compile this effect
  20. */
  21. public setEffect(effect: Nullable<Effect>, defines: Nullable<MaterialDefines> = null) {
  22. if (this._materialEffect === effect) {
  23. if (!effect) {
  24. this._materialDefines = null;
  25. }
  26. return;
  27. }
  28. this._materialDefines = defines;
  29. this._materialEffect = effect;
  30. }
  31. }
  32. /**
  33. * Defines a subdivision inside a mesh
  34. */
  35. export class SubMesh extends BaseSubMesh implements ICullable {
  36. /** @hidden */
  37. public _linesIndexCount: number;
  38. private _mesh: AbstractMesh;
  39. private _renderingMesh: Mesh;
  40. private _boundingInfo: BoundingInfo;
  41. private _linesIndexBuffer: Nullable<WebGLBuffer>;
  42. /** @hidden */
  43. public _lastColliderWorldVertices: Nullable<Vector3[]>;
  44. /** @hidden */
  45. public _trianglePlanes: Plane[];
  46. /** @hidden */
  47. public _lastColliderTransformMatrix: Matrix;
  48. /** @hidden */
  49. public _renderId = 0;
  50. /** @hidden */
  51. public _alphaIndex: number;
  52. /** @hidden */
  53. public _distanceToCamera: number;
  54. /** @hidden */
  55. public _id: number;
  56. private _currentMaterial: Nullable<Material>;
  57. /**
  58. * Add a new submesh to a mesh
  59. * @param materialIndex defines the material index to use
  60. * @param verticesStart defines vertex index start
  61. * @param verticesCount defines vertices count
  62. * @param indexStart defines index start
  63. * @param indexCount defines indices count
  64. * @param mesh defines the parent mesh
  65. * @param renderingMesh defines an optional rendering mesh
  66. * @param createBoundingBox defines if bounding box should be created for this submesh
  67. * @returns the new submesh
  68. */
  69. public static AddToMesh(materialIndex: number, verticesStart: number, verticesCount: number, indexStart: number, indexCount: number, mesh: AbstractMesh, renderingMesh?: Mesh, createBoundingBox: boolean = true): SubMesh {
  70. return new SubMesh(materialIndex, verticesStart, verticesCount, indexStart, indexCount, mesh, renderingMesh, createBoundingBox);
  71. }
  72. /**
  73. * Creates a new submesh
  74. * @param materialIndex defines the material index to use
  75. * @param verticesStart defines vertex index start
  76. * @param verticesCount defines vertices count
  77. * @param indexStart defines index start
  78. * @param indexCount defines indices count
  79. * @param mesh defines the parent mesh
  80. * @param renderingMesh defines an optional rendering mesh
  81. * @param createBoundingBox defines if bounding box should be created for this submesh
  82. */
  83. constructor(
  84. /** the material index to use */
  85. public materialIndex: number,
  86. /** vertex index start */
  87. public verticesStart: number,
  88. /** vertices count */
  89. public verticesCount: number,
  90. /** index start */
  91. public indexStart: number,
  92. /** indices count */
  93. public indexCount: number, mesh: AbstractMesh, renderingMesh?: Mesh, createBoundingBox: boolean = true) {
  94. super();
  95. this._mesh = mesh;
  96. this._renderingMesh = renderingMesh || <Mesh>mesh;
  97. mesh.subMeshes.push(this);
  98. this._trianglePlanes = [];
  99. this._id = mesh.subMeshes.length - 1;
  100. if (createBoundingBox) {
  101. this.refreshBoundingInfo();
  102. mesh.computeWorldMatrix(true);
  103. }
  104. }
  105. /**
  106. * Returns true if this submesh covers the entire parent mesh
  107. * @ignorenaming
  108. */
  109. public get IsGlobal(): boolean {
  110. return (this.verticesStart === 0 && this.verticesCount === this._mesh.getTotalVertices());
  111. }
  112. /**
  113. * Returns the submesh BoudingInfo object
  114. * @returns current bounding info (or mesh's one if the submesh is global)
  115. */
  116. public getBoundingInfo(): BoundingInfo {
  117. if (this.IsGlobal) {
  118. return this._mesh.getBoundingInfo();
  119. }
  120. return this._boundingInfo;
  121. }
  122. /**
  123. * Sets the submesh BoundingInfo
  124. * @param boundingInfo defines the new bounding info to use
  125. * @returns the SubMesh
  126. */
  127. public setBoundingInfo(boundingInfo: BoundingInfo): SubMesh {
  128. this._boundingInfo = boundingInfo;
  129. return this;
  130. }
  131. /**
  132. * Returns the mesh of the current submesh
  133. * @return the parent mesh
  134. */
  135. public getMesh(): AbstractMesh {
  136. return this._mesh;
  137. }
  138. /**
  139. * Returns the rendering mesh of the submesh
  140. * @returns the rendering mesh (could be different from parent mesh)
  141. */
  142. public getRenderingMesh(): Mesh {
  143. return this._renderingMesh;
  144. }
  145. /**
  146. * Returns the submesh material
  147. * @returns null or the current material
  148. */
  149. public getMaterial(): Nullable<Material> {
  150. var rootMaterial = this._renderingMesh.material;
  151. if (rootMaterial === null || rootMaterial === undefined) {
  152. return this._mesh.getScene().defaultMaterial;
  153. } else if ((<MultiMaterial>rootMaterial).getSubMaterial) {
  154. var multiMaterial = <MultiMaterial>rootMaterial;
  155. var effectiveMaterial = multiMaterial.getSubMaterial(this.materialIndex);
  156. if (this._currentMaterial !== effectiveMaterial) {
  157. this._currentMaterial = effectiveMaterial;
  158. this._materialDefines = null;
  159. }
  160. return effectiveMaterial;
  161. }
  162. return rootMaterial;
  163. }
  164. // Methods
  165. /**
  166. * Sets a new updated BoundingInfo object to the submesh
  167. * @returns the SubMesh
  168. */
  169. public refreshBoundingInfo(): SubMesh {
  170. this._lastColliderWorldVertices = null;
  171. if (this.IsGlobal || !this._renderingMesh || !this._renderingMesh.geometry) {
  172. return this;
  173. }
  174. var data = this._renderingMesh.getVerticesData(VertexBuffer.PositionKind);
  175. if (!data) {
  176. this._boundingInfo = this._mesh.getBoundingInfo();
  177. return this;
  178. }
  179. var indices = <IndicesArray>this._renderingMesh.getIndices();
  180. var extend: { minimum: Vector3, maximum: Vector3 };
  181. //is this the only submesh?
  182. if (this.indexStart === 0 && this.indexCount === indices.length) {
  183. let boundingInfo = this._renderingMesh.getBoundingInfo();
  184. //the rendering mesh's bounding info can be used, it is the standard submesh for all indices.
  185. extend = { minimum: boundingInfo.minimum.clone(), maximum: boundingInfo.maximum.clone() };
  186. } else {
  187. extend = Tools.ExtractMinAndMaxIndexed(data, indices, this.indexStart, this.indexCount, this._renderingMesh.geometry.boundingBias);
  188. }
  189. const extraWorldExtent = this._renderingMesh.geometry ? this._renderingMesh.geometry._boundingWorldExtraExtent : undefined;
  190. if (this._boundingInfo) {
  191. this._boundingInfo.reConstruct(extend.minimum, extend.maximum, undefined, extraWorldExtent);
  192. }
  193. else {
  194. this._boundingInfo = new BoundingInfo(extend.minimum, extend.maximum, undefined, extraWorldExtent);
  195. }
  196. return this;
  197. }
  198. /** @hidden */
  199. public _checkCollision(collider: Collider): boolean {
  200. let boundingInfo = this.getBoundingInfo();
  201. return boundingInfo._checkCollision(collider);
  202. }
  203. /**
  204. * Updates the submesh BoundingInfo
  205. * @param world defines the world matrix to use to update the bounding info
  206. * @returns the submesh
  207. */
  208. public updateBoundingInfo(world: Matrix): SubMesh {
  209. let boundingInfo = this.getBoundingInfo();
  210. if (!boundingInfo) {
  211. this.refreshBoundingInfo();
  212. boundingInfo = this.getBoundingInfo();
  213. }
  214. (<BoundingInfo>boundingInfo).update(world);
  215. return this;
  216. }
  217. /**
  218. * True is the submesh bounding box intersects the frustum defined by the passed array of planes.
  219. * @param frustumPlanes defines the frustum planes
  220. * @returns true if the submesh is intersecting with the frustum
  221. */
  222. public isInFrustum(frustumPlanes: Plane[]): boolean {
  223. let boundingInfo = this.getBoundingInfo();
  224. if (!boundingInfo) {
  225. return false;
  226. }
  227. return boundingInfo.isInFrustum(frustumPlanes, this._mesh.cullingStrategy);
  228. }
  229. /**
  230. * True is the submesh bounding box is completely inside the frustum defined by the passed array of planes
  231. * @param frustumPlanes defines the frustum planes
  232. * @returns true if the submesh is inside the frustum
  233. */
  234. public isCompletelyInFrustum(frustumPlanes: Plane[]): boolean {
  235. let boundingInfo = this.getBoundingInfo();
  236. if (!boundingInfo) {
  237. return false;
  238. }
  239. return boundingInfo.isCompletelyInFrustum(frustumPlanes);
  240. }
  241. /**
  242. * Renders the submesh
  243. * @param enableAlphaMode defines if alpha needs to be used
  244. * @returns the submesh
  245. */
  246. public render(enableAlphaMode: boolean): SubMesh {
  247. this._renderingMesh.render(this, enableAlphaMode);
  248. return this;
  249. }
  250. /**
  251. * @hidden
  252. */
  253. public _getLinesIndexBuffer(indices: IndicesArray, engine: Engine): WebGLBuffer {
  254. if (!this._linesIndexBuffer) {
  255. var linesIndices = [];
  256. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  257. linesIndices.push(indices[index], indices[index + 1],
  258. indices[index + 1], indices[index + 2],
  259. indices[index + 2], indices[index]);
  260. }
  261. this._linesIndexBuffer = engine.createIndexBuffer(linesIndices);
  262. this._linesIndexCount = linesIndices.length;
  263. }
  264. return this._linesIndexBuffer;
  265. }
  266. /**
  267. * Checks if the submesh intersects with a ray
  268. * @param ray defines the ray to test
  269. * @returns true is the passed ray intersects the submesh bounding box
  270. */
  271. public canIntersects(ray: Ray): boolean {
  272. let boundingInfo = this.getBoundingInfo();
  273. if (!boundingInfo) {
  274. return false;
  275. }
  276. return ray.intersectsBox(boundingInfo.boundingBox);
  277. }
  278. /**
  279. * Intersects current submesh with a ray
  280. * @param ray defines the ray to test
  281. * @param positions defines mesh's positions array
  282. * @param indices defines mesh's indices array
  283. * @param fastCheck defines if only bounding info should be used
  284. * @returns intersection info or null if no intersection
  285. */
  286. public intersects(ray: Ray, positions: Vector3[], indices: IndicesArray, fastCheck?: boolean): Nullable<IntersectionInfo> {
  287. const material = this.getMaterial();
  288. if (!material) {
  289. return null;
  290. }
  291. switch (material.fillMode) {
  292. case Material.PointListDrawMode:
  293. case Material.LineListDrawMode:
  294. case Material.LineLoopDrawMode:
  295. case Material.LineStripDrawMode:
  296. case Material.TriangleFanDrawMode:
  297. case Material.TriangleStripDrawMode:
  298. return null;
  299. }
  300. // LineMesh first as it's also a Mesh...
  301. if (LinesMesh) {
  302. const mesh = this._mesh instanceof InstancedMesh ? (<InstancedMesh>this._mesh).sourceMesh : this._mesh;
  303. if (mesh instanceof LinesMesh) {
  304. const linesMesh = <LinesMesh>mesh;
  305. return this._intersectLines(ray, positions, indices, linesMesh.intersectionThreshold, fastCheck);
  306. }
  307. }
  308. return this._intersectTriangles(ray, positions, indices, fastCheck);
  309. }
  310. /** @hidden */
  311. private _intersectLines(ray: Ray, positions: Vector3[], indices: IndicesArray, intersectionThreshold: number, fastCheck?: boolean): Nullable<IntersectionInfo> {
  312. var intersectInfo: Nullable<IntersectionInfo> = null;
  313. // Line test
  314. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 2) {
  315. var p0 = positions[indices[index]];
  316. var p1 = positions[indices[index + 1]];
  317. var length = ray.intersectionSegment(p0, p1, intersectionThreshold);
  318. if (length < 0) {
  319. continue;
  320. }
  321. if (fastCheck || !intersectInfo || length < intersectInfo.distance) {
  322. intersectInfo = new IntersectionInfo(null, null, length);
  323. if (fastCheck) {
  324. break;
  325. }
  326. }
  327. }
  328. return intersectInfo;
  329. }
  330. /** @hidden */
  331. private _intersectTriangles(ray: Ray, positions: Vector3[], indices: IndicesArray, fastCheck?: boolean): Nullable<IntersectionInfo> {
  332. var intersectInfo: Nullable<IntersectionInfo> = null;
  333. // Triangles test
  334. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  335. var p0 = positions[indices[index]];
  336. var p1 = positions[indices[index + 1]];
  337. var p2 = positions[indices[index + 2]];
  338. var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);
  339. if (currentIntersectInfo) {
  340. if (currentIntersectInfo.distance < 0) {
  341. continue;
  342. }
  343. if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
  344. intersectInfo = currentIntersectInfo;
  345. intersectInfo.faceId = index / 3;
  346. if (fastCheck) {
  347. break;
  348. }
  349. }
  350. }
  351. }
  352. return intersectInfo;
  353. }
  354. /** @hidden */
  355. public _rebuild(): void {
  356. if (this._linesIndexBuffer) {
  357. this._linesIndexBuffer = null;
  358. }
  359. }
  360. // Clone
  361. /**
  362. * Creates a new submesh from the passed mesh
  363. * @param newMesh defines the new hosting mesh
  364. * @param newRenderingMesh defines an optional rendering mesh
  365. * @returns the new submesh
  366. */
  367. public clone(newMesh: AbstractMesh, newRenderingMesh?: Mesh): SubMesh {
  368. var result = new SubMesh(this.materialIndex, this.verticesStart, this.verticesCount, this.indexStart, this.indexCount, newMesh, newRenderingMesh, false);
  369. if (!this.IsGlobal) {
  370. let boundingInfo = this.getBoundingInfo();
  371. if (!boundingInfo) {
  372. return result;
  373. }
  374. result._boundingInfo = new BoundingInfo(boundingInfo.minimum, boundingInfo.maximum);
  375. }
  376. return result;
  377. }
  378. // Dispose
  379. /**
  380. * Release associated resources
  381. */
  382. public dispose(): void {
  383. if (this._linesIndexBuffer) {
  384. this._mesh.getScene().getEngine()._releaseBuffer(this._linesIndexBuffer);
  385. this._linesIndexBuffer = null;
  386. }
  387. // Remove from mesh
  388. var index = this._mesh.subMeshes.indexOf(this);
  389. this._mesh.subMeshes.splice(index, 1);
  390. }
  391. // Statics
  392. /**
  393. * Creates a new submesh from indices data
  394. * @param materialIndex the index of the main mesh material
  395. * @param startIndex the index where to start the copy in the mesh indices array
  396. * @param indexCount the number of indices to copy then from the startIndex
  397. * @param mesh the main mesh to create the submesh from
  398. * @param renderingMesh the optional rendering mesh
  399. * @returns a new submesh
  400. */
  401. public static CreateFromIndices(materialIndex: number, startIndex: number, indexCount: number, mesh: AbstractMesh, renderingMesh?: Mesh): SubMesh {
  402. var minVertexIndex = Number.MAX_VALUE;
  403. var maxVertexIndex = -Number.MAX_VALUE;
  404. renderingMesh = (<Mesh>(renderingMesh || <Mesh>mesh));
  405. var indices = <IndicesArray>renderingMesh.getIndices();
  406. for (var index = startIndex; index < startIndex + indexCount; index++) {
  407. var vertexIndex = indices[index];
  408. if (vertexIndex < minVertexIndex) {
  409. minVertexIndex = vertexIndex;
  410. }
  411. if (vertexIndex > maxVertexIndex) {
  412. maxVertexIndex = vertexIndex;
  413. }
  414. }
  415. return new SubMesh(materialIndex, minVertexIndex, maxVertexIndex - minVertexIndex + 1, startIndex, indexCount, mesh, renderingMesh);
  416. }
  417. }
  418. }