How to make glass material in Unity?

Creating Glass Material in Unity

Unity is a powerful game engine that allows developers to create 3D games and experiences. One of the most challenging aspects of game development is creating realistic materials that can mimic the look and feel of real-world materials like glass. In this article, we will explore how to create a glass material in Unity.

Understanding Glass Properties

Before we dive into creating a glass material, it’s essential to understand the properties of glass. Glass is a transparent, amorphous solid that is made up of a mixture of silicon dioxide (sand), soda ash, and limestone. The properties of glass can be broken down into several key categories:

  • Transparency: Glass is transparent, meaning it allows light to pass through it.
  • Refractive index: Glass has a refractive index of approximately 1.5, which means it bends light as it passes through it.
  • Density: Glass is relatively dense, with a density of around 2.5 g/cm³.
  • Melting point: Glass has a melting point of around 1400°C (2550°F).

Creating a Glass Material in Unity

To create a glass material in Unity, we’ll use the following steps:

  1. Create a new material: In the Unity editor, go to Assets > Create > Material. Name the material "Glass".
  2. Add a shader: In the Material Inspector, click on the Shader dropdown menu and select Standard. This will give us a basic shader that we can modify to create a glass material.
  3. Add a Refraction shader: In the Material Inspector, click on the Shader dropdown menu and select Custom. Name the shader "Refraction". This shader will handle the refraction of light as it passes through the glass.
  4. Add a Refraction material: In the Material Inspector, click on the Material dropdown menu and select Add Material. Name the material "Refraction". This material will contain the Refraction shader.
  5. Add a Transparency material: In the Material Inspector, click on the Material dropdown menu and select Add Material. Name the material "Transparency". This material will contain the Transparency shader.
  6. Add a Refraction material to the Glass material: In the Material Inspector, click on the Material dropdown menu and select Add Material. Name the material "Glass". This material will contain the Refraction material.

Refraction Shader

The Refraction shader is responsible for handling the refraction of light as it passes through the glass. Here’s a breakdown of the shader:

  • Refraction: This is a basic shader that handles the refraction of light. It takes two parameters: n0 (the refractive index of the glass) and n1 (the refractive index of the air).
  • Bounce: This is a simple bounce shader that handles the reflection of light.
  • Diffuse: This is a simple diffuse shader that handles the scattering of light.

Here’s an example of the Refraction shader:

Shader "Custom/Refraction" {
Properties {
_N0 ("Refractive index of glass", 1.5) = 1.5
_N1 ("Refractive index of air", 1.0) = 1.0
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};

struct v2f {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};

sampler2D _Texture;
float4 _Texture_ST;

v2f vert(appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _Texture);
return o;
}

fixed4 frag(v2f i) : SV_Target {
fixed4 col = tex2D(_Texture, i.uv);
float n0 = _N0;
float n1 = _N1;
float2x2 refracted = float2x2(
refract(col.rgb, float4(0, 0, 1, 1), n0, n1),
refract(col.rgb, float4(1, 1, 1, 1), n0, n1)
);
col.rgb = refracted * col.rgb;
return col;
}
ENDCG
}
}
}

Transparency Material

The Transparency material is responsible for handling the transparency of the glass. Here’s a breakdown of the material:

  • Transparency: This is a basic material that handles the transparency of the glass.
  • Alpha: This is a simple alpha shader that handles the transparency of the glass.

Here’s an example of the Transparency material:

Shader "Custom/Transparency" {
Properties {
_Alpha ("Transparency", Float) = 1.0
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};

struct v2f {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};

sampler2D _Texture;
float4 _Texture_ST;

v2f vert(appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _Texture);
return o;
}

fixed4 frag(v2f i) : SV_Target {
fixed4 col = tex2D(_Texture, i.uv);
return col;
}
ENDCG
}
}
}

Creating a Glass Material in Unity

To create a glass material in Unity, we can combine the Refraction and Transparency materials. Here’s an example of how to do it:

Shader "Custom/Glass" {
Properties {
_N0 ("Refractive index of glass", 1.5) = 1.5
_N1 ("Refractive index of air", 1.0) = 1.0
_Alpha ("Transparency", Float) = 1.0
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};

struct v2f {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};

sampler2D _Texture;
float4 _Texture_ST;

v2f vert(appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _Texture);
return o;
}

fixed4 frag(v2f i) : SV_Target {
fixed4 col = tex2D(_Texture, i.uv);
float n0 = _N0;
float n1 = _N1;
float2x2 refracted = float2x2(
refract(col.rgb, float4(0, 0, 1, 1), n0, n1),
refract(col.rgb, float4(1, 1, 1, 1), n0, n1)
);
col.rgb = refracted * col.rgb;
return col;
}
ENDCG
}
}
}

Conclusion

Creating a glass material in Unity can be a challenging task, but with the right techniques and shaders, it can be achieved. In this article, we explored how to create a glass material in Unity using the Refraction and Transparency materials. We also provided an example of how to combine these materials to create a glass material. With practice and patience, you can create realistic glass materials in Unity that will enhance your game or application.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top