class CameraViewport extends BABYLON.GUI.Control { private _scene: BABYLON.Scene; private _engine: BABYLON.Engine; private _observer: BABYLON.Observer; private tmpActiveCameras: BABYLON.Camera[]; private tmpViewport = new BABYLON.Viewport(0, 0, 1, 1); private _isRendering = false; constructor(name: string, private _camera: BABYLON.Camera, performanceMode = true) { super(name); this._scene = _camera.getScene(); this._engine = this._scene.getEngine(); this.tmpActiveCameras = [_camera]; this.onBeforeDrawObservable.addOnce(() => { this._observer = this.host.getScene().onAfterCameraRenderObservable.add(() => { if (!this._isRendering) { this.renderScene(); } }); }); } public _draw(context: CanvasRenderingContext2D) { context.fillStyle = this.color; context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height); } public renderScene() { this._isRendering = true; let width = this._engine.getRenderWidth(); let height = this._engine.getRenderHeight(); let orgViewport = this._camera.viewport; this.tmpViewport.x = this._currentMeasure.left / width; this.tmpViewport.y = (height - (this._currentMeasure.top + this._currentMeasure.height)) / height; this.tmpViewport.width = this._currentMeasure.width / width; this.tmpViewport.height = this._currentMeasure.height / height; this._camera.viewport = this.tmpViewport; let wasAutoClear = this._scene.autoClear; this._scene.autoClear = false; if (this._scene.activeCameras.length > 1 || this._scene === this.host.getScene()) { let orgActiveCameras = this._scene.activeCameras; let orgActiveCamera = this._scene.activeCamera; this._scene.activeCameras = this.tmpActiveCameras; this._scene.activeCamera = this.tmpActiveCameras[0]; this._scene.render(); this._scene.activeCameras = orgActiveCameras; this._scene.activeCamera = orgActiveCamera; } else { this._scene.render(); } this._camera.viewport = orgViewport; this._scene.autoClear = wasAutoClear; this._isRendering = false; } public dispose() { this.host.getScene().onAfterCameraRenderObservable.remove(this._observer); super.dispose(); } } (BABYLON.GUI).CameraViewport = CameraViewport;