0

I am creating an open sourced 3d "doodler". It toggles between free-hand drawing and drawing at the intersection of previous line segments (indicated by a plane facing the camera/orbit control projection).

if the camera is at world position 5,0,5 and the plane is at 2,0,2, how do i tell the mouse that at any point while drawing (mouse down), it should draw itself along the plane? I am not doing offset handling correctly and would appreciate some guidance. thank you!

user3325025
  • 654
  • 7
  • 10

1 Answers1

0

ahh! finally after so many trial-and-errors! Am answering my own question b/c i think it can help others...

//subtract the unprojected mouse vector from the camera position (world coords),
//normalize it and then multiply it by the square root of the x and y diffs (distanceTo) the world coords of the plane 
//and thusly draw coplanarly(sp?) to the camera view plane

var projectedMouse = new THREE.Vector3();
projectedMouse.set(mouse.x, mouse.y, 0.5 );             
var unprojectedMouse = projectedMouse.clone().unproject( camera );                      

var pos= camera.position.clone(); 
var camPos = camera.position.clone(); 

var dir = unprojectedMouse.sub( camPos ).normalize();
pos.add(dir.multiplyScalar(currentIntersectedPoint.distanceTo(camPos)));

the difference between the above and Wes Langleys' suggestion in the other post Mouse / Canvas X, Y to Three.js World X, Y, Z below) is the calculation of the distance between z's didn't work for me but the distanceTo() method did.... distanceTo evaluates the square root of the respective squared x and y differences, which you would think is effectively the z but clearly not right in my instance, and I don't know exactly the math behind it... feel free to add the proper explanation...

var dir = unprojectedMouse.sub( this.camera.position ).normalize();
var distance = (targetZ - camera.position.z) / dir.z;

var pos = this.camera.position.clone().add( dir.multiplyScalar( distance ) );
Community
  • 1
  • 1
user3325025
  • 654
  • 7
  • 10