Search Unity

Per-Material Ambient Light Source?

Discussion in 'Shaders' started by Atomike, Nov 19, 2017.

  1. Atomike

    Atomike

    Joined:
    Mar 25, 2014
    Posts:
    15
    I want to to modify the Standard Shader so that it can use a cubemap specified by the material as an ambient light source. That way I can have multiple objects with different ambient light sources in the same scene. I'm fairly new to Shader coding so I'm not sure how to go about doing this.
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    While not an answer specifically to your question, there is already a feature that can do this for you without need to jump through hoops and manage a bunch of cubemaps yourself. Light probe volumes allow you to place points all around your scene that lighting will bake to at each of those positions, and the surface shaders of all your objects will sample ambient lighting from the nearest light probe points. This can even work for skinned characters if you assign a light probe proxy volume to them.

    https://unity3d.com/learn/tutorials/topics/graphics/probe-lighting
     
  3. Atomike

    Atomike

    Joined:
    Mar 25, 2014
    Posts:
    15
    Doesn't that require baking GI beforehand? It also isn't realtime.


    This is basically what I'm trying to achieve but doing it per material instead of having to call "DynamicGI.UpdateEnvironment ();" every frame. Also I can generate cubemaps at runtime through a script and set instances of the materials of objects to use specific cubemaps. Say have 3 realtime cubemaps per scene, one for the majority of the scene, mostly for static objects, one for changes in scenery, maybe a cave for instance, and then a third dedicated to the player and objects surrounding the player. It's in an attempt to make a "fake" Realtime GI system that takes much less resources than actual Realtime GI, at the expense of precision.
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    In your video description you mention only wanting the dynamic cube map to affect the player so that there is "less work to be done", but in reality, the majority of the work comes from baking a cube map every frame of the whole scene, objects have to be made visible in every direction and draw calls made for them, it's very expensive.

    You can achieve a more performant dynamic GI by using light probes, since they simply use spherical harmonics and are easier to update from instead of trying to render the scene a second time each frame for that cubemap. If you fill your scene with light probe points, focusing them around areas of light difference and emission, then put a "light probe proxy volume" on your character, you will get the dynamic GI like in the video, without having to update a cubemap every frame. And dynamic light emitting objects should just be actual lights attached to them.

    But if you really want to, then just look up "unity shader cubemap", there are plenty of resources for how to sample a cubemap in a shader.