babylon.subMesh.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 && (<MultiMaterial>rootMaterial).getSubMaterial) {
  86. var multiMaterial = <MultiMaterial>rootMaterial;
  87. var effectiveMaterial = multiMaterial.getSubMaterial(this.materialIndex);
  88. if (this._currentMaterial !== effectiveMaterial) {
  89. this._currentMaterial = effectiveMaterial;
  90. this._materialDefines = null;
  91. }
  92. return effectiveMaterial;
  93. }
  94. if (!rootMaterial) {
  95. return this._mesh.getScene().defaultMaterial;
  96. }
  97. return rootMaterial;
  98. }
  99. // Methods
  100. /**
  101. * Sets a new updated BoundingInfo object to the submesh.
  102. * Returns the SubMesh.
  103. */
  104. public refreshBoundingInfo(): SubMesh {
  105. this._lastColliderWorldVertices = null;
  106. if (this.IsGlobal || !this._renderingMesh || !this._renderingMesh.geometry) {
  107. return this;
  108. }
  109. var data = this._renderingMesh.getVerticesData(VertexBuffer.PositionKind);
  110. if (!data) {
  111. this._boundingInfo = this._mesh.getBoundingInfo();
  112. return this;
  113. }
  114. var indices = <IndicesArray>this._renderingMesh.getIndices();
  115. var extend: { minimum: Vector3, maximum: Vector3 };
  116. //is this the only submesh?
  117. if (this.indexStart === 0 && this.indexCount === indices.length) {
  118. let boundingInfo = this._renderingMesh.getBoundingInfo();
  119. //the rendering mesh's bounding info can be used, it is the standard submesh for all indices.
  120. extend = { minimum: boundingInfo.minimum.clone(), maximum: boundingInfo.maximum.clone() };
  121. } else {
  122. extend = Tools.ExtractMinAndMaxIndexed(data, indices, this.indexStart, this.indexCount, this._renderingMesh.geometry.boundingBias);
  123. }
  124. this._boundingInfo = new BoundingInfo(extend.minimum, extend.maximum);
  125. return this;
  126. }
  127. public _checkCollision(collider: Collider): boolean {
  128. let boundingInfo = this._renderingMesh.getBoundingInfo();
  129. return boundingInfo._checkCollision(collider);
  130. }
  131. /**
  132. * Updates the submesh BoundingInfo.
  133. * Returns the Submesh.
  134. */
  135. public updateBoundingInfo(world: Matrix): SubMesh {
  136. let boundingInfo = this.getBoundingInfo();
  137. if (!boundingInfo) {
  138. this.refreshBoundingInfo();
  139. boundingInfo = this.getBoundingInfo();
  140. }
  141. (<BoundingInfo>boundingInfo).update(world);
  142. return this;
  143. }
  144. /**
  145. * True is the submesh bounding box intersects the frustum defined by the passed array of planes.
  146. * Boolean returned.
  147. */
  148. public isInFrustum(frustumPlanes: Plane[]): boolean {
  149. let boundingInfo = this.getBoundingInfo();
  150. if (!boundingInfo) {
  151. return false;
  152. }
  153. return boundingInfo.isInFrustum(frustumPlanes);
  154. }
  155. /**
  156. * True is the submesh bounding box is completely inside the frustum defined by the passed array of planes.
  157. * Boolean returned.
  158. */
  159. public isCompletelyInFrustum(frustumPlanes: Plane[]): boolean {
  160. let boundingInfo = this.getBoundingInfo();
  161. if (!boundingInfo) {
  162. return false;
  163. }
  164. return boundingInfo.isCompletelyInFrustum(frustumPlanes);
  165. }
  166. /**
  167. * Renders the submesh.
  168. * Returns it.
  169. */
  170. public render(enableAlphaMode: boolean): SubMesh {
  171. this._renderingMesh.render(this, enableAlphaMode);
  172. return this;
  173. }
  174. /**
  175. * Returns a new Index Buffer.
  176. * Type returned : WebGLBuffer.
  177. */
  178. public getLinesIndexBuffer(indices: IndicesArray, engine: Engine): WebGLBuffer {
  179. if (!this._linesIndexBuffer) {
  180. var linesIndices = [];
  181. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  182. linesIndices.push(indices[index], indices[index + 1],
  183. indices[index + 1], indices[index + 2],
  184. indices[index + 2], indices[index]);
  185. }
  186. this._linesIndexBuffer = engine.createIndexBuffer(linesIndices);
  187. this.linesIndexCount = linesIndices.length;
  188. }
  189. return this._linesIndexBuffer;
  190. }
  191. /**
  192. * True is the passed Ray intersects the submesh bounding box.
  193. * Boolean returned.
  194. */
  195. public canIntersects(ray: Ray): boolean {
  196. let boundingInfo = this.getBoundingInfo();
  197. if (!boundingInfo) {
  198. return false;
  199. }
  200. return ray.intersectsBox(boundingInfo.boundingBox);
  201. }
  202. /**
  203. * Returns an object IntersectionInfo.
  204. */
  205. public intersects(ray: Ray, positions: Vector3[], indices: IndicesArray, fastCheck?: boolean): Nullable<IntersectionInfo> {
  206. var intersectInfo: Nullable<IntersectionInfo> = null;
  207. // LineMesh first as it's also a Mesh...
  208. if (LinesMesh && this._mesh instanceof LinesMesh) {
  209. var lineMesh = <LinesMesh>this._mesh;
  210. // Line test
  211. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 2) {
  212. var p0 = positions[indices[index]];
  213. var p1 = positions[indices[index + 1]];
  214. var length = ray.intersectionSegment(p0, p1, lineMesh.intersectionThreshold);
  215. if (length < 0) {
  216. continue;
  217. }
  218. if (fastCheck || !intersectInfo || length < intersectInfo.distance) {
  219. intersectInfo = new IntersectionInfo(null, null, length);
  220. if (fastCheck) {
  221. break;
  222. }
  223. }
  224. }
  225. }
  226. else {
  227. // Triangles test
  228. for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
  229. var p0 = positions[indices[index]];
  230. var p1 = positions[indices[index + 1]];
  231. var p2 = positions[indices[index + 2]];
  232. var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);
  233. if (currentIntersectInfo) {
  234. if (currentIntersectInfo.distance < 0) {
  235. continue;
  236. }
  237. if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
  238. intersectInfo = currentIntersectInfo;
  239. intersectInfo.faceId = index / 3;
  240. if (fastCheck) {
  241. break;
  242. }
  243. }
  244. }
  245. }
  246. }
  247. return intersectInfo;
  248. }
  249. public _rebuild(): void {
  250. if (this._linesIndexBuffer) {
  251. this._linesIndexBuffer = null;
  252. }
  253. }
  254. // Clone
  255. /**
  256. * Creates a new Submesh from the passed Mesh.
  257. */
  258. public clone(newMesh: AbstractMesh, newRenderingMesh?: Mesh): SubMesh {
  259. var result = new SubMesh(this.materialIndex, this.verticesStart, this.verticesCount, this.indexStart, this.indexCount, newMesh, newRenderingMesh, false);
  260. if (!this.IsGlobal) {
  261. let boundingInfo = this.getBoundingInfo();
  262. if (!boundingInfo) {
  263. return result;
  264. }
  265. result._boundingInfo = new BoundingInfo(boundingInfo.minimum, boundingInfo.maximum);
  266. }
  267. return result;
  268. }
  269. // Dispose
  270. /**
  271. * Disposes the Submesh.
  272. * Returns nothing.
  273. */
  274. public dispose(): void {
  275. if (this._linesIndexBuffer) {
  276. this._mesh.getScene().getEngine()._releaseBuffer(this._linesIndexBuffer);
  277. this._linesIndexBuffer = null;
  278. }
  279. // Remove from mesh
  280. var index = this._mesh.subMeshes.indexOf(this);
  281. this._mesh.subMeshes.splice(index, 1);
  282. }
  283. // Statics
  284. /**
  285. * Creates a new Submesh from the passed parameters :
  286. * - materialIndex (integer) : the index of the main mesh material.
  287. * - startIndex (integer) : the index where to start the copy in the mesh indices array.
  288. * - indexCount (integer) : the number of indices to copy then from the startIndex.
  289. * - mesh (Mesh) : the main mesh to create the submesh from.
  290. * - renderingMesh (optional Mesh) : rendering mesh.
  291. */
  292. public static CreateFromIndices(materialIndex: number, startIndex: number, indexCount: number, mesh: AbstractMesh, renderingMesh?: Mesh): SubMesh {
  293. var minVertexIndex = Number.MAX_VALUE;
  294. var maxVertexIndex = -Number.MAX_VALUE;
  295. renderingMesh = (<Mesh>(renderingMesh || <Mesh>mesh));
  296. var indices = <IndicesArray>renderingMesh.getIndices();
  297. for (var index = startIndex; index < startIndex + indexCount; index++) {
  298. var vertexIndex = indices[index];
  299. if (vertexIndex < minVertexIndex)
  300. minVertexIndex = vertexIndex;
  301. if (vertexIndex > maxVertexIndex)
  302. maxVertexIndex = vertexIndex;
  303. }
  304. return new SubMesh(materialIndex, minVertexIndex, maxVertexIndex - minVertexIndex + 1, startIndex, indexCount, mesh, renderingMesh);
  305. }
  306. }
  307. }