nightshade 0.8.2

A cross-platform data-oriented game engine.
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
const PI: f32 = 3.14159265358979323846;

fn srgb_to_linear(srgb: vec4<f32>) -> vec4<f32> {
    return vec4<f32>(pow(srgb.xyz, vec3<f32>(2.2)), srgb.w);
}

fn srgb_to_linear3(srgb: vec3<f32>) -> vec3<f32> {
    return pow(srgb, vec3<f32>(2.2));
}

fn DistributionGGX(N: vec3<f32>, H: vec3<f32>, roughness: f32) -> f32 {
    let a = roughness * roughness;
    let a2 = a * a;
    let NdotH = max(dot(N, H), 0.0);
    let NdotH2 = NdotH * NdotH;
    let nom = a2;
    let denom = (NdotH2 * (a2 - 1.0) + 1.0);
    return nom / (PI * denom * denom);
}

fn GeometrySchlickGGX(NdotV: f32, roughness: f32) -> f32 {
    let r = roughness + 1.0;
    let k = (r * r) / 8.0;
    let nom = NdotV;
    let denom = NdotV * (1.0 - k) + k;
    return nom / denom;
}

fn GeometrySmith(N: vec3<f32>, V: vec3<f32>, L: vec3<f32>, roughness: f32) -> f32 {
    let NdotV = max(dot(N, V), 0.0);
    let NdotL = max(dot(N, L), 0.0);
    let ggx2 = GeometrySchlickGGX(NdotV, roughness);
    let ggx1 = GeometrySchlickGGX(NdotL, roughness);
    return ggx1 * ggx2;
}

fn fresnelSchlick(cosTheta: f32, F0: vec3<f32>) -> vec3<f32> {
    return F0 + (1.0 - F0) * pow(max(1.0 - cosTheta, 0.0), 5.0);
}

fn fresnelSchlickRoughness(cosTheta: f32, F0: vec3<f32>, roughness: f32) -> vec3<f32> {
    return F0 + (max(vec3<f32>(1.0 - roughness), F0) - F0) * pow(max(1.0 - cosTheta, 0.0), 5.0);
}

fn getRangeAttenuation(range: f32, distance: f32) -> f32 {
    if range <= 0.0 {
        return 1.0;
    }
    let clamped_distance = max(distance, 0.01);
    return max(min(1.0 - pow(distance / range, 4.0), 1.0), 0.0) / (clamped_distance * clamped_distance);
}

fn getSpotAttenuation(pointToLight: vec3<f32>, spotDirection: vec3<f32>, outerConeCos: f32, innerConeCos: f32) -> f32 {
    let actualCos = dot(normalize(spotDirection), normalize(-pointToLight));
    if actualCos > outerConeCos {
        if actualCos < innerConeCos {
            return smoothstep(outerConeCos, innerConeCos, actualCos);
        }
        return 1.0;
    }
    return 0.0;
}

fn getVolumeTransmissionRay(normal: vec3<f32>, view: vec3<f32>, thickness: f32, ior: f32, model_scale: vec3<f32>) -> vec3<f32> {
    let refraction_vector = refract(-view, normal, 1.0 / ior);
    return normalize(refraction_vector) * thickness * model_scale;
}

fn applyVolumeAttenuation(radiance: vec3<f32>, transmission_distance: f32, attenuation_color: vec3<f32>, attenuation_distance: f32) -> vec3<f32> {
    if attenuation_distance <= 0.0 {
        return radiance;
    }
    let attenuation_coefficient = -log(max(attenuation_color, vec3<f32>(0.0001))) / attenuation_distance;
    let transmittance = exp(-attenuation_coefficient * transmission_distance);
    return transmittance * radiance;
}

fn applyIorToRoughness(roughness: f32, ior: f32) -> f32 {
    return roughness * clamp(ior * 2.0 - 2.0, 0.0, 1.0);
}

fn getTransmissionSample(reflection: vec3<f32>, roughness: f32, ior: f32) -> vec3<f32> {
    let transmission_roughness = applyIorToRoughness(roughness, ior);
    let MAX_REFLECTION_LOD = 4.0;
    return textureSampleLevel(prefiltered_env, prefiltered_sampler, reflection, transmission_roughness * MAX_REFLECTION_LOD).rgb;
}

