Rendering fixes

This commit is contained in:
luboslenco
2025-11-28 21:54:07 +01:00
parent 541938210c
commit 1bf22e09d3
25 changed files with 115 additions and 49 deletions
+16 -12
View File
@@ -28,9 +28,6 @@ const gbuffer0: tex2d;
#[set(everything)]
const gbuffer1: tex2d;
#[set(everything)]
const senvmap_brdf: tex2d;
#[set(everything)]
const senvmap_radiance: tex2d;
@@ -178,7 +175,7 @@ fun mip_from_roughness(roughness: float, num_mipmaps: float): float {
fun envmap_equirect(normal: float3, angle: float): float2 {
var phi: float = acos(normal.z);
var theta: float = atan2(normal.x, -normal.y) + PI + angle;
var theta: float = atan2(-normal.y, normal.x) + PI + angle;
return float2(theta / PI2, phi / PI);
}
@@ -201,6 +198,16 @@ fun envmap_sample(lod: float, coord: float2): float3 {
return sample_lod(senvmap_radiance4, sampler_linear, coord, 0.0).rgb;
}
// https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
fun env_brdf_approx(specular: float3, roughness: float, dotnv: float): float3 {
var c0: float4 = float4(-1.0, -0.0275, -0.572, 0.022);
var c1: float4 = float4(1.0, 0.0425, 1.04, -0.04);
var r: float4 = c0 * roughness + c1;
var a004: float = min(r.x * r.x, exp((-9.28 * dotnv) * log(2.0))) * r.x + r.y;
var ab: float2 = float2(-1.04, 1.04) * a004 + r.zw;
return specular * ab.x + ab.y;
}
fun deferred_light_frag(input: vert_out): float4 {
// normal.xy, roughness, metallic/matid
var g0: float4 = sample_lod(gbuffer0, sampler_linear, input.tex, 0.0);
@@ -236,7 +243,6 @@ fun deferred_light_frag(input: vert_out): float4 {
var p: float3 = get_pos(constants.eye, constants.eye_look, normalize(input.view_ray), depth, constants.camera_proj);
var v: float3 = normalize(constants.eye - p);
var dotnv: float = max(0.0, dot(n, v));
occ = lerp(1.0, occ, dotnv); // ao fresnel
// Envmap
@@ -251,10 +257,8 @@ fun deferred_light_frag(input: vert_out): float4 {
envl = envl / PI;
var reflection_world: float3 = reflect(-v, n);
// var lod: float = mip_from_roughness(roughness, float(constants.envmap_num_mipmaps));
// var prefiltered_color: float3 = sample_lod(senvmap_radiance, sampler_linear, envmap_equirect(reflection_world, constants.envmap_data.x), lod).rgb;
var lod: float = mip_from_roughness(roughness, 5.0);
var lod0: float = floor(lod);
var lod1: float = ceil(lod);
@@ -266,11 +270,11 @@ fun deferred_light_frag(input: vert_out): float4 {
envl.rgb = envl.rgb * albedo;
// Indirect specular
// var env_brdf: float2 = senvmap_brdf[uint2(roughness * 255.0, (1.0 - dotnv) * 255.0)].xy;
var env_brdf: float4 = senvmap_brdf[uint2(uint(roughness * 255.0), uint((1.0 - dotnv) * 255.0))];
// var env_brdf: float4 = senvmap_brdf[uint2(uint(dotnv * 255.0), uint(roughness * 255.0))];
// envl.rgb += prefiltered_color * (f0 * env_brdf.x + env_brdf.y);
// envl.rgb = envl.rgb + (prefiltered_color * (f0 * env_brdf.x + env_brdf.y));
envl.rgb = envl.rgb + prefiltered_color * env_brdf_approx(f0, roughness, dotnv) * 0.5;
//envl.rgb += prefiltered_color * (f0 * env_brdf.x + env_brdf.y) * 1.5;
envl.rgb = envl.rgb + (prefiltered_color * (f0 * env_brdf.x + env_brdf.y) * 1.5);
envl.rgb = envl.rgb * constants.envmap_data.w * occ;
var color: float4;
@@ -282,9 +286,9 @@ fun deferred_light_frag(input: vert_out): float4 {
color.rgb = color.rgb + g1.rgb; // materialid
//albedo = float3(0.0, 0.0, 0.0);
}
color.rgb = max3(color.rgb, float3(0.0, 0.0, 0.0));
color.a = 1.0; // Mark as opaque
return color;
}
+10 -7
View File
@@ -10,6 +10,9 @@ const sampler_linear: sampler;
#[set(everything)]
const radiance: tex2d;
#[set(everything)]
const noise: tex2d;
const PI: float = 3.14159265358979;
const PI2: float = 6.28318530718;
@@ -31,12 +34,14 @@ fun prefilter_envmap_vert(input: vert_in): vert_out {
}
fun rand(co: float2): float {
return frac(sin(dot(co.xy, float2(12.9898, 78.233)) % 3.14) * 43758.5453);
// return frac(sin(dot(co.xy, float2(12.9898, 78.233)) % 3.14) * 43758.5453);
var uv: float2 = frac(co * 12.34);
return sample(noise, sampler_linear, uv).r;
}
fun equirect(normal: float3): float2 {
var phi: float = acos(normal.z);
var theta: float = atan2(normal.x, -normal.y) + PI;
var theta: float = atan2(-normal.y, normal.x) + PI;
return float2(theta / PI2, phi / PI);
}
@@ -67,17 +72,15 @@ fun prefilter_envmap_frag(input: vert_out): float4 {
var i: int = 0;
while (i < int(constants.params.y)) {
var dir: float3 = normalize(lerp3(n, cos_weighted_hemisphere_direction(n, input.tex, float(i)), constants.params.x));
color.rgb = color.rgb + sample(radiance, sampler_linear, equirect(dir)).rgb;
var sampled: float3 = sample(radiance, sampler_linear, equirect(dir)).rgb;
sampled = min3(sampled, float3(10.0, 10.0, 10.0));
color.rgb = color.rgb + sampled;
//
i += 1;
//
}
color.rgb = color.rgb / constants.params.y;
// color.rgb = pow(color.rgb, float3(1.0 / 2.2, 1.0 / 2.2, 1.0 / 2.2));
color.r = pow(color.r, 1.0 / 2.2);
color.g = pow(color.g, 1.0 / 2.2);
color.b = pow(color.b, 1.0 / 2.2);
return color;
}
+1 -1
View File
@@ -33,7 +33,7 @@ fun world_pass_vert(input: vert_in): vert_out {
fun envmap_equirect(normal: float3, angle: float): float2 {
var phi: float = acos(normal.z);
var theta: float = atan2(normal.x, -normal.y) + PI + angle;
var theta: float = atan2(-normal.y, normal.x) + PI + angle;
return float2(theta / PI2, phi / PI);
}