Compiler1 min read

PBR Module

Physically based rendering structs and BRDF helpers.

Reading Time
1 min
Word Count
139
Sections
10
Try It Live

Turn the guide into code

Take the key idea from this page into the playground and validate it in a real shader instead of leaving it as theory.

Open Playground

Physically based rendering structs and BRDF helpers.

Import

bwsl
import PBR

This module imports: Math.

Usage Examples

Direct Light for a Material

Use PBR when you want a consistent metallic/roughness lighting model instead of manually combining diffuse and specular terms in every shader.

bwsl
import PBR
PBRMaterial mat;
mat.albedo = resources.baseColor;
mat.roughness = resources.roughness;
mat.metallic = resources.metallic;
mat.ao = resources.ambientOcclusion;
float3 N = normalize(input.normal);
float3 V = normalize(resources.cameraPosition - input.worldPos);
float3 L = normalize(resources.lightPosition - input.worldPos);
float3 radiance = resources.lightColor * resources.lightIntensity;
float3 color = PBR::calculateDirectLighting(mat, N, V, L, radiance);
output.color = float4(color * mat.ao, 1.0);

Custom BRDF Assembly

Use the lower-level BRDF helpers when the engine has a custom light loop but you still want GGX distribution, Smith geometry, Fresnel, and Lambert diffuse to match the rest of the renderer.

bwsl
import PBR
float3 N = normalize(input.normal);
float3 V = normalize(resources.cameraPosition - input.worldPos);
float3 L = normalize(resources.lightDir);
float3 H = normalize(V + L);
float3 F0 = lerp(float3(0.04), resources.albedo, resources.metallic);
float3 F = PBR::fresnelSchlickUE4(max(dot(V, H), 0.0), F0);
float3 specular = PBR::cookTorranceSpecular(N, V, L, F, resources.roughness);
float3 diffuse = PBR::lambertianDiffuse(resources.albedo) * (1.0 - resources.metallic);
output.color = float4((diffuse + specular) * resources.lightColor, 1.0);

Constants

None.

Structs

PBRMaterial

bwsl
struct PBRMaterial {
float3 albedo;
float roughness;
float metallic;
float ao;
};

Enums

No enums found.

Functions

fresnelSchlick

Fresnel-Schlick Approximation

bwsl
fresnelSchlick :: (float cosTheta, float3 F0) -> float3

fresnelSchlickUE4

UE4 Optimized Fresnel

bwsl
fresnelSchlickUE4 :: (float VdotH, float3 F0) -> float3

distributionGGX

GGX/Trowbridge-Reitz Normal Distribution Function

bwsl
distributionGGX :: (float NdotH, float roughness) -> float

geometrySchlickGGX

Smith's Geometry Function (Schlick-GGX)

bwsl
geometrySchlickGGX :: (float NdotV, float roughness) -> float

geometrySmith

bwsl
geometrySmith :: (float NdotV, float NdotL, float roughness) -> float

cookTorranceSpecular

Complete Cook-Torrance Specular BRDF

bwsl
cookTorranceSpecular :: (float3 N, float3 V, float3 L, float3 F, float roughness) -> float3

lambertianDiffuse

Lambertian Diffuse

bwsl
lambertianDiffuse :: (float3 albedo) -> float3

calculateDirectLighting

Complete Direct Lighting Calculation

bwsl
calculateDirectLighting :: (PBRMaterial mat, float3 N, float3 V, float3 L, float3 radiance) -> float3

3 source helpers omitted because public docs only describe the current explicit resources block model.

Source

Generated from compiler-reference/modules/PBR_module.bwsl.