gizmo-renderer 0.9.1

A custom ECS and physics engine aimed for realistic simulations.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// Deferred lighting pass — fullscreen triangle.
// Reads G-buffers, reconstructs surface data, computes PBR + CSM shadows → HDR output.
//
// SceneUniforms, LightData, compute_direct_lighting and inverse_mat4 are shared from
// common.wgsl; the anisotropic/clear-coat/env-BRDF lobes from pbr_ext.wgsl. Both are composed
// in by load_shader_composed (naga_oil). Only binding-dependent deferred code lives below:
// the fullscreen pass, procedural environment/IBL and PCSS shadows (they read `scene` and the
// shadow textures, so per the common.wgsl convention they stay out of the pure modules).
#import gizmo::common::{SceneUniforms, LightData, compute_direct_lighting}
#import gizmo::pbr_ext::{approximate_env_brdf, compute_direct_lighting_anisotropic, compute_clear_coat}

@group(0) @binding(0) var<uniform> scene: SceneUniforms;

@group(1) @binding(0) var t_shadow: texture_depth_2d_array;
@group(1) @binding(1) var s_shadow: sampler_comparison;
@group(1) @binding(2) var t_point_shadow: texture_depth_cube;

@group(2) @binding(0) var t_albedo_metallic:  texture_2d<f32>;
@group(2) @binding(1) var t_normal_roughness: texture_2d<f32>;
@group(2) @binding(2) var t_position_rel_camera:   texture_2d<f32>;
@group(2) @binding(3) var s_gbuf: sampler;
@group(2) @binding(4) var t_world_tangent:    texture_2d<f32>;

// Fullscreen triangle — no vertex buffer needed.
@vertex
fn vs_main(@builtin(vertex_index) vi: u32) -> @builtin(position) vec4<f32> {
    var pos = array<vec2<f32>, 3>(
        vec2<f32>(-1.0, -1.0),
        vec2<f32>( 3.0, -1.0),
        vec2<f32>(-1.0,  3.0),
    );
    return vec4<f32>(pos[vi], 0.0, 1.0);
}

fn select_cascade(view_depth: f32) -> u32 {
    if (view_depth < scene.cascade_splits.x) { return 0u; }
    if (view_depth < scene.cascade_splits.y) { return 1u; }
    if (view_depth < scene.cascade_splits.z) { return 2u; }
    return 3u;
}

// Mirror of `csm::SHADOW_FADE_FRACTION` / `csm::shadow_distance_fade`. Past the last cascade
// there is no shadow data, so the term snaps to "fully lit"; without a fade that snap is a
// hard line across the world at `cascade_splits.w`. See csm.rs for the reasoning and the
// cost of the alternative. `shader_shadow_fade_matches_the_rust_mirror` pins the constant.
const SHADOW_FADE_FRACTION: f32 = 0.15;

// Depth bias in METRES. Calibrated to reproduce what the old NDC constants were worth at the
// depth range they were chosen under: 0.0004 NDC over a ~105 m cascade is 4.2 cm, and the 0.00004
// floor is 4.2 mm.
const SHADOW_BIAS_METRES: f32 = 0.042;
const SHADOW_BIAS_METRES_MIN: f32 = 0.0042;

fn shadow_distance_fade(view_depth: f32, shadow_far: f32) -> f32 {
    let far = max(shadow_far, 1e-4);
    let band = max(far * SHADOW_FADE_FRACTION, 1e-4);
    return 1.0 - smoothstep(far - band, far, view_depth);
}

