Skip to content

Instantly share code, notes, and snippets.

@ktnyt
Last active May 7, 2016 19:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktnyt/04c4622e7d29d280fcf0 to your computer and use it in GitHub Desktop.
Save ktnyt/04c4622e7d29d280fcf0 to your computer and use it in GitHub Desktop.
A three.js camera for capturing a view through a static window.
THREE.WindowCamera = function(width, height, far) {
THREE.Camera.call(this);
this.type = 'WindowCamera';
this.width = width;
this.height = height;
this.far = far !== undefined ? far : 2000;
this.updateProjectionMatrix();
};
THREE.WindowCamera.prototype = Object.create(THREE.Camera.prototype);
THREE.WindowCamera.prototype.constructor = THREE.WindowCamera;
THREE.WindowCamera.prototype.updateProjectionMatrix = function() {
var xo = this.position.x;
var yo = this.position.y;
var zo = this.position.z !== 0 ? this.position.z : 0.1;
var f = this.far;
var w = this.width;
var h = this.height;
var cx = 4 / w;
var cy = 4 / h;
this.projectionMatrix.set(
cx * zo, 0, cx * -xo, 0,
0, cy * zo, cy * -yo, 0,
0, 0, 1 / f, -zo,
0, 0, -1, zo
);
};
THREE.WindowCamera.prototype.copy = function(source) {
THREE.Camera.prototype.copy.call(this, source);
this.width = source.width
this.height = source.height;
this.far = source.far;
return this;
};
THREE.WindowCamera.prototype.toJSON = function(meta) {
var data = THREE.Object3D.prototype.toJSON.call(this, meta);
data.object.width = this.width
data.object.height = this.height;
data.object.far = this.far;
return data;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment