babylon.utilityLayerRenderer.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 _afterRenderObservable:Nullable<Observer<Scene>>;
  15. private _sceneDisposeObservable: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.clearColor = new BABYLON.Color4(0,0,0,0);
  26. this.utilityLayerScene.autoClear = false;
  27. this._afterRenderObservable = this.originalScene.onAfterRenderObservable.add(()=>{
  28. if(this.shouldRender){
  29. this.render();
  30. }
  31. });
  32. this._sceneDisposeObservable = this.originalScene.onDisposeObservable.add(()=>{
  33. this.dispose();
  34. })
  35. }
  36. /**
  37. * Renders the utility layers scene on top of the original scene
  38. */
  39. public render(){
  40. this._updateCamera();
  41. this.utilityLayerScene.render();
  42. }
  43. /**
  44. * Disposes of the renderer
  45. */
  46. public dispose(){
  47. if(this._afterRenderObservable){
  48. this.originalScene.onAfterRenderObservable.remove(this._afterRenderObservable);
  49. }
  50. if(this._sceneDisposeObservable){
  51. this.originalScene.onDisposeObservable.remove(this._sceneDisposeObservable);
  52. }
  53. this.utilityLayerScene.dispose();
  54. }
  55. private _updateCamera(){
  56. this.utilityLayerScene.activeCamera=this.originalScene.activeCamera;
  57. }
  58. }
  59. }