babylon.renderingGroup.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var RenderingGroup = (function () {
  4. function RenderingGroup(index, scene) {
  5. this.index = index;
  6. this._opaqueSubMeshes = new BABYLON.SmartArray(256);
  7. this._transparentSubMeshes = new BABYLON.SmartArray(256);
  8. this._alphaTestSubMeshes = new BABYLON.SmartArray(256);
  9. this._scene = scene;
  10. }
  11. 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.length === 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. for (subIndex = 0; subIndex < this._transparentSubMeshes.length; subIndex++) {
  42. submesh = this._transparentSubMeshes.data[subIndex];
  43. submesh._distanceToCamera = submesh.getBoundingInfo().boundingSphere.centerWorld.subtract(this._scene.activeCamera.position).length();
  44. }
  45. var sortedArray = this._transparentSubMeshes.data.slice(0, this._transparentSubMeshes.length);
  46. sortedArray.sort(function (a, b) {
  47. if (a._distanceToCamera < b._distanceToCamera) {
  48. return 1;
  49. }
  50. if (a._distanceToCamera > b._distanceToCamera) {
  51. return -1;
  52. }
  53. return 0;
  54. });
  55. // Rendering
  56. engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
  57. for (subIndex = 0; subIndex < sortedArray.length; subIndex++) {
  58. submesh = sortedArray[subIndex];
  59. this._activeVertices += submesh.verticesCount;
  60. submesh.render();
  61. }
  62. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  63. }
  64. return true;
  65. };
  66. RenderingGroup.prototype.prepare = function () {
  67. this._opaqueSubMeshes.reset();
  68. this._transparentSubMeshes.reset();
  69. this._alphaTestSubMeshes.reset();
  70. };
  71. RenderingGroup.prototype.dispatch = function (subMesh) {
  72. var material = subMesh.getMaterial();
  73. var mesh = subMesh.getMesh();
  74. if (material.needAlphaBlending() || mesh.visibility < 1.0 || mesh.hasVertexAlpha) {
  75. if (material.alpha > 0 || mesh.visibility < 1.0) {
  76. this._transparentSubMeshes.push(subMesh);
  77. }
  78. } else if (material.needAlphaTesting()) {
  79. this._alphaTestSubMeshes.push(subMesh);
  80. } else {
  81. this._opaqueSubMeshes.push(subMesh); // Opaque
  82. }
  83. };
  84. return RenderingGroup;
  85. })();
  86. BABYLON.RenderingGroup = RenderingGroup;
  87. })(BABYLON || (BABYLON = {}));
  88. //# sourceMappingURL=babylon.renderingGroup.js.map