aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/effect_materials.rs
blob: bb3c6b0eb0caea8cbd33cf6f475c379c345007ad (plain) (blame)
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
use super::{
    solid_environment_texture, vulkan_texture_from_rgba_mips, PreparedEffectMaterial,
    VulkanStaticMaterial,
};
use fparkan_assets::load_effect_material_phases;
use fparkan_resource::CachedResourceRepository;
use fparkan_vfs::DirectoryVfs;
use std::{
    collections::{hash_map::Entry, HashMap},
    path::Path,
    sync::Arc,
};

/// Reuses archive reads and material selectors during a scene preparation pass.
pub(super) struct EffectMaterials {
    repository: CachedResourceRepository,
    materials: HashMap<String, PreparedEffectMaterial>,
}

impl EffectMaterials {
    pub(super) fn new(root: &Path) -> Self {
        Self {
            repository: CachedResourceRepository::new(Arc::new(DirectoryVfs::new(root))),
            materials: HashMap::new(),
        }
    }

    pub(super) fn prepare(
        &mut self,
        archive: &str,
        name: &str,
        materials: &mut Vec<VulkanStaticMaterial>,
    ) -> Result<&PreparedEffectMaterial, String> {
        let entry = match self.materials.entry(format!("{archive}/{name}")) {
            Entry::Occupied(entry) => return Ok(entry.into_mut()),
            Entry::Vacant(entry) => entry,
        };
        let key = entry.key();
        let (document, phases) = load_effect_material_phases(&self.repository, archive, name)
            .map_err(|error| format!("resolve FX MAT0 {key}: {error}"))?;
        let mut phase_material_indices = Vec::with_capacity(document.phases.len());
        let mut phase_uv_transforms = Vec::with_capacity(document.phases.len());
        let mut phase_native_flag_40 = Vec::with_capacity(document.phases.len());
        let mut phase_directional_rgb = Vec::with_capacity(document.phases.len());
        let mut phase_additive_rgb = Vec::with_capacity(document.phases.len());
        for (mips, phase) in phases {
            let texture = vulkan_texture_from_rgba_mips(mips, "FX material")?;
            let index = materials.len();
            let material_index = u16::try_from(index).map_err(|_| {
                "FX effects exceed the available 16-bit material selector space".to_string()
            })?;
            materials.push(VulkanStaticMaterial {
                material_index,
                pipeline_category: phase.pipeline_category,
                texture,
                directional_rgb: phase.directional_rgb,
                additive_rgb: phase.additive_rgb,
                uv_transform: phase.page_uv_transform,
                detail_texture: Some(solid_environment_texture([128, 128, 128, 255])),
                overlay_texture: None,
                overlay_detail_texture: None,
                // FX descriptor opacity is carried by each frame's vertex alpha.
                // Keep MAT0 alpha at one so it does not multiply the opacity a
                // second time.
                diffuse_alpha: 1.0,
                overlay_diffuse_alpha: None,
                overlay_directional_rgb: None,
                overlay_additive_rgb: None,
                specular_rgb: phase.specular_rgb,
                specular_power: phase.power,
                overlay_specular_rgb: None,
                overlay_specular_power: None,
                detail_uv_transform: [0.0, 0.0, 1.0, 1.0],
                overlay_uv_transform: None,
                overlay_detail_uv_transform: None,
                unlit: true,
                sky_nebula_stars: false,
                lightmap_mode: false,
                sky_far_depth: false,
                screen_tl: false,
            });
            phase_material_indices.push(index);
            phase_uv_transforms.push(phase.page_uv_transform);
            phase_native_flag_40.push(phase.native_flag_40);
            phase_directional_rgb.push(phase.directional_rgb);
            phase_additive_rgb.push(phase.additive_rgb);
        }
        let prepared = PreparedEffectMaterial {
            initial_pipeline_category: materials[phase_material_indices[0]].pipeline_category,
            document,
            phase_material_indices,
            phase_uv_transforms,
            phase_native_flag_40,
            phase_directional_rgb,
            phase_additive_rgb,
        };
        Ok(entry.insert(prepared))
    }
}