Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Extremely Basic "interior" Lighting?

Discussion in 'Global Illumination' started by joshcamas, Apr 11, 2019.

  1. joshcamas

    joshcamas

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

    I have an open world game, and I'm finding trying to get baked lighting to work is quite hard. However, since this game is a low poly game, I feel like I don't need fancy baked realtime GI at all, at least for now.

    Really the only reason I'm interested in Global Illumination is because without it, my dark caves are extremely bright, since they are affected by the ambient lighting.

    Would it be at all possible to somehow have a extremely basic workflow that involves adding a volume to a scene that simply modifies the ambient lighting for the objects inside? Theoretically this would allow me to add a volume to an interior and make it a lot darker without any fancy baking.

    I imagine making my own shader could do the trick? Do lit shaders have a specific part that read the ambient lighting of the scene?

    Thank you
    Josh
     
    Last edited: Apr 11, 2019
  2. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,276
    Looks like this shader idea *might* work, since the variables are indeed built in. I feel like this is such as simple idea, it can't be original... Anyways, I'm going to try to get such a concept working.

    I haven't done too much with shaders, so bear with me... however, one idea I have is to have a "AmbientVolume" component, that adds it's size / position / other settings to a static manager of sorts, which would add it to the shaders global variable. Then the shader's frag function can check if each vertex is inside a volume, and if it is, modify itself depending on settings. This sounds fine, except the whole looping volumes part. I know shaders hate for loops, so I wonder if there's a better way of doing this...

    Alternatively, maybe it doesn't need to do anything per pixel, and could check the position of the object itself, which would determine it's "brightness". Obviously this would mean an object would be uniformly "bright", but that sounds wayyyyy faster.

    Also, maybe volume position / settings could be packed into a texture, instead of multiple arrays? Eh, idk.
     
    Last edited: Apr 12, 2019
  3. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,276
    Alright, I got an extremely basic form working. Not exactly pretty (it's terrible), but it's certainly something.




    Essentially I have volumes that modify global shader variables that holds arrays of volume sizes, rotations and positions.

    I then have a shader, for each of the vertices, checks if its inside one of the volumes. If it is, then it sets the ambient lighting to 0. If it is outside, then it sets it to the global ambient lighting of the scene.

    This has countless issues, of course. It loops each volume, which of course is an evil evil thing to do in shaders, especially for each vertex. Which makes it expensive. Also, I haven't actually put in volume rotation.

    And finally the kicker - it looks terrible. This is mainly due to the fact that each vertex is affected by volumes, meaning there are huge gaps that cause ugly issues. This is even worse if there are gaps and a smaller thing (such as a character) walks inside, causing the character to be dark and the large object to be light.

    With all of these issues, this concept is probably a dead end. Sadly I do not understand shaders enough, but if there was some sort of way to push volume data to a buffer that the shader could read from, I think this would make everything a lot more realistic...
     
    Last edited: Apr 12, 2019
  4. kristijonas_unity

    kristijonas_unity

    Unity Technologies

    Joined:
    Feb 8, 2018
    Posts:
    1,080
    Hey! If the only thing you need to adjust is ambient light in your cave, you can easily do that via script. You can use ambient light to influence your global shader colors within the volume. This is something that Joakin Svarling did when developing Angry Birds - https://twitter.com/Svarlingo/status/1009834620408483840

    This process would be a little bit simpler in HDRP, as it natively supports volume based lighting approach.
     
    Adam-Bailey and joshcamas like this.
  5. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,276
    My issue is that I need to make it "area based", not "shader based". (Aka globally affecting all objects with the same material). Since interiors and exteriors are on the same map, I need some way of making everything inside of an interior dark and everything inside of an exterior light.
     
  6. Adam-Bailey

    Adam-Bailey

    Joined:
    Feb 17, 2015
    Posts:
    232
    Seems like this is fairly straightforward to do now using Shadergraph in 2019.1 in HDRP, can you suggest a way of doing this using Shadergraph with the LWRP (it is possible using an Unlit master node, but if you also want to have dynamic lights that's not useful).
     
  7. Moe_Baker

    Moe_Baker

    Joined:
    Oct 22, 2017
    Posts:
    36
    Sorry for reviving such an old post, but it's one of the only results when searching about this feature.
    I needed this feature but couldn't find any resources, so I implemented it myself in URP and would like to share it with anyone in need.

    [Effect Preview]

    The effect is implemented via a custom shader named the "Cardinal" shader. it's all implemented using Shadergraph via sub-graphs and an HLSL file so it will be fairly easy to copy to your custom shaders should you need to.
    The effect modifies the fragment process, working on individual pixels, so it gives decent results, enough for me for sure.
    It uses structured buffers so it requires shader model 4.5 / ES 3.1.
    The volumes are all boxes, my game is based on box-like rooms so it fits my needs well, but you can implement spheres and other shapes relatively quickly if needed.

    I'm not very familiar with shader/graphic code so if you spot any optimizations or tips please let me know.

    Cheers.
     

    Attached Files:

  8. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,276
    Looks nice :) Since you're uploading the buffer every frame, you could cull off-camera volumes. Speaking of which, why are you uploading the buffer every frame, is that required? If it isn't required then I would consider doing it every time the Collection changes / change in volume visibility.
     
  9. Moe_Baker

    Moe_Baker

    Joined:
    Oct 22, 2017
    Posts:
    36
    Thank you.
    For the current game I'm working on I'm gonna cull all rooms the player has passed, and the volume will be a part of those rooms; thus I don't need to cull the volumes individually, but it's definitely a good idea to implement.
    And for updating the buffer, it's true that you don't need to update the buffer every frame, only on changes, honestly, I just got lazy with that :p, will add later on.
     
    PixelDough and joshcamas like this.