Created
January 2, 2013 22:15
-
-
Save anissen/4438785 to your computer and use it in GitHub Desktop.
Testing out cu.bes by omitting index.html and setup.js
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 projector; | |
var objects = [], plane; | |
var mouse = new THREE.Vector2(), | |
offset = new THREE.Vector3(), | |
INTERSECTED, SELECTED; | |
initBoxes(); | |
function initBoxes() { | |
var geometry = new THREE.CubeGeometry( 40, 40, 40 ); | |
for ( var i = 0; i < 200; i ++ ) { | |
var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) ); | |
object.material.ambient = object.material.color; | |
object.position.x = Math.random() * 1000 - 500; | |
object.position.y = Math.random() * 600 - 300; | |
object.position.z = Math.random() * 800 - 400; | |
object.rotation.x = Math.random() * 2 * Math.PI; | |
object.rotation.y = Math.random() * 2 * Math.PI; | |
object.rotation.z = Math.random() * 2 * Math.PI; | |
object.scale.x = Math.random() * 2 + 1; | |
object.scale.y = Math.random() * 2 + 1; | |
object.scale.z = Math.random() * 2 + 1; | |
object.castShadow = true; | |
object.receiveShadow = true; | |
scene.add( object ); | |
objects.push( object ); | |
} | |
plane = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0x000000, opacity: 0.25, transparent: true, wireframe: true } ) ); | |
plane.visible = false; | |
scene.add( plane ); | |
projector = new THREE.Projector(); | |
renderer.domElement.addEventListener( 'mousemove', onDocumentMouseMove, false ); | |
renderer.domElement.addEventListener( 'mousedown', onDocumentMouseDown, false ); | |
renderer.domElement.addEventListener( 'mouseup', onDocumentMouseUp, false ); | |
} | |
function onDocumentMouseMove( event ) { | |
event.preventDefault(); | |
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1; | |
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1; | |
// | |
var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 ); | |
projector.unprojectVector( vector, camera ); | |
var raycaster = new THREE.Raycaster( camera.position, vector.subSelf( camera.position ).normalize() ); | |
if ( SELECTED ) { | |
var intersects = raycaster.intersectObject( plane ); | |
SELECTED.position.copy( intersects[ 0 ].point.subSelf( offset ) ); | |
return; | |
} | |
var intersects = raycaster.intersectObjects( objects ); | |
if ( intersects.length > 0 ) { | |
if ( INTERSECTED != intersects[ 0 ].object ) { | |
if ( INTERSECTED ) INTERSECTED.material.color.setHex( INTERSECTED.currentHex ); | |
INTERSECTED = intersects[ 0 ].object; | |
INTERSECTED.currentHex = INTERSECTED.material.color.getHex(); | |
plane.position.copy( INTERSECTED.position ); | |
plane.lookAt( camera.position ); | |
} | |
container.style.cursor = 'pointer'; | |
} else { | |
if ( INTERSECTED ) INTERSECTED.material.color.setHex( INTERSECTED.currentHex ); | |
INTERSECTED = null; | |
container.style.cursor = 'auto'; | |
} | |
} | |
function onDocumentMouseDown( event ) { | |
event.preventDefault(); | |
var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 ); | |
projector.unprojectVector( vector, camera ); | |
var raycaster = new THREE.Raycaster( camera.position, vector.subSelf( camera.position ).normalize() ); | |
var intersects = raycaster.intersectObjects( objects ); | |
if ( intersects.length > 0 ) { | |
controls.enabled = false; | |
SELECTED = intersects[ 0 ].object; | |
var intersects = raycaster.intersectObject( plane ); | |
offset.copy( intersects[ 0 ].point ).subSelf( plane.position ); | |
container.style.cursor = 'move'; | |
} | |
} | |
function onDocumentMouseUp( event ) { | |
event.preventDefault(); | |
controls.enabled = true; | |
if ( INTERSECTED ) { | |
plane.position.copy( INTERSECTED.position ); | |
SELECTED = null; | |
} | |
container.style.cursor = 'auto'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment