babylon.renderingManager.js 3.6 KB

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