babylon.renderingManager.ts 11 KB

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