babylon.renderingManager.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. module BABYLON {
  2. /**
  3. * Interface describing the different options available in the rendering manager
  4. * regarding Auto Clear between groups.
  5. */
  6. interface RenderingManageAutoClearOptions {
  7. autoClear: boolean;
  8. depth: boolean;
  9. stencil: boolean;
  10. }
  11. export class RenderingManager {
  12. /**
  13. * The max id used for rendering groups (not included)
  14. */
  15. public static MAX_RENDERINGGROUPS = 4;
  16. /**
  17. * The min id used for rendering groups (included)
  18. */
  19. public static MIN_RENDERINGGROUPS = 0;
  20. /**
  21. * Used to globally prevent autoclearing scenes.
  22. */
  23. public static AUTOCLEAR = true;
  24. private _scene: Scene;
  25. private _renderingGroups = new Array<RenderingGroup>();
  26. private _depthStencilBufferAlreadyCleaned: boolean;
  27. private _autoClearDepthStencil: { [id: number]: RenderingManageAutoClearOptions } = {};
  28. private _customOpaqueSortCompareFn: { [id: number]: Nullable<(a: SubMesh, b: SubMesh) => number> } = {};
  29. private _customAlphaTestSortCompareFn: { [id: number]: Nullable<(a: SubMesh, b: SubMesh) => number> } = {};
  30. private _customTransparentSortCompareFn: { [id: number]: Nullable<(a: SubMesh, b: SubMesh) => number> } = {};
  31. private _renderinGroupInfo: Nullable<RenderingGroupInfo> = null;
  32. constructor(scene: Scene) {
  33. this._scene = scene;
  34. for (let i = RenderingManager.MIN_RENDERINGGROUPS; i < RenderingManager.MAX_RENDERINGGROUPS; i++) {
  35. this._autoClearDepthStencil[i] = { autoClear: true, depth: true, stencil: true };
  36. }
  37. }
  38. private _clearDepthStencilBuffer(depth = true, stencil = true): void {
  39. if (this._depthStencilBufferAlreadyCleaned) {
  40. return;
  41. }
  42. this._scene.getEngine().clear(null, false, depth, stencil);
  43. this._depthStencilBufferAlreadyCleaned = true;
  44. }
  45. public render(customRenderFunction: Nullable<(opaqueSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, depthOnlySubMeshes: SmartArray<SubMesh>) => void>,
  46. activeMeshes: Nullable<AbstractMesh[]>, renderParticles: boolean, renderSprites: boolean): void {
  47. // Check if there's at least on observer on the onRenderingGroupObservable and initialize things to fire it
  48. let observable = this._scene.onRenderingGroupObservable.hasObservers() ? this._scene.onRenderingGroupObservable : null;
  49. let info: Nullable<RenderingGroupInfo> = null;
  50. if (observable) {
  51. if (!this._renderinGroupInfo) {
  52. this._renderinGroupInfo = new RenderingGroupInfo();
  53. }
  54. info = this._renderinGroupInfo;
  55. info.scene = this._scene;
  56. info.camera = this._scene.activeCamera;
  57. }
  58. // Dispatch sprites
  59. if (renderSprites) {
  60. for (let index = 0; index < this._scene.spriteManagers.length; index++) {
  61. var manager = this._scene.spriteManagers[index];
  62. this.dispatchSprites(manager);
  63. }
  64. }
  65. // Render
  66. for (let index = RenderingManager.MIN_RENDERINGGROUPS; index < RenderingManager.MAX_RENDERINGGROUPS; index++) {
  67. this._depthStencilBufferAlreadyCleaned = index === RenderingManager.MIN_RENDERINGGROUPS;
  68. var renderingGroup = this._renderingGroups[index];
  69. if (!renderingGroup && !observable)
  70. continue;
  71. let renderingGroupMask = 0;
  72. // Fire PRECLEAR stage
  73. if (observable && info) {
  74. renderingGroupMask = Math.pow(2, index);
  75. info.renderStage = RenderingGroupInfo.STAGE_PRECLEAR;
  76. info.renderingGroupId = index;
  77. observable.notifyObservers(info, renderingGroupMask);
  78. }
  79. // Clear depth/stencil if needed
  80. if (RenderingManager.AUTOCLEAR) {
  81. let autoClear = this._autoClearDepthStencil[index];
  82. if (autoClear && autoClear.autoClear) {
  83. this._clearDepthStencilBuffer(autoClear.depth, autoClear.stencil);
  84. }
  85. }
  86. if (observable && info) {
  87. // Fire PREOPAQUE stage
  88. info.renderStage = RenderingGroupInfo.STAGE_PREOPAQUE;
  89. observable.notifyObservers(info, renderingGroupMask);
  90. // Fire PRETRANSPARENT stage
  91. info.renderStage = RenderingGroupInfo.STAGE_PRETRANSPARENT;
  92. observable.notifyObservers(info, renderingGroupMask);
  93. }
  94. if (renderingGroup)
  95. renderingGroup.render(customRenderFunction, renderSprites, renderParticles, activeMeshes);
  96. // Fire POSTTRANSPARENT stage
  97. if (observable && info) {
  98. info.renderStage = RenderingGroupInfo.STAGE_POSTTRANSPARENT;
  99. observable.notifyObservers(info, renderingGroupMask);
  100. }
  101. }
  102. }
  103. public reset(): void {
  104. for (let index = RenderingManager.MIN_RENDERINGGROUPS; index < RenderingManager.MAX_RENDERINGGROUPS; index++) {
  105. var renderingGroup = this._renderingGroups[index];
  106. if (renderingGroup) {
  107. renderingGroup.prepare();
  108. }
  109. }
  110. }
  111. public dispose(): void {
  112. for (let index = RenderingManager.MIN_RENDERINGGROUPS; index < RenderingManager.MAX_RENDERINGGROUPS; index++) {
  113. var renderingGroup = this._renderingGroups[index];
  114. if (renderingGroup) {
  115. renderingGroup.dispose();
  116. }
  117. }
  118. this._renderingGroups.length = 0;
  119. }
  120. private _prepareRenderingGroup(renderingGroupId: number): void {
  121. if (this._renderingGroups[renderingGroupId] === undefined) {
  122. this._renderingGroups[renderingGroupId] = new RenderingGroup(renderingGroupId, this._scene,
  123. this._customOpaqueSortCompareFn[renderingGroupId],
  124. this._customAlphaTestSortCompareFn[renderingGroupId],
  125. this._customTransparentSortCompareFn[renderingGroupId]
  126. );
  127. }
  128. }
  129. public dispatchSprites(spriteManager: SpriteManager) {
  130. var renderingGroupId = spriteManager.renderingGroupId || 0;
  131. this._prepareRenderingGroup(renderingGroupId);
  132. this._renderingGroups[renderingGroupId].dispatchSprites(spriteManager);
  133. }
  134. public dispatchParticles(particleSystem: IParticleSystem) {
  135. var renderingGroupId = particleSystem.renderingGroupId || 0;
  136. this._prepareRenderingGroup(renderingGroupId);
  137. this._renderingGroups[renderingGroupId].dispatchParticles(particleSystem);
  138. }
  139. /**
  140. * @param subMesh The submesh to dispatch
  141. * @param [mesh] Optional reference to the submeshes's mesh. Provide if you have an exiting reference to improve performance.
  142. * @param [material] Optional reference to the submeshes's material. Provide if you have an exiting reference to improve performance.
  143. */
  144. public dispatch(subMesh: SubMesh, mesh?: AbstractMesh, material?: Nullable<Material>): void {
  145. if (mesh === undefined) {
  146. mesh = subMesh.getMesh();
  147. }
  148. var renderingGroupId = mesh.renderingGroupId || 0;
  149. this._prepareRenderingGroup(renderingGroupId);
  150. this._renderingGroups[renderingGroupId].dispatch(subMesh, mesh, material);
  151. }
  152. /**
  153. * Overrides the default sort function applied in the renderging group to prepare the meshes.
  154. * This allowed control for front to back rendering or reversly depending of the special needs.
  155. *
  156. * @param renderingGroupId The rendering group id corresponding to its index
  157. * @param opaqueSortCompareFn The opaque queue comparison function use to sort.
  158. * @param alphaTestSortCompareFn The alpha test queue comparison function use to sort.
  159. * @param transparentSortCompareFn The transparent queue comparison function use to sort.
  160. */
  161. public setRenderingOrder(renderingGroupId: number,
  162. opaqueSortCompareFn: Nullable<(a: SubMesh, b: SubMesh) => number> = null,
  163. alphaTestSortCompareFn: Nullable<(a: SubMesh, b: SubMesh) => number> = null,
  164. transparentSortCompareFn: Nullable<(a: SubMesh, b: SubMesh) => number> = null) {
  165. this._customOpaqueSortCompareFn[renderingGroupId] = opaqueSortCompareFn;
  166. this._customAlphaTestSortCompareFn[renderingGroupId] = alphaTestSortCompareFn;
  167. this._customTransparentSortCompareFn[renderingGroupId] = transparentSortCompareFn;
  168. if (this._renderingGroups[renderingGroupId]) {
  169. var group = this._renderingGroups[renderingGroupId];
  170. group.opaqueSortCompareFn = this._customOpaqueSortCompareFn[renderingGroupId];
  171. group.alphaTestSortCompareFn = this._customAlphaTestSortCompareFn[renderingGroupId];
  172. group.transparentSortCompareFn = this._customTransparentSortCompareFn[renderingGroupId];
  173. }
  174. }
  175. /**
  176. * Specifies whether or not the stencil and depth buffer are cleared between two rendering groups.
  177. *
  178. * @param renderingGroupId The rendering group id corresponding to its index
  179. * @param autoClearDepthStencil Automatically clears depth and stencil between groups if true.
  180. * @param depth Automatically clears depth between groups if true and autoClear is true.
  181. * @param stencil Automatically clears stencil between groups if true and autoClear is true.
  182. */
  183. public setRenderingAutoClearDepthStencil(renderingGroupId: number, autoClearDepthStencil: boolean,
  184. depth = true,
  185. stencil = true): void {
  186. this._autoClearDepthStencil[renderingGroupId] = {
  187. autoClear: autoClearDepthStencil,
  188. depth: depth,
  189. stencil: stencil
  190. };
  191. }
  192. }
  193. }