Introducing ShaderLabLab: Shader templating system

I made a thing!

I’ve been creating an ever expanding Uber shader and needed better systems to manage it’s complexity. Specifically being able to share ShaderLab properties between shaders and complex preprocessor logic.

Behold!

~~~ ShaderLab…Lab ~~~
GitHub - TechArtistG/ShaderLabLab: A system using T4 Templates to generate Unity ShaderLab code in the Editor

It uses T4 Templating (Mono.TextTemplating) along with a custom asset import hook to create shaders from templates. Details in the readme on Github.

For example, this:

<#
       for(int c = 0; c < 5; c++)
       {
#>
_commonProp<#=c#>  ("included prop <#=c#>", Float) = 0
<#        
       }
#>

Results in this:

_commonProp0  ("included prop 0", Float) = 0
_commonProp1  ("included prop 1", Float) = 0
_commonProp2  ("included prop 2", Float) = 0
_commonProp3  ("included prop 3", Float) = 0
_commonProp4  ("included prop 4", Float) = 0

Think of it as ASP.NET or PHP for shaders.

This could easily be expanded to a general Unity templating system but want to keep it shader specific.

I started this at about 9:30pm and its now 1:00am so there’s probably going to be some mistakes somewhere.

Happy to implement any suggestions so have at 'er!

TNKS!

Geordie
Technical Art Director
Windows Photos and Videos
Microsoft

1 Like

Cool! The lack of property sharing in ShaderLab causes me immeasurable suffering.

Could you please provide an example of an Uber shader with many passes that can be enabled/disabled?

Here’s what you’d have to do right now:

  1. Write your ShaderLab properties into a separate text asset file
  2. Write each shader pass into it’s own text asset file
  3. Write templates for each combination of passes using includes to piece together the files from steps 1 and 2 then process them with ShaderLabLab (should be automatic)
  4. Write a custom GUI script that all your shaders use. In the custom GUI script add UI to switch between the different shaders generated in step 3. Since they all have the same properties switching shaders should be seamless.

In V2 of this system I’m hoping to create a meta language similar to ShaderLab that auto-generates custom GUI scripts for shader collections.