nightshade 0.13.3

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
use crate::ecs::world::World;
use crate::render::wgpu::brdf_lut;
use crate::render::wgpu::font_atlas;
use crate::render::wgpu::passes;
use crate::render::wgpu::rendergraph::RenderGraph;

pub struct XrRenderer {
    graph: RenderGraph<World>,
    _depth_id: crate::render::wgpu::rendergraph::ResourceId,
    _scene_color_id: crate::render::wgpu::rendergraph::ResourceId,
    swapchain_id: crate::render::wgpu::rendergraph::ResourceId,
    _brdf_lut_texture: wgpu::Texture,
    brdf_lut_view: wgpu::TextureView,
    _msdf_font_textures: Vec<(wgpu::Texture, wgpu::TextureView)>,
    font_data: Vec<crate::ecs::text::resources::FontAtlasData>,
    font_initialized: bool,
}

impl XrRenderer {
    pub fn new(
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        resolution: (u32, u32),
        surface_format: wgpu::TextureFormat,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        let depth_format = wgpu::TextureFormat::Depth32Float;
        let hdr_format = wgpu::TextureFormat::Rgba16Float;

        let clear_pass = passes::ClearPass::new();
        let sky_pass = passes::SkyPass::new(device, hdr_format, depth_format);
        let grid_pass = passes::GridPass::new(device, hdr_format, depth_format);
        let scene_pass = passes::ScenePass::new(device, hdr_format, depth_format);
        let shadow_depth_pass = passes::ShadowDepthPass::new(device, queue);
        let mesh_pass = passes::MeshPass::new(device, queue, hdr_format, depth_format, resolution);
        let skinned_mesh_pass =
            passes::SkinnedMeshPass::new(device, queue, hdr_format, depth_format);
        let water_pass = passes::WaterPass::new(device, hdr_format, depth_format);
        let water_mesh_pass = passes::WaterMeshPass::new(device, hdr_format, depth_format);
        let lines_pass = passes::LinesPass::new(device, hdr_format);
        let particle_pass = passes::ParticlePass::new(device, hdr_format);

        let mut graph = RenderGraph::new();

        let depth_id = graph
            .add_depth_texture("depth")
            .size(resolution.0.max(1), resolution.1.max(1))
            .clear_depth(0.0)
            .transient();

        let scene_color_id = graph
            .add_color_texture("scene_color")
            .format(hdr_format)
            .size(resolution.0.max(1), resolution.1.max(1))
            .clear_color(wgpu::Color::BLACK)
            .transient();

        let swapchain_id = graph
            .add_color_texture("swapchain")
            .format(surface_format)
            .external();

        let shadow_depth_id = graph
            .add_depth_texture("shadow_depth")
            .size(2048, 2048)
            .format(wgpu::TextureFormat::Depth32Float)
            .clear_depth(0.0)
            .transient();

        let spotlight_shadow_atlas_id = graph
            .add_depth_texture("spotlight_shadow_atlas")
            .size(2048, 2048)
            .format(wgpu::TextureFormat::Depth32Float)
            .clear_depth(0.0)
            .transient();

        let entity_id_id = graph
            .add_color_texture("entity_id")
            .format(wgpu::TextureFormat::R32Float)
            .size(resolution.0.max(1), resolution.1.max(1))
            .usage(wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING)
            .clear_color(wgpu::Color::TRANSPARENT)
            .transient();

        let view_normals_id = graph
            .add_color_texture("view_normals")
            .format(wgpu::TextureFormat::Rgba16Float)
            .size(resolution.0.max(1), resolution.1.max(1))
            .usage(wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING)
            .clear_color(wgpu::Color::TRANSPARENT)
            .transient();

        graph.add_pass(
            Box::new(clear_pass),
            &[("color", scene_color_id), ("depth", depth_id)],
        )?;

        graph.add_pass(Box::new(sky_pass), &[("color", scene_color_id)])?;

        graph.add_pass(
            Box::new(scene_pass),
            &[("color", scene_color_id), ("depth", depth_id)],
        )?;

        graph.add_pass(
            Box::new(shadow_depth_pass),
            &[
                ("shadow_depth", shadow_depth_id),
                ("spotlight_shadow_atlas", spotlight_shadow_atlas_id),
            ],
        )?;

        graph.add_pass(
            Box::new(mesh_pass),
            &[
                ("color", scene_color_id),
                ("depth", depth_id),
                ("shadow_depth", shadow_depth_id),
                ("spotlight_shadow_atlas", spotlight_shadow_atlas_id),
                ("entity_id", entity_id_id),
                ("view_normals", view_normals_id),
            ],
        )?;

        graph.add_pass(
            Box::new(skinned_mesh_pass),
            &[
                ("color", scene_color_id),
                ("depth", depth_id),
                ("shadow_depth", shadow_depth_id),
                ("spotlight_shadow_atlas", spotlight_shadow_atlas_id),
            ],
        )?;

        graph.add_pass(
            Box::new(water_pass),
            &[("color", scene_color_id), ("depth", depth_id)],
        )?;

        graph.add_pass(
            Box::new(water_mesh_pass),
            &[("color", scene_color_id), ("depth", depth_id)],
        )?;

        graph.add_pass(
            Box::new(grid_pass),
            &[("color", scene_color_id), ("depth", depth_id)],
        )?;

        graph.add_pass(
            Box::new(lines_pass),
            &[("color", scene_color_id), ("depth", depth_id)],
        )?;

        graph.add_pass(
            Box::new(particle_pass),
            &[("color", scene_color_id), ("depth", depth_id)],
        )?;

        let bloom_width = (resolution.0 / 4).max(1);
        let bloom_height = (resolution.1 / 4).max(1);

        let bloom_texture_id = graph
            .add_color_texture("bloom")
            .format(hdr_format)
            .size(bloom_width, bloom_height)
            .clear_color(wgpu::Color::BLACK)
            .transient();

        let bloom_pass = passes::BloomPass::new(device, bloom_width * 2, bloom_height * 2);
        graph.add_pass(
            Box::new(bloom_pass),
            &[("hdr", scene_color_id), ("bloom", bloom_texture_id)],
        )?;

        let ssao_width = (resolution.0 / 2).max(1);
        let ssao_height = (resolution.1 / 2).max(1);

        let ssao_raw_id = graph
            .add_color_texture("ssao_raw")
            .format(wgpu::TextureFormat::R8Unorm)
            .size(ssao_width, ssao_height)
            .usage(wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING)
            .clear_color(wgpu::Color::WHITE)
            .transient();

        let ssao_id = graph
            .add_color_texture("ssao")
            .format(wgpu::TextureFormat::R8Unorm)
            .size(ssao_width, ssao_height)
            .usage(wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING)
            .clear_color(wgpu::Color::WHITE)
            .transient();

        let ssao_pass = passes::SsaoPass::new(device);
        graph.add_pass(
            Box::new(ssao_pass),
            &[
                ("depth", depth_id),
                ("view_normals", view_normals_id),
                ("ssao_raw", ssao_raw_id),
            ],
        )?;

        let ssao_blur_pass = passes::SsaoBlurPass::new(device);
        graph.add_pass(
            Box::new(ssao_blur_pass),
            &[
                ("ssao_raw", ssao_raw_id),
                ("depth", depth_id),
                ("view_normals", view_normals_id),
                ("ssao", ssao_id),
            ],
        )?;

        let msdf_atlas = font_atlas::generate_font_atlas_msdf(
            device,
            queue,
            font_atlas::DEFAULT_FONT_DATA,
            32.0,
        )?;

        let mut _msdf_font_textures = Vec::new();
        _msdf_font_textures.push((msdf_atlas.texture, msdf_atlas.texture_view));

        let font_data = vec![crate::ecs::text::resources::FontAtlasData {
            texture_index: 0,
            glyphs: msdf_atlas.glyphs,
            kerning: msdf_atlas.kerning,
            width: msdf_atlas.width,
            height: msdf_atlas.height,
            font_size: msdf_atlas.font_size,
            sdf_range: msdf_atlas.sdf_range,
        }];

        let mut text_pass = passes::TextPass::new(device, hdr_format, depth_format);
        text_pass.update_texture_bind_groups(device, &_msdf_font_textures);

        graph.add_pass(
            Box::new(text_pass),
            &[("color", scene_color_id), ("depth", depth_id)],
        )?;

        let postprocess_output_id = graph
            .add_color_texture("postprocess_output")
            .format(surface_format)
            .size(resolution.0.max(1), resolution.1.max(1))
            .usage(wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::RENDER_ATTACHMENT)
            .transient();

        let postprocess_pass = passes::PostProcessPass::new(device, surface_format, 0.1);
        graph.add_pass(
            Box::new(postprocess_pass),
            &[
                ("hdr", scene_color_id),
                ("bloom", bloom_texture_id),
                ("ssao", ssao_id),
                ("output", postprocess_output_id),
            ],
        )?;

        let fxaa_pass = passes::FxaaPass::new(device, surface_format);
        graph.add_pass(
            Box::new(fxaa_pass),
            &[("input", postprocess_output_id), ("output", swapchain_id)],
        )?;

        graph.compile()?;

        let (brdf_lut_texture, brdf_lut_view) = brdf_lut::generate_brdf_lut(device, queue);

        Ok(Self {
            graph,
            _depth_id: depth_id,
            _scene_color_id: scene_color_id,
            swapchain_id,
            _brdf_lut_texture: brdf_lut_texture,
            brdf_lut_view,
            _msdf_font_textures,
            font_data,
            font_initialized: false,
        })
    }

