babylon.renderingManager.ts 10 KB

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