renderingGroup.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. import { SmartArray } from "../Misc/smartArray";
  2. import { SubMesh } from "../Meshes/subMesh";
  3. import { AbstractMesh } from "../Meshes/abstractMesh";
  4. import { Nullable } from "../types";
  5. import { Vector3 } from "../Maths/math";
  6. import { IParticleSystem } from "../Particles/IParticleSystem";
  7. import { IEdgesRenderer } from "./edgesRenderer";
  8. import { ISpriteManager } from "../Sprites/spriteManager";
  9. import { Constants } from "../Engines/constants";
  10. import { Material } from "../Materials/material";
  11. import { Scene } from "../scene";
  12. import { Camera } from "../Cameras/camera";
  13. /**
  14. * This represents the object necessary to create a rendering group.
  15. * This is exclusively used and created by the rendering manager.
  16. * To modify the behavior, you use the available helpers in your scene or meshes.
  17. * @hidden
  18. */
  19. export class RenderingGroup {
  20. private _scene: Scene;
  21. private _opaqueSubMeshes = new SmartArray<SubMesh>(256);
  22. private _transparentSubMeshes = new SmartArray<SubMesh>(256);
  23. private _alphaTestSubMeshes = new SmartArray<SubMesh>(256);
  24. private _depthOnlySubMeshes = new SmartArray<SubMesh>(256);
  25. private _particleSystems = new SmartArray<IParticleSystem>(256);
  26. private _spriteManagers = new SmartArray<ISpriteManager>(256);
  27. private _opaqueSortCompareFn: Nullable<(a: SubMesh, b: SubMesh) => number>;
  28. private _alphaTestSortCompareFn: Nullable<(a: SubMesh, b: SubMesh) => number>;
  29. private _transparentSortCompareFn: (a: SubMesh, b: SubMesh) => number;
  30. private _renderOpaque: (subMeshes: SmartArray<SubMesh>) => void;
  31. private _renderAlphaTest: (subMeshes: SmartArray<SubMesh>) => void;
  32. private _renderTransparent: (subMeshes: SmartArray<SubMesh>) => void;
  33. /** @hidden */
  34. public _edgesRenderers = new SmartArray<IEdgesRenderer>(16);
  35. public onBeforeTransparentRendering: () => void;
  36. /**
  37. * Set the opaque sort comparison function.
  38. * If null the sub meshes will be render in the order they were created
  39. */
  40. public set opaqueSortCompareFn(value: Nullable<(a: SubMesh, b: SubMesh) => number>) {
  41. this._opaqueSortCompareFn = value;
  42. if (value) {
  43. this._renderOpaque = this.renderOpaqueSorted;
  44. }
  45. else {
  46. this._renderOpaque = RenderingGroup.renderUnsorted;
  47. }
  48. }
  49. /**
  50. * Set the alpha test sort comparison function.
  51. * If null the sub meshes will be render in the order they were created
  52. */
  53. public set alphaTestSortCompareFn(value: Nullable<(a: SubMesh, b: SubMesh) => number>) {
  54. this._alphaTestSortCompareFn = value;
  55. if (value) {
  56. this._renderAlphaTest = this.renderAlphaTestSorted;
  57. }
  58. else {
  59. this._renderAlphaTest = RenderingGroup.renderUnsorted;
  60. }
  61. }
  62. /**
  63. * Set the transparent sort comparison function.
  64. * If null the sub meshes will be render in the order they were created
  65. */
  66. public set transparentSortCompareFn(value: Nullable<(a: SubMesh, b: SubMesh) => number>) {
  67. if (value) {
  68. this._transparentSortCompareFn = value;
  69. }
  70. else {
  71. this._transparentSortCompareFn = RenderingGroup.defaultTransparentSortCompare;
  72. }
  73. this._renderTransparent = this.renderTransparentSorted;
  74. }
  75. /**
  76. * Creates a new rendering group.
  77. * @param index The rendering group index
  78. * @param opaqueSortCompareFn The opaque sort comparison function. If null no order is applied
  79. * @param alphaTestSortCompareFn The alpha test sort comparison function. If null no order is applied
  80. * @param transparentSortCompareFn The transparent sort comparison function. If null back to front + alpha index sort is applied
  81. */
  82. constructor(public index: number, scene: Scene,
  83. opaqueSortCompareFn: Nullable<(a: SubMesh, b: SubMesh) => number> = null,
  84. alphaTestSortCompareFn: Nullable<(a: SubMesh, b: SubMesh) => number> = null,
  85. transparentSortCompareFn: Nullable<(a: SubMesh, b: SubMesh) => number> = null) {
  86. this._scene = scene;
  87. this.opaqueSortCompareFn = opaqueSortCompareFn;
  88. this.alphaTestSortCompareFn = alphaTestSortCompareFn;
  89. this.transparentSortCompareFn = transparentSortCompareFn;
  90. }
  91. /**
  92. * Render all the sub meshes contained in the group.
  93. * @param customRenderFunction Used to override the default render behaviour of the group.
  94. * @returns true if rendered some submeshes.
  95. */
  96. public render(customRenderFunction: Nullable<(opaqueSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, depthOnlySubMeshes: SmartArray<SubMesh>) => void>, renderSprites: boolean, renderParticles: boolean, activeMeshes: Nullable<AbstractMesh[]>): void {
  97. if (customRenderFunction) {
  98. customRenderFunction(this._opaqueSubMeshes, this._alphaTestSubMeshes, this._transparentSubMeshes, this._depthOnlySubMeshes);
  99. return;
  100. }
  101. var engine = this._scene.getEngine();
  102. // Depth only
  103. if (this._depthOnlySubMeshes.length !== 0) {
  104. engine.setColorWrite(false);
  105. this._renderAlphaTest(this._depthOnlySubMeshes);
  106. engine.setColorWrite(true);
  107. }
  108. // Opaque
  109. if (this._opaqueSubMeshes.length !== 0) {
  110. this._renderOpaque(this._opaqueSubMeshes);
  111. }
  112. // Alpha test
  113. if (this._alphaTestSubMeshes.length !== 0) {
  114. this._renderAlphaTest(this._alphaTestSubMeshes);
  115. }
  116. var stencilState = engine.getStencilBuffer();
  117. engine.setStencilBuffer(false);
  118. // Sprites
  119. if (renderSprites) {
  120. this._renderSprites();
  121. }
  122. // Particles
  123. if (renderParticles) {
  124. this._renderParticles(activeMeshes);
  125. }
  126. if (this.onBeforeTransparentRendering) {
  127. this.onBeforeTransparentRendering();
  128. }
  129. // Transparent
  130. if (this._transparentSubMeshes.length !== 0) {
  131. this._renderTransparent(this._transparentSubMeshes);
  132. engine.setAlphaMode(Constants.ALPHA_DISABLE);
  133. }
  134. // Set back stencil to false in case it changes before the edge renderer.
  135. engine.setStencilBuffer(false);
  136. // Edges
  137. if (this._edgesRenderers.length) {
  138. for (var edgesRendererIndex = 0; edgesRendererIndex < this._edgesRenderers.length; edgesRendererIndex++) {
  139. this._edgesRenderers.data[edgesRendererIndex].render();
  140. }
  141. engine.setAlphaMode(Constants.ALPHA_DISABLE);
  142. }
  143. // Restore Stencil state.
  144. engine.setStencilBuffer(stencilState);
  145. }
  146. /**
  147. * Renders the opaque submeshes in the order from the opaqueSortCompareFn.
  148. * @param subMeshes The submeshes to render
  149. */
  150. private renderOpaqueSorted(subMeshes: SmartArray<SubMesh>): void {
  151. return RenderingGroup.renderSorted(subMeshes, this._opaqueSortCompareFn, this._scene.activeCamera, false);
  152. }
  153. /**
  154. * Renders the opaque submeshes in the order from the alphatestSortCompareFn.
  155. * @param subMeshes The submeshes to render
  156. */
  157. private renderAlphaTestSorted(subMeshes: SmartArray<SubMesh>): void {
  158. return RenderingGroup.renderSorted(subMeshes, this._alphaTestSortCompareFn, this._scene.activeCamera, false);
  159. }
  160. /**
  161. * Renders the opaque submeshes in the order from the transparentSortCompareFn.
  162. * @param subMeshes The submeshes to render
  163. */
  164. private renderTransparentSorted(subMeshes: SmartArray<SubMesh>): void {
  165. return RenderingGroup.renderSorted(subMeshes, this._transparentSortCompareFn, this._scene.activeCamera, true);
  166. }
  167. /**
  168. * Renders the submeshes in a specified order.
  169. * @param subMeshes The submeshes to sort before render
  170. * @param sortCompareFn The comparison function use to sort
  171. * @param cameraPosition The camera position use to preprocess the submeshes to help sorting
  172. * @param transparent Specifies to activate blending if true
  173. */
  174. private static renderSorted(subMeshes: SmartArray<SubMesh>, sortCompareFn: Nullable<(a: SubMesh, b: SubMesh) => number>, camera: Nullable<Camera>, transparent: boolean): void {
  175. let subIndex = 0;
  176. let subMesh: SubMesh;
  177. let cameraPosition = camera ? camera.globalPosition : Vector3.Zero();
  178. for (; subIndex < subMeshes.length; subIndex++) {
  179. subMesh = subMeshes.data[subIndex];
  180. subMesh._alphaIndex = subMesh.getMesh().alphaIndex;
  181. subMesh._distanceToCamera = subMesh.getBoundingInfo().boundingSphere.centerWorld.subtract(cameraPosition).length();
  182. }
  183. let sortedArray = subMeshes.data.slice(0, subMeshes.length);
  184. if (sortCompareFn) {
  185. sortedArray.sort(sortCompareFn);
  186. }
  187. for (subIndex = 0; subIndex < sortedArray.length; subIndex++) {
  188. subMesh = sortedArray[subIndex];
  189. if (transparent) {
  190. let material = subMesh.getMaterial();
  191. if (material && material.needDepthPrePass) {
  192. let engine = material.getScene().getEngine();
  193. engine.setColorWrite(false);
  194. engine.setAlphaMode(Constants.ALPHA_DISABLE);
  195. subMesh.render(false);
  196. engine.setColorWrite(true);
  197. }
  198. }
  199. subMesh.render(transparent);
  200. }
  201. }
  202. /**
  203. * Renders the submeshes in the order they were dispatched (no sort applied).
  204. * @param subMeshes The submeshes to render
  205. */
  206. private static renderUnsorted(subMeshes: SmartArray<SubMesh>): void {
  207. for (var subIndex = 0; subIndex < subMeshes.length; subIndex++) {
  208. let submesh = subMeshes.data[subIndex];
  209. submesh.render(false);
  210. }
  211. }
  212. /**
  213. * Build in function which can be applied to ensure meshes of a special queue (opaque, alpha test, transparent)
  214. * are rendered back to front if in the same alpha index.
  215. *
  216. * @param a The first submesh
  217. * @param b The second submesh
  218. * @returns The result of the comparison
  219. */
  220. public static defaultTransparentSortCompare(a: SubMesh, b: SubMesh): number {
  221. // Alpha index first
  222. if (a._alphaIndex > b._alphaIndex) {
  223. return 1;
  224. }
  225. if (a._alphaIndex < b._alphaIndex) {
  226. return -1;
  227. }
  228. // Then distance to camera
  229. return RenderingGroup.backToFrontSortCompare(a, b);
  230. }
  231. /**
  232. * Build in function which can be applied to ensure meshes of a special queue (opaque, alpha test, transparent)
  233. * are rendered back to front.
  234. *
  235. * @param a The first submesh
  236. * @param b The second submesh
  237. * @returns The result of the comparison
  238. */
  239. public static backToFrontSortCompare(a: SubMesh, b: SubMesh): number {
  240. // Then distance to camera
  241. if (a._distanceToCamera < b._distanceToCamera) {
  242. return 1;
  243. }
  244. if (a._distanceToCamera > b._distanceToCamera) {
  245. return -1;
  246. }
  247. return 0;
  248. }
  249. /**
  250. * Build in function which can be applied to ensure meshes of a special queue (opaque, alpha test, transparent)
  251. * are rendered front to back (prevent overdraw).
  252. *
  253. * @param a The first submesh
  254. * @param b The second submesh
  255. * @returns The result of the comparison
  256. */
  257. public static frontToBackSortCompare(a: SubMesh, b: SubMesh): number {
  258. // Then distance to camera
  259. if (a._distanceToCamera < b._distanceToCamera) {
  260. return -1;
  261. }
  262. if (a._distanceToCamera > b._distanceToCamera) {
  263. return 1;
  264. }
  265. return 0;
  266. }
  267. /**
  268. * Resets the different lists of submeshes to prepare a new frame.
  269. */
  270. public prepare(): void {
  271. this._opaqueSubMeshes.reset();
  272. this._transparentSubMeshes.reset();
  273. this._alphaTestSubMeshes.reset();
  274. this._depthOnlySubMeshes.reset();
  275. this._particleSystems.reset();
  276. this._spriteManagers.reset();
  277. this._edgesRenderers.reset();
  278. }
  279. public dispose(): void {
  280. this._opaqueSubMeshes.dispose();
  281. this._transparentSubMeshes.dispose();
  282. this._alphaTestSubMeshes.dispose();
  283. this._depthOnlySubMeshes.dispose();
  284. this._particleSystems.dispose();
  285. this._spriteManagers.dispose();
  286. this._edgesRenderers.dispose();
  287. }
  288. /**
  289. * Inserts the submesh in its correct queue depending on its material.
  290. * @param subMesh The submesh to dispatch
  291. * @param [mesh] Optional reference to the submeshes's mesh. Provide if you have an exiting reference to improve performance.
  292. * @param [material] Optional reference to the submeshes's material. Provide if you have an exiting reference to improve performance.
  293. */
  294. public dispatch(subMesh: SubMesh, mesh?: AbstractMesh, material?: Nullable<Material>): void {
  295. // Get mesh and materials if not provided
  296. if (mesh === undefined) {
  297. mesh = subMesh.getMesh();
  298. }
  299. if (material === undefined) {
  300. material = subMesh.getMaterial();
  301. }
  302. if (material === null || material === undefined) {
  303. return;
  304. }
  305. if (material.needAlphaBlendingForMesh(mesh)) { // Transparent
  306. this._transparentSubMeshes.push(subMesh);
  307. } else if (material.needAlphaTesting()) { // Alpha test
  308. if (material.needDepthPrePass) {
  309. this._depthOnlySubMeshes.push(subMesh);
  310. }
  311. this._alphaTestSubMeshes.push(subMesh);
  312. } else {
  313. if (material.needDepthPrePass) {
  314. this._depthOnlySubMeshes.push(subMesh);
  315. }
  316. this._opaqueSubMeshes.push(subMesh); // Opaque
  317. }
  318. mesh._renderingGroup = this;
  319. if (mesh._edgesRenderer && mesh._edgesRenderer.isEnabled) {
  320. this._edgesRenderers.push(mesh._edgesRenderer);
  321. }
  322. }
  323. public dispatchSprites(spriteManager: ISpriteManager) {
  324. this._spriteManagers.push(spriteManager);
  325. }
  326. public dispatchParticles(particleSystem: IParticleSystem) {
  327. this._particleSystems.push(particleSystem);
  328. }
  329. private _renderParticles(activeMeshes: Nullable<AbstractMesh[]>): void {
  330. if (this._particleSystems.length === 0) {
  331. return;
  332. }
  333. // Particles
  334. var activeCamera = this._scene.activeCamera;
  335. this._scene.onBeforeParticlesRenderingObservable.notifyObservers(this._scene);
  336. for (var particleIndex = 0; particleIndex < this._particleSystems.length; particleIndex++) {
  337. var particleSystem = this._particleSystems.data[particleIndex];
  338. if ((activeCamera && activeCamera.layerMask & particleSystem.layerMask) === 0) {
  339. continue;
  340. }
  341. let emitter: any = particleSystem.emitter;
  342. if (!emitter.position || !activeMeshes || activeMeshes.indexOf(emitter) !== -1) {
  343. this._scene._activeParticles.addCount(particleSystem.render(), false);
  344. }
  345. }
  346. this._scene.onAfterParticlesRenderingObservable.notifyObservers(this._scene);
  347. }
  348. private _renderSprites(): void {
  349. if (!this._scene.spritesEnabled || this._spriteManagers.length === 0) {
  350. return;
  351. }
  352. // Sprites
  353. var activeCamera = this._scene.activeCamera;
  354. this._scene.onBeforeSpritesRenderingObservable.notifyObservers(this._scene);
  355. for (var id = 0; id < this._spriteManagers.length; id++) {
  356. var spriteManager = this._spriteManagers.data[id];
  357. if (((activeCamera && activeCamera.layerMask & spriteManager.layerMask) !== 0)) {
  358. spriteManager.render();
  359. }
  360. }
  361. this._scene.onAfterSpritesRenderingObservable.notifyObservers(this._scene);
  362. }
  363. }