fn getIBLVolumeRefraction(
    normal: vec3<f32>,
    view: vec3<f32>,
    roughness: f32,
    base_color: vec3<f32>,
    F0: vec3<f32>,
    ior: f32,
    thickness: f32,
    attenuation_color: vec3<f32>,
    attenuation_distance: f32,
    model_scale: vec3<f32>
) -> vec3<f32> {
    let transmission_ray = getVolumeTransmissionRay(normal, view, thickness, ior, model_scale);
    let refracted_ray_exit = -normalize(transmission_ray);
    let transmitted_light = getTransmissionSample(refracted_ray_exit, roughness, ior);
    let attenuated_color = applyVolumeAttenuation(transmitted_light, length(transmission_ray), attenuation_color, attenuation_distance);
    let NdotV = clamp(dot(normal, view), 0.001, 1.0);
    let brdf = textureSampleLevel(brdf_lut, brdf_lut_sampler, vec2<f32>(NdotV, roughness), 0.0).rg;
    let specular_color = F0 * brdf.x + brdf.y;
    return (1.0 - specular_color) * attenuated_color * base_color;
}

struct SkinnedVertexInput {
    @location(0) position: vec3<f32>,
    @location(1) normal: vec3<f32>,
    @location(2) tex_coords: vec2<f32>,
    @location(3) tex_coords_1: vec2<f32>,
    @location(4) tangent: vec4<f32>,
    @location(5) color: vec4<f32>,
    @location(6) joint_indices: vec4<u32>,
    @location(7) joint_weights: vec4<f32>,
};

struct VertexOutput {
    @builtin(position) position: vec4<f32>,
    @location(0) normal: vec3<f32>,
    @location(1) world_pos: vec3<f32>,
    @location(2) @interpolate(flat) material_id: u32,
    @location(3) tex_coords: vec2<f32>,
    @location(4) tex_coords_1: vec2<f32>,
    @location(5) instance_tint: vec4<f32>,
    @location(6) vertex_color: vec4<f32>,
    @location(7) world_tangent: vec4<f32>,
    @location(8) view_depth: f32,
};

struct Uniforms {
    view: mat4x4<f32>,
    projection: mat4x4<f32>,
    camera_position: vec4<f32>,
    num_lights: vec4<u32>,
    ambient_light: vec4<f32>,
    light_view_projection: mat4x4<f32>,
    shadow_bias: f32,
    shadows_enabled: f32,
    global_unlit: f32,
    shadow_normal_bias: f32,
    snap_resolution: vec2<f32>,
    snap_enabled: u32,
    affine_enabled: u32,
    fog_color: vec3<f32>,
    fog_enabled: u32,
    fog_start: f32,
    fog_end: f32,
    cascade_count: u32,
    _padding2: f32,
    cascade_view_projections: array<mat4x4<f32>, 4>,
    cascade_split_distances: vec4<f32>,
    cascade_atlas_offsets: array<vec4<f32>, 4>,
    cascade_atlas_scale: vec4<f32>,
    time: f32,
    directional_light_direction: vec4<f32>,
};

struct Material {
    base_color: vec4<f32>,
    emissive_factor: vec3<f32>,
    alpha_mode: u32,
    alpha_cutoff: f32,
    has_base_texture: u32,
    has_emissive_texture: u32,
    has_normal_texture: u32,
    has_metallic_roughness_texture: u32,
    has_occlusion_texture: u32,
    normal_scale: f32,
    occlusion_strength: f32,
    roughness: f32,
    metallic: f32,
    unlit: u32,
    normal_map_flags: u32,
    uv_scale: vec2<f32>,
    transmission_factor: f32,
    has_transmission_texture: u32,
    thickness: f32,
    has_thickness_texture: u32,
    _align_attenuation: vec2<u32>,
    attenuation_color: vec3<f32>,
    attenuation_distance: f32,
    ior: f32,
    specular_factor: f32,
    _align_specular: vec2<u32>,
    specular_color_factor: vec3<f32>,
    has_specular_texture: u32,
    has_specular_color_texture: u32,
    emissive_strength: f32,
    uv_set_indices: u32,
    _pad_end: f32,
};

