// A rasterized fragment's material (raster_material, which the material's
// shader function finishes): material textures
// sampled with the view's mip bias, normal and bump maps along screen derivatives,
// a normal map's scrolling layers where the frame's time puts them,
// roughness filtered by the geometry normal's variance. Camera geometry and
// shader-driven masked casters evaluate their material through this owner.
// Reads `view`, `frame`, object records, the material and the tier's maps
// through the program's material-map provider (material_maps_basic.wgsl,
// material_maps_extended.wgsl).
fn surface_tangent_frame(i:Fragment,front:bool)->mat3x3<f32> {
let f=pbr_tangent_frame(normalize(i.normal),i.tangent);
let side=select(-1.,1.,front);
return mat3x3(f[0]*side,f[1]*side,f[2]*side);
}
fn surface_roughness(rough:f32,n:vec3<f32>,i:Fragment)->f32 {
let geometry_normal=normalize((view.view*vec4(i.normal,0.)).xyz);
return pbr_filtered_roughness(rough,geometry_normal);
}
fn surface_geometry_normal(i:Fragment,front:bool)->vec3<f32> {
return side_normal(i.normal,front);
}
// The tangent frame a normal map's texel is taken on, the base's and the
// coat's alike, as KHR_materials_clearcoat takes the coat's on the base's
// (the Khronos glTF Sample Renderer 0686eb2's NormalInfo t and b,
// material_info.glsl 196–207): the authored tangent frame where the
// material is anisotropic, else three.js r185's derivative cotangent frame
// (TangentUtils), its axes scaled alike.
fn surface_map_frame(i:Fragment,front:bool)->mat3x3<f32> {
if material.anisotropy_strength>0. {
return surface_tangent_frame(i,front);
}
let face=select(-1.0,1.0,front);
let n=surface_geometry_normal(i,front);
let dx=dpdx(i.world);
let dy=dpdy(i.world);
let uv_dx=dpdx(i.uv);
let uv_dy=dpdy(i.uv);
// Three185.1 TangentUtils uses GLSL dFdy; its WebGPU builder lowers that
// to -dpdy. Negate BOTH world and UV Y derivatives. The cotangent frame
// is scaled by dot(cross(dx,-dy),n), positive wherever n faces the view,
// an accepted back's flipped normal included, so the UV-weighted sums of
// a and b below give the surface's own +T and +B on either side; face
// reverses them on a back, to (-T,-B,-N) as surface_tangent_frame, ray_map_frame and the Khronos
// glTF Sample Renderer 0686eb2 (material_info.glsl 164–171) give.
let a=cross(-dy,n);
let b=cross(n,dx);
let tangent=(a*uv_dx.x-b*uv_dy.x)*face;
let bitangent=(a*uv_dx.y-b*uv_dy.y)*face;
let scale=inverseSqrt(max(max(dot(tangent,tangent),dot(bitangent,bitangent)),0.0000001));
return mat3x3(tangent*scale,bitangent*scale,n);
}
fn surface_normal(i:Fragment,front:bool)->vec3<f32> {
let face=select(-1.0,1.0,front);
var n=surface_geometry_normal(i,front);
if normal_maps_enabled && (material.maps&MATERIAL_MAP_NORMAL)!=0u {
var mapped:vec3<f32>;
if (material.flags&MATERIAL_NORMAL_LAYERS)!=0u {
let phase=frame.animation_phase;
let first=textureSampleBias(relief_map,tex_sampler,material_normal_layer_uv(material.normal_layers[0],i.uv,phase),view.mip_bias);
let second=textureSampleBias(relief_map,tex_sampler,material_normal_layer_uv(material.normal_layers[1],i.uv,phase),view.mip_bias);
mapped=material_layered_normal(material,first,second);
} else {
mapped=material_mapped_normal(material,textureSampleBias(relief_map,tex_sampler,i.uv,view.mip_bias));
}
n=normalize(surface_map_frame(i,front)*mapped);
}
// Three185.1 MaterialNode.NORMAL selects normalMap OR ELSE bumpMap: a bump
// map is in effect only without a normal map (scene::materials::maps), so
// its bit alone selects it, with the normal-map stage disabled too.
if bump_maps_enabled && (material.maps&MATERIAL_MAP_BUMP)!=0u {
// Three185.1 BumpMapNode: forward samples along GLSL screen derivatives,
// with normalized position derivatives so authored bump does not scale
// with world-space pixel size. WebGPU lowers GLSL dFdy to -dpdy.
n=pbr_bump_normal(relief_map,tex_sampler,i.world,n,i.uv,material.bump_scale,face);
}
return n;
}
// The coat's normal (Surface.coat_normal): its clearcoat normal map's on
// the base map's frame (surface_map_frame), where the map is in effect and
// the normal-map diagnostic switch is on, else the geometry normal.
fn surface_coat_normal(i:Fragment,front:bool)->vec3<f32> {
if normal_maps_enabled && (material.maps&MATERIAL_MAP_COAT_NORMAL)!=0u {
return normalize(surface_map_frame(i,front)*material_coat_normal(material,material_coat_normal_texel(i.uv)));
}
return surface_geometry_normal(i,front);
}
// The anisotropy direction and strength around the mapped normal `n`.
fn surface_anisotropy(i:Fragment,front:bool,n:vec3<f32>)->vec4<f32> {
return pbr_resolve_anisotropy(n,surface_tangent_frame(i,front),material.anisotropy_strength,material.anisotropy_rotation,(material.maps&MATERIAL_MAP_ANISOTROPY)!=0u,material_anisotropy_texel(i.uv));
}
// The fragment's emitted light `emission`, none from a moving instance
// while the instance emission diagnostics layer is off.
fn raster_emission(i:Fragment,emission:vec3<f32>)->vec3<f32> {
if !instance_emission_enabled && (objects[fragment_object(i)].flags&OBJECT_STATIC)==0u {
return vec3(0.);
}
return emission;
}
// A rasterized fragment's material (shader_contract.wgsl's MaterialSurface):
// its record and maps at the fragment (material_texel_surface), the normal
// mapped (surface_normal), as the material's shader function then makes it
// (material_surface, with its context, material_surface_context). Every
// raster pass that reads the material, lit and caster alike, takes it from
// here; a material without a shader takes its record and maps unchanged.
// Each Extended map is sampled only where its factor or flag leaves it
// anything to scale.
fn raster_material(i:Fragment,front:bool)->MaterialSurface {
let mr=textureSampleBias(mr_map,tex_sampler,i.uv,view.mip_bias);
let emission=textureSampleBias(emission_map,tex_sampler,i.uv,view.mip_bias).rgb;
var coat=vec4(1.);
var coat_roughness=vec4(1.);
if material.coat>0. {
coat=material_clearcoat_texel(i.uv);
coat_roughness=material_coat_roughness_texel(i.uv);
}
var transmission=vec4(1.);
if (material.flags&MATERIAL_TRANSMISSIVE)!=0u {
transmission=material_transmission_texel(i.uv);
}
var thickness=vec4(1.);
if (material.flags&MATERIAL_TRANSMISSIVE)!=0u || material.diffuse_transmission>0. {
thickness=material_thickness_texel(i.uv);
}
let texels=MaterialTexels(material_base_color(i.uv,i.color),emission,mr,coat,coat_roughness,transmission,thickness);
let recorded=material_texel_surface(texels,surface_normal(i,front));
return material_surface(recorded,material_surface_context(i,front),material_shader_params(false));
}
// What the material's shader function evaluates a fragment at
// (material_context).
fn material_surface_context(i:Fragment,front:bool)->SurfaceContext {
return material_context(fragment_object(i),i.world,surface_geometry_normal(i,front),i.uv,i.color,i.custom,i.instance,front,i.clip.xy);
}