Search Unity

Hi people,

I just thought I would take a few minutes to explain some of the new graphics features they we have going into Unity 4.1 so that you can have a real play with them.

Get the project for the things I talk about here. Note: After importing this package change the player settings to render in linear mode, as that was how the lightmaps were baked and it will look a bit weird otherwise.

Lit Shader Replace

So up to and including Unity 4.0 you have been able to do some pretty cool post processing and other things with shader replacement, but it’s always been a bit limiting, you could not have your whole scene be rendered with a different set of shaders and also include lighting. In Unity 4.1 we have made changes to how this works. Now when you use shader replacement lighting will also be calculated (if the shader you are providing supports lighting). The render path for the camera you are using for the rendering of the shader replacement will be selected (so you can use forward / deferred or vertex lit).

Using lit shader replace you can write some pretty nifty effects and tools. To show an example of what can be achieved I decided to implement a view mode in the editor that shows ‘lighting only’ this is a pretty handy tool for lighting artists to debug their scene lighting and lightmaps.

Original Scene

Lighting Only

So how does it work?

If you check out the Editor Script in the LightingOnly folder you will see that there is a new menu “LightingOnly” with two options. When this is called we render scene view with a replacement shader. The shader was written pretty quickly and does not cover every rendering eventuality (i.e. does not do trees & grass / if you have your own rendertype tags in the shader you will need to write a lighting only shader for them). But it gives an idea of the kind of things you can do. You can use lit shader replace to show a lot of cool things in the scene view.

In addition to the scene view script there is another script ‘LightingOnlyCamera’. If you put this on the camera then it will use shader replace to render the game view. You could do many interesting effects with this.

Custom Material Inspectors / Per Shader Keywords

For a long time you have been able to specify shader keywords using the ‘Shader.EnableKeyword’ and ‘Shader.DisableKeyword’ this worked on a global level and would set keywords for all shaders. This isn’t really what you want all the time though. In Unity 4.1 we have added per material keywords. What this means is that you can specify keywords on a per material level. So you can have one shader that uses one or two textures and select the combination you want from a material inspector (or from scripting)!

To do this you will need to extend the material inspector. If you open the shader ‘MyShader’ in the example project you will see there is a new field ‘CustomEditor’ this has the name of a C# class that will be used for the inspector. If you open up this class you will see that it extends from MaterialEditor. On the material you can simply set the keywords by passing them as a string array to the shaderKeywords property of the material. At some stage in the future we think it makes sense to convert Unity’s built in materials to be more like this so we don’t have a large number of shaders on the back end, but instead have some nice defines for enabling normal mapping and the like and a sexy inspector for them. Not sure when we will get around to this though.

One shader with defines for Texture1 And Texture2

I hope you enjoy the new features and have some time to experiment with them.


Answers to some questions from the comments:

But how do these keywords work? Are shaders recompiled to several version depending on the keyword combinations?
In the background this is how they work, we compile a shader for each valid combination. This means that shader compilation will take longer, but it also means less shaders to maintain. Kuba and I talk about how it works in this presentation we gave last year (as well as lots of other things):

yeah same question as VALENTIN, does the shader perform the same with 1 or 2 textures ( we pay for 2 even if we chose 1 ), is it as expensive as a conditional ?
We currently have a known bug with the shader compiler where doing texture sampling in conditionals is a bit funky, we will fix this. When it’s fixed it will perform better with one sample then two.

The shader replacement behaves as the existing shader replacement and there is only minimal documentation changes which can be seen here: The new custom material editor feature is documented in these locations: and


17 replies on “4.1 Graphics Features”

[…] in a custom way. There's a good blog post that describes how to use them. Another new feature is support for shader replacement, not just for post processing as it used to be but also for the whole scene (including […]

Thank you for this cool blog post. Now I see!
I was waiting a long time for such a feature, so this is really motivating.

What about an easy and detailed blog post about native and wrapper C/C++ plugins for Unity and how to use them on Windows ?
This is still the Holy Grail for most professional developers ( I know ).

[…] edition features alternate artwork and four bonus tracks. 2011 release, the sixth studio album from the R&B […]

[…] diva and international superstar. MDNA, the long-awaited follow-up to her 2008 album Hard Candy, features production from Benny Benassi, The Demolition Crew, William Orbit, Martin Solveig and others. […]

I feel this is one of the most important information for me. And i’m glad studying your article. But should observation on some common issues, The website style is perfect, the articles is really great : D. Excellent activity, cheers

[…] 此外,Unity 4.1版本在图形的着色和渲染方面有了大幅度的提升,详情可参看 Tutorial。 […]

Hello there, simply became aware of your blog through Google, and found that it is really informative. I’m gonna watch out for brussels. I will appreciate if you happen to continue this in future. Lots of people can be benefited out of your writing. Cheers!

@Valentin & Erik:
Unity compiles multlple vertex & pixels shader cominations using #ifdefs so there is no runtime cost beyond increased memory consumption of all the shader combinations.

These new features are cool and all, but the documentation for them (and even old features) is lacking:
– SceneView class is not documented
– “Rendering with replacement shaders” link in docs point to empty page:

I understand the possible hype of new features being brought in, but please provide documenation for us to actually be able to use it in more efficient way than trial&error guessing ;)

Very nice, but I think what’s really needed with respect to shaders is a solid, visual graph editor that supports all features that shader lab offers. Programming game logic is already challenging enough, I don’t think that most people will have the time to learn the shader lab syntax as well (me included).

@Valentin, @Erik, @Beck: indeed, this will internally compile shader into multiple variants. We’ve already been doing this for lighting & shadows; and you could do this yourself, but the options (“keywords”) could only be turned on & off globally. Now they can be set on a per-material basis.

The unity rendering pipeline talk from Unite 2012 talks about this (and other things): and

It would be nice to have clarification from the author, but I would assume that the shader keywords function as the first answer in this post indicates:

It will compile permutations for every combination of keywords. This means that keywords will generate a number shaders exponential to the number of keywords. And this would also mean that indeed, toggling off functionality will be less expensive than if the functionality was toggled on.

yeah same question as VALENTIN, does the shader perform the same with 1 or 2 textures ( we pay for 2 even if we chose 1 ), is it as expensive as a conditional ?

But how do these keywords work?
Are shaders recompiled to several versions depending on keywords combinations?

Nice, thanks for helping me keep up. More like this, please. For example, I hear there’s improvements to Mecanim. Could we drill down into that?

Sweet. That would reduce dozens of variants needed of my skin shader to just one. Too bad I have to keep compatibility with unity 3.5 for people who bought it.

Comments are closed.