When developing games and apps in Unity, graphics play a key role. One of the most important aspects of a game’s visuals is the use of materials, textures, and shaders. These three elements work together to create the visuals we see in the game, from the surface of objects to their lighting and shadows. In this article, we’ll take a detailed look at what materials, textures, and shaders are in Unity, and how to use them effectively to create quality graphics.
A material in Unity is an object that defines how the surface of a 3D object will look. A material defines things like color, reflection, transparency, and more. Each material is associated with a shader, which calculates how these properties will be displayed on the screen.
Material Basics
Materials in Unity are components that can be attached to objects.
Each material has several properties, such as:
Albedo (Primary Color) – This is the main color of the material’s surface. It is usually a texture or a solid color.
Metallic — a parameter that determines how the surface will reflect light, whether it resembles metal or not.
Smoothness — specifies how smooth the surface will be. A higher value makes the surface more reflective.
Normal Map — a normal map for adding small details to the surface, such as scratches or unevenness.
Emission — allows the material to "glow" in the dark, which is used to create neon or glowing effects.
Creating and configuring materials To create a new material in Unity:
Go to Assets -> Create -> Material.
In the window that appears, you can configure the color of the material, its reflections, gloss, and other parameters.
For more complex effects, you can add textures and connect shaders that determine the visual characteristics of the material.
Shader Graph and materials Unity has the ability to use Shader Graph, which allows you to create materials using a visual interface, without the need for programming. Shader Graph helps you create complex visual effects using nodes, making the process much easier for artists and designers.
Textures are images that are applied to the surfaces of objects. Textures define the appearance of a surface, such as brickwork, skin, water, clouds, and so on. Textures in Unity are used to add detail to objects and create realistic or stylized effects.
Texture Types
Diffuse Texture (Albedo Map) is a basic texture that is used to display the color of a surface.
Normal Map is a texture that modifies the normals of a surface, creating the illusion of detail without changing the geometry of the object. This allows you to add scratches, bumps, and other small details without increasing the polygon count.
Specular Map is a texture that controls how an object will reflect light based on its materials. It is used in more complex materials.
Bump Map — a texture that is used to create the effect of unevenness on a surface, like a normal map, but with a simpler way of processing.
Displacement Map — a texture that not only changes normals, but also actually moves the vertices of the geometry, which allows you to create more complex and detailed surfaces.
Emissive Map — a texture that makes an object emit light, creating the effect of glowing objects.
Using textures Textures are usually attached to materials through their properties. For example, you can add a texture to the Albedo property that will be responsible for the main color of the object. To add a texture, simply drag an image file (such as PNG or JPG) to the desired field of the material in Unity.
Managing textures
Unity also offers Atlases (texture atlases), which allow you to effectively use multiple textures on a single object.
It is important to configure Import Settings for textures correctly. For example, for normal maps, you need to specify that this is a normal map, and not a regular texture, so that Unity will process it correctly.
Shaders are programs that run on the graphics processing unit (GPU) and are responsible for calculations related to the visualization of objects. Shaders play a key role in the rendering process, determining how objects should look depending on various parameters (e.g. lighting, texture, material, etc.).
Shader Types
Vertex Shader - responsible for processing vertices. This shader processes the position, normals, and other data about the vertices of a 3D object.
Fragment (Pixel) Shader - responsible for calculating the color of each pixel of an object on the screen. It calculates how light falls on a surface, how textures are applied, and how various effects interact.
Surface Shaders are higher-level shaders that simplify the writing of shaders by automatically creating interactions with light sources and other objects in the scene.
Post-Processing Shaders - used to create effects such as blurs, color filters, or specular screens applied after the scene has been rendered.
Shader Example Let's create a simple shader that makes the surface of an object transparent with a smooth fade-in effect. This shader will use the alpha channel of the texture to control the transparency:
Shader "Custom/TransparentShader"
{
Properties
{
_MainTex ("Base Texture", 2D) = "white" {}
_Transparency ("Transparency", Range(0,1)) = 1
}
SubShader
{
Tags { "Queue" = "Overlay" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float _Transparency;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
half4 frag(v2f i) : SV_Target
{
half4 col = tex2D(_MainTex, i.uv);
col.a = col.a * _Transparency;
return col;
}
ENDCG
}
}
}
In this shader, we define the texture that is applied to the object and the transparency parameter, which controls the alpha channel. The lower the transparency value, the more transparent the object becomes.
Shader Graph Instead of writing shader code manually, you can use Shader Graph, a visual tool for creating shaders in Unity. It allows you to create shaders using nodes, which makes the process much easier and accessible even to artists without programming skills.
Materials, textures, and shaders are the basis of the visual component of any project in Unity. Effective use of these tools allows you to create realistic and vibrant visuals, as well as optimize the game for different platforms. Each of these elements plays an important role, and understanding how they interact with each other will help you create better and more impressive games.
Game: Perform tasks and rest cool. 2157 people play!
Play gameGame: Perform tasks and rest cool. 2569 people play!
Play gameShaders are an essential part of any graphics-intensive application. Traditionally, shaders in Unity were created using...
Read moreShaders in Unity are a vital part of the visual design of games and applications, and understanding their principles ope...
Read moreFind out if there is a limit on the number of apps a single developer can upload to Huawei AppGallery.
Read more