Realistic Glass in Three.js with MeshPhysicalMaterial

This article explains how to create highly realistic glass materials in Three.js using the advanced features of MeshPhysicalMaterial. You will learn how to configure key properties such as transmission, thickness, index of refraction (IOR), and roughness to simulate the physical behavior of light passing through solid glass objects.

To achieve a realistic glass effect in Three.js, you must use MeshPhysicalMaterial rather than standard materials. This material extends MeshStandardMaterial by adding physically-based rendering (PBR) properties specifically designed for transparent, reflective, and refractive surfaces.

Key Properties for Realistic Glass

To make a material look like glass, you need to adjust several specific properties:

Implementation Code Example

Here is a practical code configuration for a highly realistic glass material:

import * as THREE from 'three';

// Create the glass material
const glassMaterial = new THREE.MeshPhysicalMaterial({
    color: 0xffffff,            // Base color of the glass
    transparent: true,          // Enable transparency
    transmission: 1.0,          // Maximum light transmission
    opacity: 1.0,               // Base opacity (keep at 1.0 when using transmission)
    roughness: 0.05,            // Low roughness for a smooth, shiny surface
    metalness: 0.0,             // Glass is non-metallic
    ior: 1.5,                   // Index of Refraction for standard glass
    thickness: 1.2,             // Simulates the physical volume of the object
    specularIntensity: 1.0,     // Intensity of the specular reflection
    specularColor: new THREE.Color(0xffffff),
    clearcoat: 1.0,             // Extra glossy layer on top
    clearcoatRoughness: 0.1     // Roughness of the clearcoat layer
});

// Apply the material to a geometry
const geometry = new THREE.IcosahedronGeometry(2, 3);
const glassMesh = new THREE.Mesh(geometry, glassMaterial);
scene.add(glassMesh);

The Importance of Environment Maps

Glass does not look realistic in a dark or empty scene because it relies entirely on reflecting and refracting its surroundings. To make your glass look convincing:

  1. Add an Environment Map (HDRI): Use an EXRLoader or RGBELoader to load a high-dynamic-range environment map, and set it as the scene’s background and environment:

    scene.environment = hdrTexture;
  2. Enable Shadow Map Support: Glass objects should cast and receive shadows for realistic depth. Ensure your renderer has shadow maps enabled, and set glassMesh.castShadow = true and glassMesh.receiveShadow = true.