Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.

    Read our full announcement for more information and let us know if you have any questions.

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

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

  1. Kantic


    Jan 8, 2020
    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)


    Thanks in advance.
  2. bgolus


    Dec 7, 2012
    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.

    And finally here's the documentation on Surface Shaders. I recommend checking out the example page that links to for some, well, examples.
    Kantic likes this.
  3. Kantic


    Jan 8, 2020
    thx so much!!