Three. js sample code for getting the 3D coordinates clicked by the mouse

Source: Internet
Author: User
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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.