× {{alert.msg}} Never ask again
Receive New Tutorials
GET IT FREE

An Introduction to Materials & Standard Shader in Unity

– {{showDate(postTime)}}

This is originally posted by the author on his blog, Marching Bytes. This version has been edited for clarity and some parts may appear different from the original post.


intro Every beautiful looking game contains a variety of different surfaces: wood, metal, rubber, plastics, holograms, shiny alien artifacts, and so on. To achieve the different look and feel of each kind of material, we can use [Unity](https://unity3d.com/), specifically, Unity Standard Sharer. In this tutorial, we will be experimenting with the Standard Shader to create a variety of materials. Also, we will touch upon the concept of Physically Based Rendering and see how it affects the materials created using the Standard Shader.

Getting Started

To help me explain this tutorial, I have used sample materials, which you can download here to help you understand the concepts better. This project contains some sample materials and the assets required to create those materials. Once you have the project open in Unity, open the Main scene and hit the play button. You should see a couple of beautiful looking spheres in your game window. Before we proceed, let’s go over a couple of concepts.
  • Shaders: Shaders are scripts which contain the algorithms that tell the computer how to render each pixel.
  • Materials: Materials define how a surface is to be rendered. This is achieved by selecting a shader for the material and then setting the public properties of the underlying shader.
  • Physically Based Rendering (PBR): PBR is a technique which mimics the interaction of light with different materials in a realistic way. In Unity, PBR is provided out-of-the-box when using the Unity Standard Shader.

Playing around with the Unity Standard Shader

Yay! It is time to create some beautiful materials.
  1. Create a new scene by selecting File\New Scene
  2. Create a new sphere by selecting GameObject\3D Object\Sphere
Note that the sphere that you created is white-ish in color. All newly created objects are assigned the default material by default. Let’s create a new material and assign it to our sphere.
  1. Create a new material by selecting Assets\Create\Material
  2. Give this material a name. In our case, it is CoolMaterial.
  3. Assign this material to your sphere.
  4. Select the sphere
  5. Select the MeshRenderer component
  6. In the Materials drop-down, select the Element 0 and select CoolMaterial
unity standard shader If you have noticed, assigning this new material to our sphere made no visible change to its look and feel. This is because our cool new material is set up at the defaults. Default settings == No oomph! It's time to liven things up! Select the CoolMaterial in the project explorer and inspect its properties in the inspector. The first drop-down selects the shader that we want to use for this material. Leave the shader selection at Standard.

Rendering Modes

The standard shader comes with four different rendering modes:
  • Opaque: The default value, is suitable for rendering opaque objects
  • Cutout: Allows you to render objects with either fully transparent or fully opaque areas
  • Fade: The level of transparency allows you to fade objects
  • Transparent: Allows you to render transparent objects such as glass, clear plastics, etc.
If you are as inquisitive as I am, you might have tried changing the rendering mode of the material but noticed no change in the output. This is because the rendering mode does not act on a material by itself but works based on the settings of the Albedo parameter.

Albedo

The Albedo parameter of the material defines the color and transparency of the material. There are two ways to change the color of the material:
  1. Edit the color and transparency by using the color picker. However, this will affect the whole object
  2. The recommended way is to assign a texture to the Albedo parameter. This texture will define the color and transparency of the material. Note that the transparency values will affect your material only if the Rendering Mode is set to one of the non-opaque values.
Assign a texture to the Albedo of your material by selecting the circle icon next to the Albedo label. unity standard shader Select the camo texture from the list of available textures. Notice that your sphere is covered with a jazzy pattern now! It is still possible to change the overall color tint of the material by modifying the color property. Go ahead and give it a try. unity standard shader Now set your rendering mode to Cutout. Notice that there is no change in the material’s output. This is because the texture does not have any transparent areas. In the Albedo parameter, select the cut texture and notice how the sphere has fully transparent and fully opaque areas now. If your texture has varying levels of transparency, you can set the cutoff value by using the Alpha Cutoff slider. unity standard shader On to faded materials. Set the rendering mode to Fade. In the Albedo parameter, select the camo texture and reduce the transparency of the material by selecting the color picker, and reducing the alpha value (alpha is controlled by the A value in the color picker). unity standard shader If you want your material to have varying levels of transparency, set the rendering mode to Transparent and assign an albedo texture with varying levels of transparency. In our case, select the transparent texture and check out the result. unity standard shader

Metallic

The metallic parameter defines the amount of light reflected by the material. The metallic parameter can be controlled in the following ways:
  1. By modifying the Metallic and Smoothness sliders:he metallic value defines how ‘metallic’ a material is. A metallic value closer to 1 will reflect more light. The smoothness value defines how smooth the material is.
  2. By assigning a texture map to the Metallic parameter: When you assign a texture to the metallic property, both of the Metallic and Smoothness sliders will disappear. The Red channel of the texture controls the metallic values of the material. The Alpha channel of the texture controls the smoothness of the material. Assigning a texture to the metallic parameter is useful when your material has areas of differing metallic and smoothness values.
Just to try things out, set the rendering mode to Opaque, assign the camo texture to albedo, and camo texture to both metallic parameters then check out the result. unity standard shader Since the camo texture is an image with red and white, the whole material took on the metallic look. In case you are wondering why, white is a combination of RGB values of 255 each. Since the metallic value is set based on the R channel, the whole material has the metallic value set to 1. Itching to achieve the same result by using the Metal and Smoothness sliders? Just remove the metallic texture and pull the sliders to their max values. unity standard shader

Normal Map

Normal maps are special types of textures which allow you to add surface details like scratches and grooves to your material. Of course, these details can also be added directly to the 3D model that you are using. However, creating these details on the 3D models results in a higher number of polygons, which require more computation power to render. If we use normal maps to render these details, the graphics rendering hardware can easily render the details at a very low computation cost. Normal maps are special kind of images, which can be produced using 3D modeling applications or by hand. These textures store the information required by Unity to decide how the surface reacts to light. For more details, refer to this link: http://docs.unity3d.com/Manual/StandardShaderMaterialParameterNormalMap.html For the purpose of this article, we have created a normal map texture named camo_normal. Assign this texture to the normal map field of the material. Notice how the white-colored area of the material is now bumped up and the sphere appears to be an uneven surface. unity standard shader

Height Map

Height mapping is a technique similar to normal mapping. However, this technique is more performance intensive compared to the normal mapping. Using heightmaps, it is possible to achieve a feel wherein the surfaces closer to the camera is more exaggerated as compared to the surface further away from the camera. Most of the times, heightmaps are used in conjunction with normal maps. unity standard shader

Occlusion

Occlusion maps are used to define the amount of indirect lighting incident on particular areas of your object. Occlusion maps are usually created in 3D applications based on the topology of the 3D model that this will be applied to. Occlusion maps are grayscale images, with white indicating areas that should receive full indirect lighting, and black indicating no indirect lighting. I know, I know. Occlusion maps are a bit difficult to wrap your head around. But, once you get the hang of it, it is quite easy to simulate realistic objects in your game. Beauty++ :]