    pub fn render_eye(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        world: &mut World,
        eye_texture_view: wgpu::TextureView,
        resolution: (u32, u32),
    ) -> Result<(), Box<dyn std::error::Error>> {
        self.graph.set_external_texture(
            self.swapchain_id,
            None,
            eye_texture_view,
            resolution.0,
            resolution.1,
        );

        let command_buffers = self.graph.execute(device, queue, world)?;
        queue.submit(command_buffers);

        Ok(())
    }

    pub fn process_commands(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        world: &mut World,
    ) {
        let mut texture_load_commands = Vec::new();
        let mut hdr_skybox_commands = Vec::new();
        let mut procedural_ibl_commands = Vec::new();
        let mut ibl_snapshot_commands = Vec::new();

        for cmd in world.resources.command_queue.iter() {
            match cmd {
                crate::ecs::world::WorldCommand::LoadTexture {
                    name,
                    rgba_data,
                    width,
                    height,
                } => {
                    texture_load_commands.push((name.clone(), rgba_data.clone(), *width, *height));
                }
                crate::ecs::world::WorldCommand::LoadHdrSkybox { hdr_data } => {
                    hdr_skybox_commands.push(hdr_data.clone());
                }
                crate::ecs::world::WorldCommand::LoadHdrSkyboxFromPath { path } => {
                    match std::fs::read(path) {
                        Ok(bytes) => {
                            hdr_skybox_commands.push(bytes);
                        }
                        Err(error) => {
                            tracing::error!(
                                "Failed to read HDR file {}: {}",
                                path.display(),
                                error
                            );
                        }
                    }
                }
                crate::ecs::world::WorldCommand::CaptureProceduralAtmosphereIBL {
                    atmosphere,
                    time,
                } => {
                    procedural_ibl_commands.push((*atmosphere, *time));
                }
                crate::ecs::world::WorldCommand::CaptureIblSnapshots { atmosphere, hours } => {
                    ibl_snapshot_commands.push((*atmosphere, hours.clone()));
                }
                _ => {}
            }
        }

        if !texture_load_commands.is_empty() {
            for (name, rgba_data, width, height) in texture_load_commands {
                if let Err(e) = world.resources.texture_cache.load_texture_from_raw_rgba(
                    device,
                    queue,
                    name.clone(),
                    &rgba_data,
                    (width, height),
                ) {
                    tracing::error!("Failed to load texture: {}", e);
                    continue;
                }

                if let Some(mesh_pass) = self.graph.get_pass_mut("mesh_pass")
                    && let Some(mesh_pass) =
                        (mesh_pass as &mut dyn std::any::Any).downcast_mut::<passes::MeshPass>()
                    && let Some(texture_entry) = world.resources.texture_cache.get(&name)
                {
                    mesh_pass.register_texture(
                        name.clone(),
                        texture_entry.view.clone(),
                        texture_entry.sampler.clone(),
                    );
                }

                if let Some(skinned_mesh_pass) = self.graph.get_pass_mut("skinned_mesh_pass")
                    && let Some(skinned_mesh_pass) = (skinned_mesh_pass as &mut dyn std::any::Any)
                        .downcast_mut::<passes::SkinnedMeshPass>()
                    && let Some(texture_entry) = world.resources.texture_cache.get(&name)
                {
                    skinned_mesh_pass.register_texture(
                        name,
                        texture_entry.view.clone(),
                        texture_entry.sampler.clone(),
                    );
                }
            }

            world
                .resources
                .command_queue
                .retain(|cmd| !matches!(cmd, crate::ecs::world::WorldCommand::LoadTexture { .. }));
        }

        if !hdr_skybox_commands.is_empty() {
            let mut ibl_views = None;

            if let Some(sky_pass) = self.graph.get_pass_mut("sky_pass")
                && let Some(sky_pass) =
                    (sky_pass as &mut dyn std::any::Any).downcast_mut::<passes::geometry::SkyPass>()
            {
                for hdr_data in hdr_skybox_commands {
                    ibl_views = Some(sky_pass.load_hdr_skybox(device, queue, &hdr_data));
                }
            }

            if let Some((irradiance_view, prefiltered_view)) = ibl_views {
                if let Some(mesh_pass) = self.graph.get_pass_mut("mesh_pass")
                    && let Some(mesh_pass) =
                        (mesh_pass as &mut dyn std::any::Any).downcast_mut::<passes::MeshPass>()
                {
                    mesh_pass.update_ibl_textures(
                        self.brdf_lut_view.clone(),
                        irradiance_view.clone(),
                        prefiltered_view.clone(),
                    );
                }

                if let Some(skinned_mesh_pass) = self.graph.get_pass_mut("skinned_mesh_pass")
                    && let Some(skinned_mesh_pass) = (skinned_mesh_pass as &mut dyn std::any::Any)
                        .downcast_mut::<passes::geometry::SkinnedMeshPass>(
                    )
                {
                    skinned_mesh_pass.set_ibl_textures(
                        self.brdf_lut_view.clone(),
                        irradiance_view.clone(),
                        prefiltered_view.clone(),
                    );
                }

                world.resources.ibl_views.brdf_lut_view = Some(self.brdf_lut_view.clone());
                world.resources.ibl_views.irradiance_view = Some(irradiance_view);
                world.resources.ibl_views.prefiltered_view = Some(prefiltered_view);
            }

            world.resources.command_queue.retain(|cmd| {
                !matches!(
                    cmd,
                    crate::ecs::world::WorldCommand::LoadHdrSkybox { .. }
                        | crate::ecs::world::WorldCommand::LoadHdrSkyboxFromPath { .. }
                )
            });
        }

        #[cfg(feature = "assets")]
        if !procedural_ibl_commands.is_empty() {
            let mut ibl_views = None;

            if let Some(sky_pass) = self.graph.get_pass_mut("sky_pass")
                && let Some(sky_pass) =
                    (sky_pass as &mut dyn std::any::Any).downcast_mut::<passes::geometry::SkyPass>()
            {
                for (atmosphere, time) in procedural_ibl_commands {
                    ibl_views =
                        sky_pass.capture_procedural_atmosphere(device, queue, atmosphere, time);
                }
            }

            if let Some((irradiance_view, prefiltered_view)) = ibl_views {
                if let Some(mesh_pass) = self.graph.get_pass_mut("mesh_pass")
                    && let Some(mesh_pass) =
                        (mesh_pass as &mut dyn std::any::Any).downcast_mut::<passes::MeshPass>()
                {
                    mesh_pass.update_ibl_textures(
                        self.brdf_lut_view.clone(),
                        irradiance_view.clone(),
                        prefiltered_view.clone(),
                    );
                }

                if let Some(skinned_mesh_pass) = self.graph.get_pass_mut("skinned_mesh_pass")
                    && let Some(skinned_mesh_pass) = (skinned_mesh_pass as &mut dyn std::any::Any)
                        .downcast_mut::<passes::geometry::SkinnedMeshPass>(
                    )
                {
                    skinned_mesh_pass.set_ibl_textures(
                        self.brdf_lut_view.clone(),
                        irradiance_view.clone(),
                        prefiltered_view.clone(),
                    );
                }

                world.resources.ibl_views.brdf_lut_view = Some(self.brdf_lut_view.clone());
                world.resources.ibl_views.irradiance_view = Some(irradiance_view);
                world.resources.ibl_views.prefiltered_view = Some(prefiltered_view);
            }

            world.resources.command_queue.retain(|cmd| {
                !matches!(
                    cmd,
                    crate::ecs::world::WorldCommand::CaptureProceduralAtmosphereIBL { .. }
                )
            });
        }

        #[cfg(feature = "assets")]
        if !ibl_snapshot_commands.is_empty() {
            if let Some(sky_pass) = self.graph.get_pass_mut("sky_pass")
                && let Some(sky_pass) =
                    (sky_pass as &mut dyn std::any::Any).downcast_mut::<passes::geometry::SkyPass>()
            {
                for (atmosphere, hours) in ibl_snapshot_commands {
                    sky_pass.capture_ibl_snapshots(device, queue, atmosphere, &hours);
                }
            }

            world.resources.command_queue.retain(|cmd| {
                !matches!(
                    cmd,
                    crate::ecs::world::WorldCommand::CaptureIblSnapshots { .. }
                )
            });
        }

        if let Some(lines_pass) = self.graph.get_pass_mut("lines_pass")
            && let Some(lines_pass) =
                (lines_pass as &mut dyn std::any::Any).downcast_mut::<passes::LinesPass>()
        {
            passes::sync_lines_data(lines_pass, device, queue, world);
        }
    }

