babylon.utilityLayerRenderer.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. module BABYLON {
  2. /**
  3. * Renders a layer on top of an existing scene
  4. */
  5. export class UtilityLayerRenderer implements IDisposable {
  6. /**
  7. * The scene that is rendered on top of the original scene
  8. */
  9. public utilityLayerScene:Scene;
  10. /**
  11. * If the utility layer should automatically be rendered on top of existing scene
  12. */
  13. public shouldRender:boolean = true;
  14. private _afterRenderObserver:Nullable<Observer<Scene>>;
  15. private _sceneDisposeObserver:Nullable<Observer<Scene>>;
  16. /**
  17. * Instantiates a UtilityLayerRenderer
  18. * @param originalScene the original scene that will be rendered on top of
  19. */
  20. constructor(/** the original scene that will be rendered on top of */ public originalScene:Scene){
  21. // Create scene which will be rendered in the foreground and remove it from being referenced by engine to avoid interfering with existing app
  22. this.utilityLayerScene = new BABYLON.Scene(originalScene.getEngine());
  23. originalScene.getEngine().scenes.pop();
  24. // Render directly on top of existing scene without clearing
  25. this.utilityLayerScene.autoClear = false;
  26. this._afterRenderObserver = this.originalScene.onAfterRenderObservable.add(()=>{
  27. if(this.shouldRender){
  28. this.render();
  29. }
  30. });
  31. this._sceneDisposeObserver = this.originalScene.onDisposeObservable.add(()=>{
  32. this.dispose();
  33. })
  34. }
  35. /**
  36. * Renders the utility layers scene on top of the original scene
  37. */
  38. public render(){
  39. this._updateCamera();
  40. this.utilityLayerScene.render();
  41. }
  42. /**
  43. * Disposes of the renderer
  44. */
  45. public dispose(){
  46. if(this._afterRenderObserver){
  47. this.originalScene.onAfterRenderObservable.remove(this._afterRenderObserver);
  48. }
  49. if(this._sceneDisposeObserver){
  50. this.originalScene.onDisposeObservable.remove(this._sceneDisposeObserver);
  51. }
  52. this.utilityLayerScene.dispose();
  53. }
  54. private _updateCamera(){
  55. this.utilityLayerScene.activeCamera = this.originalScene.activeCamera;
  56. }
  57. }
  58. }