Emission

The emission parameter controls the color and intensity of the light emitted from the material. Once you set the emission values, the material will appear to be lit up from the inside. Emission is enabled by setting the emission property to a value greater than 0. The emission values can be set by either supplying an emission color, setting up an emission texture, or by a combination these two values. If you supply the emission texture, the color values of the texture are used to define the emission color and intensity. unity standard shader

Detail Masks and Detail Maps

Detail Maps allow you to define secondary textures for your material. Detail Masks allow you to mask areas of the material where the detail maps would be applied. Detail maps and masks allow you to overlay secondary textures over the main textures, thus, allowing you to add fine-grained details to your material.

Physically Based Shading and Rendering

Unity applies Physically Based Shading (PBS) on all materials using the Standard Shader. PBS is an advanced lighting calculation model which allows the scene to appear more realistic under different lighting conditions. PBS calculates the sum total of all direct and indirect light falling on the objects; and then renders the scene. Check out this cool gif and notice the change in look and feel when the lighting conditions change: unity standard shader

Experimenting with other shader types

Apart from the Standard Shader, Unity also provides a couple of specialized shaders which can be used for different objects.

Skybox

Skybox is a special shader which can be applied to your scene to change the overall atmosphere. To create a skybox material, create a new material and select Skybox\Procedural shader. This shader allows you to create a skybox material with defined sky parameters. unity standard shader To apply the skybox to your scene, select Window\Lighting. In the lighting editor, select your material for the skybox. unity standard shader The procedural skybox exposes a couple of parameters. Try playing around with these and notice the results in the scene. unity standard shader

Toon Shader

The toon shader is a specialized shader which allows you to add a cartoonish look to the objects. Toon shader is provided as a part of the Unity Standard Assets and needs to be imported into your project separately. Select Assets\Import Package\Effects, and in the resulting window which opens, select Import. Now that you have the toon shader imported into your project, create a new material and select the Toon\Basic Outline shader. This shader allows you to define the outline of the object. Try experimenting with the other toon shader variants and see what you can come up with! unity standard shader

Where to go from here?

I hope that you enjoyed this tutorial about creating materials using the Standard Shader. To learn more about materials and shaders, I suggest that you check out the following resources: If you have any questions, feel free to reach out to me.
###Other tutorials you might be interested in: - [Procedurally Generated Nebulae in Unity3D](https://www.codementor.io/c_sharp/tutorial/procedural-generation-visual-rendering-unity3d) - [5 Best Practices Beginner Mobile Game Developers Must Know](https://www.codementor.io/ios/tutorial/mobile-ios-game-development-best-practices) - [How to Get Started with Animation Design](https://www.codementor.io/design/tutorial/learn-animation-design-classical-principles)


Author
Preet Minhas
Preet Minhas
5.0
Software Architect with experience in building cutting-edge AR, VR, mobile & web solutions
* 6+ years of experience in developing native iOS apps using Objective-C and Swift * 2+ years of experience in developing hybrid apps using Ionic framework * Expert in rapid API development...
Hire the Author

Questions about this tutorial?  Get Live 1:1 help from Unity3d experts!
Ricardo
Ricardo
5.0
Senior Software Engineer
Software engineer with 13+ years of professional experience. If you have a problem feel free to contact me, I always go above and beyond to try to...
Hire this Expert
Michael Urvan
Michael Urvan
5.0
C# .NET, Unity 3D, jQuery, AJAX, Javascript, HTML5, CSS3, Bootstrap, SQL, Entity Framework,Code First developer with 10+ yrs exp
Being a software developer is more than just learning to program in a computer language. I have been a developer for nearly two decades. I have...
Hire this Expert

Or Become a Codementor!

Live 1:1 help from expert developers

Codementor is your live 1:1 expert mentor helping you in real time.

comments powered by Disqus