babylon.renderingManager.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var RenderingManager = (function () {
  4. function RenderingManager(scene) {
  5. this._renderingGroups = new Array();
  6. this._scene = scene;
  7. }
  8. RenderingManager.prototype._renderParticles = function (index, activeMeshes) {
  9. if (this._scene._activeParticleSystems.length === 0) {
  10. return;
  11. }
  12. // Particles
  13. var activeCamera = this._scene.activeCamera;
  14. var beforeParticlesDate = BABYLON.Tools.Now;
  15. for (var particleIndex = 0; particleIndex < this._scene._activeParticleSystems.length; particleIndex++) {
  16. var particleSystem = this._scene._activeParticleSystems.data[particleIndex];
  17. if (particleSystem.renderingGroupId !== index) {
  18. continue;
  19. }
  20. if ((activeCamera.layerMask & particleSystem.layerMask) === 0) {
  21. continue;
  22. }
  23. this._clearDepthBuffer();
  24. if (!particleSystem.emitter.position || !activeMeshes || activeMeshes.indexOf(particleSystem.emitter) !== -1) {
  25. this._scene._activeParticles += particleSystem.render();
  26. }
  27. }
  28. this._scene._particlesDuration += BABYLON.Tools.Now - beforeParticlesDate;
  29. };
  30. RenderingManager.prototype._renderSprites = function (index) {
  31. if (!this._scene.spritesEnabled || this._scene.spriteManagers.length === 0) {
  32. return;
  33. }
  34. // Sprites
  35. var activeCamera = this._scene.activeCamera;
  36. var beforeSpritessDate = BABYLON.Tools.Now;
  37. for (var id = 0; id < this._scene.spriteManagers.length; id++) {
  38. var spriteManager = this._scene.spriteManagers[id];
  39. if (spriteManager.renderingGroupId === index && ((activeCamera.layerMask & spriteManager.layerMask) !== 0)) {
  40. this._clearDepthBuffer();
  41. spriteManager.render();
  42. }
  43. }
  44. this._scene._spritesDuration += BABYLON.Tools.Now - beforeSpritessDate;
  45. };
  46. RenderingManager.prototype._clearDepthBuffer = function () {
  47. if (this._depthBufferAlreadyCleaned) {
  48. return;
  49. }
  50. this._scene.getEngine().clear(0, false, true);
  51. this._depthBufferAlreadyCleaned = true;
  52. };
  53. RenderingManager.prototype._renderSpritesAndParticles = function () {
  54. if (this._currentRenderSprites) {
  55. this._renderSprites(this._currentIndex);
  56. }
  57. if (this._currentRenderParticles) {
  58. this._renderParticles(this._currentIndex, this._currentActiveMeshes);
  59. }
  60. };
  61. RenderingManager.prototype.render = function (customRenderFunction, activeMeshes, renderParticles, renderSprites) {
  62. this._currentActiveMeshes = activeMeshes;
  63. this._currentRenderParticles = renderParticles;
  64. this._currentRenderSprites = renderSprites;
  65. for (var index = 0; index < RenderingManager.MAX_RENDERINGGROUPS; index++) {
  66. this._depthBufferAlreadyCleaned = false;
  67. var renderingGroup = this._renderingGroups[index];
  68. var needToStepBack = false;
  69. this._currentIndex = index;
  70. if (renderingGroup) {
  71. this._clearDepthBuffer();
  72. if (!renderingGroup.onBeforeTransparentRendering) {
  73. renderingGroup.onBeforeTransparentRendering = this._renderSpritesAndParticles.bind(this);
  74. }
  75. if (!renderingGroup.render(customRenderFunction)) {
  76. this._renderingGroups.splice(index, 1);
  77. needToStepBack = true;
  78. this._renderSpritesAndParticles();
  79. }
  80. }
  81. else {
  82. this._renderSpritesAndParticles();
  83. }
  84. if (needToStepBack) {
  85. index--;
  86. }
  87. }
  88. };
  89. RenderingManager.prototype.reset = function () {
  90. this._renderingGroups.forEach(function (renderingGroup, index, array) {
  91. if (renderingGroup) {
  92. renderingGroup.prepare();
  93. }
  94. });
  95. };
  96. RenderingManager.prototype.dispatch = function (subMesh) {
  97. var mesh = subMesh.getMesh();
  98. var renderingGroupId = mesh.renderingGroupId || 0;
  99. if (!this._renderingGroups[renderingGroupId]) {
  100. this._renderingGroups[renderingGroupId] = new BABYLON.RenderingGroup(renderingGroupId, this._scene);
  101. }
  102. this._renderingGroups[renderingGroupId].dispatch(subMesh);
  103. };
  104. RenderingManager.MAX_RENDERINGGROUPS = 4;
  105. return RenderingManager;
  106. })();
  107. BABYLON.RenderingManager = RenderingManager;
  108. })(BABYLON || (BABYLON = {}));