babylon.subMesh.ts 12 KB

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