fn get_single_preset_environment(preset: u32, dir: vec3<f32>, roughness: f32, sun_dot: f32, zenith: f32, horizon_blend: f32) -> vec3<f32> {
    // Preset 0: Sunset Gold
    if (preset == 0u) {
        let sky_color = mix(vec3<f32>(0.05, 0.02, 0.08), vec3<f32>(0.1, 0.05, 0.15), max(zenith, 0.0));
        let horizon_color = vec3<f32>(0.9, 0.35, 0.1);
        let base_sky = mix(horizon_color, sky_color, pow(max(zenith, 0.0), 0.5));
        
        let sun_power = mix(4096.0, 16.0, roughness);
        let sun_glow = pow(sun_dot, sun_power) * vec3<f32>(20.0, 12.0, 3.0) * mix(1.0, 0.1, roughness);
        
        let rim_dir = normalize(vec3<f32>(-0.8, 0.3, -0.5));
        let rim_dot = max(dot(dir, rim_dir), 0.0);
        let rim_glow = pow(rim_dot, mix(256.0, 8.0, roughness)) * vec3<f32>(0.2, 0.3, 0.6) * mix(1.0, 0.15, roughness);
        
        let ground_color = vec3<f32>(0.12, 0.05, 0.03);
        
        let sky_val = base_sky * 0.5 + sun_glow * 0.4 + rim_glow;
        let ground_val = mix(ground_color, horizon_color * 0.3, pow(1.0 + clamp(zenith, -1.0, 0.0), 4.0)) + sun_glow * 0.1;
        return mix(ground_val, sky_val, horizon_blend);
    }
    
    // Preset 1: Studio Neutral
    if (preset == 1u) {
        let sky_color = vec3<f32>(0.1, 0.12, 0.15);
        let horizon_color = vec3<f32>(0.4, 0.42, 0.45);
        let base_sky = mix(horizon_color, sky_color, max(zenith, 0.0));
        
        let key_dir = normalize(vec3<f32>(0.6, 0.8, 0.5));
        let key_dot = max(dot(dir, key_dir), 0.0);
        let key_glow = pow(key_dot, mix(1024.0, 12.0, roughness)) * vec3<f32>(6.0, 5.8, 5.5) * mix(1.0, 0.2, roughness);
        
        let fill_dir = normalize(vec3<f32>(-0.8, 0.2, -0.6));
        let fill_dot = max(dot(dir, fill_dir), 0.0);
        let fill_glow = pow(fill_dot, mix(512.0, 8.0, roughness)) * vec3<f32>(1.5, 1.8, 2.5) * mix(1.0, 0.15, roughness);
        
        let rim_dir = normalize(vec3<f32>(0.0, 0.1, -1.0));
        let rim_dot = max(dot(dir, rim_dir), 0.0);
        let rim_glow = pow(rim_dot, mix(2048.0, 16.0, roughness)) * vec3<f32>(3.0, 2.5, 1.8) * mix(1.0, 0.2, roughness);
        
        let ground_color = vec3<f32>(0.03, 0.03, 0.035);
        
        let sky_val = base_sky * 0.3 + key_glow * 0.4 + fill_glow * 0.2 + rim_glow * 0.3;
        let ground_val = mix(ground_color, horizon_color * 0.15, pow(1.0 + clamp(zenith, -1.0, 0.0), 4.0)) + key_glow * 0.1;
        return mix(ground_val, sky_val, horizon_blend);
    }
    
    // Preset 2: Midnight Neon
    if (preset == 2u) {
        let sky_color = vec3<f32>(0.005, 0.005, 0.02);
        let horizon_color = vec3<f32>(0.01, 0.01, 0.03);
        let base_sky = mix(horizon_color, sky_color, max(zenith, 0.0));
        
        let neon1_dir = normalize(vec3<f32>(0.7, 0.2, 0.5));
        let neon1_dot = max(dot(dir, neon1_dir), 0.0);
        let neon1_glow = pow(neon1_dot, mix(2048.0, 8.0, roughness)) * vec3<f32>(18.0, 0.0, 12.0) * mix(1.0, 0.15, roughness);
        
        let neon2_dir = normalize(vec3<f32>(-0.7, 0.3, -0.5));
        let neon2_dot = max(dot(dir, neon2_dir), 0.0);
        let neon2_glow = pow(neon2_dot, mix(1024.0, 6.0, roughness)) * vec3<f32>(0.0, 14.0, 18.0) * mix(1.0, 0.12, roughness);
        
        let top_dir = vec3<f32>(0.0, 1.0, 0.0);
        let top_glow = pow(max(dot(dir, top_dir), 0.0), 3.0) * vec3<f32>(0.4, 0.0, 0.8);
        
        let ground_color = vec3<f32>(0.005, 0.005, 0.008);
        
        let sky_val = base_sky * 0.2 + neon1_glow + neon2_glow + top_glow * 0.15;
        let ground_val = mix(ground_color, horizon_color * 0.2, pow(1.0 + clamp(zenith, -1.0, 0.0), 4.0)) + (neon1_glow + neon2_glow) * 0.05;
        return mix(ground_val, sky_val, horizon_blend);
    }
    
    // Default Preset 3: Classic Daylight
    let sky_color = mix(vec3<f32>(0.5, 0.65, 1.0), vec3<f32>(0.05, 0.2, 0.6), max(zenith, 0.0));
    let base_sky = sky_color;
    
    let sun_power = mix(2048.0, 16.0, roughness);
    let sun_glow = pow(sun_dot, sun_power) * vec3<f32>(15.0, 12.0, 8.0) * mix(1.0, 0.2, roughness);
    
    let sun_dir = normalize(-scene.sun_direction.xyz);
    let horizon_glow = pow(1.0 - max(zenith, 0.0), 4.0) * vec3<f32>(1.0, 0.6, 0.3) * max(dot(dir, sun_dir) * 0.5 + 0.5, 0.0);
    
    let ground_color = vec3<f32>(0.05, 0.05, 0.05);
    
    let sky_val = base_sky * 0.8 + sun_glow * 0.05 + horizon_glow;
    let ground_val = mix(ground_color, horizon_glow * 0.2, pow(1.0 + clamp(zenith, -1.0, 0.0), 4.0)) + sun_glow * 0.1;
    return mix(ground_val, sky_val, horizon_blend);
}

