From 3d223c1a4a2f0db6ba34eae90085e4533d549eb5 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 22 Mar 2018 15:58:43 -0700 Subject: [PATCH] Match screen size as much as possible --- demo/client/src/3d-demo.ts | 8 +++++-- demo/client/src/view.ts | 47 ++++++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/demo/client/src/3d-demo.ts b/demo/client/src/3d-demo.ts index dc80d17a..ef0d5738 100644 --- a/demo/client/src/3d-demo.ts +++ b/demo/client/src/3d-demo.ts @@ -407,7 +407,11 @@ class ThreeDView extends DemoView implements TextRenderContext { if (this.vrDisplay.isPresenting) { const that = this; - this.resized(); + const eye = this.vrDisplay.getEyeParameters("left"); + + this.vrDisplayHeight = eye.renderHeight; + this.vrDisplayWidth = eye.renderWidth * 2; + this.resizeToFit(true); function vrCallback(): void { if (that.vrDisplay == null || !that.renderer.inVR) { return; @@ -420,7 +424,7 @@ class ThreeDView extends DemoView implements TextRenderContext { this.vrDisplay.requestAnimationFrame(vrCallback); } else { this.renderer.inVR = false; - this.resized(); + this.resizeToFit(true); } }); } diff --git a/demo/client/src/view.ts b/demo/client/src/view.ts index 9954acb7..60969ae8 100644 --- a/demo/client/src/view.ts +++ b/demo/client/src/view.ts @@ -55,6 +55,9 @@ export abstract class PathfinderView { suppressAutomaticRedraw: boolean; + vrDisplayWidth: number | null; + vrDisplayHeight: number | null; + protected abstract get camera(): Camera; private dirty: boolean; @@ -67,6 +70,9 @@ export abstract class PathfinderView { this.suppressAutomaticRedraw = false; this.canvas = unwrapNull(document.getElementById('pf-canvas')) as HTMLCanvasElement; window.addEventListener('resize', () => this.resizeToFit(false), false); + + this.vrDisplayHeight = null; + this.vrDisplayWidth = null; } setDirty(): void { @@ -115,24 +121,37 @@ export abstract class PathfinderView { this.setDirty(); } + protected inVR(): boolean { + return false; + } + protected resizeToFit(initialSize: boolean): void { if (!this.canvas.classList.contains('pf-no-autoresize')) { - const windowWidth = window.innerWidth; - const canvasTop = this.canvas.getBoundingClientRect().top; - const height = window.scrollY + window.innerHeight - canvasTop; - const devicePixelRatio = window.devicePixelRatio; + if (this.inVR()) { + const width = unwrapNull(this.vrDisplayWidth); + const height = unwrapNull(this.vrDisplayHeight); + // these are already in device pixel units, no need to multiply + this.canvas.style.width = width + 'px'; + this.canvas.style.height = height + 'px'; + this.canvas.width = width; + this.canvas.height = height; + } else { + const width = window.innerWidth; + const canvasTop = this.canvas.getBoundingClientRect().top; + const height = window.scrollY + window.innerHeight - canvasTop; + const devicePixelRatio = window.devicePixelRatio; + const canvasSize = new Float32Array([width, height]) as glmatrix.vec2; + glmatrix.vec2.scale(canvasSize, canvasSize, devicePixelRatio); + glmatrix.vec2.round(canvasSize, canvasSize); - const canvasSize = new Float32Array([windowWidth, height]) as glmatrix.vec2; - glmatrix.vec2.scale(canvasSize, canvasSize, devicePixelRatio); - glmatrix.vec2.round(canvasSize, canvasSize); + this.canvas.style.width = width + 'px'; + this.canvas.style.height = height + 'px'; + this.canvas.width = canvasSize[0]; + this.canvas.height = canvasSize[1]; + } - this.canvas.style.width = windowWidth + 'px'; - this.canvas.style.height = height + 'px'; - this.canvas.width = canvasSize[0]; - this.canvas.height = canvasSize[1]; } - this.resized(); } } @@ -278,6 +297,10 @@ export abstract class DemoView extends PathfinderView implements RenderContext { this.renderer.canvasResized(); } + protected inVR(): boolean { + return this.renderer.inVR; + } + protected initContext(): void { // Initialize the OpenGL context. this.gl = expectNotNull(this.canvas.getContext('webgl', { antialias: false, depth: true }),