const NORMAL_MAP_FLIP_Y: u32 = 1u;
const NORMAL_MAP_TWO_COMPONENT: u32 = 2u;

const UV_SET_BASE: u32 = 0u;
const UV_SET_EMISSIVE: u32 = 1u;
const UV_SET_NORMAL: u32 = 2u;
const UV_SET_METALLIC_ROUGHNESS: u32 = 3u;
const UV_SET_OCCLUSION: u32 = 4u;
const UV_SET_TRANSMISSION: u32 = 5u;
const UV_SET_THICKNESS: u32 = 6u;
const UV_SET_SPECULAR: u32 = 7u;
const UV_SET_SPECULAR_COLOR: u32 = 8u;

fn get_uv_for_texture(uv0: vec2<f32>, uv1: vec2<f32>, uv_set_indices: u32, texture_index: u32) -> vec2<f32> {
    let use_uv1 = (uv_set_indices >> texture_index) & 1u;
    return select(uv0, uv1, use_uv1 == 1u);
}

struct SkinnedObjectData {
    transform_index: u32,
    mesh_id: u32,
    material_id: u32,
    joint_offset: u32,
    morph_weights: array<f32, 8>,
    morph_target_count: u32,
    morph_displacement_offset: u32,
    mesh_vertex_offset: u32,
    mesh_vertex_count: u32,
};

struct MorphDisplacement {
    position: vec3<f32>,
    _pad0: f32,
    normal: vec3<f32>,
    _pad1: f32,
    tangent: vec3<f32>,
    _pad2: f32,
};

struct Light {
    position: vec4<f32>,
    direction: vec4<f32>,
    color: vec4<f32>,
    light_type: u32,
    range: f32,
    inner_cone: f32,
    outer_cone: f32,
    shadow_index: i32,
    light_size: f32,
    _padding: vec2<f32>,
};

const LIGHT_TYPE_DIRECTIONAL: u32 = 0u;
const LIGHT_TYPE_POINT: u32 = 1u;
const LIGHT_TYPE_SPOT: u32 = 2u;

struct OITOutput {
    @location(0) accum: vec4<f32>,
    @location(1) reveal: f32,
};

@group(0) @binding(0)
var<uniform> uniforms: Uniforms;

@group(1) @binding(0)
var<storage, read> materials: array<Material>;

@group(1) @binding(1)
var<storage, read> objects: array<SkinnedObjectData>;

@group(1) @binding(2)
var<storage, read> lights: array<Light>;

@group(1) @binding(3)
var<storage, read> joint_matrices: array<mat4x4<f32>>;

@group(1) @binding(4)
var<storage, read> instance_custom_data: array<vec4<f32>>;

@group(1) @binding(5)
var<storage, read> morph_displacements: array<MorphDisplacement>;

struct LightGrid {
    offset: u32,
    count: u32,
};

struct ClusterUniforms {
    inverse_projection: mat4x4<f32>,
    screen_size: vec2<f32>,
    z_near: f32,
    z_far: f32,
    cluster_count: vec4<u32>,
    tile_size: vec2<f32>,
    num_lights: u32,
    num_directional_lights: u32,
};

const MAX_LIGHTS_PER_CLUSTER: u32 = 256u;

@group(1) @binding(7)
var<storage, read> light_grid: array<LightGrid>;

@group(1) @binding(8)
var<storage, read> light_indices: array<u32>;

@group(1) @binding(9)
var<uniform> cluster_uniforms: ClusterUniforms;

@group(2) @binding(0)
var base_texture: texture_2d<f32>;

@group(2) @binding(1)
var base_sampler: sampler;

@group(2) @binding(2)
var emissive_texture: texture_2d<f32>;

@group(2) @binding(3)
var emissive_sampler: sampler;

@group(2) @binding(4)
var normal_texture: texture_2d<f32>;