// Procedural HDR Environment Presets with interpolation and smooth blending
fn get_procedural_environment(dir: vec3<f32>, roughness: f32) -> vec3<f32> {
    let zenith = dir.y;
    let sun_dir = normalize(-scene.sun_direction.xyz);
    let sun_dot = max(dot(dir, sun_dir), 0.0);
    
    // Smooth transition factor at the horizon (zenith = 0.0)
    let horizon_blend = smoothstep(0.0, 1.0, clamp(zenith * 10.0 + 0.5, 0.0, 1.0));
    
    let color1 = get_single_preset_environment(scene.environment_preset, dir, roughness, sun_dot, zenith, horizon_blend);
    
    if (scene.environment_blend_t > 0.001) {
        let color2 = get_single_preset_environment(scene.environment_preset_b, dir, roughness, sun_dot, zenith, horizon_blend);
        return mix(color1, color2, scene.environment_blend_t);
    }
    
    return color1;
}

// The anisotropic GGX, clear-coat and env-BRDF lobes now live in gizmo::pbr_ext (imported at
// the top). inverse_mat4 / compute_direct_lighting come from gizmo::common. Only the
// binding-dependent PCSS shadow filter + procedural environment remain deferred-local.

fn search_blockers(
    shadow_uv: vec2<f32>, receiver_depth: f32, ci: u32, texel: f32
) -> vec2<f32> {
    var num_blockers = 0.0;
    var sum_depth = 0.0;

    let search_radius = 2; 
    let step = texel * 1.5;

    for (var x = -search_radius; x <= search_radius; x++) {
        for (var y = -search_radius; y <= search_radius; y++) {
            let offset = vec2<f32>(f32(x), f32(y)) * step;
            let sample_depth = textureSampleLevel(t_shadow, s_gbuf, shadow_uv + offset, ci, 0i);
            
            if (sample_depth < receiver_depth) {
                num_blockers += 1.0;
                sum_depth += sample_depth;
            }
        }
    }

    return vec2<f32>(num_blockers, sum_depth);
}

