Physically Based Shading in Unity 5: A Primer

October 29, 2014 in Technology

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!

Comments (55)

Subscribe to comments
  1. Kjetil

    November 13, 2014 at 9:45 pm / 

    Will it be possible to adjust specular reflection based on normal angle to the camera. Usually done by using fresnel approximations. It’s quite important to be able to create realistic materials and I can’t see any input value that allows us to do that here. For example there is a big difference in how glass and chrome works. With glass the reflection becomes a lot stronger when you look at the surface at glancing angles, while it is almost invisible when you look directly towards the glass surface. On a chrome surface the angle of the surface compared to the camera matters less.

    I guess you expect us to add this ourselves? Not hard to do, I just thought it should be part of the standard shader.

    1. Rod Lopez

      November 14, 2014 at 12:14 pm / 

      Absolutely! The Standard shader has built-in features to control Fresnel reflections. No need to implement your own :)

      1. Kjetil

        November 14, 2014 at 9:18 pm / 

        So, how do you adjust the fresnel value? It does not seem like this is exposed in the standard shader?

        1. Kjetil

          November 14, 2014 at 10:10 pm / 

          I tried it out, and I see now. You lift the “angle of incidence curve” by using a brighter specular. It’s not just a basic specular multiplier/color.
          That works.

        2. Rod Lopez

          November 15, 2014 at 11:59 am / 

          It is not quite the brightness of the spec, but the roughness of the material which controls the way Fresnel kicks in. Smoother materials present more of the effect than rougher materials.

  2. Marco Siino

    November 8, 2014 at 11:53 am / 

    I have to change the way i create my 3d assets/models or texture to tale the benefits PBS? Or i just create the models and apply the texture ad i did before (ie with a diffuse map, a normal map and if its the case a specular map) ? There is a tutorial that explain what change from the 3d asset creation side so that pple that sells 3d models on the asset store knows right now how to prepare their models to being used with PBS instead of making their work not compatible with PBS?

    1. Rod Lopez

      November 8, 2014 at 12:43 pm / 

      Geometries are not likely to change at all, though there may be changes in your texture data. The reason is that PBS provides a lot of lighting information and it can disagree with lighting that’s baked in the textures. If you are going for realism then you may also want to adjust to what real materials are like, what specular ranges make sense and that sort of thing.
      We will be getting information out on how to work with data for PBS, both porting and creating. Keep an eye on the blog! ;)

  3. Mig

    November 6, 2014 at 4:04 am / 

    Can this system be customized like ShaderLab or is it a closed system? What if I want to pack data into a single texture (for example albedo, spec, occlusion and masks) for single texture read? Is that possible?

    1. Rod Lopez

      November 6, 2014 at 10:40 am / 

      This is written in ShaderLab (ShaderLab is part of the context that has become more powerful in 5.0), so it is completely open. In this demo, for instance, we cloned and modified the Standard shader to create a terrain shader that had other texture inputs.

      1. Mig

        November 6, 2014 at 4:35 pm / 

        Cool. Are there any examples we can look at to see how we can customize our own shaders with the new PBS stuff?

        1. Mig

          November 7, 2014 at 4:43 pm / 

          Oh, I guess I’m a bit slow… Just noticed this in the beta 12 release notes:

          Shaders: Surface shaders can use physically based shading now; the same as Standard shader uses.
          Use “Standard” lighting function, and “SurfaceOutputStandard” output structure.
          Do an #include “UnityPBSLighting.cginc” in your shader to get it.
          Default options require shader model 3.0, so add a “#pragma target 3.0” too.

          Jolly good.

      2. CosmosTbear

        November 25, 2014 at 7:19 pm / 

        Could you share this Standard Terrain shader?

        1. Rod Lopez

          November 26, 2014 at 3:57 pm / 

          Absolutely. It will be included in the project.

  4. Pedro

    November 5, 2014 at 10:17 pm / 

    that would be great added some features like image effects, at least not as many as there are in unity pro, but at least some basic effects to the free version of unity. :)

  5. Web76

    November 5, 2014 at 9:38 am / 

    I have a scene with ocean all around and a offshore vessel that is moving (floating).. the cranes and equipment on the ship is also dynamic..
    How should I set that scene up to get good lightning…
    Earlier I could bake the ship as Static, and then just uncheck the static box, and still have baked lightmaps on the ship even it was moving…. this is not possible now.. because I cannot move object when checked as lightmap static…

    1. Joachim Ante

      November 5, 2014 at 12:45 pm / 

      There is a little triangle. Where you can specify what type of static you want.
      Eg. you can specify lightmap static vs batching static.

      That said, the workflow you describe is also possible in unity 5, you simply go out of iterative mode and into on demand mode, thus you have a traditional bake button style workflow if that is what you prefer.
      (That said, the approach you describe with mark static / hit bake / revert mark static is a poor workflow and it is definately recommended to use the accurate static setting for your boat)

  6. dede

    November 3, 2014 at 5:15 pm / 

    tolong d bantu

  7. Jens Lindberg

    November 2, 2014 at 10:36 am / 

    Is Unity 5 targeting Q1 2015 for release?

  8. Yudhistira Vidhiatama

    November 2, 2014 at 5:24 am / 

    Nice post! I really can’t wait for the release of the Viking Village Asset and of course, Unity 5! The new PBS is awesome!

    The new material editor GUI looks nice, but I have one question, where can I place my roughness map on that material editor?

    1. Rod Lopez

      November 3, 2014 at 9:52 am / 

      Roughness you can plug into the alpha of the Specular map. Keep in mind that we use smoothness rather than roughness (the brighter the map the shinier the object)

  9. awss

    November 2, 2014 at 12:56 am / 

    Very realistic.

  10. CynicatPro

    October 31, 2014 at 11:23 am / 

    i have a question: is the new shader modular? as in can i write a parallax occlusion effect and a tessellation effect and mix/match? if so this would be awesome for the asset store as people could make individual shader parts instead of full shaders. more compatibility.

  11. Adam

    October 30, 2014 at 7:51 pm / 

    In lighting an outdoor scene like the VIking example you showed, What percentage of light in the scene should be handled by the Dynamic GI / Skyboxes vs Directional Lights to get a nice out door look? Finding a balance between the two has been a struggle for me.

  12. pikeman

    October 30, 2014 at 11:12 am / 

    What does this mean for writing my own vertex and fragment shaders for use in Unity? Anything?

    1. Rod Lopez

      October 30, 2014 at 1:51 pm / 

      You can write your own shaders exactly as you used to. What changes is that now you have extra information available to that shader. That’s this context that I was talking about.

  13. jdmulti

    October 30, 2014 at 8:56 am / 

    Btw, there is also a bug on the website. I read all the comments until the bottom, but for some reason I can’t scroll all the way up to the header of the website. The top part of the website is unreachable after reading all comments. I use Google Chrome

    1. jdmulti

      October 30, 2014 at 8:58 am / 

      Edit: Seams to happens after posting some bigger comments tho.

  14. jdmulti

    October 30, 2014 at 8:51 am / 

    it’s good to read that the viking scene is comming to the assets store. I think a lot of people could learn from that kind of scenes.

    I’ve also looked trough the videos about Unity 5.x and I was wondering if we can expect more demo scenes. I saw this transporter scene and the scene with a living room in a glass cilinder. Can we expect those to appear in the asset store as well? Would be cool to be honest.

    Last thing to notice with this PBS workflow. In some cases you want this whole sky to appear, have your materials nicely reflecting but in some cases you do not want this sky but you do want the reflections. What I mean is product visualisation, where materials appear to be realistic with reflections and GI, but the sky is pitch black or a gradient. For now what I’ve played with, they sky influences GI and reflections, but can I have all the goodies without the sky appearing?

    Further on, keep these blog posts comming, I enjoy reading them. :)

    1. Rod Lopez

      October 30, 2014 at 11:10 am / 

      Yes, you can absolutely use a different sky for calculating reflections and lighting than the one you’ll use as a background.
      You can also use that feature to take away the sun from the sky in reflections, so you can use analytical lights and not get double highlights, while keeping the sun nice and pretty in the skybox. The Doll project, for instance, does that.

      1. jdmulti

        October 30, 2014 at 1:40 pm / 

        Thanks for the answer. Btw, the doll project, is that the one showcasing the new Unity 5.x shaders? Will it be available in the asset store when unity 5.0 is released? Just curious =)

        1. Rod Lopez

          October 30, 2014 at 1:47 pm / 

          That’s the plan!

      2. Bit Barrel Media

        October 31, 2014 at 9:03 am / 

        “Yes, you can absolutely use a different sky for calculating reflections and lighting than the one you’ll use as a background.”
        How do you do that?

        1. Wolfos

          November 3, 2014 at 12:19 pm / 

          In render settings, change ‘default reflection’ from skybox to custom.

  15. Carlos R

    October 30, 2014 at 3:17 am / 

    Can’t wait to start using PBS in my games. One question though, is this fully supported targeting iOS? performance? Limitations?

  16. EdgarR

    October 29, 2014 at 9:33 pm / 

    The 5 version of Unity will have the browser of assets enhanced, or will be satanically slow as in version 4.5.5 ???

    Someone said that disabling some network devices improves the speed who you acess the assets, but this is a bug, so I wonder if it will continue the same hell to access the Unity Asset Store?

    1. Aras Pranckevičius

      October 29, 2014 at 9:49 pm / 

      If you’re talking about the in-editor asset store interface… I think it’s much, much smoother in 5.0.

      1. EdgarR

        October 29, 2014 at 9:57 pm / 

        Yes, I talked the internal web browser, this buggy it is very very slow, it takes 10 minutes for me to to access the page from a plugin, the 3D model, etc.

        But this bug can not be fixed in version 4.6 for example ??? The sooner the better, and should not be complex.

        1. Frank

          October 31, 2014 at 3:24 am / 

          The hardest problems of all are the one’s that “should not be complex.”

  17. Joshua4missions

    October 29, 2014 at 8:26 pm / 

    For you Blender users, this of it as rendering with the internal rendering engine and cycles. The internal engine can certainly render it fine, but cycles renders it in a more realistic manner with better lighting behavior.

  18. Augis

    October 29, 2014 at 7:56 pm / 

    What is native PBR’s relation to Marmoset toolback plugin?

  19. omar

    October 29, 2014 at 6:54 pm / 

    cool,but is there is a way to add a gloosy map on it

    1. Aras Pranckevičius

      October 29, 2014 at 7:12 pm / 

      Not sure what you mean. There’s a texture map for specular color (RGB) and glosiness aka smoothness (aka “inverse of roughness”) in alpha channel right there in the material.

  20. Pino

    October 29, 2014 at 6:13 pm / 

    This is cool, but I was expecting something along the lines of the UE4 Meterial Editor… any hope to see something like that happen?

  21. Wolfos

    October 29, 2014 at 6:12 pm / 

    Would love to get my hands on that viking village. There isn’t too much public info on PBR and most of it’s limited to very shiny materials. I struggled a bit trying to create convincing unfinished wood since that is very much not shiny so I’ll be studying your examples and I hope you guys could give some info on how the materials were created.

    1. Adam Buckner

      October 29, 2014 at 7:16 pm / 

      Look in the learn section of the Unity website, and watch the video on the standard shader. There is one point where the spec. maps on diffuse materials are shown. They’re all a very dark grey.

    2. Adam Buckner

      October 29, 2014 at 7:25 pm / 

      The standard shader, PBR lighting, GI and Enlighten will be part of the Unity 5 foundation. As far as I know, any discussion on where the “free/pro feature split” will be hasn’t happened yet. Rest assured, you will be able to make your game successfully in whatever version you choose.

  22. Sasa

    October 29, 2014 at 6:10 pm / 

    Great :)

  23. Korno

    October 29, 2014 at 3:47 pm / 

    Very cool. Is there any information about what will be available in free vs pro? It seems like this is replacing the current lighting system. How about mobile lightning?

    1. Stratos

      October 29, 2014 at 5:57 pm / 

      It would be so good if all these where in the free version. Fingers crossed :P

    2. Adam Buckner

      October 29, 2014 at 7:12 pm / 

      Global Illumination scales from desktop and console to mobile. Take a look at insist blog post:

      http://blogs.unity3d.com/2014/09/18/global-illumination-in-unity-5/

    3. Adam Buckner

      October 29, 2014 at 7:26 pm / 

      The standard shader, PBR lighting, GI and Enlighten will be part of the Unity 5 foundation. As far as I know, any discussion on where the “free/pro feature split” will be hasn’t happened yet. Rest assured, you will be able to make your game successfully in whatever version you choose.

      (Whoops! Replied to the wrong post!)

      1. AaronC

        October 30, 2014 at 4:16 am / 

        So hype us up with features only to announce they are for pro users.. I have to say I do tire of this strategy Unity Tech.

        1. Moose

          October 30, 2014 at 1:26 pm / 

          When did they last do that ???

  24. Akira_san

    October 29, 2014 at 3:01 pm / 

    Cool!

    1. CoolStoryMan

      October 30, 2014 at 3:52 am / 

      Story …

Comments are closed.