babylon.subMesh.ts 12 KB

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