Created
December 3, 2015 12:36
-
-
Save nickjanssen/666517984aff72eaf798 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var controlsEnabled = false; | |
var prevTime = performance.now(); | |
var velocity = new THREE.Vector3(); | |
var raycaster = new THREE.Raycaster(); | |
var gameObjects = {}; | |
var canMove = true; | |
var player = null; | |
var app = function () { | |
this.renderer = null; | |
this.scene = null; | |
this.camera = null; | |
this.vrControls = null; | |
}; | |
var teleportPlayer = function (target) { | |
var yawObject = player.children[0]; | |
player.position.copy(target.position); | |
}; | |
var howlerGlobal = Howler; | |
var sound = new Howl({ | |
src: ['sound/03 Dark Caverns.mp3'] | |
}); | |
app.prototype.init = function (elemId) { | |
var deferred = $q.defer(); | |
// Get the canvas element from our HTML above | |
var canvas = document.getElementById(elemId); | |
this.scene = new THREE.Scene(); | |
var me = this; | |
this.renderer = new THREE.WebGLRenderer({ | |
canvas: canvas | |
}); | |
this.renderer.setPixelRatio( window.devicePixelRatio ); | |
this.renderer.setClearColor( 0xff0000 ); | |
this.renderer.setSize( window.innerWidth, window.innerHeight ); | |
// this.renderer.autoClear = false; | |
this.renderer.getSize = function () { | |
return { | |
width: window.innerWidth, | |
height: window.innerHeight | |
}; | |
}; | |
this.vrEffect = new THREE.VREffect(this.renderer, function (err) { | |
if (err) { | |
console.error(err); | |
} | |
}); | |
this.vrEffect.setSize( window.innerWidth, window.innerHeight ); | |
this.vrManager = new WebVRManager(this.renderer, this.vrEffect, {hideButton: false}); | |
this.touchInputVector = new THREE.Vector3(); | |
this.colliders = []; | |
this.camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.01, 100000 ); | |
this.vrControls = new THREE.VRControls( this.camera ); | |
var loader = new THREE.ObjectLoader(); | |
loader.setCrossOrigin('Anonymous'); | |
loader.load(assetsUrl + 'models/scene.json', function (obj) { | |
document.addEventListener( 'touchstart', function () { | |
console.log('touchstart'); | |
me.touchInputVector.set(0,0,-1) | |
}); | |
document.addEventListener( 'touchend', function () { | |
console.log('touchend'); | |
me.touchInputVector.set(0,0,0) | |
}); | |
me.scene.add(obj); | |
player = new THREE.Object3D(); | |
player.add(me.camera); | |
me.scene.add( player ); | |
obj.traverse(function (child) { | |
switch(child.userData.type) { | |
case 'BoxCollider': | |
var geometry = new THREE.BoxGeometry(child.userData.size[0]*child.parent.scale.x, child.userData.size[1]*child.parent.scale.y, child.userData.size[2]*child.parent.scale.z); | |
// Bake the rotations as normals in collision reports are not rotated | |
var worldRot = child.parent.getWorldQuaternion(); | |
_.each(geometry.vertices, function(vertex) { | |
vertex.applyQuaternion(worldRot); | |
}); | |
geometry.computeFaceNormals(); | |
var colliderMesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial()); | |
me.colliders.push(colliderMesh); | |
me.scene.add(colliderMesh); | |
colliderMesh.visible = false; | |
colliderMesh.position.copy(child.parent.position.clone().add((new THREE.Vector3()).fromArray(child.userData.center))); | |
break; | |
} | |
if (!gameObjects[child.name]) { | |
gameObjects[child.name] = child; | |
} | |
}); | |
me.animate(); | |
deferred.resolve(); | |
}); | |
window.addEventListener('resize', this.resize.bind(this), false); | |
sound.play(); | |
return deferred.promise; | |
}; | |
app.prototype.animate = function () { | |
requestAnimationFrame(this.animate.bind(this)); | |
this.render(); | |
this.update(); | |
} | |
app.prototype.render = function () { | |
this.vrManager.render( this.scene, this.camera ); | |
}; | |
app.prototype.update = function () { | |
var time = performance.now(); | |
var delta = ( time - prevTime ) / 1000; | |
this.vrControls.update(); | |
var toSound = gameObjects['Music'].position.clone().sub(player.position); | |
var sp = gameObjects['Music'].position; | |
Howler.pos(player.position.x, player.position.y, player.position.z); | |
var ov = new THREE.Vector3(0, 0, -1); | |
ov.applyQuaternion(this.camera.quaternion); | |
Howler.orientation(ov.x, ov.y, ov.z, 0, 1, 0); | |
sound.pos(sp.x, sp.y, sp.z); | |
var orientation = toSound.clone().normalize(); | |
if ( canMove ) { | |
var moveForward = false; | |
var moveBackward = false; | |
var moveLeft = false; | |
var moveRight = false; | |
// keyboard controls | |
if (Keyboard.getKey(KEYS.W) || Keyboard.getKey(KEYS.UP)) { | |
moveForward = true; | |
} | |
if (Keyboard.getKey(KEYS.S) || Keyboard.getKey(KEYS.DOWN)) { | |
moveBackward = true; | |
} | |
if (Keyboard.getKey(KEYS.A) || Keyboard.getKey(KEYS.LEFT)) { | |
moveLeft = true; | |
} | |
if (Keyboard.getKey(KEYS.D) || Keyboard.getKey(KEYS.RIGHT)) { | |
moveRight = true; | |
} | |
var inputVector = new THREE.Vector3(); | |
// react to changes | |
if (moveForward) { | |
inputVector.z -= 1; | |
} | |
if (moveBackward) { | |
inputVector.z += 1; | |
} | |
if (moveLeft) { | |
inputVector.x -= 1; | |
} | |
if (moveRight) { | |
inputVector.x += 1; | |
} | |
inputVector.add(this.touchInputVector); | |
inputVector.applyQuaternion(this.camera.quaternion); | |
var velocity = new THREE.Vector3(); | |
velocity.add(inputVector); | |
velocity.multiplyScalar(2); | |
velocity.multiplyScalar(delta); | |
velocity.y = 0; | |
if (inputVector.lengthSq() > 0) { | |
raycaster.set(player.position, velocity); | |
var intersects = raycaster.intersectObjects( this.colliders ); | |
if ( intersects.length > 0 && intersects[0].distance < 0.5) { | |
var raycastNormal = intersects[0].face.normal; | |
var raycastGroundPosition = intersects[0].point; | |
var distanceInside = 0.5-intersects[0].distance; | |
var add = raycastNormal.clone().multiplyScalar(-velocity.clone().dot(raycastNormal)); | |
velocity.add(add); | |
} | |
} | |
// Running | |
if (Keyboard.getKey(KEYS.SHIFT)) { | |
velocity.multiplyScalar(2); | |
} | |
player.position.add(velocity); | |
prevTime = time; | |
} | |
}; | |
app.prototype.resize = function () { | |
if (this.camera) { | |
this.camera.aspect = window.innerWidth / window.innerHeight; | |
this.camera.updateProjectionMatrix(); | |
this.vrEffect.setSize( window.innerWidth, window.innerHeight ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
THREE is not defined for me...