0

I want to calculate the UV co-ordinates of a single point of a material rendered on 3D cube.

I am finding the mouse pointer intersection with the camera ray using THREE.Raycaster as:

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

  var vector = new THREE.Vector3(mouse.x, mouse.y, 1);
  vector.unproject(camera);
  var raycaster = new THREE.Raycaster(camera.position, vector.sub(camera.position).normalize());

  var intersects = raycaster.intersectObjects(scene.children);

I have tried calculating the points but the x,y co-ordinates mapping changes with zoom-in or zoom-out. The code snippet is as follows:

 var pixelX = intersects[0].uv.x;
 var pixelY = intersects[0].uv.y;

where intersects[0] represents the intersecting points of the ray from the camera.

For more details please refer: http://plnkr.co/edit/YYN8aechHGTKXvPv6tOo?p=preview.

On the basis of the plunker, suppose I get the uv co-ordinates(say x1,y1) of point from where the spiral object starts. But after zooming-in or zooming-out the cube the uv co-ordinates of that point don't remain the same i.e it changes to some random point (x2,y2)

  • 1
    Not an answer: Do not instantiate a new raycaster every time the mouse moves! See [this answer](https://stackoverflow.com/questions/18553209/orthographic-camera-and-selecting-objects-with-raycast/18553739#18553739). Also, you have an animation loop, so remove `controls.addEventListener( change )` and `controls.update()`. – WestLangley Jul 18 '17 at 19:25
  • `intersects` actually represents an array of intersection objects, not the face. Look at the documentation for [`intersectObject`](https://threejs.org/docs/#api/core/Raycaster.intersectObject), then debug your code to look at what you're receiving. I just debugged your plunker, and the UV values coming back from the intersection are what I would expect to see. – TheJim01 Jul 18 '17 at 20:40
  • @TheJim01 sorry my bad. I wanted to say the index indicated the face of the cube which is derived by using Math.floor(intersects[0].faceIndex / 2); – Mohit Patel Jul 18 '17 at 20:53
  • I don't see what you're seeing. Your plunkr is working as expected, and zooming does not change the UV positions for me. I do see some event lag (probably caused by plunkr or the issues WestLangley raised). What browser are you using? – TheJim01 Jul 18 '17 at 21:24

0 Answers0