babylon.renderingGroup.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var RenderingGroup = (function () {
  4. //ANY
  5. function RenderingGroup(index, scene) {
  6. this.index = index;
  7. this._opaqueSubMeshes = new BABYLON.SmartArray(256);
  8. this._transparentSubMeshes = new BABYLON.SmartArray(256);
  9. this._alphaTestSubMeshes = new BABYLON.SmartArray(256);
  10. this._scene = scene;
  11. }
  12. 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.length === 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. 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.setAlphaMode(2); //ANY BABYLON.Engine.ALPHA_COMBINE);
  58. for (subIndex = 0; subIndex < sortedArray.length; subIndex++) {
  59. submesh = sortedArray[subIndex];
  60. this._activeVertices += submesh.verticesCount;
  61. submesh.render();
  62. }
  63. engine.setAlphaMode(0); //ANY BABYLON.Engine.ALPHA_DISABLE);
  64. }
  65. return true;
  66. };
  67. RenderingGroup.prototype.prepare = function () {
  68. this._opaqueSubMeshes.reset();
  69. this._transparentSubMeshes.reset();
  70. this._alphaTestSubMeshes.reset();
  71. };
  72. RenderingGroup.prototype.dispatch = function (subMesh) {
  73. var material = subMesh.getMaterial();
  74. var mesh = subMesh.getMesh();
  75. if (material.needAlphaBlending() || mesh.visibility < 1.0) {
  76. if (material.alpha > 0 || mesh.visibility < 1.0) {
  77. this._transparentSubMeshes.push(subMesh);
  78. }
  79. } else if (material.needAlphaTesting()) {
  80. this._alphaTestSubMeshes.push(subMesh);
  81. } else {
  82. this._opaqueSubMeshes.push(subMesh); // Opaque
  83. }
  84. };
  85. return RenderingGroup;
  86. })();
  87. BABYLON.RenderingGroup = RenderingGroup;
  88. })(BABYLON || (BABYLON = {}));
  89. //# sourceMappingURL=babylon.renderingGroup.js.map