Search Unity

Using Ambient Trilight

Discussion in 'Shaders' started by jvo3dc, Aug 10, 2017.

  1. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Working on a sky model, I noticed my shaders don't support the Trilight Ambient setting, while the Standard shader of course does.

    I've taken a look at the Standard shader, but there nothing that strikes me as being the keyword I need to respond to. (I'm assuming that different ambient modes require different shader variants.)

    Does anyone have a hint? The difference is very clear. Top is de standard shader and bottom is my shader.

    trilight_ambient.jpg
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    The "default" way of doing ambient in custom shaders is using UNITY_LIGHTMODEL_AMBIENT. This is basically the same as just using the equator color for everything. The standard shader uses the the light probe spherical harmonics for lighting. To use this you need:

    ShadeSH9(half4(worldNormal.xyz, 1.0))

    That world normal can either be done in the vertex shader and passed as a color to the fragment shader, or done in the fragment shader if you need normal map support.
     
  3. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Thanks, I was already getting the impression that ambient was not handled separately. I came out at SHEvalLinearL2 and SHEvalLinearL0L1, but that indeed did not seem the correct function to look at. I'll check ShadeSH9. Not a bad thing to also support light probes while I'm at it.

    So what are the keyword options? If I look at ShadeSHPerVertex/ShadeSHPerPixel I see:
    - UNITY_SAMPLE_FULL_SH_PER_PIXEL (seems to be the highest quality option, using ShadeSH9)
    - UNITY_STANDARD_SIMPLE (Still uses ShadeSH9, but per vertex)
    - Mystery option 3 that uses SHEvalLinearL2 per vertex and SHEvalLinearL0L1 per pixel

    I also don't see the above keywords in a multi_compile. So I'm assuming this is not really a setting, but more a platform specific keyword like UNITY_UV_STARTS_AT_TOP.

    Edit: Ah, ShadeSH9 just consists of SHEvalLinearL2 and SHEvalLinearL0L1. So the whole question is whether to do things per vertex or per pixel. (Nice way to hide that.)

    Edit2: Ok, that did the trick. Too bad the ambient input is just 3 colors, while it is sent as 9 to the shaders. I don't mind calculating all 9 and setting those.
     
    Last edited: Aug 11, 2017
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    In case you didn't figure it out already, those keywords aren't coming from the engine, or even directly part of multi compile / shader feature. They're defines the standard shader sets based on other factors such as target, if normal mapping is enabled, or if lightmaps are enabled.

    The three ambient colors are sent to the shaders individually as well. You could use the world normal y component to lerp between them if you wanted. The issue I ran into is I couldn't find any way to know what type of ambient lighting was selected; Skybox (SH), single color (UNITY_LIGHTMODEL_AMBIENT), or trilight (unity_AmbientSky, Equator, Ground I think?).

    The other thing is it's actually not significantly faster to use the three colors and lerp between them vs using the SH, though it is a little more accurate. The great thing with SH is how cheap it is to calculate.
     
  5. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Ah, so they come from the editor script for the shader. I have my own set that includes whether a normal map is enabled or not, so I could switch on these. For now I do everything per pixel, which should be fine.

    In terms of accuracy you could do better if you use some more specific code. The equator color has a very wide influence now, while it is generally a fairly thin strip of brighter light. On the other side, just lerping the actual equator color to the sky color solves that too. I don't think I'll try to fully integrate the atmosphere formula.

    Anyway, it looks pretty decent now. Up to the real tricky part, clouds...

    24_hour.png