Global Illumination (SSGI 3.0) - ReForge-MMD/ray-mmd-reforge GitHub Wiki
Global Illumination (SSPT / RTGI & SSGI 3.0)
Ray-MMD ReForge features a unified next-generation real-time Global Illumination engine providing both Screen-Space Path Tracing (SSPT / RTGI 3.0) inspired by Marty McFly's qUINT RTGI and SSGI 3.0 inspired by Unreal Engine 5's Lumen Screen-Space Ray Tracing (SSRT). Operating natively on Direct3D 9 Shader Model 3.0, it delivers true room-filling indirect radiance, rich contact bounces, and physical color bleeding.
Visual Comparison
Disabled (GI_ENABLE 0) |
Mode 1: Accent (GI_ENABLE 1)* |
Mode 2: Indirect (GI_ENABLE 2) |
|---|---|---|
![]() |
![]() |
![]() |
*Note: In the Accent mode preview above, the SSGI+ controller slider was set to maximum to clearly demonstrate local bounce distribution.
Core Architecture & Key Innovations
Unlike classic screen-space gathers that march along flat 2D pixel offsets on the screen, SSGI 3.0 evaluates physical radiative transfer directly in 3D view-space:
graph TD
A[G-Buffer: Normal & Linear Depth] --> B[Duff 2017 Orthonormal Tangent Basis]
B --> C[Stratified Halton 2,3 Cosine Hemisphere Rays]
C --> D[Per-Pixel IGN Rotation & Step Jitter]
D --> E[Analytical Screen-Edge Clipping ClipRayToScreenEdge]
E --> F[3D Raymarching with Adaptive Thickness Gating]
F --> G[Hit Detected: Sample Radiance & Emissive]
F --> H[Ray Exits Viewport / Miss: Seamless IBL Fallback]
G --> I[Raw Indirect Radiance]
H --> I
I --> J[5x5 Outlier Rejection with Karis Luminance Weighting]
J --> K[Dyadic Stride 1.0 -> 2.0 A-Trous Bilateral Filter]
K --> L[Tangent-Plane Distance Edge-Stopping BilateralPlaneWeight]
L --> M[Composite & Rough Specular GI]
1. Tom Duff et al. (JCGT 2017) Orthonormal Tangent Basis
To cast rays within the upper hemisphere of a surface, a clean local coordinate frame (Normal, Tangent, Bitangent) is required. SSGI 3.0 utilizes Tom Duff's branchless algorithm to construct an orthonormal basis around the view-space normal $\mathbf{N}$: $$\mathbf{T} = \left( 1.0 + \text{sign} \cdot N_x^2 \cdot a,; \text{sign} \cdot N_x \cdot N_y \cdot a,; -\text{sign} \cdot N_x \right)$$ $$\mathbf{B} = \left( N_x \cdot N_y \cdot a,; 1.0 + \text{sign} \cdot N_y^2 \cdot a,; -N_y \right)$$ where $a = \frac{1.0}{1.0 + |N_z|}$ and $\text{sign} = \text{sign}(N_z)$. This formulation contains zero conditional branches and zero trigonometric functions, executing in just 8 ALU instructions on Shader Model 3.0.
2. Stratified Cosine-Weighted Hemisphere Sampling
Rays are distributed across the hemisphere using a low-discrepancy Halton (2, 3) sequence warped with exact Lambertian cosine weighting: $$\phi = 2\pi \cdot u_1, \quad \sin\theta = \sqrt{u_2}, \quad \cos\theta = \sqrt{1.0 - u_2}$$ $$\mathbf{L}{\text{local}} = \left(\cos\phi \sin\theta,; \sin\phi \sin\theta,; \cos\theta\right)$$ $$\mathbf{L}{\text{world}} = \mathbf{L}{\text{local}}.x \cdot \mathbf{T} + \mathbf{L}{\text{local}}.y \cdot \mathbf{B} + \mathbf{L}_{\text{local}}.z \cdot \mathbf{N}$$ This concentrates ray budgets where radiative transfer is mathematically greatest ($\cos\theta$), minimizing variance while preserving physical energy conservation.
3. Per-Pixel Direction Set Rotation & Decorrelation
To eliminate structural radial banding and starburst artifacts without exploding ray counts, the entire ray constellation is rotated around the normal per pixel using Interleaved Gradient Noise (IGN / Cranley-Patterson rotation) across $[0, 2\pi)$. Elevation jitter and step offset jitter are mapped to independent noise channels, completely eliminating grazing-angle self-intersections.
4. Analytical Screen-Edge Clipping (ClipRayToScreenEdge)
When a 3D ray projects outside the active camera frustum, SSGI 3.0 does not arbitrarily clip or zero out the contribution. Instead, an analytical clipping function calculates the exact boundary intersection parameter $t_{\text{clip}} \in [0, 1]$:
$$t_{\text{clip}} = \min\left(\frac{\text{boundary}_x - \text{origin}_x}{\text{delta}_x},; \frac{\text{boundary}_y - \text{origin}_y}{\text{delta}_y}\right)$$
The ray step is scaled to terminate precisely at the screen edge, where it smoothly blends into the surrounding HDR environment sky radiance (EnvLightMap / IBL). This eliminates hard black boundary lines and edge flickering during rapid camera motion.
5. Adaptive Depth Thickness Gating & Backface Rejection
To prevent light bleeding through thin objects (such as fingers, ears, anime hair strands, and thin architectural walls), SSGI 3.0 evaluates dynamic thickness thresholds:
$$\text{thickness} = \max\left(1.5,; 0.03 \cdot z_{\text{origin}} + 0.15 \cdot \text{rayDist}\right)$$
If a ray penetrates behind a surface deeper than thickness, or if the hit surface normal points in the same direction as the ray (backface hit), the intersection is rejected.
Spatial Reconstruction & Dyadic À-Trous Denoiser
SSGI 3.0 introduces a state-of-the-art multi-stage spatial reconstruction pipeline in Shader/SSGI/SSGI_Filter.fxsub:
1. Karis Luminance Outlier Rejection (5x5 Pre-Filter)
Before bilateral blurring, raw indirect radiance is pre-filtered using Brian Karis's highlight weighting: $$w_{\text{karis}} = \frac{1.0}{1.0 + \text{Luminance}(C) \cdot 0.20}$$ This suppresses specular fireflies, subpixel noise spikes, and over-bright emissive sparks without dimming true diffuse bounce radiance.
2. Dyadic Stride Progression ($1.0 \to 2.0$)
Replaced legacy odd-stride filtering with a dyadic $2^i$ progression. Stride 1.0 followed by stride 2.0 ensures that spatial kernels overlap smoothly, completely eliminating the 3x3 sub-lattice grid artifact common in older MMD shaders.
3. Tangent-Plane Distance Edge-Stopping (BilateralPlaneWeight)
Traditional bilateral filters compare raw scalar depth values, which mistakenly blurs away light on slanted floors or curved character faces. SSGI 3.0 calculates true point-to-plane distance against the center pixel's tangent plane: $$d_{\text{plane}} = \left| \mathbf{N}{\text{center}} \cdot (\mathbf{P}{\text{sample}} - \mathbf{P}{\text{center}}) \right|$$ $$w{\text{plane}} = \exp\left( -\frac{d_{\text{plane}}^2}{2 \cdot \sigma_d^2} \right)$$ This allows indirect light to flow smoothly across curved limbs and angled walls while preserving razor-sharp geometric silhouettes.
Operating Modes
Configured via GI_ENABLE in ray.conf:
Mode 3: RTGI / Screen-Space Path Tracing (GI_ENABLE 3 - Default)
- Focus: True screen-space path-traced room radiance, multi-bounce ambient transfer, and physical color bleeding inspired by Marty McFly's qUINT RTGI.
- Key Mechanisms:
- Monte Carlo Golden Angle Sampling: Cosine-weighted hemisphere path tracing with golden spiral rotation (
SSPT_GOLDEN_ANGLE = 2.39996323) and 1D Bayer dither stratification. Employs Marty McFly's projected-axis orthonormal tangent frame (BuildTangentMatrix) for seamless hemisphere orientation across steep surface curvature. - Progressive Quadratic Ray Stepping: Ray progression follows $\lambda = s \cdot \sqrt{s}$, balancing dense near-field resolution for sharp contact shadows with broad reach for room-scale diffuse bounce.
- Analytical Depth Thickness Gating: Depth thickness delta test ($\delta = (p_{\text{sample}} - p_{\text{ray}}) \cdot \text{invThickness}$) eliminates false light leaking through thin geometry (walls, fingers, hair).
- Secondary Bounce Feedback & Color Bleeding (
SSPT_BOUNCES 1): Propagates radiant color transfer from lit surfaces and emissive meshes onto surrounding geometry with a multi-bounce feedback loop (SSPT_BOUNCE_WEIGHT 0.35). - Viewport-Boundary Escaped Ray Radiance: Rays exiting screen boundaries without geometric intersection smoothly sample boundary environment sky radiance, preventing dark halos in close-ups and camera-adjacent angles.
- Physical Albedo-Driven Skin Multi-Bounce: Dynamic albedo interreflection curve ($1.0 + \frac{\text{albedo} \cdot 2.2}{\max(1.0 - \text{albedo} \cdot 0.5, 0.1)}$) and linearized cavity AO, preventing skin from appearing unnaturally dark or crushed in shadows.
- Dielectric Glass Bypass & Transmission: Surfaces tagged with
SHADINGMODELID_GLASSimmediately return0.0fdiffuse GI to eliminate milky fogging, while raymarching traversal cleanly skips glass hits viacontinueso rays transmit through windows and bottles without false occlusion shadows. - Edge-Preserving Cross-Bilateral Wavelet Denoiser: Separable À-Trous wavelet filter with Karis luminance outlier suppression and dyadic stride progression ($1.0 \to 2.0$), preserving geometric silhouettes and surface creases.
- Direct Solar Shadow Retention (
SSPT_SHADOW_RETENTION 0.40f): Scales indirect GI againstShadowMapSampwith a quadratic penumbra response curve, preserving deep cast shadows without allowing indirect bounce to wash out directional sun shadow contrast. - Deferred Post-GI Ambient Occlusion (
PostProcessAOComposite.fxsub): Runs after GI combine to uniformly darken the full composite (direct + IBL + GI), preventing GI bounce from bleaching contact shadows and crevices.
- Monte Carlo Golden Angle Sampling: Cosine-weighted hemisphere path tracing with golden spiral rotation (
Mode 2: Indirect / Multi-Bounce (GI_ENABLE 2)
- Focus: Cinematic, room-filling diffuse indirect illumination inspired by Unreal Engine 5 Lumen SSRT.
- Key Mechanisms:
- 150-Meter World Reach: Rays travel across scenes to gather radiance from distant walls, trees, and skies.
- 180-Degree Lambertian Emission: True physical hemisphere radiance integration.
- Multi-Bounce Compensation: Secondary and tertiary indirect light reflections between colored surfaces, saturating ambient shadows with natural color bleeding.
- Photometric Soft-Knee Compression: Boosts deep shadows while softly rolling off high-intensity highlights, preventing skin burnouts: $$\text{GI}_{\text{final}} = \frac{\text{GI}}{1.0 + \text{GI} \cdot 0.65}$$
Mode 1: Accent (GI_ENABLE 1)
- Focus: Focused local contact bounces, crisp contact shadows, and vibrant emissive lighting.
- Characteristics: Short world radius, strict ray angle validation, and zero background light wash.
- Best Used For: Bright daytime outdoor scenes, high-action dance animations, and stylized anime visuals where high local contrast is preferred.
Calibrated Quality Presets (GI_QUALITY)
Unified quality tiers shared across both SSGI and SSPT in ray.conf:
| Quality Level | Ray Count | Step Count | Total Steps / px | Target Hardware | Recommended Usage |
|---|---|---|---|---|---|
| Level 1 (Low) | 4 rays | 12 steps | 48 steps | Integrated GPUs / Steam Deck | Real-time viewport staging, mobile setups |
| Level 2 (Medium) | 8 rays | 12 steps | 96 steps | GTX 1060 / RTX 2060 / RX 6600 | Balanced high-speed production |
| Level 3 (High) | 12 rays | 14 steps | 168 steps | RTX 3070 / RTX 4070 / RX 7800 | High-density scenes, detailed foliage & interiors |
| Level 4 (Cinematic) | 16 rays | 16 steps | 256 steps | RTX 4080 / RTX 4090 / Studio rigs | Final offline animation export / 4K render |
SSPT / RTGI Parameter Reference (ray.conf)
| Parameter | Default | Description |
|---|---|---|
SSPT_SAMPLE_RADIUS |
16.0f |
Maximum path-tracing ray search distance in world space units. |
SSPT_Z_THICKNESS |
1.0f |
Analytical depth thickness threshold preventing light leaks through thin walls, fingers, and hair. |
SSPT_IL_AMOUNT |
2.0f |
Indirect lighting bounce intensity multiplier. |
SSPT_AO_AMOUNT |
1.0f |
Ambient occlusion darkening power curve. |
SSPT_SHADOW_RETENTION |
0.40f |
Direct sun shadow retention factor. Prevents indirect GI bounce from washing out deep directional shadows. |
SSPT_SKY_LIGHT_AMOUNT |
0.05f |
Minimum environment sky radiance weight for unoccluded/escaped rays. |
SSPT_BOUNCES |
1 |
Multi-bounce indirect reflection feedback loop (0: off, 1: on). |
SSPT_BOUNCE_WEIGHT |
0.35f |
Radiance contribution weight of secondary bounces. |
SSPT_FILTER_SENSITIVITY |
1.0f |
Sensitivity threshold of the cross-bilateral wavelet edge-preserving denoiser. |
Motion-Adaptive Temporal Denoising (SSGITemporalDenoise)
Ray-MMD ReForge incorporates a dedicated post-filter temporal reprojection denoiser for both SSGI and SSPT. Running after the final spatial wavelet passes, it stabilizes Monte Carlo noise and converges to a grain-free indirect radiance field:
- Motion-Adaptive Responsiveness: Evaluates instantaneous screen-space camera velocity (
pixelMotion = (uvCoord - prevUV) * ViewportSize). In static frames, alpha rests at0.05(95% history accumulation) for noise-free convergence. When rapid camera motion is detected, alpha dynamically scales up to0.35to prevent lighting trails. - Surface Normal Rejection: Evaluates bilateral normal alignment between current and reprojected geometry (
dot(curNormal, prevNormal)). Stale history across rotated surface curvature and silhouettes is rejected to eliminate false bleeding. - Continuous System-Elapsed Dithering: Ray marching dither patterns are driven by system elapsed time rather than animation playback time, ensuring noise patterns never freeze when animation playback is paused.
- Note on MMD Viewport vs. Final Render:
[!NOTE] If brief lighting trails are observed during manual camera interaction in the MMD interactive viewport at low framerates, this is completely normal.
- In the interactive viewport, the lighting immediately stabilizes into a clean static frame as soon as camera motion stops.
- On final image or video export (AVI / image sequence), frames render deterministically at 30/60 FPS, completely eliminating trails during camera movements and delivering a rock-solid, clean image.
Rough Specular GI (SSGI_SPECULAR_GI)
The denoised SSGI radiance field is automatically reused as a Fresnel($F_0$)-weighted rough specular indirect term. It fills colored indirect reflections on matte/rough materials (e.g. concrete, wood, rough metals, satin cloth) where mirror-only SSR misses and the IBL cubemap lacks local scene context:
- Enabled via
SSGI_SPECULAR_GI 1inray.conf(default). - Automatically disabled for skin, glass, and emissive materials.
- Dynamically controlled by material roughness and the SSGI gather cone angle.
Real-Time Control via ray_controller.pmx
All SSGI 3.0 parameters can be animated and adjusted live in MMD without reloading shaders:
| Morph Slider | Panel | Description |
|---|---|---|
SSGI+ |
Main | Boosts global illumination radiance multiplier (up to $3.0\times$). |
SSGI- |
Main | Smoothly attenuates global illumination strength down to zero. |
SSGIEmissiveBoost |
Other | Scales emissive mesh contribution to indirect light bounces. |
SSGIModeGain |
Other | Extra multiplier on top of the selected GI mode. |
SSGISkyLight |
Other | Weights HDR sky radiance gathered at ray termination (EnvLightMap IBL projection). |
SSGI Visibility Masking (SSGIVisibilityMap)
When GI_ENABLE > 0, Ray-MMD ReForge exposes a dedicated SSGIVisibilityMap tab in the MMD MMEffect window.
Purpose
- Prevent Unwanted Emitter Bleed: Anime eyes and stylized models often use slight emissive properties (
emissiveIntensity = 0.15) so pupils don't go pitch black in cast shadows. The SSGI visibility mask stops these emissive elements from acting as giant light bulbs that bleed bright bounce light onto the surrounding face. - Prevent Receiver Over-Brightening: Masked objects do not receive ambient indirect room illumination, preserving stylized anime color palettes.
How to Use
- Open the MMEffect window in MMD.
- Switch to the
SSGIVisibilityMaptab. - Select your model's eye or accessory subsets.
- Right-click and assign
Shadow/SSGI visibility 0.0.fx(or partial visibility presets0.1.fxto0.9.fx).


