I am trying to get the following value: for any vector in 3D space (the blue one), I need to project it on the green plane so I can compute the angle alpha on this green plane between the projected vector (purple) and the z axis of the camera.
So far, my best attempt was by first trying to get the green plane, whichever the camera position is in 3D space, assuming that the green plane normal is pointing to +y :
var greenPlane= new THREE.Vector3( 0, 1, 0 );
greenPlane.applyQuaternion( camera.quaternion );
But the values get by tracing the purple vector are incorrect. My final purpose is to detect if the purple vector is pointing towards left or right of the screen, so I can correctly place a label at its extremity.
I am not familiar with applyMatrix4 function (very few tutos about how to use it).
Does this apply a transformation to the blue vector so we get its coordinates in the camera/view coordinates? And I am confused, you mention purple, but for me I can’t get the purple unless I am figuring out what is the green plane.
But if I can project the blue vector directly to the camera plane, then I can use cos to see if it’s positive or negative yes. I am only concerned about left or right with respect to the view (screen).
I would try this (camera.matrixWorldInverse is the view matrix):
// ensure matrix is up to date
camera.updateMatrixWorld();
// transform original vector into eye space, assuming blue vector is in world space
blueVector.applyMatrix4( camera.matrixWorldInverse );
The green one has only be declared var greenPlane= new THREE.Vector3( 0, 1, 0 ). If I move the camera it doesn’t go along. If I want it to actually be the green plane I have to apply a transformation in respect to the camera position, and this was (supposedly) achieved by greenPlane.applyQuaternion( camera.quaternion );
But following your previous idea, projecting the blue directly to the view should work.
camera.updateMatrixWorld();
blueVector.applyMatrix4( camera.matrixWorldInverse );
const zAxis = new THREE.Vector( 0, 0, - 1 );
blueVectorProject= blueVector.projectOnPlane(zAxis );
// then I compute the cos value between blueVectorProject and view x axis
I have to try yes, not familiar with it. Good opportunity to start!
BTW, do you know a good source where we can learn about all these Vector and Matrix manipulations for the 3D world. I didn’t know what matrixWorldInverse was representing for instance.
Thanks Michael (I should all put you name on the credits of our projects )