Search Unity

RayCast mouse position to VFX Graph

Discussion in 'Visual Effect Graph' started by xofreshxo, Mar 9, 2022.

  1. xofreshxo

    xofreshxo

    Joined:
    Apr 12, 2017
    Posts:
    79
    I'm trying to add some interactivity to our VFX graph visualization and would like to be able to mouseover different Particle Cubes (i.e. 50 floating cubes) and then highlight the ones that I've moused over.

    Is there a component that I could add to the VFX graph or an easy way to detect a raycast from the mouse position onto the individual cube?

    Thanks!
     
  2. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,314
    I think this is currently not possible. Particles are simulated on GPU, they do not have any colliders/shapes and right now there is no way to access particle data to create custom check, however there is feature you can vote on roadmap.

    If you must use VFX graph then I guess the only way is to create some custom math in graph, like checking distance from particle to provided line (ray) or in very specific situations you could do something similar to this.
     
    VladVNeykov likes this.
  3. VladVNeykov

    VladVNeykov

    Unity Technologies

    Joined:
    Sep 16, 2016
    Posts:
    550
    Yup, as @Qriva explained it's not something you can do out of the box.

    Depending on your setup, you can explore a few options:

    1. If you don't care for an exact raycast, but more of a "under the mouse" look, you can possibly on click convert your mouse position to world position
    Code (CSharp):
    1. Camera.main.ScreenToWorldPoint(mousePos);
    and then feed that to the VFX graph and do a 2D distance check between this position and the particle position.

    2. If your particles render to depth, I think it's also possible to create a workaround by calculating the position from the camera depth texture in a compute shader and converting it to world position and then feeding this in your graph and doing a distance check with your particles to highlight the nearest one(s). This is probably the most involved option.

    3. And If you are having only 50 cubes, you may want to consider using the built-in Particle System which runs on the CPU and should allow you to interact with raycasts.

    Hope this helps!
     
    Qriva likes this.