Skip to content

Conversation

@willeastcott
Copy link
Contributor

@willeastcott willeastcott commented Dec 21, 2025

Adds a dedicated isotropic GGX specular shader path for non-anisotropic materials, improving performance and code clarity. Previously, all GGX specular used the anisotropic implementation even when anisotropy was disabled.

Changes

  • Added new lightSpecularGGX.js shader files (GLSL and WGSL) implementing isotropic GGX
  • Introduced LIT_ANISOTROPY shader define to control which specular path is used
  • Added useAnisotropy option to material options builders
  • Updated shader include logic to use isotropic GGX when anisotropy is not needed

Technical Details

New Isotropic GGX Implementation

// Isotropic GGX (glTF 2.0 compliant)
float calcLightSpecular(float gloss, vec3 worldNormal, vec3 viewDir, vec3 h, vec3 lightDirNorm) {
    float roughness = max((1.0 - gloss) * (1.0 - gloss), 0.001);
    float alpha = roughness * roughness;

    float NoH = max(dot(worldNormal, h), 0.0);
    float NoV = max(dot(worldNormal, viewDir), 0.0);
    float NoL = max(dot(worldNormal, -lightDirNorm), 0.0);

    // GGX Distribution
    float NoH2 = NoH * NoH;
    float denom = NoH2 * (alpha - 1.0) + 1.0;
    float D = alpha / (PI * denom * denom);

    // Smith G (height-correlated)
    float alpha2 = alpha * alpha;
    float lambdaV = NoL * sqrt(NoV * NoV * (1.0 - alpha2) + alpha2);
    float lambdaL = NoV * sqrt(NoL * NoL * (1.0 - alpha2) + alpha2);
    float G = 0.5 / max(lambdaV + lambdaL, 0.00001);

    return D * G;
}

Shader Path Selection

#ifdef LIT_GGX_SPECULAR
    #ifdef LIT_ANISOTROPY
        #include "lightSpecularAnisoGGXPS"  // Full anisotropic path
    #else
        #include "lightSpecularGGXPS"       // New optimized isotropic path
    #endif
#else
    #include "lightSpecularBlinnPS"         // Legacy Blinn-Phong
#endif

Anisotropy Detection

useAnisotropy is set to true only when:

  • enableGGXSpecular is enabled, AND
  • anisotropyIntensity > 0 OR anisotropyMap is set

Performance Benefits

The isotropic path avoids:

  • TBN matrix operations for anisotropic tangent/binormal calculation
  • Additional dot products (ToH, BoH, ToV, BoV, ToL, BoL)
  • Vector length calculations for anisotropic lambda terms

This benefits the common case where materials don't use anisotropy.

Files Changed

  • New: src/scene/shader-lib/glsl/chunks/lit/frag/lightSpecularGGX.js
  • New: src/scene/shader-lib/wgsl/chunks/lit/frag/lightSpecularGGX.js
  • src/scene/shader-lib/glsl/chunks/lit/frag/pass-forward/litForwardPostCode.js
  • src/scene/shader-lib/wgsl/chunks/lit/frag/pass-forward/litForwardPostCode.js
  • src/scene/shader-lib/glsl/collections/shader-chunks-glsl.js
  • src/scene/shader-lib/wgsl/collections/shader-chunks-wgsl.js
  • src/scene/shader-lib/programs/lit-shader.js
  • src/scene/materials/standard-material-options-builder.js
  • src/scene/materials/lit-material-options-builder.js

API Changes

New shader define: LIT_ANISOTROPY - set when material uses anisotropy

New material option: useAnisotropy - automatically determined based on material properties

Breaking Changes

None. Existing materials will automatically use the appropriate path based on their anisotropy settings.

Fixes #2869
Fixes #8295

Checklist

  • I have read the contributing guidelines
  • My code follows the project's coding standards
  • This PR focuses on a single change

@willeastcott willeastcott self-assigned this Dec 21, 2025
@willeastcott willeastcott added enhancement Request for a new feature performance Relating to load times or frame rate area: graphics Graphics related issue labels Dec 21, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a dedicated isotropic GGX specular shader implementation to optimize performance for materials that don't use anisotropy. Previously, all GGX specular materials used the anisotropic implementation even when anisotropy was disabled, which included unnecessary tangent-space calculations.

Key Changes:

  • New isotropic GGX shader files (GLSL and WGSL) with simplified calculations
  • New LIT_ANISOTROPY shader define to control which specular path is selected
  • Automatic detection of anisotropy usage in material options builders

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/scene/shader-lib/glsl/chunks/lit/frag/lightSpecularGGX.js New GLSL isotropic GGX specular implementation
src/scene/shader-lib/wgsl/chunks/lit/frag/lightSpecularGGX.js New WGSL isotropic GGX specular implementation
src/scene/shader-lib/glsl/collections/shader-chunks-glsl.js Registers new GLSL shader chunk
src/scene/shader-lib/wgsl/collections/shader-chunks-wgsl.js Registers new WGSL shader chunk
src/scene/shader-lib/glsl/chunks/lit/frag/pass-forward/litForwardPostCode.js Adds conditional logic to select between isotropic and anisotropic GGX paths, and updates reflection direction includes
src/scene/shader-lib/wgsl/chunks/lit/frag/pass-forward/litForwardPostCode.js Same conditional logic for WGSL version
src/scene/shader-lib/programs/lit-shader.js Adds LIT_ANISOTROPY shader define
src/scene/materials/standard-material-options-builder.js Sets useAnisotropy based on material properties
src/scene/materials/lit-material-options-builder.js Sets useAnisotropy to false (LitMaterial doesn't support anisotropy)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@willeastcott willeastcott merged commit 1aea944 into main Dec 21, 2025
7 checks passed
@willeastcott willeastcott deleted the iso-ggx branch December 21, 2025 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: graphics Graphics related issue enhancement Request for a new feature performance Relating to load times or frame rate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing Isotropic GGX Specular Implementation Implement non-anisotropic GGX shader chunks

3 participants