Skip to content

Instantly share code, notes, and snippets.

@chrisallick
Created July 3, 2012 22:08
Show Gist options
  • Save chrisallick/3043723 to your computer and use it in GitHub Desktop.
Save chrisallick/3043723 to your computer and use it in GitHub Desktop.
function render() {
rotation.x += ( target.x - rotation.x ) * 0.05;
rotation.y += ( target.y - rotation.y ) * 0.05;
distance += ( distanceTarget - distance ) * 0.05;
camera.position.x = distance * Math.sin( rotation.x ) * Math.cos( rotation.y );
camera.position.y = distance * Math.sin( rotation.y );
camera.position.z = distance * Math.cos( rotation.x ) * Math.cos( rotation.y );
if( cubes.length > 0 ) {
// this is amazing...
//console.log( camera.position.distanceTo( cubes[0] ) - distance + 200 );
var hits = false;
for( var i = 0, len = cubes.length; i < len; i++ ) {
if( (camera.position.distanceTo( cubes[i] ) - distance + 200 ) < 4 ) {
hits = true;
if( !videos[i].displayed ) {
videos[i].displayed = true;
$("#video_wrapper").addClass("gotime").html("<img src='http://img.youtube.com/vi/"+videos[i].vid+"/0.jpg' />" );
current_vid = videos[i].vid;
}
} else {
videos[i].displayed = false;
}
}
if( !hits ) {
$("#video_wrapper").removeClass("gotime").html("");
}
}
renderer.clear();
renderer.render( scene, camera );
renderer.render( sceneAtmosphere, camera );
}
function checkIntersection(evt) {
if( bMoved ) {
} else {
// get the mouse position and create
// a projector for the ray
var mouseX = evt.offsetX || evt.clientX,
mouseY = evt.offsetY || evt.clientY,
projector = new THREE.Projector();
// set up a new vector in the correct
// coordinates system
var vector = new THREE.Vector3(
(mouseX / window.innerWidth) * 2 - 1,
-(mouseY / window.innerHeight) * 2 + 1,
0.5);
// now "unproject" the point on the screen
// back into the the scene itself. This gives
// us a ray direction
projector.unprojectVector(vector, camera);
// create a ray from our current camera position
// with that ray direction and see if it hits the sphere
var ray = new THREE.Ray(camera.position, vector.subSelf(camera.position).normalize()),
intersects = ray.intersectObject(mesh);
if( intersects.length > 0 ) {
var temproid = intersects[0].face.centroid;
cube = new THREE.Mesh(
new Cube( 10, 10, 30 ),
new THREE.MeshLambertMaterial( { color: 0xFF0000 } )
);
cube.position.x = temproid.x;
cube.position.y = temproid.y;
cube.position.z = temproid.z;
cube.lookAt( mesh.position );
cube.updateMatrix();
cubes.push( cube.position );
scene.addObject( cube );
}
}
bMoved = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment