Search Unity

How to keep the default lighting on a unlit shader?(noob needs help!)

Discussion in 'Shaders' started by Kantic, Jan 26, 2021.

  1. Kantic

    Kantic

    Joined:
    Jan 8, 2020
    Posts:
    11
    Hi, I'm working on terrain generation and I'd like to do it with shaders.
    Since I need to do per vertex computations and then per pixel/fragment(I don't quite get the difference yet)
    I would prefer using unlit shaders instead of compute shaders.

    While everything works fine when I implement my algorithms i loose lighting wich is something DONT want to make by my self.

    So my question is:
    How do I keep lighting on my object and still apply my custom unlit shader?

    I'm guessing I would have to add something to my code, I'm displacing the vertices so a second material wouldn't work(at least i think so)

    ANY HELP IS APPRECIATED!

    Thanks in advance.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Some basic terminology:

    "vertex fragment shader" - What GPUs use to render meshes. If you're rendering a mesh of some kind, it's using a vertex fragment shader. Really these should be referred to as "vertex and fragment shaders", but the and has been omitted for brevity as they're really two separate shader stages, the vertex shader and fragment shader, that GPUs use together to render stuff. Vertex shaders take a vertex of a mesh and calculates the screen space position, as well as calculate and/or pass along any other data based on the mesh vertex that might be needed by the fragment shader. The GPU takes the screen space vertex positions and finds the pixel coverage of the triangles that use those vertices. It then calls the fragment shader for each pixel covered by each triangle. The fragment shader then uses the data passed to it from the vertex shader (interpolated from 3 vertices based on that pixels position within the triangle) and from the material properties to calculate a single output color value. The end result is the output from the (usually more than one) fragment shader run at each pixel is the image you see on screen.

    "compute shader" - Arbitrary code that can do basically anything you want, but runs on the GPU. Similar to vertex fragment shaders in that they're both written in HLSL shader code, and that they run on the GPU. But compute shaders aren't necessarily used for rendering objects directly (though they can do that). More often they're used to do some kind of math heavy work that the CPU isn't as good at, like the position of a lot of particles, or maybe processing a single image for post processing effects (screen space ambient occlusion, automatic exposure, etc.) If you don't really understand vertex fragment shaders, I guarantee you're not doing anything with compute shaders, at all.

    "unlit shader" - A shader that does not interact with lighting systems in any way. If you merge a lit shader with an unlit shader, it's either no longer an unlit shader or no longer a lit shader. This is more a pet peeve of mine than a real problem. But really you're asking "how do I easily use custom code while interacting with Unity's lighting system".

    "Surface Shader" - This is the answer to the question you were trying to ask. Surface Shaders are vertex fragment shader generators that abstract away most of the complicated code that handles lighting and lets you only write a small amount of code needed to do whatever custom thing you want to do.

    Watch this if you need a really basic high level introduction to what shaders are.


    Then read this (all 5 parts) to get a slightly more in depth idea of how to use and write shaders, including Surface Shaders.
    https://www.alanzucconi.com/2015/06/10/a-gentle-introduction-to-shaders-in-unity3d/

    And finally here's the documentation on Surface Shaders. I recommend checking out the example page that links to for some, well, examples.
    https://docs.unity3d.com/Manual/SL-SurfaceShaders.html
     
    Kantic likes this.
  3. Kantic

    Kantic

    Joined:
    Jan 8, 2020
    Posts:
    11
    thx so much!!