@group(2) @binding(5)
var normal_sampler: sampler;

@group(2) @binding(6)
var metallic_roughness_texture: texture_2d<f32>;

@group(2) @binding(7)
var metallic_roughness_sampler: sampler;

@group(2) @binding(8)
var occlusion_texture: texture_2d<f32>;

@group(2) @binding(9)
var occlusion_sampler: sampler;

@group(2) @binding(10)
var transmission_texture: texture_2d<f32>;

@group(2) @binding(11)
var transmission_sampler: sampler;

@group(2) @binding(12)
var thickness_texture: texture_2d<f32>;

@group(2) @binding(13)
var thickness_sampler: sampler;

@group(2) @binding(14)
var specular_texture: texture_2d<f32>;

@group(2) @binding(15)
var specular_sampler: sampler;

@group(2) @binding(16)
var specular_color_texture: texture_2d<f32>;

@group(2) @binding(17)
var specular_color_sampler: sampler;

@group(3) @binding(0)
var shadow_texture: texture_depth_2d;

@group(3) @binding(1)
var shadow_sampler: sampler;

@group(3) @binding(2)
var spotlight_shadow_atlas: texture_depth_2d;

@group(3) @binding(3)
var spotlight_shadow_sampler: sampler;

@group(3) @binding(4)
var<storage, read> spotlight_shadows: array<vec4<f32>>;

@group(3) @binding(5)
var brdf_lut: texture_2d<f32>;

@group(3) @binding(6)
var brdf_lut_sampler: sampler;

@group(3) @binding(7)
var irradiance_map: texture_cube<f32>;

@group(3) @binding(8)
var irradiance_sampler: sampler;

@group(3) @binding(9)
var prefiltered_env: texture_cube<f32>;

@group(3) @binding(10)
var prefiltered_sampler: sampler;

@vertex
fn vs_main(
    in: SkinnedVertexInput,
    @builtin(vertex_index) vertex_index: u32,
    @builtin(instance_index) instance_id: u32
) -> VertexOutput {
    var out: VertexOutput;

    let object = objects[instance_id];

    var position = in.position;
    var normal = in.normal;
    var tangent = in.tangent.xyz;

    if object.morph_target_count > 0u {
        let local_vertex_index = vertex_index - object.mesh_vertex_offset;

        for (var target_index = 0u; target_index < object.morph_target_count; target_index = target_index + 1u) {
            let weight = object.morph_weights[target_index];
            if abs(weight) > 0.0001 {
                let displacement_index = object.morph_displacement_offset
                    + target_index * object.mesh_vertex_count
                    + local_vertex_index;
                let displacement = morph_displacements[displacement_index];
                position = position + displacement.position * weight;
                normal = normal + displacement.normal * weight;
                tangent = tangent + displacement.tangent * weight;
            }
        }
        normal = normalize(normal);
        tangent = normalize(tangent);
    }

    var skinned_position = vec3<f32>(0.0, 0.0, 0.0);
    var skinned_normal = vec3<f32>(0.0, 0.0, 0.0);
    var skinned_tangent = vec3<f32>(0.0, 0.0, 0.0);

    let joint_offset = object.joint_offset;

    for (var index = 0u; index < 4u; index = index + 1u) {
        let joint_index = in.joint_indices[index];
        let joint_weight = in.joint_weights[index];

        if (joint_weight > 0.0) {
            let joint_matrix = joint_matrices[joint_offset + joint_index];

            let transformed_pos = joint_matrix * vec4<f32>(position, 1.0);
            skinned_position = skinned_position + transformed_pos.xyz * joint_weight;

            let normal_matrix = mat3x3<f32>(
                joint_matrix[0].xyz,
                joint_matrix[1].xyz,
                joint_matrix[2].xyz
            );
            let transformed_normal = normal_matrix * normal;
            skinned_normal = skinned_normal + transformed_normal * joint_weight;

            let transformed_tangent = normal_matrix * tangent;
            skinned_tangent = skinned_tangent + transformed_tangent * joint_weight;
        }
    }

    skinned_normal = normalize(skinned_normal);
    skinned_tangent = normalize(skinned_tangent);

    let world_position = vec4<f32>(skinned_position, 1.0);
    out.world_pos = world_position.xyz;

    let view_position = uniforms.view * world_position;
    var clip_position = uniforms.projection * view_position;

    if uniforms.snap_enabled == 1u {
        let resolution = uniforms.snap_resolution;
        let snapped_x = round(clip_position.x * resolution.x / clip_position.w) * clip_position.w / resolution.x;
        let snapped_y = round(clip_position.y * resolution.y / clip_position.w) * clip_position.w / resolution.y;
        clip_position.x = snapped_x;
        clip_position.y = snapped_y;
    }

    out.position = clip_position;
    out.normal = skinned_normal;
    out.world_tangent = vec4<f32>(skinned_tangent, in.tangent.w);
    out.material_id = object.material_id;
    out.tex_coords = in.tex_coords;
    out.tex_coords_1 = in.tex_coords_1;
    out.instance_tint = instance_custom_data[instance_id];
    out.vertex_color = in.color;
    out.view_depth = -view_position.z;

    return out;
}

fn getLightIntensity(light: Light, point_to_light: vec3<f32>, world_pos: vec3<f32>) -> vec3<f32> {
    var range_attenuation = 1.0;
    var spot_attenuation = 1.0;

    if light.light_type != LIGHT_TYPE_DIRECTIONAL {
        range_attenuation = getRangeAttenuation(light.range, length(point_to_light));
    }
    if light.light_type == LIGHT_TYPE_SPOT {
        spot_attenuation = getSpotAttenuation(point_to_light, light.direction.xyz, light.outer_cone, light.inner_cone);
    }

    return range_attenuation * spot_attenuation * light.color.rgb;
}

fn get_normal(
    world_normal: vec3<f32>,
    world_pos: vec3<f32>,
    tex_coord: vec2<f32>,
    normal_sample: vec3<f32>,
    has_normal_texture: bool,
    normal_scale: f32
) -> vec3<f32> {
    let tangent_normal = normal_sample * 2.0 - 1.0;

    let q1 = dpdx(world_pos);
    let q2 = dpdy(world_pos);
    let st1 = dpdx(tex_coord);
    let st2 = dpdy(tex_coord);

    let N = normalize(world_normal);
    let T = normalize(q1 * st2.y - q2 * st1.y);
    let B = -normalize(cross(N, T));
    let TBN = mat3x3<f32>(T, B, N);

    let mapped_normal = normalize(TBN * tangent_normal) * vec3<f32>(vec2<f32>(normal_scale), 1.0);
    return select(N, mapped_normal, has_normal_texture);
}

fn get_cluster_index(frag_coord: vec2<f32>, linear_depth: f32) -> u32 {
    let tile = vec2<u32>(frag_coord / cluster_uniforms.tile_size);
    let log_ratio = log(cluster_uniforms.z_far / cluster_uniforms.z_near);
    let slice = u32(log(linear_depth / cluster_uniforms.z_near) / log_ratio * f32(cluster_uniforms.cluster_count.z));
    let clamped_slice = clamp(slice, 0u, cluster_uniforms.cluster_count.z - 1u);
    return tile.x + tile.y * cluster_uniforms.cluster_count.x + clamped_slice * cluster_uniforms.cluster_count.x * cluster_uniforms.cluster_count.y;
}

fn weight(z: f32, a: f32) -> f32 {
    let z_scale = 5.0;
    let z_power = 200.0;
    let max_weight = 3000.0;
    return a * max(0.01, min(max_weight, 10.0 / (1e-5 + pow(abs(z) / z_scale, z_power))));
}

