aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/model_materials.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-02 20:55:24 +0300
committerValentin Popov <valentin@popov.link>2026-12-02 20:55:24 +0300
commitbd3fbb71cdef1cda0e4db5efa14c632ae72859d6 (patch)
tree610ba427410fcabdb59e80dcf26bf430852d5d08 /apps/fparkan-game/src/model_materials.rs
parentf35ccda5dd88cb0ddebed33efa2660c1b9742cbe (diff)
downloadfparkan-bd3fbb71cdef1cda0e4db5efa14c632ae72859d6.tar.xz
fparkan-bd3fbb71cdef1cda0e4db5efa14c632ae72859d6.zip
fix(ui): restore native cockpit miniature lighting
Diffstat (limited to 'apps/fparkan-game/src/model_materials.rs')
-rw-r--r--apps/fparkan-game/src/model_materials.rs192
1 files changed, 192 insertions, 0 deletions
diff --git a/apps/fparkan-game/src/model_materials.rs b/apps/fparkan-game/src/model_materials.rs
new file mode 100644
index 00000000..8e44dde7
--- /dev/null
+++ b/apps/fparkan-game/src/model_materials.rs
@@ -0,0 +1,192 @@
+//! Scene and instrument variants of a model's authored materials.
+
+use super::{
+ page_uv_transform, phase_texture_name, phase_uv_transforms_from_assets,
+ prepared_texture_to_vulkan, solid_environment_texture, MaterialAnimationBinding,
+ MaterialPhaseBinding, MaterialVariantSet, MissionAssets, PreparedTextureUsage, PreparedVisual,
+ VulkanStaticMaterial, VulkanStaticMesh,
+};
+use fparkan_material::MaterialCoefficients;
+use fparkan_render::cockpit_lighting::CAMERA_ADDITIVE;
+
+#[derive(Clone, Copy, PartialEq, Eq)]
+pub(super) enum ModelMaterialStyle {
+ Scene,
+ #[cfg(windows)]
+ Instrument,
+}
+
+pub(super) fn apply_instrument_camera_color(coefficients: &mut MaterialCoefficients) {
+ // Terrain+0x29b90 replaces the material's additive color and opacity.
+ coefficients.additive_rgb = CAMERA_ADDITIVE;
+ coefficients.opacity = 1.0;
+}
+
+pub(super) fn static_preview_component_materials(
+ assets: &MissionAssets,
+ visual: &PreparedVisual,
+ mesh: &VulkanStaticMesh,
+ static_material_phase: Option<u16>,
+ materials: &mut Vec<VulkanStaticMaterial>,
+ material_animations: &mut Vec<MaterialAnimationBinding>,
+ style: ModelMaterialStyle,
+) -> Result<Vec<(u16, u16)>, String> {
+ let instrument = match style {
+ ModelMaterialStyle::Scene => false,
+ #[cfg(windows)]
+ ModelMaterialStyle::Instrument => true,
+ };
+ let mut source_selectors = mesh
+ .draw_ranges
+ .iter()
+ .map(|range| range.material_index)
+ .collect::<Vec<_>>();
+ source_selectors.sort_unstable();
+ source_selectors.dedup();
+ source_selectors
+ .into_iter()
+ .map(|source_selector| {
+ let material_id = visual
+ .material_ids
+ .get(usize::from(source_selector))
+ .ok_or_else(|| {
+ format!(
+ "static preview MSH batch selector {source_selector} has no prepared WEAR material"
+ )
+ })?;
+ let material = assets.material_by_id(*material_id).ok_or_else(|| {
+ format!("static preview prepared material {source_selector} is unavailable")
+ })?;
+ let pipeline_category = if instrument {
+ 0
+ } else {
+ material.pipeline_category
+ };
+ let initial_phase_index = usize::from(static_material_phase.unwrap_or(0))
+ .min(material.mat0.phases.len().saturating_sub(1));
+ if material.mat0.phases.is_empty() {
+ return Err(format!(
+ "static preview material {source_selector} has no MAT0 phases"
+ ));
+ }
+ let mut phase_material_indices = Vec::with_capacity(material.mat0.phases.len());
+ for phase in &material.mat0.phases {
+ let texture_name = if instrument {
+ None
+ } else {
+ phase_texture_name(phase)
+ };
+ let prepared_texture = texture_name.as_ref().and_then(|name| {
+ assets.textures.iter().find(|texture| {
+ texture.usage == PreparedTextureUsage::Diffuse
+ && texture.source.name == *name
+ })
+ });
+ // Empty MAT0 texture names remain valid untextured materials.
+ let texture = prepared_texture
+ .map(|texture| {
+ prepared_texture_to_vulkan(texture, "static preview diffuse texture")
+ })
+ .transpose()?
+ .unwrap_or_else(|| solid_environment_texture([255, 255, 255, 255]));
+ let mut coefficients = phase.coefficients();
+ if instrument {
+ apply_instrument_camera_color(&mut coefficients);
+ }
+ let uv_transform = if let Some(texture) = prepared_texture {
+ page_uv_transform(
+ texture.texm.width(),
+ texture.texm.height(),
+ &texture.texm.page_rects(),
+ coefficients.page_index,
+ )?
+ } else {
+ [0.0, 0.0, 1.0, 1.0]
+ };
+ let preview_selector = u16::try_from(materials.len()).map_err(|_| {
+ "static preview exceeds the available 16-bit material selector space"
+ .to_string()
+ })?;
+ materials.push(VulkanStaticMaterial {
+ material_index: preview_selector,
+ pipeline_category,
+ texture,
+ detail_texture: None,
+ overlay_texture: None,
+ overlay_detail_texture: None,
+ diffuse_alpha: coefficients.opacity,
+ overlay_diffuse_alpha: None,
+ directional_rgb: coefficients.directional_rgb,
+ additive_rgb: coefficients.additive_rgb,
+ uv_transform,
+ overlay_directional_rgb: None,
+ overlay_additive_rgb: None,
+ specular_rgb: coefficients.specular_rgb,
+ specular_power: coefficients.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: false,
+ sky_nebula_stars: false,
+ lightmap_mode: false,
+ sky_far_depth: false,
+ screen_tl: false,
+ });
+ phase_material_indices.push(usize::from(preview_selector));
+ }
+ let preview_selector = u16::try_from(
+ *phase_material_indices
+ .get(initial_phase_index)
+ .ok_or_else(|| "MAT0 phase variant table is empty".to_string())?,
+ )
+ .map_err(|_| "MAT0 phase variant selector exceeds u16".to_string())?;
+ material_animations.push(MaterialAnimationBinding {
+ initial_phase_index,
+ normalized_phase_override: None,
+ ambient_fx_material_alpha: false,
+ cockpit_instrument: instrument,
+ variants: vec![MaterialVariantSet {
+ phase_material_indices,
+ range_indices: Vec::new(),
+ pipeline_category,
+ }],
+ base: MaterialPhaseBinding {
+ document: material.mat0.clone(),
+ animation_block_index: 0,
+ wear_row_start_ms: 0,
+ random_state: material_id.raw() as u32,
+ frozen_phase_index: static_material_phase.map(usize::from),
+ phase_uv_transforms: if instrument {
+ vec![[0.0, 0.0, 1.0, 1.0]; material.mat0.phases.len()]
+ } else {
+ phase_uv_transforms_from_assets(&material.mat0, assets)
+ },
+ },
+ detail: None,
+ overlay: None,
+ overlay_detail: None,
+ stage_textures: None,
+ });
+ Ok((source_selector, preview_selector))
+ })
+ .collect()
+}
+
+#[cfg(test)]
+#[test]
+fn instrument_camera_replaces_the_base_color_and_opacity() {
+ let phase = fparkan_material::MaterialPhase {
+ parameters: [100; 18],
+ texture_raw: [0; 16],
+ };
+ let original = phase.coefficients();
+ let mut instrument = original;
+ apply_instrument_camera_color(&mut instrument);
+ assert_eq!(instrument.additive_rgb, [0.0, 0.5, 0.0]);
+ assert_eq!(instrument.opacity, 1.0);
+ assert_eq!(instrument.directional_rgb, original.directional_rgb);
+ assert_eq!(instrument.specular_rgb, original.specular_rgb);
+ assert_eq!(instrument.power, original.power);
+}