Every beautiful looking game contains a variety of different surfaces: wood, metal, rubber, plastics, holograms, shiny alien artifacts & so much more! When using Unity, this visual jazz can be achieved by using the Unity Standard Shader to create materials with different kinds of look & feel.
In this tutorial, we will be experimenting with the Standard Shader & create a variety of Materials using this amazing shader. Also, we will touch upon the concept of Physically Based Rendering & see how it affects the materials created using the Standard Shader.
Download the Material Fun Project. 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.
- Create a new scene by selecting File\New Scene
- 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.
- Create a new material by selecting Assets\Create\Material
- Give this material a name. In our case, it is CoolMaterial.
- Assign this material to your sphere.
- Select the sphere
- Select the MeshRenderer component
- In the Materials drop-down, select the Element 0 and select CoolMaterial
If you have noticed, assigning this new material to our sphere made no visible change to its look & 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 & 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.
The standard shader comes with 4 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.
The Albedo parameter of the material defines the color & transparency of the material. There are two ways to change the color of the material:
- Edit the color & transparency by using the color picker. However, this will affect the whole object
- The recommended way is to assign a texture to the Albedo parameter. This texture will define the color & 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.
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 & give it a try.
Now set your rendering mode to Cutout. Notice that there is no change in the material’s output. This is because or texture does not have any transparent areas. In the Albedo parameter, select the cut texture & notice that the sphere has fully transparent & fully opaque areas now. If your texture has varying levels of transparency, you can set the cutoff value by using the Alpha Cutoff slider.
On to faded materials. Set the rendering mode to Fade. In the Albedo parameter, select the camo texture & reduce the transparency of the material by selecting the color picker & reducing the alpha value (alpha is controlled by the A value in the color picker).
If you want your material to have varying levels of transparency, set the rendering mode to Transparent & assign an albedo texture having varying levels of transparency. In our case, select the transparent texture and check out the result.
The metallic parameter defines amount of light reflected by the material. The metallic parameter can be controlled in the following ways:
- By modifying the Metallic & Smoothness sliders. The 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.
- By assigning a texture map to the Metallic parameter. When you assign a texture to the metallic property, both of the Metallic & 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 & smoothness values.
Just to try things out, set the rendering mode to Opaque, assign the camo texture to albedo and camo texture to metallic parameters both and check out the result.
Since the camo texture is an image with red & white color, 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 & Smoothness sliders? Just remove the metallic texture & pull the sliders to their max values.
Normal maps are special types of textures which allow you to add surface details like scratches & 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 the following 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 & the sphere appears to be an uneven surface.
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 farther away from the camera. Most of the times, heightmaps are used in conjunction with normal maps.
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++ :]
The emission parameter controls the color & intensity of the light emitted from the material. Once you set the emission values, the material will appear to 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 of these two values. If you supply the emission texture, the color values of the texture are used to define the emission color & intensity.
Detail Masks & 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 & 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 & 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 & indirect light falling on the objects and then renders the scene.
Check out this cool gif & notice the change in look and feel when the lighting conditions change:
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 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 & select Skybox\Procedural shader. This shader allows you to create a skybox material with defined sky parameters.
To apply the skybox to your scene, select Window\Lighting. In the lighting editor, select your material for the skybox.
The procedural skybox exposes a couple of parameters. Try playing around with these and notice the results in the scene.
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 & 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 & 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!
Where to go from here?
I hope that you enjoyed this tutorial about creating materials using the Standard Shader.
To learn more about materials & shaders, I suggest that you check out the following resources:
As always, keep checking this site for more tutorials. If you have any questions, feel free to reach out to me using the comments section below!