Skip to content

Instantly share code, notes, and snippets.

@moxuse
Created June 3, 2014 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moxuse/460d6f97895837a04c6b to your computer and use it in GitHub Desktop.
Save moxuse/460d6f97895837a04c6b to your computer and use it in GitHub Desktop.
THREE.SpringCamera.js
/*
SpringCamera.js
*/
THREE.SpringCamera = function (_width, _height) {
/*
constructor
*/
var VIEW_ANGLE = 45, ASPECT = _width / _height, NEAR = 0.01, FAR = 10000000;
this.nextCamPosX = 0;
this.nextCamPosY = 0;
this.nextCamPosZ = 0;
this.curCamPosX = 0;
this.curCamPosY = 0;
this.curCamPosZ = 0;
this.speedX = 0;
this.speedY = 0;
this.speedZ = 0;
this.friction = 0.4;
this.spring = 0.5;
this.position.z = 500;
this.position.x = 0;
this.position.y = 0;
this.lookAtPosition = new THREE.Vector3(0, 0, 0);
this.randPosition = new THREE.Vector3(0, 0, 0);
this.init();
THREE.PerspectiveCamera.call(this, VIEW_ANGLE, ASPECT, NEAR, FAR);
};
THREE.SpringCamera.prototype = new THREE.PerspectiveCamera();
THREE.SpringCamera.prototype.init = function () {
this.randPosition.x = Math.random() * 12.5 - 6.0;
this.randPosition.y = Math.random() * 6.5 - 3.0;
this.randPosition.z = Math.random() * 0.5 - 0.0;
};
THREE.SpringCamera.prototype.update = function (_target) {
this.position = this.animateCamera();
switch (_target.mode) {
case 'FAR':
this.lookAtPosition = new THREE.Vector3(this.randPosition.x, this.randPosition.y,0);
break;
case 'NEAR':
var newPos = new THREE.Vector3(_target.position.x + this.randPosition.x, _target.position.y - this.randPosition.y, _target.position.z + this.randPosition.z);
this.lookAtPosition = newPos;
//console.log(_target.name);
break;
case 'NEAR_CENTER':
this.lookAtPosition = _target.position;
//console.log(_target.name);
break;
default:
break;
};
this.lookAt(this.lookAtPosition);
};
THREE.SpringCamera.prototype.next = function (_type, _coordinate) {
switch (_type) {
case 'slow':
this.nextCamPosX = (Math.random() * 200 - 100);
this.nextCamPosY = (Math.random() * 200 - 250);
this.nextCamPosZ = Math.random() * 200 + 250;
this.friction = Math.random() * 0.04 + 0.02;
this.spring = Math.random() * 0.1 + 0.08;
break;
case 'fast':
this.nextCamPosX = _coordinate.x;
this.nextCamPosY = _coordinate.y + (Math.random() * 20 - 20);
this.nextCamPosZ = Math.random() * 20 + 10;
this.friction = Math.random() * 0.05 + 0.1;
this.spring = 0.5+ Math.random() * 0.1 + 0.1;
break;
case 'fast-far':
this.nextCamPosX = (Math.random() * 200 - 100);
this.nextCamPosY = (Math.random() * 200 - 250);
this.nextCamPosZ = Math.random() * 200 + 300;
this.friction = Math.random() * 0.02 + 0.07;
this.spring = 0.5+ Math.random() * 0.1 + 0.05;
break;
default:
break;
};
};
THREE.SpringCamera.prototype.animateCamera = function () {
var ax, ay, az;
ax = (this.nextCamPosX - this.curCamPosX) * this.spring;
this.speedX += ax;
this.speedX *= this.friction;
this.curCamPosX += this.speedX;
ay = (this.nextCamPosY - this.curCamPosY) * this.spring;
this.speedY += ay;
this.speedY *= this.friction;
this.curCamPosY += this.speedY;
az = (this.nextCamPosZ - this.curCamPosZ) * this.spring;
this.speedZ += az;
this.speedZ *= this.friction;
this.curCamPosZ += this.speedZ;
return new THREE.Vector3( this.curCamPosX, this.curCamPosY, this.curCamPosZ );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment