babylon.renderingGroup.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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) {
  12. if (customRenderFunction) {
  13. customRenderFunction(this._opaqueSubMeshes, this._alphaTestSubMeshes, this._transparentSubMeshes);
  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. // Transparent
  35. if (this._transparentSubMeshes.length) {
  36. for (subIndex = 0; subIndex < this._transparentSubMeshes.length; subIndex++) {
  37. submesh = this._transparentSubMeshes.data[subIndex];
  38. submesh._alphaIndex = submesh.getMesh().alphaIndex;
  39. submesh._distanceToCamera = submesh.getBoundingInfo().boundingSphere.centerWorld.subtract(this._scene.activeCamera.position).length();
  40. }
  41. var sortedArray = this._transparentSubMeshes.data.slice(0, this._transparentSubMeshes.length);
  42. sortedArray.sort(function (a, b) {
  43. // Alpha index first
  44. if (a._alphaIndex > b._alphaIndex) {
  45. return 1;
  46. }
  47. if (a._alphaIndex < b._alphaIndex) {
  48. return -1;
  49. }
  50. // Then distance to camera
  51. if (a._distanceToCamera < b._distanceToCamera) {
  52. return 1;
  53. }
  54. if (a._distanceToCamera > b._distanceToCamera) {
  55. return -1;
  56. }
  57. return 0;
  58. });
  59. // Rendering
  60. engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
  61. for (subIndex = 0; subIndex < sortedArray.length; subIndex++) {
  62. submesh = sortedArray[subIndex];
  63. submesh.render();
  64. }
  65. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  66. }
  67. return true;
  68. };
  69. RenderingGroup.prototype.prepare = function () {
  70. this._opaqueSubMeshes.reset();
  71. this._transparentSubMeshes.reset();
  72. this._alphaTestSubMeshes.reset();
  73. };
  74. RenderingGroup.prototype.dispatch = function (subMesh) {
  75. var material = subMesh.getMaterial();
  76. var mesh = subMesh.getMesh();
  77. if (material.needAlphaBlending() || mesh.visibility < 1.0 || mesh.hasVertexAlpha) {
  78. this._transparentSubMeshes.push(subMesh);
  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