I’m using OrthographicCamera in a project and Raycaster to detect clicks.
const scene = new Scene();
let viewSize = 100;
let aspectRatio = window.innerWidth / window.innerHeight;
const camera = new OrthographicCamera(
(-aspectRatio * viewSize) / 2,
(aspectRatio * viewSize) / 2,
viewSize / 2,
-viewSize / 2,
-1000,
1000,
);
const canvas = document.createElement('canvas');
canvas.id = 'canvas';
const renderer = new WebGLRenderer({ canvas, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.render(scene, camera);
...
const intersects = (layerMesh, e) => {
const raycaster = new Raycaster();
const { clientX, clientY } = e;
let mouse = { x: 0, y: 0 };
mouse.x = (clientX / window.innerWidth) * 2 - 1;
mouse.y = -(clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const intersected = raycaster.intersectObject(layerMesh);
if (intersected.length > 0) {
return true;
}
return false;
}
My apoligies. It will mostly serve as a pseudo code, because i’m not sure that I can publish
it. This way of using Raycaster works pretty fine. Until I’m changing z-position of the Mesh
via mesh.position.z = 4
.
In the project z-index controls whether the element on the screen goes in front, or puts behind another element.
Can somebody explain, what I’m missing? Don’t understand, why the raycaster doesn’t detect mesh with the changed z-position.
I had some guesses, and tried to change some properties of raycaster.ray.origin
and raycaster.ray.direction
but it didn’t help.
Found a code from this link Three.js Raycasting with Camera as Origin - Stack Overflow
It worked out. But still i don’t understand, maybe somebody can explain. Thanks a lot.