1

Three.js r70 source code

setFromCamera: function ( coords, camera ) {

        // camera is assumed _not_ to be a child of a transformed object

        if ( camera instanceof THREE.PerspectiveCamera ) {

            this.ray.origin.copy( camera.position );
            this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( camera.position ).normalize();

        }

this line,set the mouse's position and z's default value 0.5.

this.ray.direction.set( coords.x, coords.y, 0.5 )

most example code about raycasting like

mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

I wonder how it works ,what does the ( event.clientX / window.innerWidth ) * 2 - 1 mean, and why set the z 0.5?

Jinceon
  • 1,292
  • 2
  • 13
  • 21
  • See http://stackoverflow.com/questions/11036106/three-js-projector-and-ray-objects/ – WestLangley Mar 03 '15 at 02:10
  • @WestLangley thanks very much. it's too difficult to understand now, but i know that's what i want. – Jinceon Mar 03 '15 at 03:59
  • if difficult, use [EventControls](http://stackoverflow.com/questions/28675875/threejs-how-to-pick-just-one-type-of-objects/28679672#28679672) – gevaraweb Mar 03 '15 at 15:10

1 Answers1

0

event its every time your mouse click client.x is the mouse position on x-axiz width.innerheight is how height your screen size is when you have a 2d mouse you have to separate it so when you are on left half of screen you go left and right you go right so you divide the screen height in 2 and compare where the 1 on the end mouse the mouse registration around so if you want to offset the crosshairs you can do to

function onDocumentMouseMove(e) {
    e.preventDefault();
    
var fleece = window.document.body.scrollHeight;
var fleeceb = window.document.body.scrollWidth;
mouse.x = (e.clientX / fleeceb) * 2 - 1;
    mouse.y = - (e.clientY / fleece) * 2 + 1;
}
nale
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 31 '21 at 14:10