Search Unity

Real time lighting in custom shaders

Discussion in 'Shaders' started by scottharber, Mar 14, 2017.

  1. scottharber

    scottharber

    Joined:
    May 14, 2014
    Posts:
    42
    Hi,

    I've got a set of entirely custom shaders (custom vert, custom frag). If I wanted to have Unity's real time lighting model working within them, is there a way to do that? e.g. is there a function I can #include into my existing shaders that would enable the real time lighting?

    Apologies if this is a dumb question btw; I couldn't find any info in the usual places.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,336
  3. scottharber

    scottharber

    Joined:
    May 14, 2014
    Posts:
    42
    Thanks @bgolus :)

    I knew about that page, but the examples only seem to support one directional light and vertex-based ambient from lightprobes. Is there a way to support multiple light sources and real time GI?
     
  4. Lost-in-the-Garden

    Lost-in-the-Garden

    Joined:
    Nov 18, 2015
    Posts:
    176
    What we did was to start with a simple surface shader and then in the editor click the "show generate shader" button in the inspector. This spits out the result of all the macro and code generation magic that happens under the hood. We then took a large crowbar and a hammer, to rip out all the features we did not use and only left the stuff in we use.

    It's dirty work, but it works.

    For multiple light sources you would have to also read up on how unity can render them in multiple additive passes.
     
    bgolus likes this.
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,336
    It's good to understand that Surface shaders are just vertex fragment shader generators, so there's nothing a surface shader can do that a custom shader can't. Surface shaders are kind of ugly to look at, but they were designed to be somewhat modular which makes starting with them instead of going straight to the actual standard.shader a little easier to convert into something custom.
     
  6. scottharber

    scottharber

    Joined:
    May 14, 2014
    Posts:
    42