babylon.renderingGroup.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. module BABYLON {
  2. export class RenderingGroup {
  3. private _scene: Scene
  4. private _opaqueSubMeshes = new BABYLON.SmartArray<SubMesh>(256);
  5. private _transparentSubMeshes = new BABYLON.SmartArray<SubMesh>(256);
  6. private _alphaTestSubMeshes = new BABYLON.SmartArray<SubMesh>(256);
  7. private _activeVertices: number;
  8. constructor(public index: number, scene: Scene) {
  9. this._scene = scene;
  10. }
  11. public render(customRenderFunction: (opaqueSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>) => void): boolean {
  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: number;
  22. var submesh: 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((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. public prepare(): void {
  71. this._opaqueSubMeshes.reset();
  72. this._transparentSubMeshes.reset();
  73. this._alphaTestSubMeshes.reset();
  74. }
  75. public dispatch(subMesh: SubMesh): void {
  76. var material = subMesh.getMaterial();
  77. var mesh = subMesh.getMesh();
  78. if (material.needAlphaBlending() || mesh.visibility < 1.0 || mesh.hasVertexAlpha) { // Transparent
  79. this._transparentSubMeshes.push(subMesh);
  80. } else if (material.needAlphaTesting()) { // Alpha test
  81. this._alphaTestSubMeshes.push(subMesh);
  82. } else {
  83. this._opaqueSubMeshes.push(subMesh); // Opaque
  84. }
  85. }
  86. }
  87. }