Course contentsShow
← Unity Game Development
Lesson 615 of 8289. Rendering and VisualsFree lesson

Creating Transparent and Refractive Glass Materials

Configure alpha blending and screen color sampling to build realistic glass materials with depth-based refraction and transparency effects.

In this lesson, you will learn how to configure alpha blending and screen-space color sampling in Shader Graph to simulate the way light passes through and bends within glass surfaces.

The Physics of Glass

To create realistic glass, we must move beyond standard opaque rendering. Glass is defined by two primary characteristics: transparency (allowing light to pass through) and refraction (bending light as it enters a denser medium).

In Shader Graph, you must first set your Material's Surface Type to Transparent. Unlike opaque objects that overwrite the depth buffer, transparent objects use the Alpha channel to determine how much of the underlying scene color is visible.

Implementing Refraction via Screen Color

Refraction occurs when the light path is distorted by the object's surface normals. To achieve this, we use the Scene Color node. This node samples the rendered scene from the perspective of the camera, effectively "grabbing" what is behind the object.

  1. Distortion: Connect a Normal Vector node to a Transform node (converting from World to Tangent space). Use this to offset the Screen Position UVs. By multiplying this offset by a small factor (the Refraction Strength), you cause the Scene Color to look shifted.
  2. Sampling: Feed these distorted UVs into the UV input of the Scene Color node.
  3. Output: Plug the resulting color into the Base Color slot of your Master Node.

Controlling Transparency

Transparency is managed through the Alpha slot. For glass, a constant value is rarely realistic. Use a Fresnel Effect node to increase opacity at grazing angles (the edges of the glass) while keeping the center more transparent. This simulates the behavior where glass looks more reflective and less clear when viewed from the side.

// Concept: Logic flow for a Refractive Node
float2 refractionUV = ScreenUV + (Normal * RefractionStrength);
float4 sceneColor = SampleSceneColor(refractionUV);
float fresnel = FresnelEffect(Normal, ViewDir, Power);
// Apply alpha based on fresnel for realistic edge-weighting

By balancing the Refraction Strength with an appropriate Alpha value, you create a convincing illusion of depth that reacts dynamically to the objects placed behind your glass meshes.

Key Takeaway: Realistic glass is achieved by combining Fresnel-based alpha masking with a distortion offset applied to the Scene Color sampling node to simulate light refraction.