@fragment
fn fs_main(in: VertexOutput, @builtin(front_facing) is_front_face: bool) -> OITOutput {
    let material = materials[in.material_id];
    let uv0 = in.tex_coords * material.uv_scale;
    let uv1 = in.tex_coords_1 * material.uv_scale;

    let base_uv = get_uv_for_texture(uv0, uv1, material.uv_set_indices, UV_SET_BASE);
    let emissive_uv = get_uv_for_texture(uv0, uv1, material.uv_set_indices, UV_SET_EMISSIVE);
    let normal_uv = get_uv_for_texture(uv0, uv1, material.uv_set_indices, UV_SET_NORMAL);
    let metallic_roughness_uv = get_uv_for_texture(uv0, uv1, material.uv_set_indices, UV_SET_METALLIC_ROUGHNESS);
    let occlusion_uv = get_uv_for_texture(uv0, uv1, material.uv_set_indices, UV_SET_OCCLUSION);
    let transmission_uv = get_uv_for_texture(uv0, uv1, material.uv_set_indices, UV_SET_TRANSMISSION);
    let thickness_uv = get_uv_for_texture(uv0, uv1, material.uv_set_indices, UV_SET_THICKNESS);
    let specular_uv = get_uv_for_texture(uv0, uv1, material.uv_set_indices, UV_SET_SPECULAR);
    let specular_color_uv = get_uv_for_texture(uv0, uv1, material.uv_set_indices, UV_SET_SPECULAR_COLOR);

    let normal_tex_sample = textureSampleLevel(normal_texture, normal_sampler, normal_uv, 0.0).xyz;

    let normal = get_normal(
        in.normal,
        in.world_pos,
        normal_uv,
        normal_tex_sample,
        material.has_normal_texture != 0u,
        material.normal_scale
    );

    let base_tex_sample = textureSample(base_texture, base_sampler, base_uv);
    let emissive_tex_sample = textureSample(emissive_texture, emissive_sampler, emissive_uv);

    var base_color = material.base_color * in.instance_tint * in.vertex_color;
    if material.has_base_texture != 0u {
        base_color = base_color * srgb_to_linear(base_tex_sample);
    }

    let albedo = base_color.rgb;

    let mr_sample = textureSampleLevel(metallic_roughness_texture, metallic_roughness_sampler, metallic_roughness_uv, 0.0);
    var metallic = material.metallic;
    var roughness = material.roughness;
    if material.has_metallic_roughness_texture != 0u {
        roughness = roughness * mr_sample.g;
        metallic = metallic * mr_sample.b;
    }

    let occlusion_sample = textureSampleLevel(occlusion_texture, occlusion_sampler, occlusion_uv, 0.0).r;
    var occlusion = 1.0;
    if material.has_occlusion_texture != 0u {
        occlusion = occlusion_sample;
    }

    var transmission_factor = material.transmission_factor;
    if material.has_transmission_texture != 0u {
        let transmission_sample = textureSampleLevel(transmission_texture, transmission_sampler, transmission_uv, 0.0).r;
        transmission_factor = transmission_factor * transmission_sample;
    }

    var thickness = material.thickness;
    if material.has_thickness_texture != 0u {
        let thickness_sample = textureSampleLevel(thickness_texture, thickness_sampler, thickness_uv, 0.0).g;
        thickness = thickness * thickness_sample;
    }

    var specular_factor = material.specular_factor;
    if material.has_specular_texture != 0u {
        let specular_sample = textureSampleLevel(specular_texture, specular_sampler, specular_uv, 0.0).a;
        specular_factor = specular_factor * specular_sample;
    }

    var specular_color_factor = material.specular_color_factor;
    if material.has_specular_color_texture != 0u {
        let specular_color_sample = textureSampleLevel(specular_color_texture, specular_color_sampler, specular_color_uv, 0.0).rgb;
        specular_color_factor = specular_color_factor * srgb_to_linear3(specular_color_sample);
    }

    var emission = material.emissive_factor * material.emissive_strength;
    if material.has_emissive_texture != 0u {
        emission = emission * srgb_to_linear3(emissive_tex_sample.rgb);
    }

    var final_color: vec3<f32>;

    if material.unlit != 0u || uniforms.global_unlit > 0.5 {
        final_color = albedo + emission;
    } else {
        let N = normal;
        let V = normalize(uniforms.camera_position.xyz - in.world_pos);
        let R = reflect(-V, N);

        let ior = material.ior;
        let dielectric_f0 = pow((ior - 1.0) / (ior + 1.0), 2.0);
        let dielectric_specular_f0 = min(vec3<f32>(dielectric_f0) * specular_color_factor, vec3<f32>(1.0));
        var F0 = mix(dielectric_specular_f0, albedo, metallic);

        var Lo = vec3<f32>(0.0);

        for (var index = 0u; index < cluster_uniforms.num_directional_lights; index = index + 1u) {
            let light = lights[index];

            let point_to_light = -light.direction.xyz;

            let L = normalize(point_to_light);
            let H = normalize(V + L);

            let radiance = getLightIntensity(light, point_to_light, in.world_pos);

            let NDF = DistributionGGX(N, H, roughness);
            let G = GeometrySmith(N, V, L, roughness);
            let F = fresnelSchlick(max(dot(H, V), 0.0), F0);

            let numerator = NDF * G * F;
            let denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001;
            let specular = numerator / denominator;

            let kS = F;
            var kD = vec3<f32>(1.0) - kS;
            kD = kD * (1.0 - metallic);

            let NdotL = max(dot(N, L), 0.0);

            let light_contribution = (kD * albedo / PI + specular) * radiance * NdotL;

            Lo = Lo + light_contribution;
        }

        let cluster_index = get_cluster_index(in.position.xy, in.view_depth);
        let grid = light_grid[cluster_index];
        let base_offset = cluster_index * MAX_LIGHTS_PER_CLUSTER;

        for (var index = 0u; index < min(grid.count, MAX_LIGHTS_PER_CLUSTER); index = index + 1u) {
            let light_index = light_indices[base_offset + index];
            let light = lights[cluster_uniforms.num_directional_lights + light_index];

            let point_to_light = light.position.xyz - in.world_pos;

            let L = normalize(point_to_light);
            let H = normalize(V + L);

            let radiance = getLightIntensity(light, point_to_light, in.world_pos);

            let NDF = DistributionGGX(N, H, roughness);
            let G = GeometrySmith(N, V, L, roughness);
            let F = fresnelSchlick(max(dot(H, V), 0.0), F0);

            let numerator = NDF * G * F;
            let denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001;
            let specular = numerator / denominator;

            let kS = F;
            var kD = vec3<f32>(1.0) - kS;
            kD = kD * (1.0 - metallic);

            let NdotL = max(dot(N, L), 0.0);

            let light_contribution = (kD * albedo / PI + specular) * radiance * NdotL;

            Lo = Lo + light_contribution;
        }

        let F = fresnelSchlickRoughness(max(dot(N, V), 0.0), F0, roughness);
        let kS = F;
        var kD = 1.0 - kS;
        kD = kD * (1.0 - metallic);

        let irradiance = textureSampleLevel(irradiance_map, irradiance_sampler, N, 0.0).rgb;
        var diffuse_color = irradiance * albedo;

        let MAX_REFLECTION_LOD = 4.0;
        let prefiltered_color = textureSampleLevel(prefiltered_env, prefiltered_sampler, R, roughness * MAX_REFLECTION_LOD).rgb;
        let brdf = textureSampleLevel(brdf_lut, brdf_lut_sampler, vec2<f32>(max(dot(N, V), 0.0), roughness), 0.0).rg;
        let specular_ibl = prefiltered_color * (F * brdf.x + brdf.y) * specular_factor;

        if transmission_factor > 0.0 {
            let model_scale = vec3<f32>(1.0);
            let transmission = getIBLVolumeRefraction(
                N, V, roughness, albedo, F0, ior,
                thickness, material.attenuation_color, material.attenuation_distance, model_scale
            );
            diffuse_color = mix(diffuse_color, transmission, transmission_factor);
        }

        var ambient = kD * diffuse_color + specular_ibl;

        ambient = mix(ambient, ambient * occlusion, material.occlusion_strength);

        let color = ambient + Lo;

        final_color = color + emission;
    }

    let alpha = base_color.a * (1.0 - transmission_factor);
    let w = weight(in.position.z, alpha);

    var out: OITOutput;
    out.accum = vec4<f32>(final_color, 1.0) * w;
    out.reveal = 1.0 - alpha;

    return out;
}