Search Unity

4.1 Graphics Features

March 15, 2013 in Technology | 4 min. read
Topics covered
Share

Is this article helpful for you?

Thank you for your feedback!

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

 


March 15, 2013 in Technology | 4 min. read

Is this article helpful for you?

Thank you for your feedback!

Topics covered