babylon.renderingGroup.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.RenderingGroup = function (index, scene) {
  5. this.index = index;
  6. this._scene = scene;
  7. this._opaqueSubMeshes = new BABYLON.Tools.SmartArray(256);
  8. this._transparentSubMeshes = new BABYLON.Tools.SmartArray(256);
  9. this._alphaTestSubMeshes = new BABYLON.Tools.SmartArray(256);
  10. };
  11. // Methods
  12. BABYLON.RenderingGroup.prototype.render = function (customRenderFunction, beforeTransparents) {
  13. if (customRenderFunction) {
  14. customRenderFunction(this._opaqueSubMeshes, this._alphaTestSubMeshes, this._transparentSubMeshes, beforeTransparents);
  15. return true;
  16. }
  17. if (this._opaqueSubMeshes.length === 0 && this._alphaTestSubMeshes.length === 0 && this._transparentSubMeshes === 0) {
  18. return false;
  19. }
  20. var engine = this._scene.getEngine();
  21. // Opaque
  22. var subIndex;
  23. var submesh;
  24. for (subIndex = 0; subIndex < this._opaqueSubMeshes.length; subIndex++) {
  25. submesh = this._opaqueSubMeshes.data[subIndex];
  26. this._activeVertices += submesh.verticesCount;
  27. submesh.render();
  28. }
  29. // Alpha test
  30. engine.setAlphaTesting(true);
  31. for (subIndex = 0; subIndex < this._alphaTestSubMeshes.length; subIndex++) {
  32. submesh = this._alphaTestSubMeshes.data[subIndex];
  33. this._activeVertices += submesh.verticesCount;
  34. submesh.render();
  35. }
  36. engine.setAlphaTesting(false);
  37. if (beforeTransparents) {
  38. beforeTransparents();
  39. }
  40. // Transparent
  41. if (this._transparentSubMeshes.length) {
  42. // Sorting
  43. for (subIndex = 0; subIndex < this._transparentSubMeshes.length; subIndex++) {
  44. submesh = this._transparentSubMeshes.data[subIndex];
  45. submesh._distanceToCamera = submesh.getBoundingInfo().boundingSphere.centerWorld.subtract(this._scene.activeCamera.position).length();
  46. }
  47. var sortedArray = this._transparentSubMeshes.data.slice(0, this._transparentSubMeshes.length);
  48. sortedArray.sort(function (a, b) {
  49. if (a._distanceToCamera < b._distanceToCamera) {
  50. return 1;
  51. }
  52. if (a._distanceToCamera > b._distanceToCamera) {
  53. return -1;
  54. }
  55. return 0;
  56. });
  57. // Rendering
  58. engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
  59. for (subIndex = 0; subIndex < sortedArray.length; subIndex++) {
  60. submesh = sortedArray[subIndex];
  61. this._activeVertices += submesh.verticesCount;
  62. submesh.render();
  63. }
  64. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  65. }
  66. return true;
  67. };
  68. BABYLON.RenderingGroup.prototype.prepare = function () {
  69. this._opaqueSubMeshes.reset();
  70. this._transparentSubMeshes.reset();
  71. this._alphaTestSubMeshes.reset();
  72. };
  73. BABYLON.RenderingGroup.prototype.dispatch = function (subMesh) {
  74. var material = subMesh.getMaterial();
  75. var mesh = subMesh.getMesh();
  76. if (material.needAlphaBlending() || mesh.visibility < 1.0) { // Transparent
  77. if (material.alpha > 0 || mesh.visibility < 1.0) {
  78. this._transparentSubMeshes.push(subMesh);
  79. }
  80. } else if (material.needAlphaTesting()) { // Alpha test
  81. this._alphaTestSubMeshes.push(subMesh);
  82. } else {
  83. this._opaqueSubMeshes.push(subMesh); // Opaque
  84. }
  85. };
  86. })();