fn filter_pcss(
    shadow_uv: vec2<f32>, receiver_depth: f32, ci: u32, bias: f32, texel: f32
) -> f32 {
    let blockers = search_blockers(shadow_uv, receiver_depth, ci, texel);
    let num_blockers = blockers.x;
    
    if (num_blockers < 0.5) {
        return 1.0;
    }

    let avg_blocker_depth = blockers.y / num_blockers;

    // (receiver - blocker) / blocker * light_size
    // The sun is nearly a point source at infinity → small angular size → crisp
    // shadows that only soften with distance from the caster. 0.015 modelled a
    // large area light (a soft, unrealistic blob); 0.004 keeps contact-hardening
    // but a sun-like edge.
    let light_size = 0.004;
    let penumbra = (receiver_depth - avg_blocker_depth) / max(avg_blocker_depth, 0.0001) * light_size;

    // Crisp sun shadow: keep the PCF radius near one texel — just enough to
    // anti-alias the shadow-map edge (no blocky stair-stepping), not so much that
    // it turns soft/mushy. The higher SHADOW_MAP_RES (3072) makes one texel small
    // on screen, so a ~1-texel filter reads as a sharp, straight edge.
    let filter_radius = clamp(penumbra, texel * 0.6, texel * 1.2);

    var shadow_sum = 0.0;
    let grid_size = 2;
    let step = filter_radius / 2.0;

    for (var x = -grid_size; x <= grid_size; x++) {
        for (var y = -grid_size; y <= grid_size; y++) {
            let offset = vec2<f32>(f32(x), f32(y)) * step;
            // `textureSampleCompareLevel`, not `textureSampleCompare`: the latter takes implicit
            // derivatives, and this loop sits inside the non-uniform `shadow_uv` bounds branch. On
            // D3D12 that is a "gradient instruction used in a loop with varying iteration" and FXC
            // fails the whole pipeline — the Deferred Lighting pipeline did not compile on Windows
            // at all, so nothing rendered. A shadow map has no mips, so level 0 is what was meant.
            shadow_sum += textureSampleCompareLevel(t_shadow, s_shadow, shadow_uv + offset, ci, receiver_depth - bias);
        }
    }

    return shadow_sum / 25.0;
}

fn compute_height_fog(world_pos: vec3<f32>, camera_pos: vec3<f32>) -> vec4<f32> {
    let view_vec = world_pos - camera_pos;
    let dist = length(view_vec);
    let view_dir = view_vec / max(dist, 0.0001);

    var fog_color = vec3<f32>(0.5, 0.6, 0.7); // default Daylight
    var fog_density = 0.015;
    var fog_height_falloff = 0.05;
    var fog_base_height = -5.0; // base height of fog plane

    if (scene.environment_preset == 0u) {
        // Sunset Gold
        fog_color = vec3<f32>(0.85, 0.38, 0.15);
        fog_density = 0.025;
        fog_height_falloff = 0.08;
    } else if (scene.environment_preset == 1u) {
        // Studio Neutral
        fog_color = vec3<f32>(0.2, 0.22, 0.25);
        fog_density = 0.008;
        fog_height_falloff = 0.04;
    } else if (scene.environment_preset == 2u) {
        // Midnight Neon
        fog_color = vec3<f32>(0.12, 0.02, 0.25);
        fog_density = 0.035;
        fog_height_falloff = 0.12;
    }

    // Blend fog color if we are interpolating presets!
    if (scene.environment_blend_t > 0.001) {
        var fog_color_2 = vec3<f32>(0.5, 0.6, 0.7);
        var fog_density_2 = 0.015;
        var fog_height_falloff_2 = 0.05;

        if (scene.environment_preset_b == 0u) {
            fog_color_2 = vec3<f32>(0.85, 0.38, 0.15);
            fog_density_2 = 0.025;
            fog_height_falloff_2 = 0.08;
        } else if (scene.environment_preset_b == 1u) {
            fog_color_2 = vec3<f32>(0.2, 0.22, 0.25);
            fog_density_2 = 0.008;
            fog_height_falloff_2 = 0.04;
        } else if (scene.environment_preset_b == 2u) {
            fog_color_2 = vec3<f32>(0.12, 0.02, 0.25);
            fog_density_2 = 0.035;
            fog_height_falloff_2 = 0.12;
        }
        fog_color = mix(fog_color, fog_color_2, scene.environment_blend_t);
        fog_density = mix(fog_density, fog_density_2, scene.environment_blend_t);
        fog_height_falloff = mix(fog_height_falloff, fog_height_falloff_2, scene.environment_blend_t);
    }

    // Volumetric analytical scattering (height-decay fog integration)
    let cam_y = camera_pos.y - fog_base_height;
    let dir_y = view_dir.y;

    var fog_amount = 0.0;
    if (abs(dir_y) < 0.0001) {
        fog_amount = fog_density * exp(-fog_height_falloff * cam_y) * dist;
    } else {
        let falloff_dir_y = fog_height_falloff * dir_y;
        fog_amount = (fog_density * exp(-fog_height_falloff * cam_y) * (1.0 - exp(-falloff_dir_y * dist))) / falloff_dir_y;
    }

    let fog_factor = 1.0 - clamp(exp(-fog_amount), 0.0, 1.0);
    return vec4<f32>(fog_color, fog_factor);
}

