babylon.renderingGroup.js 4.0 KB

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