I shall begin with mentioning that I’m a total beginner, so I assume that there is a minor issue I keep missing over and over again while trying to make it work.
In short: I loaded a GLTF scene with 3 different objects and added the scene separately. I want to make each of the objects open a new page using a URL. Raycaster seems to work just fine, but when I add multiple URLs all the objects seem to open the same (first mentioned) page.
The console shows me that each object reacts on its own, but when adding URLs they all work together. I figured maybe I should group my objects once they GTFL is loaded, but I can’t seem to figure it out.
What am I missing?
Here is my GLTFLoader and Raycaster
const loader = new GLTFLoader().setPath('public/foldery/');
loader.load('folder 3d.gltf', (gltf) => {
const mesh = gltf.scene;
mesh.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
mesh.position.set(0, 1.35, 0);
scene.add(mesh);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let selectedObject = undefined;
document.addEventListener('mousedown', onMouseDown);
function onMouseDown(event) {
const coords = new THREE.Vector2(
(event.clientX / renderer.domElement.clientWidth) * 2 - 1,
-((event.clientY / renderer.domElement.clientHeight) * 2 - 1),
);
raycaster.setFromCamera(coords, camera);
var intersections = raycaster.intersectObjects(scene.children, true);
if (intersections.length > 0) { // it seems to work up to this point
window.open('https://www.amazon.com') (intersectObjects(cube1));
window.open('https://www.google.com') (intersectObjects(cube2));
window.open('https://www.facebook.com') (intersectObjects(cube3)); //all 3 cubes open amazon!
}
};
});
Is it possible to add a custom data to objects in Blender?
If yes, then it’s enough to have, for example, object.userData.link property, that stores a URL.
And all you need to do is to obtain that URL.
Demo: https://codepen.io/prisoner849/full/vEBBVVE