babylon.renderingManager.js 3.6 KB

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