babylon.renderingManager.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var RenderingManager = (function () {
  4. function RenderingManager(scene) {
  5. this._renderingGroups = new Array();
  6. this._autoClearDepthStencil = {};
  7. this._customOpaqueSortCompareFn = {};
  8. this._customAlphaTestSortCompareFn = {};
  9. this._customTransparentSortCompareFn = {};
  10. this._scene = scene;
  11. for (var i = RenderingManager.MIN_RENDERINGGROUPS; i < RenderingManager.MAX_RENDERINGGROUPS; i++) {
  12. this._autoClearDepthStencil[i] = true;
  13. }
  14. }
  15. RenderingManager.prototype._renderParticles = function (index, activeMeshes) {
  16. if (this._scene._activeParticleSystems.length === 0) {
  17. return;
  18. }
  19. // Particles
  20. var activeCamera = this._scene.activeCamera;
  21. this._scene._particlesDuration.beginMonitoring();
  22. for (var particleIndex = 0; particleIndex < this._scene._activeParticleSystems.length; particleIndex++) {
  23. var particleSystem = this._scene._activeParticleSystems.data[particleIndex];
  24. if (particleSystem.renderingGroupId !== index) {
  25. continue;
  26. }
  27. if ((activeCamera.layerMask & particleSystem.layerMask) === 0) {
  28. continue;
  29. }
  30. this._clearDepthStencilBuffer();
  31. if (!particleSystem.emitter.position || !activeMeshes || activeMeshes.indexOf(particleSystem.emitter) !== -1) {
  32. this._scene._activeParticles.addCount(particleSystem.render(), false);
  33. }
  34. }
  35. this._scene._particlesDuration.endMonitoring(false);
  36. };
  37. RenderingManager.prototype._renderSprites = function (index) {
  38. if (!this._scene.spritesEnabled || this._scene.spriteManagers.length === 0) {
  39. return;
  40. }
  41. // Sprites
  42. var activeCamera = this._scene.activeCamera;
  43. this._scene._spritesDuration.beginMonitoring();
  44. for (var id = 0; id < this._scene.spriteManagers.length; id++) {
  45. var spriteManager = this._scene.spriteManagers[id];
  46. if (spriteManager.renderingGroupId === index && ((activeCamera.layerMask & spriteManager.layerMask) !== 0)) {
  47. this._clearDepthStencilBuffer();
  48. spriteManager.render();
  49. }
  50. }
  51. this._scene._spritesDuration.endMonitoring(false);
  52. };
  53. RenderingManager.prototype._clearDepthStencilBuffer = function () {
  54. if (this._depthStencilBufferAlreadyCleaned) {
  55. return;
  56. }
  57. this._scene.getEngine().clear(0, false, true, true);
  58. this._depthStencilBufferAlreadyCleaned = true;
  59. };
  60. RenderingManager.prototype._renderSpritesAndParticles = function () {
  61. if (this._currentRenderSprites) {
  62. this._renderSprites(this._currentIndex);
  63. }
  64. if (this._currentRenderParticles) {
  65. this._renderParticles(this._currentIndex, this._currentActiveMeshes);
  66. }
  67. };
  68. RenderingManager.prototype.render = function (customRenderFunction, activeMeshes, renderParticles, renderSprites) {
  69. this._currentActiveMeshes = activeMeshes;
  70. this._currentRenderParticles = renderParticles;
  71. this._currentRenderSprites = renderSprites;
  72. for (var index = RenderingManager.MIN_RENDERINGGROUPS; index < RenderingManager.MAX_RENDERINGGROUPS; index++) {
  73. this._depthStencilBufferAlreadyCleaned = index === RenderingManager.MIN_RENDERINGGROUPS;
  74. var renderingGroup = this._renderingGroups[index];
  75. var needToStepBack = false;
  76. this._currentIndex = index;
  77. if (renderingGroup) {
  78. if (this._autoClearDepthStencil[index]) {
  79. this._clearDepthStencilBuffer();
  80. }
  81. if (!renderingGroup.onBeforeTransparentRendering) {
  82. renderingGroup.onBeforeTransparentRendering = this._renderSpritesAndParticles.bind(this);
  83. }
  84. if (!renderingGroup.render(customRenderFunction)) {
  85. this._renderingGroups.splice(index, 1);
  86. needToStepBack = true;
  87. this._renderSpritesAndParticles();
  88. }
  89. }
  90. else {
  91. this._renderSpritesAndParticles();
  92. }
  93. if (needToStepBack) {
  94. index--;
  95. }
  96. }
  97. };
  98. RenderingManager.prototype.reset = function () {
  99. for (var index = RenderingManager.MIN_RENDERINGGROUPS; index < RenderingManager.MAX_RENDERINGGROUPS; index++) {
  100. var renderingGroup = this._renderingGroups[index];
  101. if (renderingGroup) {
  102. renderingGroup.prepare();
  103. }
  104. }
  105. };
  106. RenderingManager.prototype.dispatch = function (subMesh) {
  107. var mesh = subMesh.getMesh();
  108. var renderingGroupId = mesh.renderingGroupId || 0;
  109. if (!this._renderingGroups[renderingGroupId]) {
  110. this._renderingGroups[renderingGroupId] = new BABYLON.RenderingGroup(renderingGroupId, this._scene, this._customOpaqueSortCompareFn[renderingGroupId], this._customAlphaTestSortCompareFn[renderingGroupId], this._customTransparentSortCompareFn[renderingGroupId]);
  111. }
  112. this._renderingGroups[renderingGroupId].dispatch(subMesh);
  113. };
  114. /**
  115. * Overrides the default sort function applied in the renderging group to prepare the meshes.
  116. * This allowed control for front to back rendering or reversly depending of the special needs.
  117. *
  118. * @param renderingGroupId The rendering group id corresponding to its index
  119. * @param opaqueSortCompareFn The opaque queue comparison function use to sort.
  120. * @param alphaTestSortCompareFn The alpha test queue comparison function use to sort.
  121. * @param transparentSortCompareFn The transparent queue comparison function use to sort.
  122. */
  123. RenderingManager.prototype.setRenderingOrder = function (renderingGroupId, opaqueSortCompareFn, alphaTestSortCompareFn, transparentSortCompareFn) {
  124. if (opaqueSortCompareFn === void 0) { opaqueSortCompareFn = null; }
  125. if (alphaTestSortCompareFn === void 0) { alphaTestSortCompareFn = null; }
  126. if (transparentSortCompareFn === void 0) { transparentSortCompareFn = null; }
  127. if (this._renderingGroups[renderingGroupId]) {
  128. var group = this._renderingGroups[renderingGroupId];
  129. group.opaqueSortCompareFn = this._customOpaqueSortCompareFn[renderingGroupId];
  130. group.alphaTestSortCompareFn = this._customAlphaTestSortCompareFn[renderingGroupId];
  131. group.transparentSortCompareFn = this._customTransparentSortCompareFn[renderingGroupId];
  132. }
  133. this._customOpaqueSortCompareFn[renderingGroupId] = opaqueSortCompareFn;
  134. this._customAlphaTestSortCompareFn[renderingGroupId] = alphaTestSortCompareFn;
  135. this._customTransparentSortCompareFn[renderingGroupId] = transparentSortCompareFn;
  136. };
  137. /**
  138. * Specifies whether or not the stencil and depth buffer are cleared between two rendering groups.
  139. *
  140. * @param renderingGroupId The rendering group id corresponding to its index
  141. * @param autoClearDepthStencil Automatically clears depth and stencil between groups if true.
  142. */
  143. RenderingManager.prototype.setRenderingAutoClearDepthStencil = function (renderingGroupId, autoClearDepthStencil) {
  144. this._autoClearDepthStencil[renderingGroupId] = autoClearDepthStencil;
  145. };
  146. /**
  147. * The max id used for rendering groups (not included)
  148. */
  149. RenderingManager.MAX_RENDERINGGROUPS = 4;
  150. /**
  151. * The min id used for rendering groups (included)
  152. */
  153. RenderingManager.MIN_RENDERINGGROUPS = 0;
  154. return RenderingManager;
  155. }());
  156. BABYLON.RenderingManager = RenderingManager;
  157. })(BABYLON || (BABYLON = {}));