Search Unity

Converting unlit shader to lit?

Discussion in 'Shaders' started by Triangles4, Jan 2, 2020.

  1. Triangles4

    Triangles4

    Joined:
    Jan 1, 2020
    Posts:
    9
    Would anyone be able to tell me how to transform an unlit shader to lit? I am experimenting with a liquid shader that was created by some one else, but I am trying to figure out how to make it stop glowing in the dark. Would anyone be able to tell me how to do so and help me with their expertise?

    This is the shader I am playing around with https://pastebin.com/ppbzx7mn
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Ah, a MinionsArt shader.
    https://www.patreon.com/posts/quick-game-art-18245226

    So, adding lighting to the sides of the liquid isn't too hard. It'll take a bit to properly translate how she's calculating the top cutoff from a vertex fragment shader to a Surface Shader, which is the easiest way to add support for lighting to a shader (including support for multiple lights and shadows). The problem is the "top" surface is an illusion. Really that's just the back face of the mesh with a solid color. If you add shadows and lighting to that, it'll very clearly not be a top surface. To handle everything correctly you have to override the normal to be facing "up", which is possible though non-trivial to do with Surface Shaders, and override the per pixel depth ... which is not possible with a Surface Shader.

    To do that means hand writing several custom passes that do some (minimal) in shader raytracing to compute the surface depth of that not-actually there top surface.
     
    Last edited: Jan 3, 2020
  3. Triangles4

    Triangles4

    Joined:
    Jan 1, 2020
    Posts:
    9
    Hmm... dang... Is it possible to go around it in another way perhaps? Like... less ambient light = more black tint? Something like that would be possible? Because that could also solve it in a way
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    If you don't need shadow receiving or multiple lights, you can look at the vertex fragment shader examples which have some basic lit shader examples:
    https://docs.unity3d.com/Manual/SL-VertexFragmentShaderExamples.html

    The main trick will be doing that backface check to change the world space normal used to something like
    float3(0,1,0)
    . It won't be quite right if you're doing the wobble, but you can probably get something usable with
    normalize(float3(_WobbleX, 1, _WobbleZ))
    . It won't be correct, but it'll at least change as the surface wobbles.
     
  5. Triangles4

    Triangles4

    Joined:
    Jan 1, 2020
    Posts:
    9
    Where would I insert this? Tbh I am a code dunce and wouldn't even know how to implement what you just explained.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Time to start learning then.
     
  7. Triangles4

    Triangles4

    Joined:
    Jan 1, 2020
    Posts:
    9
    Well no matter where I tried to plug it in the code it always ended up breaking the code except one spot, it didn't seem to do anything sadly