some refactoring

This commit is contained in:
Tuomas Katajisto 2026-04-05 12:59:08 +03:00
parent e6382913fa
commit a4a3c086e2
3 changed files with 3681 additions and 5893 deletions

View File

@ -172,7 +172,7 @@ get_low_res :: (width: s32, height: s32, max_dimension: s32 = 720) -> (s32, s32)
get_render_size :: () -> (s32, s32) { get_render_size :: () -> (s32, s32) {
w,h := get_window_size(); w,h := get_window_size();
// w, h = get_low_res(w,h, 480); // w, h = get_low_res(w,h, 480);
return w * 2, h * 2; return w, h;
} }
flip_y_if_plat :: inline (v: Vector2) -> Vector2 { flip_y_if_plat :: inline (v: Vector2) -> Vector2 {

File diff suppressed because it is too large Load Diff

View File

@ -13,16 +13,16 @@ layout(binding=0) uniform trile_vs_params {
out vec3 cam; out vec3 cam;
out vec3 to_center; out vec3 to_center;
out vec3 vpos; // The actual position; out vec3 vpos;
out vec3 ipos; // Trile space position; out vec3 ipos;
out vec4 fnormal; out vec4 fnormal;
out vec3 orig_normal; // Unrotated object-space normal, used for trixel lookup out vec3 orig_normal;
out vec3 trileCenter; out vec3 trileCenter;
out vec3 cv; out vec3 cv;
mat3 rot_x(float a) { float c=cos(a),s=sin(a); return mat3(1,0,0, 0,c,-s, 0,s,c); } mat3 rot_x(float a) { float c=cos(a),s=sin(a); return mat3(1,0,0, 0,c,-s, 0,s,c); }
mat3 rot_z(float a) { float c=cos(a),s=sin(a); return mat3(c,-s,0, s,c,0, 0,0,1); }
mat3 rot_y(float a) { float c=cos(a),s=sin(a); return mat3(c,0,s, 0,1,0, -s,0,c); } mat3 rot_y(float a) { float c=cos(a),s=sin(a); return mat3(c,0,s, 0,1,0, -s,0,c); }
mat3 rot_z(float a) { float c=cos(a),s=sin(a); return mat3(c,-s,0, s,c,0, 0,0,1); }
mat3 get_orientation_matrix(int ori) { mat3 get_orientation_matrix(int ori) {
int face = ori / 4; int face = ori / 4;
@ -46,8 +46,8 @@ void main() {
gl_Position = mvp * vec4(rotated + instance.xyz, 1.0); gl_Position = mvp * vec4(rotated + instance.xyz, 1.0);
fnormal = vec4(rot * normal.xyz, 0.0); fnormal = vec4(rot * normal.xyz, 0.0);
orig_normal = normal.xyz; // unrotated, for trixel lookup orig_normal = normal.xyz;
to_center = centre.xyz - position.xyz; // unrotated, for trixel lookup to_center = centre.xyz - position.xyz;
vpos = rotated + instance.xyz; vpos = rotated + instance.xyz;
ipos = position.xyz; ipos = position.xyz;
cam = camera; cam = camera;
@ -117,116 +117,68 @@ layout(binding = 4) uniform texture2D rdm_atlas;
layout(binding = 5) uniform texture2D brdf_lut; layout(binding = 5) uniform texture2D brdf_lut;
layout(binding = 3) uniform sampler rdmsmp; layout(binding = 3) uniform sampler rdmsmp;
const float PI = 3.1412854; const float PI = 3.1415927;
// --- SKY START --- const float ROUGHNESS_RAYMARCH_MAX = 0.2; // Below this roughness, actually try to get sharp reflection from RDM.
const float ROUGHNESS_SPEC_CUTOFF = 0.7; // Above this roughness, disregard RDM specular lighting entirely.
// ---- SKY ----
const float cirrus = 0.5; const float cirrus = 0.5;
const float cumulus = 20.0;
float hash(float n) float hash(float n) {
{
return fract(sin(n) * 43758.5453123); return fract(sin(n) * 43758.5453123);
} }
float noise(vec3 x) float noise(vec3 x) {
{
vec3 f = fract(x); vec3 f = fract(x);
float n = dot(floor(x), vec3(1.0, 157.0, 113.0)); float n = dot(floor(x), vec3(1.0, 157.0, 113.0));
return mix(mix(mix(hash(n + 0.0), hash(n + 1.0), f.x), return mix(mix(mix(hash(n + 0.0), hash(n + 1.0), f.x),
mix(hash(n + 157.0), hash(n + 158.0), f.x), f.y), mix(hash(n + 157.0), hash(n + 158.0), f.x), f.y),
mix(mix(hash(n + 113.0), hash(n + 114.0), f.x), mix(mix(hash(n + 113.0), hash(n + 114.0), f.x),
mix(hash(n + 270.0), hash(n + 271.0), f.x), f.y), f.z); mix(hash(n + 270.0), hash(n + 271.0), f.x), f.y), f.z);
} }
const mat3 m = mat3(0.0, 1.60, 1.20, -1.6, 0.72, -0.96, -1.2, -0.96, 1.28); const mat3 fbm_m = mat3(0.0, 1.60, 1.20, -1.6, 0.72, -0.96, -1.2, -0.96, 1.28);
float fbm(vec3 p)
{
float f = 0.0;
f += noise(p) / 2.0; p = m * p * 1.1;
f += noise(p) / 4.0; p = m * p * 1.2;
f += noise(p) / 6.0; p = m * p * 1.3;
f += noise(p) / 12.0; p = m * p * 1.4;
f += noise(p) / 24.0;
return f;
}
vec3 filmic_aces(vec3 v)
{
v = v * mat3(
0.59719f, 0.35458f, 0.04823f,
0.07600f, 0.90834f, 0.01566f,
0.02840f, 0.13383f, 0.83777f
);
return (v * (v + 0.0245786f) - 9.0537e-5f) /
(v * (0.983729f * v + 0.4329510f) + 0.238081f) * mat3(
1.60475f, -0.53108f, -0.07367f,
-0.10208f, 1.10813f, -0.00605f,
-0.00327f, -0.07276f, 1.07602f
);
}
vec3 sky(vec3 skypos, vec3 sunpos) { vec3 sky(vec3 skypos, vec3 sunpos) {
vec3 sunCol = sunDisk.xyz;
vec3 baseSky = skyBase.xyz;
vec3 topSky = skyTop.xyz;
float sDist = dot(normalize(skypos), normalize(sunpos));
vec3 npos = normalize(skypos); vec3 npos = normalize(skypos);
float sDist = dot(npos, normalize(sunpos));
vec3 skyGradient = mix(skyBase, skyTop, clamp(npos.y * 2.0, 0.0, 0.7));
vec3 result = skyGradient;
vec3 skyGradient = mix(baseSky, topSky, clamp(skypos.y * 2.0, 0.0, 0.7)); result += sunHalo * clamp((sDist - 0.95) * 10.0, 0.0, 0.8) * 0.2;
vec3 final = skyGradient; if (sDist > 0.9999)
final += sunHalo.xyz * clamp((sDist - 0.95) * 10.0, 0.0, 0.8) * 0.2; result = sunDisk;
// Sun disk result += mix(horizonHalo, vec3(0.0), clamp(abs(npos.y) * 80.0, 0.0, 1.0)) * 0.1;
if(sDist > 0.9999) { return result;
final = sunDisk.xyz;
}
// Horizon halo
final += mix(horizonHalo.xyz, vec3(0.0,0.0,0.0), clamp(abs(npos.y) * 80.0, 0.0, 1.0)) * 0.1;
final = vec3(final);
// Cirrus Clouds
if(hasClouds == 1) {
float density = smoothstep(1.0 - cirrus, 1.0, fbm(npos.xyz / npos.y * 2.0 + time * 0.05)) * 0.3;
final.rgb = mix(final.rgb, vec3(1.0, 1.0, 1.0), max(0.0, npos.y) * density * 2.0);
}
return final;
} }
// ---- PBR FUNCTIONS ---- vec3 sky_reflect(vec3 R, vec3 sunpos) {
if (R.y < 0.0) R = reflect(R, vec3(0.0, 1.0, 0.0));
return sky(R, sunpos);
}
// ---- PBR ----
float DistributionGGX(vec3 N, vec3 H, float roughness) { float DistributionGGX(vec3 N, vec3 H, float roughness) {
float a = roughness*roughness; float a = roughness * roughness;
float a2 = a*a; float a2 = a * a;
float NdotH = max(dot(N, H), 0.0); float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH*NdotH; float denom = NdotH * NdotH * (a2 - 1.0) + 1.0;
float num = a2; return a2 / (PI * denom * denom);
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return num / denom;
}
float GeometrySchlickGGX(float NdotV, float roughness) {
float r = (roughness + 1.0);
float k = (r*r) / 8.0;
float num = NdotV;
float denom = NdotV * (1.0 - k) + k;
return num / denom;
} }
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) { float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) {
float r = roughness + 1.0;
float k = (r * r) / 8.0;
float NdotV = max(dot(N, V), 0.0); float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0); float NdotL = max(dot(N, L), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness); float ggx1 = NdotL / (NdotL * (1.0 - k) + k);
float ggx1 = GeometrySchlickGGX(NdotL, roughness); float ggx2 = NdotV / (NdotV * (1.0 - k) + k);
return ggx1 * ggx2; return ggx1 * ggx2;
} }
@ -238,176 +190,115 @@ vec3 FresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness) {
return F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0); return F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
} }
// ---- RDM FUNCTIONS ---- // ---- RDM HELPERS ----
float roughness_to_rdm_size(int roughness) { // Hemioct encoding (Cigolle2014)
return pow(2.0, float((7 - roughness) + 1)); vec2 rdm_hemioct(vec3 v, int face) {
}
int rdm_index_from_normal(vec3 N) {
vec3 n_leftright = vec3(0.0, 0.0, 1.0);
vec3 n_updown = vec3(0.0, 1.0, 0.0);
vec3 n_frontback = vec3(1.0, 0.0, 0.0);
int res = 0;
res += int(dot(-n_updown, N) >= 0.98) * 1;
res += int(dot(n_leftright, N) >= 0.98) * 2;
res += int(dot(-n_leftright, N) >= 0.98) * 3;
res += int(dot(n_frontback, N) >= 0.98) * 4;
res += int(dot(-n_frontback, N) >= 0.98) * 5;
return res;
}
// Taken from Cigolle2014Vector.pdf
vec2 rdm_get_hemioct(vec3 v, int index, vec2 off) {
vec3 vc = v; vec3 vc = v;
if(index / 2 == 0) { if (face / 2 == 0) { vc.z = v.y; vc.y = v.z; }
vc.z = v.y; if (face / 2 == 2) { vc.z = v.x; vc.x = v.z; }
vc.y = v.z; if (face % 2 == 1) { vc.z *= -1.0; }
}
if(index / 2 == 2) {
vc.z = v.x;
vc.x = v.z;
}
if(index % 2 == 1) {
vc.z *= -1.0;
}
vc.x += off.x;
vc.y += off.y;
normalize(vc);
vec2 p = vc.xy * (1.0 / (abs(vc.x) + abs(vc.y) + vc.z)); vec2 p = vc.xy * (1.0 / (abs(vc.x) + abs(vc.y) + vc.z));
vec2 res = vec2(p.x + p.y, p.x - p.y); return vec2(p.x + p.y, p.x - p.y) * 0.5 + 0.5;
res.x = (res.x + 1.0) * 0.5;
res.y = (res.y + 1.0) * 0.5;
return res;
} }
float rdm_offset_y(int index) { int rdm_face_from_normal(vec3 N) {
return float((index / 2)) * (1.0/3.0); vec3 a = abs(N);
if (a.y >= a.x && a.y >= a.z) return N.y >= 0.0 ? 0 : 1;
if (a.z >= a.x && a.z >= a.y) return N.z >= 0.0 ? 2 : 3;
return N.x >= 0.0 ? 4 : 5;
} }
float rdm_offset_x(int index) { vec4 rdm_atlas_rect(ivec3 local_pos, int roughness) {
return float((index % 2)) * (1.0/2.0);
}
vec4 rdm_get_atlas_rect(ivec3 local_pos, int roughness) {
int rdm_index = local_pos.x + local_pos.y * 32 + local_pos.z * 1024 + roughness * 32768; int rdm_index = local_pos.x + local_pos.y * 32 + local_pos.z * 1024 + roughness * 32768;
int tx = rdm_index % 512; return texelFetch(sampler2D(rdm_lookup, trilesmp), ivec2(rdm_index % 512, rdm_index / 512), 0);
int ty = rdm_index / 512;
return texelFetch(sampler2D(rdm_lookup, trilesmp), ivec2(tx, ty), 0);
} }
ivec2 rdm_face_pixel_offset(vec4 atlas_rect, int face, int rdmSize) { ivec2 rdm_face_offset(vec4 rect, int face, int rdmSize, ivec2 atlasSize) {
ivec2 atlasSize = textureSize(sampler2D(rdm_atlas, rdmsmp), 0);
int col = face % 2; int col = face % 2;
int row = face / 2; int row = face / 2;
int ox = int(atlas_rect.x * float(atlasSize.x)) + col * rdmSize; return ivec2(int(rect.x * float(atlasSize.x)) + col * rdmSize,
int oy = int(atlas_rect.y * float(atlasSize.y)) + row * rdmSize; int(rect.y * float(atlasSize.y)) + row * rdmSize);
return ivec2(ox, oy);
} }
vec3 sample_rdm(vec3 N, vec3 V, vec3 rdm_center, vec3 diff, int roughness, ivec3 local_pos) { vec2 rdm_face_uv(int face) {
int face = rdm_index_from_normal(N); if (face <= 1) return vec2(ipos.x, ipos.z);
vec4 atlas_rect = rdm_get_atlas_rect(local_pos, roughness); if (face <= 3) return vec2(ipos.x, ipos.y);
if (atlas_rect.z <= 0.0) return vec3(1.0, 0.0, 1.0); // No data - magenta return vec2(ipos.z, ipos.y);
}
ivec2 atlasTexSize = textureSize(sampler2D(rdm_atlas, rdmsmp), 0); // ---- RDM SPECULAR ----
int rdmSizeInt = int(atlas_rect.z * float(atlasTexSize.x)) / 2;
float rdmSize = float(rdmSizeInt);
ivec2 faceOffset = rdm_face_pixel_offset(atlas_rect, face, rdmSizeInt);
vec2 uv;
if (face == 0 || face == 1) { // +Y / -Y
uv = vec2(ipos.x, ipos.z);
} else if (face == 2 || face == 3) { // +Z / -Z
uv = vec2(ipos.x, ipos.y);
} else { // +X / -X
uv = vec2(ipos.z, ipos.y);
}
vec3 reflected = normalize(reflect(V, N));
if (roughness > 1) {
vec3 samplePos = normalize(diff + 2.0 * reflected);
vec2 hemiUV = rdm_get_hemioct(samplePos, face, vec2(0.0));
vec2 atlasSize = vec2(textureSize(sampler2D(rdm_atlas, rdmsmp), 0));
vec2 texUV = (vec2(faceOffset) + hemiUV * rdmSize) / atlasSize;
return texture(sampler2D(rdm_atlas, rdmsmp), texUV).rgb;
}
vec3 rdm_spec_raymarch(vec3 N, vec3 V, vec3 diff, int face, ivec2 faceOffset, int rdmSize, vec2 atlasInvSize) {
vec3 reflected = reflect(V, N);
float maxDist = 20.0; float maxDist = 20.0;
int steps = 10; int steps = 40;
float stepSize = maxDist / float(steps);
for (int i = 0; i < steps; i++) { for (int i = 0; i < steps; i++) {
float t = maxDist * float(i + 1) / float(steps); float t = stepSize * float(i + 1);
vec3 samplePos = diff + t * reflected; vec3 samplePos = diff + t * reflected;
if (dot(samplePos, N) < 0.0) continue; if (dot(samplePos, N) < 0.0) continue;
vec2 hemiUV = rdm_get_hemioct(normalize(samplePos), face, vec2(0.0)); vec3 dir = normalize(samplePos);
vec2 atlasSize = vec2(textureSize(sampler2D(rdm_atlas, rdmsmp), 0)); vec2 hemiUV = rdm_hemioct(dir, face);
vec2 texCoord = (vec2(faceOffset) + hemiUV * rdmSize) / atlasSize; vec2 texCoord = (vec2(faceOffset) + hemiUV * float(rdmSize)) * atlasInvSize;
vec4 rdmSample = texture(sampler2D(rdm_atlas, rdmsmp), texCoord, 0); vec4 s = texture(sampler2D(rdm_atlas, rdmsmp), texCoord, 0);
float depth = rdmSample.a;
float dist = length(samplePos);
float stepSize = maxDist / float(steps);
if (depth > 0.0 && depth < dist && depth + stepSize > dist) { float dist = length(samplePos);
return rdmSample.rgb; if (s.a > 0.0 && s.a < dist && s.a + stepSize > dist)
} return s.rgb;
} }
vec3 skyDir = reflected; return sky_reflect(reflected, sunPosition);
if (skyDir.y < 0.0) skyDir = reflect(skyDir, vec3(0.0, 1.0, 0.0));
return sky(skyDir, sunPosition);
} }
vec3 sample_rdm_diff_map(vec3 N, ivec3 local_pos, vec3 fallback) { vec3 rdm_spec_single(vec3 N, vec3 V, vec3 diff, int face, ivec2 faceOffset, int rdmSize, vec2 atlasInvSize) {
vec4 atlas_rect = rdm_get_atlas_rect(local_pos, 7); vec3 reflected = reflect(V, N);
if (atlas_rect.z <= 0.0) return fallback; vec3 sampleDir = normalize(diff + 2.0 * reflected);
vec2 hemiUV = rdm_hemioct(sampleDir, face);
int face = rdm_index_from_normal(N); vec2 texCoord = (vec2(faceOffset) + hemiUV * float(rdmSize)) * atlasInvSize;
int rdmSize = int(roughness_to_rdm_size(7)); return texture(sampler2D(rdm_atlas, rdmsmp), texCoord).rgb;
ivec2 faceOffset = rdm_face_pixel_offset(atlas_rect, face, rdmSize);
vec2 pos = rdm_get_hemioct(N, face, vec2(0.0));
ivec2 texCoord = ivec2(faceOffset.x + int(pos.x * float(rdmSize)),
faceOffset.y + int(pos.y * float(rdmSize)));
return texelFetch(sampler2D(rdm_atlas, rdmsmp), texCoord, 0).rgb;
} }
int isign(float f) { vec3 rdm_sample_diff_probe(vec3 N, ivec3 local_pos, vec3 fallback) {
return f < 0.0 ? -1 : 1; vec4 rect = rdm_atlas_rect(local_pos, 7);
if (rect.z <= 0.0) return fallback;
int face = rdm_face_from_normal(N);
int rdmSize = int(pow(2.0, float((7 - 7) + 1)));
ivec2 atlasSize = textureSize(sampler2D(rdm_atlas, rdmsmp), 0);
ivec2 fOff = rdm_face_offset(rect, face, rdmSize, atlasSize);
vec2 pos = rdm_hemioct(N, face);
return texelFetch(sampler2D(rdm_atlas, rdmsmp),
ivec2(fOff.x + int(pos.x * float(rdmSize)),
fOff.y + int(pos.y * float(rdmSize))), 0).rgb;
} }
int isign(float f) { return f < 0.0 ? -1 : 1; }
vec3 smix(vec3 a, vec3 b, float t) { vec3 smix(vec3 a, vec3 b, float t) {
float power = 1.6; float power = 1.6;
float smoothT = pow(t, power) / (pow(t, power) + pow(1.0 - t, power)); float st = pow(t, power) / (pow(t, power) + pow(1.0 - t, power));
return mix(a, b, smoothT); return mix(a, b, st);
} }
vec3 sample_rdm_diff(vec3 N, vec3 diff, ivec3 local_pos) { vec3 rdm_indirect_diffuse(vec3 N, vec3 diff, ivec3 local_pos) {
int face = rdm_index_from_normal(N); int face = rdm_face_from_normal(N);
vec3 ambientPlaceholder = vec3(0.3, 0.3, 0.4); vec3 ambient = vec3(0.3, 0.3, 0.4);
vec2 delta = vec2(0.0); vec2 delta;
if (face == 0 || face == 1) { if (face <= 1) delta = vec2(diff.x, diff.z);
delta = vec2(diff.x, diff.z); else if (face <= 3) delta = vec2(diff.x, diff.y);
} else if (face == 2 || face == 3) { else delta = vec2(diff.z, diff.y);
delta = vec2(diff.x, diff.y);
} else {
delta = vec2(diff.z, diff.y);
}
ivec3 s0 = ivec3(0, 0, 0);
ivec3 s1, s2, s3; ivec3 s1, s2, s3;
if (face == 0 || face == 1) { if (face <= 1) {
s1 = ivec3(isign(delta.x), 0, 0); s1 = ivec3(isign(delta.x), 0, 0);
s2 = ivec3(0, 0, isign(delta.y)); s2 = ivec3(0, 0, isign(delta.y));
s3 = ivec3(isign(delta.x), 0, isign(delta.y)); s3 = ivec3(isign(delta.x), 0, isign(delta.y));
} else if (face == 2 || face == 3) { } else if (face <= 3) {
s1 = ivec3(isign(delta.x), 0, 0); s1 = ivec3(isign(delta.x), 0, 0);
s2 = ivec3(0, isign(delta.y), 0); s2 = ivec3(0, isign(delta.y), 0);
s3 = ivec3(isign(delta.x), isign(delta.y), 0); s3 = ivec3(isign(delta.x), isign(delta.y), 0);
@ -417,18 +308,18 @@ vec3 sample_rdm_diff(vec3 N, vec3 diff, ivec3 local_pos) {
s3 = ivec3(0, isign(delta.y), isign(delta.x)); s3 = ivec3(0, isign(delta.y), isign(delta.x));
} }
vec3 p0 = sample_rdm_diff_map(N, ivec3(mod(vec3(local_pos + s0), 32.0)), ambientPlaceholder); vec3 p0 = rdm_sample_diff_probe(N, ivec3(mod(vec3(local_pos), 32.0)), ambient);
vec3 p1 = sample_rdm_diff_map(N, ivec3(mod(vec3(local_pos + s1), 32.0)), ambientPlaceholder); vec3 p1 = rdm_sample_diff_probe(N, ivec3(mod(vec3(local_pos + s1), 32.0)), ambient);
vec3 p2 = sample_rdm_diff_map(N, ivec3(mod(vec3(local_pos + s2), 32.0)), ambientPlaceholder); vec3 p2 = rdm_sample_diff_probe(N, ivec3(mod(vec3(local_pos + s2), 32.0)), ambient);
vec3 p3 = sample_rdm_diff_map(N, ivec3(mod(vec3(local_pos + s3), 32.0)), ambientPlaceholder); vec3 p3 = rdm_sample_diff_probe(N, ivec3(mod(vec3(local_pos + s1 + s2),32.0)), ambient);
return smix( return smix(smix(p0, p1, abs(delta.x)),
smix(p0, p1, abs(delta.x)), smix(p2, p3, abs(delta.x)),
smix(p2, p3, abs(delta.x)), abs(delta.y));
abs(delta.y)
);
} }
// ---- HSV ----
vec3 rgb2hsv(vec3 c) { vec3 rgb2hsv(vec3 c) {
vec4 K = vec4(0.0, -1.0/3.0, 2.0/3.0, -1.0); vec4 K = vec4(0.0, -1.0/3.0, 2.0/3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
@ -444,152 +335,146 @@ vec3 hsv2rgb(vec3 c) {
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
} }
// ---- MAIN ----
void main() { void main() {
if (vpos.y < planeHeight - 0.01 && is_reflection == 1) { if (vpos.y < planeHeight - 0.01 && is_reflection == 1) discard;
discard;
}
// Trixel material sampling Get trixel material.
vec3 pos_after_adjust = ipos - orig_normal * 0.02; vec3 sample_pos = ipos - orig_normal * 0.02;
int count = 0;
vec4 trixel_material; vec4 trixel_material;
int maxCount = 5; int maxSteps = is_reflection == 1 ? 1 : 3;
if(is_reflection == 1) maxCount = 1; for (int i = 0; i < maxSteps; i++) {
while (count < maxCount) { ivec2 texel = ivec2(
int xpos = int(clamp(pos_after_adjust.z, 0.0001, 0.99999) * 16.0); int(clamp(sample_pos.z, 0.0001, 0.99999) * 16.0),
int ypos = int(clamp(pos_after_adjust.y, 0.0001, 0.99999) * 16.0); int(clamp(sample_pos.y, 0.0001, 0.99999) * 16.0) +
int zpos = int(clamp(pos_after_adjust.x, 0.0001, 0.99999) * 16.0); int(clamp(sample_pos.x, 0.0001, 0.99999) * 16.0) * 16
);
trixel_material = texelFetch(sampler2D(triletex, trilesmp), ivec2(xpos, ypos + zpos * 16), 0); trixel_material = texelFetch(sampler2D(triletex, trilesmp), texel, 0);
if (length(trixel_material) > 0.01) break; if (dot(trixel_material, trixel_material) > 0.0001) break;
pos_after_adjust += to_center * 0.1; sample_pos += to_center * 0.1;
count++;
} }
vec3 albedo = trixel_material.xyz; vec3 albedo = vec3(1.0, 1.0, 1.0);
int packedMaterial = int(round(trixel_material.w * 255.0));
float emittance = 0.0; int packed = int(round(trixel_material.w * 255.0));
int roughnessInt = 0; float emittance = 0.0;
float roughness = 0.05; int roughnessInt = 7;
float metallic = 0.0; float roughness = 1.0;
if ((packedMaterial & 0x1) != 0) { float metallic = 0.0;
emittance = float((packedMaterial >> 1) & 0x7F) / 127.0;
if ((packed & 0x1) != 0) {
emittance = float((packed >> 1) & 0x7F) / 127.0;
} else { } else {
roughnessInt = (packedMaterial >> 5) & 0x7; roughnessInt = (packed >> 5) & 0x7;
roughness = max(float(roughnessInt) / 7.0, 0.05); roughness = max(float(roughnessInt) / 7.0, 0.05);
metallic = float((packedMaterial >> 3) & 0x3) / 3.0; metallic = float((packed >> 3) & 0x3) / 3.0;
} }
// Snap normal to nearest axis to avoid interpolation noise // Avoid noise in normals which appears for some reason.
vec3 absN = abs(fnormal.xyz); vec3 absN = abs(fnormal.xyz);
vec3 N; vec3 N;
if (absN.x >= absN.y && absN.x >= absN.z) { if (absN.x >= absN.y && absN.x >= absN.z) N = vec3(sign(fnormal.x), 0.0, 0.0);
N = vec3(sign(fnormal.x), 0.0, 0.0); else if (absN.y >= absN.x && absN.y >= absN.z) N = vec3(0.0, sign(fnormal.y), 0.0);
} else if (absN.y >= absN.x && absN.y >= absN.z) { else N = vec3(0.0, 0.0, sign(fnormal.z));
N = vec3(0.0, sign(fnormal.y), 0.0);
} else {
N = vec3(0.0, 0.0, sign(fnormal.z));
}
// Simplified lighting evaluation for planar reflection.
if (is_reflection == 1) { if (is_reflection == 1) {
vec3 V = normalize(cam - vpos.xyz);
vec3 L = normalize(sunPosition); vec3 L = normalize(sunPosition);
float NdotL = max(dot(N, L), 0.0); float NdotL = max(dot(N, L), 0.0);
vec3 light = albedo * NdotL * sunLightColor * sunIntensity; frag_color = vec4(albedo * (NdotL * sunLightColor * sunIntensity + 0.1), 1.0);
light += 0.1 * albedo;
frag_color = vec4(light, 1.0);
return; return;
} }
vec3 V = normalize(cam - vpos.xyz); // Evaluate direct light.
vec3 V = normalize(cam - vpos);
vec3 L = normalize(sunPosition); vec3 L = normalize(sunPosition);
vec3 H = normalize(V + L); vec3 H = normalize(V + L);
vec3 F0 = vec3(0.04);
F0 = mix(F0, albedo, metallic);
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001;
vec3 specular = numerator / denominator;
float NdotL = max(dot(N, L), 0.0); float NdotL = max(dot(N, L), 0.0);
vec3 kD = vec3(1.0) - F; float NdotV = max(dot(N, V), 0.0);
kD *= 1.0 - metallic; float HdotV = max(dot(H, V), 0.0);
// Shadow vec3 F0 = mix(vec3(0.04), albedo, metallic);
vec4 light_proj_pos = mvp_shadow * vec4(floor(vpos.xyz * 16.0) / 16.0, 1.0); vec3 F = fresnelSchlick(HdotV, F0);
vec3 light_pos = light_proj_pos.xyz / light_proj_pos.w; float NDF = DistributionGGX(N, H, roughness);
light_pos = light_pos * 0.5 + 0.5; float G = GeometrySmith(N, V, L, roughness);
light_pos.z -= 0.001;
float shadowp = texture(sampler2DShadow(shadowtex, shadowsmp), light_pos);
// Direct lighting vec3 specular = (NDF * G * F) / (4.0 * NdotV * NdotL + 0.0001);
vec3 light = shadowp * (kD * albedo / PI + specular) * NdotL * sunLightColor * sunIntensity; vec3 kD = (1.0 - F) * (1.0 - metallic);
// RDM indirect lighting // Shadow lookup.
vec3 hemispherePos = trileCenter + N * 0.49; vec4 light_proj = mvp_shadow * vec4(floor(vpos * 16.0) / 16.0, 1.0);
vec3 light_ndc = light_proj.xyz / light_proj.w * 0.5 + 0.5;
light_ndc.z -= 0.001;
float shadow = texture(sampler2DShadow(shadowtex, shadowsmp), light_ndc);
vec3 light = shadow * (kD * albedo / PI + specular) * NdotL * sunLightColor * sunIntensity;
// --- Indirect lighting ---
ivec3 local = ivec3(mod(floor(trileCenter), 32.0)); ivec3 local = ivec3(mod(floor(trileCenter), 32.0));
vec4 atlas_rect_check = rdm_get_atlas_rect(local, roughnessInt); vec4 atlas_rect = rdm_atlas_rect(local, roughnessInt);
float ssao_sample = texture(sampler2D(ssaotex, rdmsmp), vec2(gl_FragCoord.x / float(screen_w), gl_FragCoord.y / float(screen_h)), 0).r; float ssao = texture(sampler2D(ssaotex, rdmsmp),
gl_FragCoord.xy / vec2(float(screen_w), float(screen_h))).r;
// Emissive — self-lit, not shadowed.
vec3 emissive = albedo * emittance * emissive_scale; vec3 emissive = albedo * emittance * emissive_scale;
if (rdm_enabled == 1 && atlas_rect_check.z > 0.0) { if (rdm_enabled == 100 && atlas_rect.z > 0.0) {
vec3 Frough = FresnelSchlickRoughness(max(dot(N, V), 0.0), F0, roughness); vec3 Frough = FresnelSchlickRoughness(NdotV, F0, roughness);
vec3 hemispherePos = trileCenter + N * 0.49;
vec3 diff = vpos - hemispherePos;
// Indirect specular // Indirect specular
vec3 indirectSpec = sample_rdm(N, -cv, if (roughness < ROUGHNESS_SPEC_CUTOFF) {
hemispherePos, vpos - hemispherePos, roughnessInt, local) * rdm_tint; int face = rdm_face_from_normal(N);
ivec2 atlasSize = textureSize(sampler2D(rdm_atlas, rdmsmp), 0);
vec2 atlasInvSize = 1.0 / vec2(atlasSize);
int rdmSize = int(atlas_rect.z * float(atlasSize.x)) / 2;
ivec2 fOff = rdm_face_offset(atlas_rect, face, rdmSize, atlasSize);
// For metallic surfaces: desaturate the reflection so Frough (which uses albedo vec3 indirectSpec;
// as F0 for metals) applies the metal tint cleanly without double-tinting. if (roughness < ROUGHNESS_RAYMARCH_MAX) {
float specLum = dot(indirectSpec, vec3(0.2126, 0.7152, 0.0722)); indirectSpec = rdm_spec_raymarch(N, -cv, diff, face, fOff, rdmSize, atlasInvSize);
indirectSpec = mix(indirectSpec, vec3(specLum), metallic); } else {
indirectSpec = rdm_spec_single(N, -cv, diff, face, fOff, rdmSize, atlasInvSize);
}
indirectSpec *= rdm_tint;
vec2 envBRDF = texture(sampler2D(brdf_lut, rdmsmp), vec2(max(dot(N, V), 0.0), roughness)).rg; // Desaturate for metals to avoid double-tinting
float NdotV_s = max(dot(N, V), 0.0); float specLum = dot(indirectSpec, vec3(0.2126, 0.7152, 0.0722));
float roughnessBell = 1.0 - 0.7 * sin(roughness * PI); indirectSpec = mix(indirectSpec, vec3(specLum), metallic);
float grazingSuppress = 1.0 - 0.9 * roughness * sin(roughness * PI) * pow(1.0 - NdotV_s, 2.0);
float specRoughFade = 1.0 - clamp((roughness - 0.5) / 0.3, 0.0, 1.0);
light += indirectSpec * (Frough * envBRDF.x + envBRDF.y) * rdm_spec_scale * roughnessBell * grazingSuppress * specRoughFade;
// Indirect diffuse (interpolated from neighbor probes) vec2 envBRDF = texture(sampler2D(brdf_lut, rdmsmp), vec2(NdotV, roughness)).rg;
vec3 indirectDiff = sample_rdm_diff(N, vpos - hemispherePos, local) * rdm_tint; float roughnessBell = 1.0 - 0.7 * sin(roughness * PI);
float grazingSuppress = 1.0 - 0.9 * roughness * sin(roughness * PI) * pow(1.0 - NdotV, 2.0);
float specRoughFade = 1.0 - clamp((roughness - 0.5) / 0.3, 0.0, 1.0);
light += indirectSpec * (Frough * envBRDF.x + envBRDF.y)
* rdm_spec_scale * roughnessBell * grazingSuppress * specRoughFade;
}
// Indirect diffuse
vec3 indirectDiff = rdm_indirect_diffuse(N, diff, local) * rdm_tint;
float diffLuma = dot(indirectDiff, vec3(0.2126, 0.7152, 0.0722)); float diffLuma = dot(indirectDiff, vec3(0.2126, 0.7152, 0.0722));
indirectDiff = mix(vec3(diffLuma), indirectDiff, rdm_diff_saturation); indirectDiff = mix(vec3(diffLuma), indirectDiff, rdm_diff_saturation);
vec3 kDiff = 1.0 - Frough; vec3 kDiff = (1.0 - Frough) * (1.0 - metallic);
kDiff *= 1.0 - metallic;
light += (kDiff * indirectDiff / PI * albedo) * ssao_sample * rdm_diff_scale; light += kDiff * indirectDiff / PI * albedo * ssao * rdm_diff_scale;
if (rdm_diff_scale < 0.001) {
light += ambient_color * ambient_intensity * albedo * ssao_sample; // Ambient floor
} if (rdm_diff_scale < 0.001 || length(light) < ambient_intensity)
if (length(light) < ambient_intensity) { light += ambient_color * max(ambient_intensity - length(light), 0.0) * albedo * ssao;
light += ambient_color * (ambient_intensity - length(light)) * albedo * ssao_sample;
}
} else { } else {
// Fallback: ambient + sky reflection when no RDM data (or RDM disabled). light += ambient_color * ambient_intensity * albedo * ssao;
light += ambient_color * ambient_intensity * albedo * ssao_sample;
vec3 R = reflect(-V, N); vec3 R = reflect(-V, N);
if (R.y < 0.0) R = reflect(R, vec3(0.0, 1.0, 0.0)); light += F * sky_reflect(R, sunPosition) * 0.1;
light += F * sky(R, sunPosition) * 0.1;
} }
vec3 final_color = light + emissive; vec3 final_color = light + emissive;
if (hsv_lighting == 1) {
float albedo_lum = dot(albedo, vec3(0.2126, 0.7152, 0.0722)) + 0.001;
float light_lum = dot(final_color, vec3(0.2126, 0.7152, 0.0722));
vec3 hsv = rgb2hsv(albedo);
hsv.z = clamp(hsv.z * (light_lum / albedo_lum), 0.0, 1.0);
final_color = hsv2rgb(hsv);
}
frag_color = vec4(mix(deepColor, final_color, smoothstep(0.0, planeHeight, vpos.y)), 1.0); frag_color = vec4(mix(deepColor, final_color, smoothstep(0.0, planeHeight, vpos.y)), 1.0);
if (is_preview == 1) {
frag_color.rgb = mix(frag_color.rgb, vec3(0.3, 0.7, 1.0), 0.5); if (is_preview == 1) frag_color.rgb = mix(frag_color.rgb, vec3(0.3, 0.7, 1.0), 0.5);
} else if (is_preview == 2) { else if (is_preview == 2) frag_color.rgb = mix(frag_color.rgb, vec3(1.0, 0.3, 0.2), 0.5);
frag_color.rgb = mix(frag_color.rgb, vec3(1.0, 0.3, 0.2), 0.5);
}
} }
@end @end