# Real time lighting in custom shaders

**URL:** https://discussions.unity.com/t/real-time-lighting-in-custom-shaders/658262
**Category:** Shaders & Materials
**Tags:** Shaders
**Created:** [March 14, 2017, 4:23pm UTC](https://discussions.unity.com/t/real-time-lighting-in-custom-shaders/658262 "2017-03-14T16:23:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![scottharber](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@scottharber](https://discussions.unity.com/u/scottharber)
#### Post date: [March 14, 2017, 4:23pm UTC](https://discussions.unity.com/t/real-time-lighting-in-custom-shaders/658262/1 "2017-03-14T16:23:36Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![bgolus](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bgolus/32/391570_2.png) [@bgolus](https://discussions.unity.com/u/bgolus)
#### Post date: [March 14, 2017, 5:57pm UTC](https://discussions.unity.com/t/real-time-lighting-in-custom-shaders/658262/2 "2017-03-14T17:57:35Z")

</div>

> **[Unity - Manual: Custom shader fundamentals](https://docs.unity3d.com/Manual/SL-VertexFragmentShaderExamples.html)**

---

<div class="post-metadata">

### Author: ![scottharber](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@scottharber](https://discussions.unity.com/u/scottharber)
#### Post date: [March 15, 2017, 8:33am UTC](https://discussions.unity.com/t/real-time-lighting-in-custom-shaders/658262/3 "2017-03-15T08:33:27Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Lost-in-the-Garden](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lost-in-the-garden/32/337708_2.png) [@Lost-in-the-Garden](https://discussions.unity.com/u/Lost-in-the-Garden)
#### Post date: [March 15, 2017, 1:21pm UTC](https://discussions.unity.com/t/real-time-lighting-in-custom-shaders/658262/4 "2017-03-15T13:21:33Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![bgolus](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bgolus/32/391570_2.png) [@bgolus](https://discussions.unity.com/u/bgolus)
#### Post date: [March 15, 2017, 5:02pm UTC](https://discussions.unity.com/t/real-time-lighting-in-custom-shaders/658262/5 "2017-03-15T17:02:35Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![scottharber](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@scottharber](https://discussions.unity.com/u/scottharber)
#### Post date: [March 30, 2017, 12:54pm UTC](https://discussions.unity.com/t/real-time-lighting-in-custom-shaders/658262/6 "2017-03-30T12:54:51Z")

</div>

In case anyone else gets stuck on this, I found a blog online that explains (to an extent) how to get the GI and shadows into a custom shader. It doesn’t seem to handle multiple light sources though, but I’ll update the thread with details if I figure it out.

> **[How to Explore Unity 5's Shader System Code - IV - Global Illumination code...](https://www.shadercat.com/how-to-explore-unity-5s-shader-system-code-iv-global-illumination-code-overview/)**
>
> Last time
> \[https://www.shadercat.com/how-to-explore-unity-5s-shader-system-code-iii-reading-the-standard-shader-the-pbs-version/\] 
> we looked at the Physically Based lighting function in the Standard Shader.
> 
> This time we'll chase down how the Global...
