babylon.renderingManager.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. module BABYLON {
  2. export class RenderingManager {
  3. public static MAX_RENDERINGGROUPS = 4;
  4. private _scene: Scene;
  5. private _renderingGroups = new Array<RenderingGroup>();
  6. private _depthBufferAlreadyCleaned: boolean;
  7. constructor(scene: Scene) {
  8. this._scene = scene;
  9. }
  10. private _renderParticles(index: number, activeMeshes: AbstractMesh[]): void {
  11. if (this._scene._activeParticleSystems.length === 0) {
  12. return;
  13. }
  14. // Particles
  15. var beforeParticlesDate = Tools.Now;
  16. for (var particleIndex = 0; particleIndex < this._scene._activeParticleSystems.length; particleIndex++) {
  17. var particleSystem = this._scene._activeParticleSystems.data[particleIndex];
  18. if (particleSystem.renderingGroupId !== index) {
  19. continue;
  20. }
  21. this._clearDepthBuffer();
  22. if (!particleSystem.emitter.position || !activeMeshes || activeMeshes.indexOf(particleSystem.emitter) !== -1) {
  23. this._scene._activeParticles += particleSystem.render();
  24. }
  25. }
  26. this._scene._particlesDuration += Tools.Now - beforeParticlesDate;
  27. }
  28. private _renderSprites(index: number): void {
  29. if (this._scene.spriteManagers.length === 0) {
  30. return;
  31. }
  32. // Sprites
  33. var beforeSpritessDate = Tools.Now;
  34. for (var id = 0; id < this._scene.spriteManagers.length; id++) {
  35. var spriteManager = this._scene.spriteManagers[id];
  36. if (spriteManager.renderingGroupId === index) {
  37. this._clearDepthBuffer();
  38. spriteManager.render();
  39. }
  40. }
  41. this._scene._spritesDuration += Tools.Now - beforeSpritessDate;
  42. }
  43. private _clearDepthBuffer(): void {
  44. if (this._depthBufferAlreadyCleaned) {
  45. return;
  46. }
  47. this._scene.getEngine().clear(0, false, true);
  48. this._depthBufferAlreadyCleaned = true;
  49. }
  50. public render(customRenderFunction: (opaqueSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, beforeTransparents: () => void) => void,
  51. activeMeshes: AbstractMesh[], renderParticles: boolean, renderSprites: boolean): void {
  52. for (var index = 0; index < BABYLON.RenderingManager.MAX_RENDERINGGROUPS; index++) {
  53. this._depthBufferAlreadyCleaned = false;
  54. var renderingGroup = this._renderingGroups[index];
  55. if (renderingGroup) {
  56. this._clearDepthBuffer();
  57. if (!renderingGroup.render(customRenderFunction, () => {
  58. if (renderSprites) {
  59. this._renderSprites(index);
  60. }
  61. })) {
  62. this._renderingGroups.splice(index, 1);
  63. }
  64. } else if (renderSprites) {
  65. this._renderSprites(index);
  66. }
  67. if (renderParticles) {
  68. this._renderParticles(index, activeMeshes);
  69. }
  70. }
  71. }
  72. public reset(): void {
  73. for (var index in this._renderingGroups) {
  74. var renderingGroup = this._renderingGroups[index];
  75. renderingGroup.prepare();
  76. }
  77. }
  78. public dispatch(subMesh: SubMesh): void {
  79. var mesh = subMesh.getMesh();
  80. var renderingGroupId = mesh.renderingGroupId || 0;
  81. if (!this._renderingGroups[renderingGroupId]) {
  82. this._renderingGroups[renderingGroupId] = new BABYLON.RenderingGroup(renderingGroupId, this._scene);
  83. }
  84. this._renderingGroups[renderingGroupId].dispatch(subMesh);
  85. }
  86. }
  87. }