babylon.renderingGroup.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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._alphaIndex = submesh.getMesh().alphaIndex;
  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. // Alpha index first
  49. if (a._alphaIndex > b._alphaIndex) {
  50. return 1;
  51. }
  52. if (a._alphaIndex < b._alphaIndex) {
  53. return -1;
  54. }
  55. // Then distance to camera
  56. if (a._distanceToCamera < b._distanceToCamera) {
  57. return 1;
  58. }
  59. if (a._distanceToCamera > b._distanceToCamera) {
  60. return -1;
  61. }
  62. return 0;
  63. });
  64. // Rendering
  65. engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
  66. for (subIndex = 0; subIndex < sortedArray.length; subIndex++) {
  67. submesh = sortedArray[subIndex];
  68. this._activeVertices += submesh.verticesCount;
  69. submesh.render();
  70. }
  71. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  72. }
  73. return true;
  74. };
  75. RenderingGroup.prototype.prepare = function () {
  76. this._opaqueSubMeshes.reset();
  77. this._transparentSubMeshes.reset();
  78. this._alphaTestSubMeshes.reset();
  79. };
  80. RenderingGroup.prototype.dispatch = function (subMesh) {
  81. var material = subMesh.getMaterial();
  82. var mesh = subMesh.getMesh();
  83. if (material.needAlphaBlending() || mesh.visibility < 1.0 || mesh.hasVertexAlpha) {
  84. this._transparentSubMeshes.push(subMesh);
  85. } else if (material.needAlphaTesting()) {
  86. this._alphaTestSubMeshes.push(subMesh);
  87. } else {
  88. this._opaqueSubMeshes.push(subMesh); // Opaque
  89. }
  90. };
  91. return RenderingGroup;
  92. })();
  93. BABYLON.RenderingGroup = RenderingGroup;
  94. })(BABYLON || (BABYLON = {}));
  95. //# sourceMappingURL=babylon.renderingGroup.js.map