Search Unity

Global Volumes Affecting Shader

Discussion in 'Shaders' started by joshcamas, Apr 11, 2019.

  1. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,277
    Hello friends

    I've had several cases where I have volumes that I want to affect shaders - aka allowing the shader to check if a vertex/pixel is inside a volume, and doing whatever I want to do if that is the case.

    However, my current solution is pretty ugly, I'd say - it involves unraveling a for loop, which feels incredibly wrong. Essentially I have a volume manager that saves arrays of volume sizes and positions to a shader global variable. Then in the shader, I loop the volumes size/positions.

    Is there a better way to do this?

    Josh
     
  2. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
    How many overlap there is from one mesh with the volumes?
    You could probably have a spatial partioning based on the minimal amount of overlap per volume (just like it's done per light)
     
  3. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,277
    I'd be fine with the one volume per mesh. How does this spatial partitioning thing work in shaders?
     
  4. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
    Nah you do it before the shader, then you passed it to the shader, so now it only have one set of data. That's basically the rendering pipeline, we sort object on cpu, and only pass relevant data together to gpu. Basically in this case you would expose the volume parameter (say position and radius if spherical), and pass it by code before rendering.
     
  5. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,277
    Interesting. How would I go through all objects that have that shader?
     
  6. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
    Why not loop through the objects with a certain material, or better, have the object register if seen onto a list. Or simply design object so they have a flag or created inside the proper list. Depending of either they are static or not, you can also register then in spatial partition (like zone you have define, when they enter they register to that, then before rendering you check the zone and then look at what it contain. etc ....