Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving Texture Map coordinates from Object face Three.js

I'm trying to take a 3D Object in Three.js, look at a point on the surface of that object and find the corresponding point on a texture file (preferably with x,y coordinates).

Right now I'm casting a ray to find the point on the face of the object. Each point has a corresponding face that I think should somehow correspond to the texture map. Is there a known way of retrieving this information? (perhaps by somehow inverting the UV Mapping function?) If not I will continue to work on the problem and post a solution if I can figure it out.

like image 461
HighSkyd Avatar asked Sep 25 '26 19:09

HighSkyd


1 Answers

I was able to solve this problem in the following way:

//Find the Index of the face at the center of the screen

var vector = new THREE.Vector3(0, 0, 0.5);
projector.unprojectVector(vector, camera);

var raycaster = new THREE.Raycaster(camera.position, vector.sub(camera.position).normalize());

var intersects = raycaster.intersectObject(object, true);

if (intersects.length > 0) {
    var index = intersects[0].faceIndex;
}

//Find the UV Coordinates of the the three vertices of the face at that index

var point1,point2,point3;
point1.x = objectGlobal.children[0].geometry.faceVertexUvs[0][index][0].x;
point1.y = objectGlobal.children[0].geometry.faceVertexUvs[0][index][0].y

point2.x = objectGlobal.children[0].geometry.faceVertexUvs[0][index][1].x;
point2.y = objectGlobal.children[0].geometry.faceVertexUvs[0][index][1].y;

point3.x = objectGlobal.children[0].geometry.faceVertexUvs[0][index][2].x;
point3.y = objectGlobal.children[0].geometry.faceVertexUvs[0][index][2].y;
like image 198
HighSkyd Avatar answered Sep 27 '26 07:09

HighSkyd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!