Categories & Tags
Archive

4.1 Graphics Features

March 15, 2013 in Demos, Tutorials and Tips by

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): https://www.dropbox.com/s/1akw1kv0sby1let/Unite12_UnityRenderingPipeline.zip

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.

Documentation
The shader replacement behaves as the existing shader replacement and there is only minimal documentation changes which can be seen here: http://docs.unity3d.com/Documentation/Components/SL-ShaderReplacement.html. The new custom material editor feature is documented in these locations: http://docs.unity3d.com/Documentation/Components/SL-CustomMaterialEditors.html and http://docs.unity3d.com/Documentation/ScriptReference/MaterialEditor.html

 


Share this post

Comments (17)

Comments are closed.

15 Mar 2013, 5:39 am

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.

Victor
15 Mar 2013, 5:42 am

Amen. I still dream of the graph editor.

Indy
15 Mar 2013, 7:46 am

Very good idea with mini demos of new features!

15 Mar 2013, 9:57 am

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?

Valentin
15 Mar 2013, 10:01 am

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

Erik
15 Mar 2013, 12:50 pm

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 ?

15 Mar 2013, 1:40 pm

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: http://answers.unity3d.com/questions/228332/using-custom-keywords-in-cg-shaders.html

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.

16 Mar 2013, 1:01 am

@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): http://video.unity3d.com/video/6957514/unite-2012-the-unity and http://is.gd/UnityRenderingPipeline

Cascho
16 Mar 2013, 10:15 am

Also, still dreaming of a node based graph editor.

Alan
17 Mar 2013, 7:08 am

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).

Peter
17 Mar 2013, 9:37 am

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: http://docs.unity3d.com/Documentation/ScriptReference/Camera.SetReplacementShader.html

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 ;)

Joachim Ante
18 Mar 2013, 3:18 pm

@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.

18 Mar 2013, 9:42 pm

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!

19 Mar 2013, 3:26 am

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

Tim Cooper
20 Mar 2013, 3:18 am

Answered some of the questions at the end of the original blog post.

26 Mar 2013, 10:22 am

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 ).

24 May 2013, 6:04 am

I think the spam protection needs upgrading, guys :P

Leave a Reply

Comments are closed.