This article mainly introduces Three. js 3D coordinate sample code for getting mouse clicks. It has some reference value. If you are interested, you can refer to it. This article mainly introduces Three. js 3D coordinate sample code for getting mouse clicks. It has some reference value. If you are interested, you can refer to it.
Due to work requirements, three. js is useless, and there is very little information on the Internet, so let me get the coordinates, it is really one of the first two big. Finally, I realized it.
Now that you want to get the 3D coordinates of the mouse click, I believe you have both the camera object and scene. If you do not know any of them, you can first look at these two objects. Here, we will mainly talk about how to obtain 3D coordinates. We will not mention the principles. Code:
Function onDocumentMouseDown (event) {event. preventDefault (); var vector = new THREE. vector3 (); // three-dimensional coordinate object vector. set (event. clientX/window. innerWidth) * 2-1,-(event. clientY/window. innerHeight) * 2 + 1, 0.5); vector. unproject (camera); var raycaster = new THREE. raycaster (camera. position, vector. sub (camera. position ). normalize (); var intersects = raycaster. intersectObjects (scene. children); if (intersects. length> 0) {var selected = intersects [0]; // obtain the console of the first object. log ("x coordinate:" + selected. point. x); console. log ("y coordinate:" + selected. point. y); console. log ("zcoordinate:" + selected. point. z );}
What I understand is that when the mouse clicks, the screen receives two-dimensional coordinates. The two-dimensional coordinates are connected to the camera, extending in the direction of the angle of view to form a ray, this ray will interwork with the objects in the scene and receive all these overlapping objects. The first object is the closest to the camera, and the last is the farthest from the camera. Generally, the first intersecting object is used as the object clicked by the mouse.
The above is a detailed description of the sample code for Three. js to get the 3D coordinates of the mouse click. For more information, see other related articles in the first PHP community!