Search Unity

Physically Based Shading in Unity 5: A Primer

October 29, 2014 in Technology | 6 min. read
Topics covered
Share

What is Physically Based Shading? Physically Based Shading (PBS for short) simulates the interactions between materials and light in a way that mimics reality. PBS has only recently become possible in real-time graphics. In situations where lighting and materials need to play together intuitively and realistically, it's a big win.

The idea behind Physically Based Shading is to create a user friendly way of achieving a consistent, plausible look under different lighting conditions. It models how light behaves in reality, without using multiple ad-hoc models that may or may not work.

To do so it follows principles of physics, including energy conservation (meaning that objects never reflect more light than they receive), Fresnel reflections (all surfaces become more reflective at grazing angles), and how surfaces occlude themselves (what is called Geometry Term), among others.

Unity 5 includes what we call Standard Shader, which puts together a full PBS model and makes it easily accessible to Unity users. The Standard Shader is designed with hard surfaces in mind (which are also known as “architectural” materials), and can deal with most real world materials like stone, ceramics, brass, silver or rubber. It will even do a decent job with non-hard materials like skin, hair or cloth.

What about “my” content?

Keep in mind that Physically Based Shading doesn’t necessarily mean “realistic”, and it doesn’t mean it will dictate how the game (or the assets) look by imposing limitations. It adapts to different styles and esthetics ranging from accurately scanned- , to traditionally photographed-, to hand-painted textures.

For you guys making a flat-lit 2D sprite based game - PBS is not on the top of your need-to-have list. But… if you want to play with PBS and get the best out of the Standard shader we have a few tips for you!

Let’s dive in!

When thinking about lighting in Unity 5, it is handy to divide concepts into what we called the Context, which is information that comes from Unity itself and the Content, which is the information that is authored by you directly.

The Context

When lighting an object it is important to have an understanding on what the environment around the object looks like. Unity has classically had helpers, like light probes, that would be able to sample the diffuse lighting in a location. In Unity 5 we go much further:

Covering the whole range

HDR information is one important element for PBS. For instance, it helps to have information of environments where the sun can be ten times brighter than a blue sky. Unity 5 has a new native pipeline for HDR formats, you can just import .hdr and .exr images directly.

Adding shine

Reflection probes represent the reflections that exist at a certain location. There is one by default in a scene in Unity 5 (which you can look at in Edit->Scene Render Settings->Default Reflection). That reflection can be custom or depend purely on the sky and have no location.

You can, of course, create your own reflection probes. Just go to GameObject->Light->Create Reflection probe.

You’ll get something that looks like the image to the right:

You can then just drag it to whatever location in the scene and it’ll take care of getting information on what the surroundings look like.

Every reflection probe has an area of influence (that shows as a yellow box around the probe). Objects inside that box will pick their reflection data from the probe.

More per-pixel:

Thanks to Unity 5’s dynamic GI, light probes now also contain indirect light bounces, which the Standard shader applies per pixel. Normal maps now look great whether or not light is hitting them directly.

In this scene there is a single light pointing downwards, the sides and bottom of the barrel get no contributions from directional lighting.

Dynamic GI

Global illumination is an important part of the context that’s needed for PBS. To get a comprehensive overview of how it will work in Unity 5 nothing better than to check our blogpost on Dynamic GI

Color Space

PBS and the Standard shader work both in Linear and Gamma modes. HDR encoding, the data in reflections probes and the rest of the content will adapt to the color space you choose. But you should keep in Linear space whenever possible for the most correct (and usually most pleasing) visual results.

The Content

Content is the data that is directly authored by you. The Standard shader does bring a few changes to the traditional Unity material workflow that we hope you will like.

The Material Editor

(as introduced in an earlier blogpost)

The Standard shader introduces a new material editor. The new editor tries to make it easier to work with PBS materials than what it was with non-PBS materials before.

The editor is more compact now, with all possible options for the material there, from the get go. No need to choose a different shader to change texture channels, no more “texture unused, please choose another shader” messages. No more changing shader to change the blending mode.

You have a number of texture slots, which are not mandatory, any slot that is left empty will have its code optimised away so you don’t have to worry about it. Unity will take whatever data you put on the editor and create the right code to make it run at maximum efficiency.

Tip: You can Ctrl+click on textures for a large preview, which will also let you check the contents of the color  and alpha channels separately!

Lighting as you would expect

Of course this whole PBS talk also combines with Unity 5’s dynamic GI workflow, the GI system is totally aware of the way the Standard shader behaves and takes that into account when lighting a scene.

The combination of PBS and Enlighten GI make it possible to change the lighting conditions of an entire scene quickly, and get results that make sense!

For this village, you will notice that the last shot has a different lighting setup than the scene with which we opened this post. Still objects look solid and deep, everything just falls in place. That’s exactly what the physically-based shading magic is all about. Once materials are built taking PBS into account, they become completely independent of the lighting conditions, it actually is a ton less work to work with PBS.

That’s why we love it, and that’s why we think you’ll love it too.

viking_village2

The Viking Village will be invading an Asset Store near you when 5.0 ships!

Next we’ll be digging into how exactly material channels are put together, tips and hints on how to author textures and a lot more! Stay tuned in you are interested on in-depth asset creation details!

October 29, 2014 in Technology | 6 min. read
Topics covered