An Introduction to Materials & Standard Shader in Unity
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.
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 StartedTo 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 ShaderYay! 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
- 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
Rendering ModesThe 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.
AlbedoThe Albedo parameter of the material defines the color and transparency of the material. There are two ways to change the color of the material:
- Edit the color and 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 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.
MetallicThe metallic parameter defines the amount of light reflected by the material. The metallic parameter can be controlled in the following ways:
- 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.
- 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.
Normal MapNormal 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.
Height MapHeight 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.
OcclusionOcclusion 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++ :]
EmissionThe 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.
Detail Masks and Detail MapsDetail 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 RenderingUnity 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:
Experimenting with other shader typesApart from the Standard Shader, Unity also provides a couple of specialized shaders which can be used for different objects.
SkyboxSkybox 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. 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.
Toon ShaderThe 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!
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:
###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)
Questions about this tutorial? Get Live 1:1 help from Unity3d experts!