Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. function onDocumentMouseDown( event ) {
  2.  
  3. event.preventDefault();
  4.  
  5. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  6. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  7. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  8.  
  9. mouseXOnMouseDown = event.clientX - windowHalfX;
  10. targetRotationOnMouseDown = targetRotation;
  11.  
  12. // position of mouse + screen calc
  13. var vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
  14. projector.unprojectVector( vector, camera );
  15.  
  16. // create a ray from the camera thru the vector
  17. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  18.  
  19. // array of intersecting object (front to back)
  20. var intersects = ray.intersectObjects( objects );
  21.  
  22. // are we hitting something?
  23. if ( intersects.length > 0 ) {
  24.  
  25. // first intersection should be it..
  26. //intersects[ 0 ].object.material.color.setHex( Math.random() * 0xffffff | 0x80000000 );
  27. //intersects[ 0 ].face.material[ 0 ].color.setHex( Math.random() * 0xffffff | 0x80000000 );
  28. //intersects[ 0 ].face.materialIndex = Math.floor(Math.random() * materials.length);
  29. //intersects[ 0 ].face.color.setHex( Math.random() * 0xffffff );
  30. //intersects[ 0 ].object.material.color.setHex( Math.random() * 0xffffff );
  31.  
  32. console.log("Intersect[0]: "+intersects[0].object);
  33.  
  34. // tell me face index
  35. console.log("Face index: "+intersects[0].faceIndex);
  36.  
  37. // gimme face as THREE.Face3/4 object
  38. console.log(intersects[0].face);
  39.  
  40. // face color
  41. // the face's indices are labeled with these characters
  42. var faceIndices = ['a', 'b', 'c', 'd'];
  43.  
  44. var face = geometry1.faces[intersects[0].faceIndex];
  45.  
  46. // determine if face is a tri or a quad
  47. var numberOfSides = ( face instanceof THREE.Face3 ) ? 3 : 4;
  48.  
  49. // assign color to each vertex of current face
  50. for( var j = 0; j < numberOfSides; j++ )
  51. {
  52. var vertexIndex = face[ faceIndices[ j ] ];
  53. // initialize color variable
  54. var color = new THREE.Color( 0xffffff );
  55. color.setRGB( Math.random(), 0, 0 );
  56. face.vertexColors[ j ] = color;
  57. }
  58.  
  59. var material1 = new THREE.MeshBasicMaterial( {
  60. color: Math.random() * 0xffffff,
  61. shading: THREE.FlatShading,
  62. vertexColors: THREE.VertexColors
  63. });
  64.  
  65. var directionalLight = new THREE.DirectionalLight(0xEEEEEE);
  66. directionalLight.position.set(10, 1, 1).normalize();
  67. scene.add(directionalLight);
  68.  
  69. var cube = new THREE.Mesh(new THREE.CubeGeometry(50, 50, 50,1,1,1), material1);
  70. cube.dynamic = true;
  71. scene.add(cube);
  72.  
  73. // create a particle on hit position
  74. var particle = new THREE.Particle( particleMaterial );
  75. //var particle = new THREE.Particle(new THREE.MeshLambertMaterial( { color: 0x2D303D, wireframe: false, shading: THREE.FlatShading } ));
  76. particle.position = intersects[ 0 ].point;
  77. particle.scale.x = particle.scale.y = 8;
  78. scene.add( particle );
  79. }
  80.  
  81. // Parse all the faces
  82. /*for ( var i in intersects ) {
  83. intersects[ i ].face.materialIndex = Math.floor(Math.random() * materials.length);
  84. }*/
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement