Hi.
I want to write a simple function what determine the position on a plane by given a position on screen(-1,1) and a distance from a camera.
I tried to update the position of the plane matrix, the log says it’s at the good position, but the raycasting always calculating with the prevous poition.
Here is my function:
const relativeposplanegeom = new THREE.PlaneBufferGeometry(2000,2000)
const relativeposplane = new THREE.Mesh(relativeposplanegeom,basematerial)
relativeposplane.name = "relativeposplane";
relativeposplane.position.set(0,0,0);
cameragroup.add(relativeposplane);
async function getrelativeposition(distance,screenx,screeny){
const pointer = new THREE.Vector2(screenx,screeny);
raycaster.setFromCamera(pointer,camera);
const obj = cameragroup.getObjectByName("relativeposplane");
if(typeof obj!=='undefined')
{
obj.position.z = distance;
//obj.matrix.setPosition( 0,0,distance );
obj.updateMatrix();
console.table(obj.position); //it says it's at 0,0,-0.2(good position)
const positionhit = await raycaster.intersectObjects(cameragroup.children);
console.table(positionhit);
if(positionhit.length>0)
{
return positionhit[0].point;
}
}
console.log("relativeplane is undefined");
return [100,100,100];
}export { getrelativeposition };
Here is the calling(from an another js file):
let pos = await threeengine.getrelativeposition(-0.2,0.8,0);
frameobject.position.x = pos.x;
console.table(pos);//return [100,100,100], because it can't see the plane(It can see if the plane.pos.z is negative )
Sorry, for my amateur code,I am a beginner in js