Skip to content
Snippets Groups Projects
Commit 147897cc authored by Alexander Rose's avatar Alexander Rose
Browse files

viewOffset support for orthographic camera

parent 6ecfd41c
No related branches found
No related tags found
No related merge requests found
...@@ -233,25 +233,36 @@ namespace Camera { ...@@ -233,25 +233,36 @@ namespace Camera {
const _center = Vec3.zero(); const _center = Vec3.zero();
function updateOrtho(camera: Camera) { function updateOrtho(camera: Camera) {
const { viewport, state: { zoom, near, far } } = camera; const { viewport, state: { zoom, near, far }, viewOffset } = camera
const fullLeft = (viewport.width - viewport.x) / -2 const fullLeft = -(viewport.width - viewport.x) / 2
const fullRight = (viewport.width - viewport.x) / 2 const fullRight = (viewport.width - viewport.x) / 2
const fullTop = (viewport.height - viewport.y) / 2 const fullTop = (viewport.height - viewport.y) / 2
const fullBottom = (viewport.height - viewport.y) / -2 const fullBottom = -(viewport.height - viewport.y) / 2
const dx = (fullRight - fullLeft) / (2 * zoom) const dx = (fullRight - fullLeft) / (2 * zoom)
const dy = (fullTop - fullBottom) / (2 * zoom) const dy = (fullTop - fullBottom) / (2 * zoom)
const cx = (fullRight + fullLeft) / 2 const cx = (fullRight + fullLeft) / 2
const cy = (fullTop + fullBottom) / 2 const cy = (fullTop + fullBottom) / 2
const left = cx - dx let left = cx - dx
const right = cx + dx let right = cx + dx
const top = cy + dy let top = cy + dy
const bottom = cy - dy let bottom = cy - dy
if (viewOffset && viewOffset.enabled) {
const zoomW = zoom / (viewOffset.width / viewOffset.fullWidth)
const zoomH = zoom / (viewOffset.height / viewOffset.fullHeight)
const scaleW = (fullRight - fullLeft) / viewOffset.width
const scaleH = (fullTop - fullBottom) / viewOffset.height
left += scaleW * (viewOffset.offsetX / zoomW)
right = left + scaleW * (viewOffset.width / zoomW)
top -= scaleH * (viewOffset.offsetY / zoomH)
bottom = top - scaleH * (viewOffset.height / zoomH)
}
// build projection matrix // build projection matrix
Mat4.ortho(camera.projection, left, right, bottom, top, Math.abs(near), Math.abs(far)) Mat4.ortho(camera.projection, left, right, top, bottom, near, far)
// build view matrix // build view matrix
Vec3.add(_center, camera.position, camera.direction) Vec3.add(_center, camera.position, camera.direction)
...@@ -261,7 +272,7 @@ function updateOrtho(camera: Camera) { ...@@ -261,7 +272,7 @@ function updateOrtho(camera: Camera) {
function updatePers(camera: Camera) { function updatePers(camera: Camera) {
const aspect = camera.viewport.width / camera.viewport.height const aspect = camera.viewport.width / camera.viewport.height
const { state: { fov, near, far }, viewOffset } = camera; const { state: { fov, near, far }, viewOffset } = camera
let top = near * Math.tan(0.5 * fov) let top = near * Math.tan(0.5 * fov)
let height = 2 * top let height = 2 * top
......
...@@ -778,33 +778,7 @@ namespace Mat4 { ...@@ -778,33 +778,7 @@ namespace Mat4 {
} }
/** /**
* Generates a frustum matrix with the given bounds * Generates a perspective projection (frustum) matrix with the given bounds
*/
export function frustum(out: Mat4, left: number, right: number, bottom: number, top: number, near: number, far: number) {
const rl = 1 / (right - left);
const tb = 1 / (top - bottom);
const nf = 1 / (near - far);
out[0] = (near * 2) * rl;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = (near * 2) * tb;
out[6] = 0;
out[7] = 0;
out[8] = (right + left) * rl;
out[9] = (top + bottom) * tb;
out[10] = (far + near) * nf;
out[11] = -1;
out[12] = 0;
out[13] = 0;
out[14] = (far * near * 2) * nf;
out[15] = 0;
return out;
}
/**
* Generates a perspective projection matrix with the given bounds
*/ */
export function perspective(out: Mat4, left: number, right: number, top: number, bottom: number, near: number, far: number) { export function perspective(out: Mat4, left: number, right: number, top: number, bottom: number, near: number, far: number) {
const x = 2 * near / (right - left); const x = 2 * near / (right - left);
...@@ -812,8 +786,8 @@ namespace Mat4 { ...@@ -812,8 +786,8 @@ namespace Mat4 {
const a = (right + left) / (right - left); const a = (right + left) / (right - left);
const b = (top + bottom) / (top - bottom); const b = (top + bottom) / (top - bottom);
const c = - (far + near) / (far - near); const c = -(far + near) / (far - near);
const d = - 2 * far * near / (far - near); const d = -2 * far * near / (far - near);
out[0] = x; out[0] = x;
out[1] = 0; out[1] = 0;
...@@ -827,17 +801,17 @@ namespace Mat4 { ...@@ -827,17 +801,17 @@ namespace Mat4 {
out[9] = b; out[9] = b;
out[10] = c; out[10] = c;
out[11] = -1; out[11] = -1;
out[ 12 ] = 0; out[12] = 0;
out[ 13 ] = 0; out[13] = 0;
out[ 14 ] = d; out[14] = d;
out[ 15 ] = 0; out[15] = 0;
return out; return out;
} }
/** /**
* Generates a orthogonal projection matrix with the given bounds * Generates a orthogonal projection matrix with the given bounds
*/ */
export function ortho(out: Mat4, left: number, right: number, bottom: number, top: number, near: number, far: number) { export function ortho(out: Mat4, left: number, right: number, top: number, bottom: number, near: number, far: number) {
const w = 1.0 / (right - left); const w = 1.0 / (right - left);
const h = 1.0 / (top - bottom); const h = 1.0 / (top - bottom);
const p = 1.0 / (far - near); const p = 1.0 / (far - near);
...@@ -846,22 +820,22 @@ namespace Mat4 { ...@@ -846,22 +820,22 @@ namespace Mat4 {
const y = (top + bottom) * h; const y = (top + bottom) * h;
const z = (far + near) * p; const z = (far + near) * p;
out[ 0 ] = 2 * w; out[0] = 2 * w;
out[ 1 ] = 0; out[1] = 0;
out[ 2 ] = 0; out[2] = 0;
out[ 3 ] = 0; out[3] = 0;
out[ 4 ] = 0; out[4] = 0;
out[ 5 ] = 2 * h; out[5] = 2 * h;
out[ 6 ] = 0; out[6] = 0;
out[ 7 ] = 0; out[7] = 0;
out[ 8 ] = 0; out[8] = 0;
out[ 9 ] = 0; out[9] = 0;
out[ 10 ] = - 2 * p; out[10] = -2 * p;
out[ 11 ] = 0; out[11] = 0;
out[ 12 ] = - x; out[12] = -x;
out[ 13 ] = - y; out[13] = -y;
out[ 14 ] = - z; out[14] = -z;
out[ 15 ] = 1; out[15] = 1;
return out; return out;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment