babylon.renderingManager.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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>) => 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. this._renderingGroups.splice(index, 1);
  59. }
  60. }
  61. this._renderSprites(index);
  62. if (renderParticles) {
  63. this._renderParticles(index, activeMeshes);
  64. }
  65. }
  66. }
  67. public reset(): void {
  68. for (var index in this._renderingGroups) {
  69. var renderingGroup = this._renderingGroups[index];
  70. renderingGroup.prepare();
  71. }
  72. }
  73. public dispatch(subMesh: SubMesh): void {
  74. var mesh = subMesh.getMesh();
  75. var renderingGroupId = mesh.renderingGroupId || 0;
  76. if (!this._renderingGroups[renderingGroupId]) {
  77. this._renderingGroups[renderingGroupId] = new BABYLON.RenderingGroup(renderingGroupId, this._scene);
  78. }
  79. this._renderingGroups[renderingGroupId].dispatch(subMesh);
  80. }
  81. }
  82. }