babylon.renderingGroup.js 3.7 KB

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