    pub fn update_per_frame_passes(&mut self, world: &mut World) {
        let point_shadow_view = self
            .graph
            .get_pass_mut("shadow_depth_pass")
            .and_then(|pass| {
                (pass as &dyn std::any::Any)
                    .downcast_ref::<passes::ShadowDepthPass>()
                    .map(|shadow_pass| shadow_pass.point_light_cubemap_view().clone())
            });

        if let Some(view) = point_shadow_view {
            if let Some(mesh_pass) = self.graph.get_pass_mut("mesh_pass")
                && let Some(mesh_pass) =
                    (mesh_pass as &mut dyn std::any::Any).downcast_mut::<passes::MeshPass>()
            {
                mesh_pass.update_point_shadow_cubemap(view.clone());
            }
            if let Some(skinned_mesh_pass) = self.graph.get_pass_mut("skinned_mesh_pass")
                && let Some(skinned_mesh_pass) = (skinned_mesh_pass as &mut dyn std::any::Any)
                    .downcast_mut::<passes::SkinnedMeshPass>()
            {
                skinned_mesh_pass.update_point_shadow_cubemap(view);
            }
        }

        {
            let raw_hour = world.resources.graphics.day_night.hour;
            let hour = raw_hour - (raw_hour / 24.0).floor() * 24.0;
            let world_id = world.resources.world_id;
            let mut bracket_views: Option<(
                wgpu::TextureView,
                wgpu::TextureView,
                wgpu::TextureView,
                wgpu::TextureView,
                f32,
            )> = None;

            if let Some(sky_pass) = self.graph.get_pass_mut("sky_pass")
                && let Some(sky_pass) =
                    (sky_pass as &mut dyn std::any::Any).downcast_mut::<passes::geometry::SkyPass>()
                && let Some((index_a, index_b, blend)) =
                    sky_pass.select_ibl_bracket(world.resources.graphics.atmosphere, hour)
            {
                let irr_a =
                    sky_pass.ibl_snapshots[index_a]
                        .1
                        .create_view(&wgpu::TextureViewDescriptor {
                            dimension: Some(wgpu::TextureViewDimension::Cube),
                            ..Default::default()
                        });
                let pref_a =
                    sky_pass.ibl_snapshots[index_a]
                        .2
                        .create_view(&wgpu::TextureViewDescriptor {
                            dimension: Some(wgpu::TextureViewDimension::Cube),
                            ..Default::default()
                        });
                let irr_b =
                    sky_pass.ibl_snapshots[index_b]
                        .1
                        .create_view(&wgpu::TextureViewDescriptor {
                            dimension: Some(wgpu::TextureViewDimension::Cube),
                            ..Default::default()
                        });
                let pref_b =
                    sky_pass.ibl_snapshots[index_b]
                        .2
                        .create_view(&wgpu::TextureViewDescriptor {
                            dimension: Some(wgpu::TextureViewDimension::Cube),
                            ..Default::default()
                        });
                bracket_views = Some((irr_a, pref_a, irr_b, pref_b, blend));
            }

            if let Some((irr_a, pref_a, irr_b, pref_b, blend)) = bracket_views {
                world.resources.graphics.ibl_blend_factor = blend;

                if let Some(mesh_pass) = self.graph.get_pass_mut("mesh_pass")
                    && let Some(mesh_pass) =
                        (mesh_pass as &mut dyn std::any::Any).downcast_mut::<passes::MeshPass>()
                {
                    mesh_pass.update_ibl_textures_blended(
                        self.brdf_lut_view.clone(),
                        irr_a.clone(),
                        pref_a.clone(),
                        irr_b.clone(),
                        pref_b.clone(),
                        blend,
                    );
                }

                if let Some(skinned_mesh_pass) = self.graph.get_pass_mut("skinned_mesh_pass")
                    && let Some(skinned_mesh_pass) = (skinned_mesh_pass as &mut dyn std::any::Any)
                        .downcast_mut::<passes::geometry::SkinnedMeshPass>(
                    )
                {
                    skinned_mesh_pass.update_ibl_textures_blended_for_world(
                        world_id,
                        passes::geometry::BlendedIblViews {
                            brdf_lut: self.brdf_lut_view.clone(),
                            irradiance_a: irr_a.clone(),
                            prefiltered_a: pref_a.clone(),
                            irradiance_b: irr_b,
                            prefiltered_b: pref_b,
                            blend_factor: blend,
                        },
                    );
                }

                world.resources.ibl_views.brdf_lut_view = Some(self.brdf_lut_view.clone());
                world.resources.ibl_views.irradiance_view = Some(irr_a);
                world.resources.ibl_views.prefiltered_view = Some(pref_a);
            }
        }
    }

    pub fn initialize_fonts(&mut self, world: &mut World) {
        if !self.font_initialized {
            for font_data in &self.font_data {
                let font_index = world
                    .resources
                    .text_cache
                    .font_manager
                    .add_font(font_data.clone());
                if world.resources.text_cache.font_manager.default_font == 0 {
                    world.resources.text_cache.font_manager.default_font = font_index;
                }
            }
            self.font_initialized = true;
        }
    }

    pub fn graph_mut(&mut self) -> &mut RenderGraph<World> {
        &mut self.graph
    }
}