vasturiano / 3d-force-graph

3D force-directed graph component using ThreeJS/WebGL
https://vasturiano.github.io/3d-force-graph/example/large-graph/
MIT License
4.48k stars 803 forks source link

adding a hover or click listener to an external asset #649

Closed itsPreto closed 7 months ago

itsPreto commented 8 months ago

how does one go about adding either a listener for hover or click events on this custom mesh I added to the scene?

   // Create the mesh (geometry + material)
    const planeGeometry = new THREE.PlaneGeometry(2000, 1000); // Adjust size as needed
    const planeMaterial = new THREE.MeshLambertMaterial({ color: 0xFF0000, side: THREE.DoubleSide });
    const mesh = new THREE.Mesh(planeGeometry, planeMaterial);

    // Position the mesh behind the graph. You might need to adjust these values.
    // The 'z' coordinate moves the mesh forward and backward from the camera's point of view.
    mesh.position.set(0, 0, -1000); // These values are just placeholders. Replace with actual calculations if needed.

    // Rotate the mesh to be horizontal. Adjust if it does not appear horizontal.
    // The rotation is in radians, and you're rotating around the X-axis.
    mesh.rotation.x = -Math.PI / 2; // -90 degrees

    // Add the mesh to the scene
    Graph.scene().add(mesh);
vasturiano commented 8 months ago

@itsPreto thanks for reaching out.

Without setting up your own Raycaster and pointer detection system I don't think you will be able to do that out of the box. The module doesn't know or recognise foreign objects added to the scene directly.

itsPreto commented 8 months ago

Thanks @vasturiano that sounds like a big headache for little reward so I'll put it on low priority for now lol but since we're already here and I don't feel like this deserves a whole issue-- do you know of any ways to override the default camera movement/logic for the 'fly' orbitControls?

Currently the camera moves when I move the mouse which can make navigating (accurately) somewhat hard. It would be nice to make it so that he camera only moves when I hold down the the mouse (right or left key), and once I let go it either stops immediately or 'trackballs' to a stop.

vasturiano commented 8 months ago

@itsPreto I'd suggest digging a bit deeper into the docs of ORbitControls to see if there's a way to configure it to behave the way you want.