Skip to content

Commit

Permalink
Merge branch 'master' into jdarpinian
Browse files Browse the repository at this point in the history
  • Loading branch information
jdarpinian committed Apr 9, 2013
2 parents 33c25cf + 5eff1a4 commit b43ec21
Show file tree
Hide file tree
Showing 7 changed files with 584 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "third_party/gyp"]
path = third_party/gyp
url = https://github.com/benvanik/gyp.git
[submodule "third_party/oculus-sdk"]
path = third_party/oculus-sdk
url = https://github.com/benvanik/oculus-sdk.git
165 changes: 165 additions & 0 deletions examples/js/controls/OculusRiftControls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
* Based on THREE.PointerLockControls by mrdoob.
* @author benvanik
*/

THREE.OculusRiftControls = function ( camera ) {

var scope = this;

var pitchObject = new THREE.Object3D();
pitchObject.add( camera );

var yawObject = new THREE.Object3D();
yawObject.position.y = 10;
yawObject.add( pitchObject );

var moveForward = false;
var moveBackward = false;
var moveLeft = false;
var moveRight = false;

var isOnObject = false;
var canJump = false;

var velocity = new THREE.Vector3();

var PI_2 = Math.PI / 2;

this.moveSpeed = 0.12 / 2;
this.jumpSpeed = 3;

var onMouseMove = function ( event ) {

if ( scope.enabled === false ) return;

var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;

yawObject.rotation.y -= movementX * 0.002;
pitchObject.rotation.x -= movementY * 0.002;

pitchObject.rotation.x = Math.max( - PI_2, Math.min( PI_2, pitchObject.rotation.x ) );

};

var onKeyDown = function ( event ) {

switch ( event.keyCode ) {

case 38: // up
case 87: // w
moveForward = true;
break;

case 37: // left
case 65: // a
moveLeft = true; break;

case 40: // down
case 83: // s
moveBackward = true;
break;

case 39: // right
case 68: // d
moveRight = true;
break;

case 32: // space
if ( canJump === true ) velocity.y += this.jumpSpeed;
canJump = false;
break;

}

}.bind(this);

var onKeyUp = function ( event ) {

switch( event.keyCode ) {

case 38: // up
case 87: // w
moveForward = false;
break;

case 37: // left
case 65: // a
moveLeft = false;
break;

case 40: // down
case 83: // a
moveBackward = false;
break;

case 39: // right
case 68: // d
moveRight = false;
break;

}

};

document.addEventListener( 'mousemove', onMouseMove, false );
document.addEventListener( 'keydown', onKeyDown, false );
document.addEventListener( 'keyup', onKeyUp, false );

this.enabled = false;

this.getObject = function () {

return yawObject;

};

this.isOnObject = function ( boolean ) {

isOnObject = boolean;
canJump = boolean;

};

this.update = function ( delta, vrstate ) {

//if ( scope.enabled === false ) return;

// TODO: use vrstate to compute look

delta *= 0.1;

velocity.x += ( - velocity.x ) * 0.08 * delta;
velocity.z += ( - velocity.z ) * 0.08 * delta;

velocity.y -= 0.10 * delta;

if ( moveForward ) velocity.z -= this.moveSpeed * delta;
if ( moveBackward ) velocity.z += this.moveSpeed * delta;

if ( moveLeft ) velocity.x -= this.moveSpeed * delta;
if ( moveRight ) velocity.x += this.moveSpeed * delta;

if ( isOnObject === true ) {

velocity.y = Math.max( 0, velocity.y );

}

yawObject.translateX( velocity.x );
yawObject.translateY( velocity.y );
yawObject.translateZ( velocity.z );

if ( yawObject.position.y < 10 ) {

velocity.y = 0;
yawObject.position.y = 10;

canJump = true;

}

};

};
124 changes: 124 additions & 0 deletions examples/js/effects/OculusRiftEffect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* @author troffmo5 / http://github.com/troffmo5
*
* Effect to render the scene in stereo 3d side by side with lens distortion.
* It is written to be used with the Oculus Rift (http://www.oculusvr.com/) but
* it works also with other HMD using the same technology
*/

THREE.OculusRiftEffect = function ( renderer ) {

// Configuration
this.separation = 3;
this.distortion = 0.1;
this.aspectFactor = 1;
this.fov = 110;

var displayWidth = 1280;
var displayHeight = 800;

// initialization
var _width = displayWidth / 2;
var _height = displayHeight;

var _pCamera = new THREE.PerspectiveCamera();
_pCamera.matrixAutoUpdate = false;
_pCamera.target = new THREE.Vector3();

var _scene = new THREE.Scene();

var _oCamera = new THREE.OrthographicCamera( -1, 1, 1, -1, 1, 1000 );
_oCamera.position.z = 1;
_scene.add( _oCamera );

// pre-render hooks
this.preLeftRender = function() {};
this.preRightRender = function() {};

renderer.autoClear = false;
renderer.setSize( displayWidth, displayHeight );

camera.aspect = displayWidth / displayHeight;
camera.updateProjectionMatrix();

var _params = { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBAFormat };
var _renderTarget = new THREE.WebGLRenderTarget( displayWidth, displayHeight, _params );
var _material = new THREE.ShaderMaterial( {
uniforms: {
"tex": { type: "t", value: _renderTarget },
"c": { type: "f", value: this.distortion }
},
vertexShader: [
"varying vec2 vUv;",
"void main() {",
" vUv = uv;",
" gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),

// Formula used from the paper: "Applying and removing lens distortion in post production"
// by Gergely Vass , Tamás Perlaki
// http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.136.3745
fragmentShader: [
"uniform float c;",
"uniform sampler2D tex;",
"varying vec2 vUv;",
"void main()",
"{",
" vec2 uv = vUv;",
" vec2 vector = uv * 2.0 - 1.0;",
" float factor = 1.0/(1.0+c);",
" float vectorLen = length(vector);",
" vec2 direction = vector / vectorLen;",
" float newLen = vectorLen + c * pow(vectorLen,3.0);",
" vec2 newVector = direction * newLen * factor;",
" newVector = (newVector + 1.0) / 2.0;",
" if (newVector.x < 0.0 || newVector.x > 1.0 || newVector.y < 0.0 || newVector.y > 1.0)",
" gl_FragColor = vec4(0.0,0.0,0.0,1.0);",
" else",
" gl_FragColor = texture2D(tex, newVector);",
"}"
].join("\n")
} );
var mesh = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), _material );
_scene.add( mesh );

this.render = function ( scene, camera ) {
renderer.clear();
_material.uniforms['c'].value = this.distortion;

// camera parameters
if (camera.matrixAutoUpdate) camera.updateMatrix();
_pCamera.fov = this.fov ? this.fov : camera.fov;
_pCamera.aspect = camera.aspect / (2*this.aspectFactor);
_pCamera.near = camera.near;
_pCamera.far = camera.far;
_pCamera.updateProjectionMatrix();

// Render left
this.preLeftRender();

var offset = new THREE.Vector3(-this.separation,0,0);
_pCamera.matrix.copy(camera.matrixWorld);
_pCamera.matrix.translate(offset);
_pCamera.matrixWorldNeedsUpdate = true;

renderer.setViewport( 0, 0, _width, _height );
renderer.render( scene, _pCamera, _renderTarget, true );
renderer.render( _scene, _oCamera );

// Render right
this.preRightRender();

offset.set(this.separation,0,0);
_pCamera.matrix.copy(camera.matrixWorld);
_pCamera.matrix.translate(offset);
_pCamera.matrixWorldNeedsUpdate = true;

renderer.setViewport( _width, 0, _width, _height );
renderer.render( scene, _pCamera, _renderTarget, true );

renderer.render( _scene, _oCamera );
};

};
File renamed without changes.
Loading

0 comments on commit b43ec21

Please sign in to comment.