@fragment
fn fs_main(@builtin(position) frag_coord: vec4<f32>) -> @location(0) vec4<f32> {
    let uv = frag_coord.xy;
    let iuv = vec2<i32>(i32(uv.x), i32(uv.y));

    let albedo_metallic  = textureLoad(t_albedo_metallic,  iuv, 0);
    let normal_roughness = textureLoad(t_normal_roughness, iuv, 0);
    let pos_sample       = textureLoad(t_position_rel_camera,   iuv, 0);
    let tangent_sample   = textureLoad(t_world_tangent,    iuv, 0);

    // Unwritten pixels (skipped geometry, unlit objects) — render clean dark grey background (Bevy parity)
    if (pos_sample.w < 0.5) { 
        return vec4<f32>(0.05, 0.05, 0.05, 1.0);
    }

    let albedo    = albedo_metallic.rgb;
    let metallic  = albedo_metallic.a;
    let N         = normalize(normal_roughness.xyz);
    let roughness = normal_roughness.a;
    // G-buffer position is camera-relative (see gbuffer.wgsl); put it back in world space.
    let world_pos = pos_sample.xyz + scene.camera_pos.xyz;

    let min_roughness = max(roughness, 0.05);
    let f0            = mix(vec3<f32>(0.04), albedo, metallic);

    var raw_tangent = tangent_sample.xyz;
    if (length(raw_tangent) < 0.001) {
        if (abs(N.x) > 0.9) {
            raw_tangent = cross(vec3<f32>(0.0, 1.0, 0.0), N);
        } else {
            raw_tangent = cross(vec3<f32>(1.0, 0.0, 0.0), N);
        }
    }
    let T = normalize(raw_tangent);
    let bitangent_sign = select(-1.0, 1.0, tangent_sample.w >= 0.0); // handedness only; never 0 (a null tangent.w would zero the bitangent)
    let clear_coat = clamp((abs(tangent_sample.w) - 0.01) / 0.99, 0.0, 1.0);

    let B = normalize(cross(N, T) * bitangent_sign);

    let w_val = pos_sample.w;
    let subsurface = floor(w_val) / 100.0;
    let anisotropy = clamp((w_val - floor(w_val) - 0.5) / 0.49, 0.0, 1.0);

    // --- Physically Based IBL (Procedural HDR Environment Maps) ---
    let V = normalize(scene.camera_pos.xyz - world_pos);
    let NdV = max(dot(N, V), 0.001);
    
    // 1. Diffuse IBL (Irradiance)
    let ambient_base = vec3<f32>(0.02, 0.02, 0.025);
    let irradiance = get_procedural_environment(N, 1.0) + ambient_base;
    var ambient = albedo * irradiance * (1.0 - metallic);
    if (subsurface > 0.0) {
        let sss_color = vec3<f32>(0.96, 0.28, 0.15);
        ambient += ambient * subsurface * sss_color * 0.45;
    }
    
    // 2. Specular IBL (Pre-filtered Environment Map with Anisotropic Stretch)
    var R = reflect(-V, N);
    if (anisotropy > 0.0) {
        let anisotropy_stretch = anisotropy * (1.0 - roughness);
        let anisotropic_direction = cross(cross(N, T), N);
        R = normalize(mix(R, anisotropic_direction, anisotropy_stretch));
    }
    let R_rough = normalize(mix(R, N, roughness)); 
    let specular_env = get_procedural_environment(R_rough, roughness);
    
    // 3. Environment BRDF (Lazarov Analytical Split-Sum LUT approximation)
    let env_brdf_lut = approximate_env_brdf(NdV, roughness);
    var specular_ibl = specular_env * (f0 * env_brdf_lut.x + env_brdf_lut.y);

    // --- 4. Clear Coat Specular & Attenuation IBL ---
    if (clear_coat > 0.0) {
        let F_env = 0.04 + (1.0 - 0.04) * pow(1.0 - NdV, 5.0);
        let coat_atten_env = 1.0 - clear_coat * F_env;

        ambient = ambient * coat_atten_env;
        specular_ibl = specular_ibl * coat_atten_env;

        let R_coat = normalize(mix(reflect(-V, N), N, 0.08));
        let specular_env_coat = get_procedural_environment(R_coat, 0.08);
        let env_brdf_lut_coat = approximate_env_brdf(NdV, 0.08);
        let specular_ibl_coat = specular_env_coat * (0.04 * env_brdf_lut_coat.x + env_brdf_lut_coat.y);

        specular_ibl += specular_ibl_coat * clear_coat;
    }

    // --- CSM Shadow ---
    var shadow_visibility = 1.0;
    let view_depth = dot(world_pos - scene.camera_pos.xyz, scene.camera_forward.xyz);
    // cascade_splits.w = far edge of the last cascade = the whole covered range.
    let shadow_fade = shadow_distance_fade(view_depth, scene.cascade_splits.w);
    if (scene.sun_direction.w > 0.5 && shadow_fade > 0.0) {
        let ci         = select_cascade(view_depth);

        // Normal-offset shadows: cascade'in DÜNYA texel boyutuna ORANTILI offset ile
        // örnek noktasını yüzeyden ayır. Sabit N*0.0018 yalnızca en yakın cascade'e
        // uyuyordu; uzak cascade'lerde (texel çok daha büyük) offset texel'in ~1/4'ü
        // kalıp yüzeyi temizleyemiyor → diagonal self-shadow acne. Ortho X ölçeği
        // sx = |M'nin lineer satır-0'ı| (V ortonormal), world_texel = 2·uv_texel/sx;
        // ~2 texel offset her cascade'de acne'yi keser, peter-pan minimum.
        // Asıl acne çözümü gölge-pass'te FRONT-FACE CULLING (arka yüzler haritada) — aydınlık
        // ön yüz kendi derinliğiyle kıyaslanmaz. Burada yalnız silüet/temas için ufak, cascade
        // texel'ine orantılı normal-offset kalır (grazing 1/NoL patch'i artık gereksiz).
        let m = scene.light_view_proj[ci];
        let sx = length(vec3<f32>(m[0][0], m[1][0], m[2][0]));
        let world_texel = 2.0 * scene.cascade_params.y / max(sx, 1e-6);
        let offset_pos = world_pos + N * world_texel * 2.0;
        let light_clip = m * vec4<f32>(offset_pos, 1.0);

        let light_ndc  = light_clip.xyz / light_clip.w;
        let shadow_uv  = vec2<f32>(light_ndc.x * 0.5 + 0.5, light_ndc.y * -0.5 + 0.5);

        if (shadow_uv.x >= 0.0 && shadow_uv.x <= 1.0 &&
            shadow_uv.y >= 0.0 && shadow_uv.y <= 1.0 && light_ndc.z <= 1.0) {
            let slope  = 1.0 - max(dot(N, normalize(-scene.sun_direction.xyz)), 0.0);
            // Normal-offset (yukarıdaki world_texel·2) örneği yüzeyden ittiği için depth
            // bias küçük kalır. Eski düz-zemin tabanı `if (N.y>0.99){bias=max(bias,0.005)}`
            // 50x aşırı düzeltmeydi ve gölgeyi kaynağın tabanından peter-pan'ledi.
            // **The bias is metres, converted here — not an NDC constant.** An NDC bias means a different
            // world distance for every cascade and changes the moment the projection's depth range
            // does, which made it impossible to widen that range without peter-panning every
            // shadow. The third row of the cascade matrix is NDC-z per world metre, exactly as the
            // first row is used for the normal offset above.
            let sz     = length(vec3<f32>(m[0][2], m[1][2], m[2][2]));
            let bias   = max(SHADOW_BIAS_METRES * slope, SHADOW_BIAS_METRES_MIN) * sz;
            let texel  = scene.cascade_params.y;
            // Walk the sampled term back to "lit" over the last stretch of the covered range,
            // so the end of the shadow maps is a gradient rather than a line.
            shadow_visibility = mix(1.0, filter_pcss(shadow_uv, light_ndc.z, ci, bias, texel), shadow_fade);
        }
    }

    var total_lighting = vec3<f32>(0.0);

    // --- Directional Sun ---
    if (scene.sun_direction.w > 0.5) {
        let L = normalize(-scene.sun_direction.xyz);
        var sun_light = vec3<f32>(0.0);
        if (anisotropy > 0.0) {
            sun_light = compute_direct_lighting_anisotropic(
                N, V, L, T, B, albedo, min_roughness, metallic, anisotropy, f0,
                scene.sun_color.rgb, scene.sun_color.w, shadow_visibility
            );
        } else {
            sun_light = compute_direct_lighting(
                N, V, L, albedo, min_roughness, metallic, f0,
                scene.sun_color.rgb, scene.sun_color.w, shadow_visibility
            );
        }

        if (clear_coat > 0.0) {
            let H = normalize(V + L);
            let VoH = max(dot(V, H), 0.0);
            let F_c = 0.04 + (1.0 - 0.04) * pow(1.0 - VoH, 5.0);
            let coat_atten = 1.0 - clear_coat * F_c;

            let coat_spec = compute_clear_coat(N, V, L, scene.sun_color.rgb, scene.sun_color.w, shadow_visibility);
            sun_light = sun_light * coat_atten + coat_spec * clear_coat;
        }

        if (subsurface > 0.0) {
            let sss_wrap = 0.35;
            let sss_ndl = max((dot(N, L) + sss_wrap) / (1.0 + sss_wrap), 0.0);
            let sss_power = 8.0;
            let sss_scale = 0.65;
            let sss_trans = pow(max(dot(-V, L), 0.0), sss_power) * sss_scale * (1.0 - metallic);
            
            let sss_color = vec3<f32>(0.96, 0.28, 0.15);
            let sss_contrib = (sss_ndl * 0.12 + sss_trans) * subsurface * sss_color;
            
            sun_light += sss_contrib * scene.sun_color.rgb * scene.sun_color.w * shadow_visibility;
        }

        total_lighting += sun_light;
    }

    // --- Dynamic Lights ---
    for (var i = 0u; i < scene.num_lights; i++) {
        let light      = scene.lights[i];
        let light_type = u32(light.params.y);
        let intensity  = light.position.w;
        var L: vec3<f32>;
        var atten: f32 = 1.0;

        if (light_type == 2u) {
            L = normalize(-light.direction.xyz);
        } else {
            let to_light = light.position.xyz - world_pos;
            let dist     = length(to_light);
            let radius   = max(light.color.a, 0.001);
            L = normalize(to_light);
            let d_over_r = dist / radius;
            atten = clamp(1.0 - d_over_r * d_over_r * d_over_r * d_over_r, 0.0, 1.0);
            atten = (atten * atten) / (dist * dist + 0.01);

            if (light_type == 1u) {
                let spot_dir = normalize(light.direction.xyz);
                let cos_a    = dot(-L, spot_dir);
                let inner    = light.direction.w;
                let outer    = light.params.x;
                let eps      = max(inner - outer, 0.001);
                let sf       = clamp((cos_a - outer) / eps, 0.0, 1.0);
                atten *= sf * sf;
            } else if (light_type == 0u) {
                // Point Light Shadow (optional). There is a single point-shadow cube;
                // it belongs to ONE designated caster whose cubemap was rendered this
                // frame. cascade_params.w carries (caster_index + 1), with 0 meaning
                // "no point shadow this frame". The old code applied this one cube to
                // EVERY point light using each light's own position, so every light but
                // the caster got a shadow centred on the wrong place.
                let sp_idx = u32(scene.cascade_params.w);
                if (scene.point_shadows_enabled > 0u && sp_idx > 0u && i == sp_idx - 1u) {
                    let dir_from_light = world_pos - light.position.xyz;
                    let abs_dir = abs(dir_from_light);
                    let z_near = 0.1;
                    // Far plane tracks the light's radius (the same value the CPU builds
                    // the cube projection with) instead of a hardcoded 100 that clipped
                    // large lights and wasted depth precision on small ones.
                    let z_far  = max(light.color.a, 1.0);
                    let z_val  = max(abs_dir.x, max(abs_dir.y, abs_dir.z));
                    let clip_z = (z_far * (z_val - z_near)) / (z_val * (z_far - z_near));

                    // Slope-scaled bias only. The old flat-ground floor
                    // `if (N.y > 0.99) { bias = max(bias, 0.01); }` was the same 200x
                    // over-correction the CSM path already dropped — it peter-panned the
                    // shadow off a flat receiver's contact point.
                    //
                    // **Metres, converted — and here the NDC form was far worse than it was for
                    // the cascades.** `clip_z` above is a perspective depth, so its derivative
                    // falls off as 1/d²: a fixed NDC bias therefore grows as the *square* of the
                    // distance from the light. Worked out for the old 0.0005 — 5 mm a metre from
                    // the light, 50 cm at the edge of a 10 m light, **1.99 m** at the edge of a
                    // 20 m one and **12.5 m** at the edge of a 50 m one. Surfaces out near a large
                    // light's range simply stop receiving shadow, because the comparison has been
                    // pushed metres past every caster.
                    //
                    // d(clip_z)/d(z_val) = z_far·z_near / (z_val²·(z_far − z_near)), so multiplying
                    // a metre figure by it gives the NDC bias that means that many metres *here*.
                    // The constant is the cascades' own SHADOW_BIAS_METRES: it is the same
                    // physical question — how far to push a depth comparison to clear a surface —
                    // and one engine answering it two ways is worse than either answer.
                    let slope = 1.0 - max(dot(N, normalize(dir_from_light)), 0.0);
                    let ndc_per_metre =
                        (z_far * z_near) / max(z_val * z_val * (z_far - z_near), 1e-6);
                    let bias = max(SHADOW_BIAS_METRES * slope, SHADOW_BIAS_METRES_MIN)
                        * ndc_per_metre;
                    let shadow_vis = textureSampleCompareLevel(t_point_shadow, s_shadow, dir_from_light, clip_z - bias);
                    atten *= shadow_vis;
                }
            }
        }

        var light_color_contrib = vec3<f32>(0.0);
        if (anisotropy > 0.0) {
            light_color_contrib = compute_direct_lighting_anisotropic(
                N, V, L, T, B, albedo, min_roughness, metallic, anisotropy, f0,
                light.color.rgb, intensity, atten
            );
        } else {
            light_color_contrib = compute_direct_lighting(
                N, V, L, albedo, min_roughness, metallic, f0,
                light.color.rgb, intensity, atten
            );
        }

        if (clear_coat > 0.0) {
            let H = normalize(V + L);
            let VoH = max(dot(V, H), 0.0);
            let F_c = 0.04 + (1.0 - 0.04) * pow(1.0 - VoH, 5.0);
            let coat_atten = 1.0 - clear_coat * F_c;

            let coat_spec = compute_clear_coat(N, V, L, light.color.rgb, intensity, atten);
            light_color_contrib = light_color_contrib * coat_atten + coat_spec * clear_coat;
        }

        total_lighting += light_color_contrib;
    }

    // Exposure is NOT applied here anymore — it is a single post-process knob applied over
    // the whole composited HDR (deferred + sky + unlit), so it can't compound or skip the
    // sky/unlit forward objects. scene.exposure is left in the uniform for layout stability.
    var final_color = ambient + total_lighting + specular_ibl;

    // Apply volumetric analytical height fog
    let fog = compute_height_fog(world_pos, scene.camera_pos.xyz);
    final_color = mix(final_color, fog.rgb, fog.a);

    // Shading Mode overrides
    if (scene.shading_mode == 1u) {
        // Normals
        return vec4<f32>(N * 0.5 + 0.5, 1.0);
    } else if (scene.shading_mode == 2u) {
        // Albedo
        return vec4<f32>(albedo, 1.0);
    } else if (scene.shading_mode == 3u) {
        // Roughness/Metallic
        return vec4<f32>(roughness, metallic, 0.0, 1.0);
    } else if (scene.shading_mode == 4u) {
        // Shadows debug
        return vec4<f32>(vec3<f32>(shadow_visibility), 1.0);
    } else if (scene.shading_mode == 5u) {
        // Tangents View
        return vec4<f32>(T * 0.5 + 0.5, 1.0);
    } else if (scene.shading_mode == 6u) {
        // Clear Coat View
        return vec4<f32>(vec3<f32>(clear_coat), 1.0);
    }

    return vec4<f32>(final_color, 1.0);
}