Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function onDocumentMouseDown( event ) {
- event.preventDefault();
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- document.addEventListener( 'mouseup', onDocumentMouseUp, false );
- document.addEventListener( 'mouseout', onDocumentMouseOut, false );
- mouseXOnMouseDown = event.clientX - windowHalfX;
- targetRotationOnMouseDown = targetRotation;
- // position of mouse + screen calc
- var vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
- projector.unprojectVector( vector, camera );
- // create a ray from the camera thru the vector
- var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
- // array of intersecting object (front to back)
- var intersects = ray.intersectObjects( objects );
- // are we hitting something?
- if ( intersects.length > 0 ) {
- // first intersection should be it..
- //intersects[ 0 ].object.material.color.setHex( Math.random() * 0xffffff | 0x80000000 );
- //intersects[ 0 ].face.material[ 0 ].color.setHex( Math.random() * 0xffffff | 0x80000000 );
- //intersects[ 0 ].face.materialIndex = Math.floor(Math.random() * materials.length);
- //intersects[ 0 ].face.color.setHex( Math.random() * 0xffffff );
- //intersects[ 0 ].object.material.color.setHex( Math.random() * 0xffffff );
- console.log("Intersect[0]: "+intersects[0].object);
- // tell me face index
- console.log("Face index: "+intersects[0].faceIndex);
- // gimme face as THREE.Face3/4 object
- console.log(intersects[0].face);
- // face color
- // the face's indices are labeled with these characters
- var faceIndices = ['a', 'b', 'c', 'd'];
- var face = geometry1.faces[intersects[0].faceIndex];
- // determine if face is a tri or a quad
- var numberOfSides = ( face instanceof THREE.Face3 ) ? 3 : 4;
- // assign color to each vertex of current face
- for( var j = 0; j < numberOfSides; j++ )
- {
- var vertexIndex = face[ faceIndices[ j ] ];
- // initialize color variable
- var color = new THREE.Color( 0xffffff );
- color.setRGB( Math.random(), 0, 0 );
- face.vertexColors[ j ] = color;
- }
- var material1 = new THREE.MeshBasicMaterial( {
- color: Math.random() * 0xffffff,
- shading: THREE.FlatShading,
- vertexColors: THREE.VertexColors
- });
- var directionalLight = new THREE.DirectionalLight(0xEEEEEE);
- directionalLight.position.set(10, 1, 1).normalize();
- scene.add(directionalLight);
- var cube = new THREE.Mesh(new THREE.CubeGeometry(50, 50, 50,1,1,1), material1);
- cube.dynamic = true;
- scene.add(cube);
- // create a particle on hit position
- var particle = new THREE.Particle( particleMaterial );
- //var particle = new THREE.Particle(new THREE.MeshLambertMaterial( { color: 0x2D303D, wireframe: false, shading: THREE.FlatShading } ));
- particle.position = intersects[ 0 ].point;
- particle.scale.x = particle.scale.y = 8;
- scene.add( particle );
- }
- // Parse all the faces
- /*for ( var i in intersects ) {
- intersects[ i ].face.materialIndex = Math.floor(Math.random() * materials.length);
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement