babylon.renderingManager.ts 9.8 KB

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