babylon.renderingGroup.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
  41. for (subIndex = 0; subIndex < this._transparentSubMeshes.length; subIndex++) {
  42. submesh = this._transparentSubMeshes.data[subIndex];
  43. this._activeVertices += submesh.verticesCount;
  44. submesh.render();
  45. }
  46. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  47. return true;
  48. };
  49. BABYLON.RenderingGroup.prototype.prepare = function () {
  50. this._opaqueSubMeshes.reset();
  51. this._transparentSubMeshes.reset();
  52. this._alphaTestSubMeshes.reset();
  53. };
  54. BABYLON.RenderingGroup.prototype.dispatch = function (subMesh) {
  55. var material = subMesh.getMaterial();
  56. var mesh = subMesh.getMesh();
  57. if (material.needAlphaBlending() || mesh.visibility < 1.0) { // Transparent
  58. if (material.alpha > 0 || mesh.visibility < 1.0) {
  59. this._transparentSubMeshes.push(subMesh); // Opaque
  60. }
  61. } else if (material.needAlphaTesting()) { // Alpha test
  62. this._alphaTestSubMeshes.push(subMesh);
  63. } else {
  64. this._opaqueSubMeshes.push(subMesh);
  65. }
  66. };
  67. })();