1. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  2. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  3. Unity 2017.1 is now released.
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  6. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

Real time lighting in custom shaders

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

  1. scottharber

    scottharber

    Joined:
    May 14, 2014
    Posts:
    41
    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:
    2,951
  3. scottharber

    scottharber

    Joined:
    May 14, 2014
    Posts:
    41
    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:
    148
    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:
    2,951
    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:
    41