babylon.renderingGroup.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. submesh.render();
  26. }
  27. // Alpha test
  28. engine.setAlphaTesting(true);
  29. for (subIndex = 0; subIndex < this._alphaTestSubMeshes.length; subIndex++) {
  30. submesh = this._alphaTestSubMeshes.data[subIndex];
  31. submesh.render();
  32. }
  33. engine.setAlphaTesting(false);
  34. if (beforeTransparents) {
  35. beforeTransparents();
  36. }
  37. // Transparent
  38. if (this._transparentSubMeshes.length) {
  39. for (subIndex = 0; subIndex < this._transparentSubMeshes.length; subIndex++) {
  40. submesh = this._transparentSubMeshes.data[subIndex];
  41. submesh._alphaIndex = submesh.getMesh().alphaIndex;
  42. submesh._distanceToCamera = submesh.getBoundingInfo().boundingSphere.centerWorld.subtract(this._scene.activeCamera.position).length();
  43. }
  44. var sortedArray = this._transparentSubMeshes.data.slice(0, this._transparentSubMeshes.length);
  45. sortedArray.sort(function (a, b) {
  46. // Alpha index first
  47. if (a._alphaIndex > b._alphaIndex) {
  48. return 1;
  49. }
  50. if (a._alphaIndex < b._alphaIndex) {
  51. return -1;
  52. }
  53. // Then distance to camera
  54. if (a._distanceToCamera < b._distanceToCamera) {
  55. return 1;
  56. }
  57. if (a._distanceToCamera > b._distanceToCamera) {
  58. return -1;
  59. }
  60. return 0;
  61. });
  62. // Rendering
  63. engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
  64. for (subIndex = 0; subIndex < sortedArray.length; subIndex++) {
  65. submesh = sortedArray[subIndex];
  66. submesh.render();
  67. }
  68. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  69. }
  70. return true;
  71. };
  72. RenderingGroup.prototype.prepare = function () {
  73. this._opaqueSubMeshes.reset();
  74. this._transparentSubMeshes.reset();
  75. this._alphaTestSubMeshes.reset();
  76. };
  77. RenderingGroup.prototype.dispatch = function (subMesh) {
  78. var material = subMesh.getMaterial();
  79. var mesh = subMesh.getMesh();
  80. if (material.needAlphaBlending() || mesh.visibility < 1.0 || mesh.hasVertexAlpha) {
  81. this._transparentSubMeshes.push(subMesh);
  82. } else if (material.needAlphaTesting()) {
  83. this._alphaTestSubMeshes.push(subMesh);
  84. } else {
  85. this._opaqueSubMeshes.push(subMesh); // Opaque
  86. }
  87. };
  88. return RenderingGroup;
  89. })();
  90. BABYLON.RenderingGroup = RenderingGroup;
  91. })(BABYLON || (BABYLON = {}));
  92. //# sourceMappingURL=babylon.renderingGroup.js.map