aboutsummaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-14 23:54:44 +0300
committerValentin Popov <valentin@popov.link>2026-11-14 23:54:44 +0300
commit38915ebfb381fb00c6e4f1949b960bb142a7d7fe (patch)
treea16676ed6d311742c7721e326a757d018f607f50 /apps
parentc09870a4b28de3f140a6312e6f8159556187d13b (diff)
downloadfparkan-38915ebfb381fb00c6e4f1949b960bb142a7d7fe.tar.xz
fparkan-38915ebfb381fb00c6e4f1949b960bb142a7d7fe.zip
feat(fx): render manual laser projectile trails
Load the constant-width Op5 layers and their original materials from the projectile Control group. Draw the sampled segment through its first contact and hide it after that frame. Coalesce native subdivisions while keeping both layers. Effect.dll RVA 0x2a20 was checked directly with two world positions. A diagnostic input replay started Firing Range on the original map, crossed the bridge, captured and boarded its flying warbot, climbed, and fired. The two trails are visible on the terrain-hit frame with Vulkan validation clean.
Diffstat (limited to 'apps')
-rw-r--r--apps/fparkan-game/src/main.rs56
-rw-r--r--apps/fparkan-game/src/projectile_trail.rs133
2 files changed, 184 insertions, 5 deletions
diff --git a/apps/fparkan-game/src/main.rs b/apps/fparkan-game/src/main.rs
index d201c884..07bac2aa 100644
--- a/apps/fparkan-game/src/main.rs
+++ b/apps/fparkan-game/src/main.rs
@@ -8,6 +8,7 @@ mod control_effects;
use control_effects::{advance_control_effect_instances, owner_control_capacity};
mod mission_hints;
mod player_collision;
+mod projectile_trail;
mod script_coverage;
mod terrain_lod;
@@ -4927,6 +4928,8 @@ struct ProjectileDrawSlot {
shooter: Option<ObjectHandle>,
vertex_start: usize,
draw_ranges: std::ops::Range<usize>,
+ trails: Vec<projectile_trail::ProjectileTrail>,
+ trail_segment: Option<[[f32; 3]; 2]>,
}
struct PlayerWeaponLayout {
@@ -9467,11 +9470,13 @@ fn append_static_preview_component(
}
fn prepare_projectile_draw(
+ root: &Path,
engine: &Engine,
archive: &str,
name: &str,
capacity: impl FnOnce(u32) -> u32,
preview: &mut StaticPreviewScene,
+ trail_materials: Option<&mut HashMap<String, PreparedEffectMaterial>>,
) -> Result<Option<ProjectileDraw>, String> {
let projectile = load_player_projectile(engine, archive, name)?;
let assets = &projectile.assets;
@@ -9506,6 +9511,9 @@ fn prepare_projectile_draw(
&mut preview.material_animations,
)?;
let capacity = capacity(projectile.lifetime_ms).min(128);
+ let layers = trail_materials.map_or_else(Vec::new, |materials| {
+ projectile_trail::prepare(root, &projectile, name, preview, materials)
+ });
let mut instances = Vec::new();
for _ in 0..capacity {
let vertex_start = preview.mesh.vertices.len();
@@ -9516,11 +9524,18 @@ fn prepare_projectile_draw(
&selector_remap,
Some(&mut preview.material_animations),
)?;
+ let draw_ranges = range_start..preview.mesh.draw_ranges.len();
+ let trails = layers
+ .iter()
+ .map(|&layer| projectile_trail::ProjectileTrail::allocate(&mut preview.mesh, layer))
+ .collect::<Result<_, _>>()?;
instances.push(ProjectileDrawSlot {
flight: None,
shooter: None,
vertex_start,
- draw_ranges: range_start..preview.mesh.draw_ranges.len(),
+ draw_ranges,
+ trails,
+ trail_segment: None,
});
}
Ok(Some(ProjectileDraw {
@@ -9813,6 +9828,7 @@ fn run_static_vulkan_mode(
let cockpit = mission_engine.as_ref().and_then(starting_cockpit);
let mut player_weapon_layouts = HashMap::new();
let mut projectile_draws = Vec::new();
+ let mut projectile_effect_materials = HashMap::new();
if let (Some(engine), Some(cockpit)) = (&mission_engine, cockpit) {
let mut pools = Vec::<(String, String, Vec<u32>)>::new();
let hero = (
@@ -9866,11 +9882,13 @@ fn run_static_vulkan_mode(
})
};
let draw = match prepare_projectile_draw(
+ root,
engine,
&archive,
&name,
capacity,
&mut preview,
+ Some(&mut projectile_effect_materials),
) {
Ok(draw @ Some(_)) => draw,
Ok(None) => {
@@ -9887,7 +9905,7 @@ fn run_static_vulkan_mode(
}
let autodemo_draws = mission_engine.as_ref().map_or_else(Vec::new, |engine| {
fparkan_runtime::autodemo_projectile_types(engine).iter().map(|kind| {
- match prepare_projectile_draw(engine, &kind.archive, &kind.name, |_| kind.capacity, &mut preview) {
+ match prepare_projectile_draw(root, engine, &kind.archive, &kind.name, |_| kind.capacity, &mut preview, None) {
Ok(Some(draw)) => Some(draw),
result => {
let reason = result.err().unwrap_or_else(|| "effect-only body".into());
@@ -10759,6 +10777,7 @@ impl StaticVulkanApp {
for (pool, draw) in self.projectile_draws.iter_mut().enumerate() {
let Some(draw) = draw else { continue };
for instance in &mut draw.instances {
+ instance.trail_segment = None;
let (Some(flight), Some(shooter)) = (&mut instance.flight, instance.shooter) else {
continue;
};
@@ -10771,6 +10790,15 @@ impl StaticVulkanApp {
projectile_unit_hit(engine, shooter, segment).filter(|hit| {
terrain_contact.is_none_or(|ground| hit.fraction < ground)
});
+ if !instance.trails.is_empty() {
+ let [from, to] = segment;
+ let fraction = unit_contact
+ .map(|hit| hit.fraction)
+ .or(terrain_contact)
+ .unwrap_or(1.0);
+ instance.trail_segment =
+ Some([from, add3(from, scale3(sub3(to, from), fraction))]);
+ }
Ok(terrain_contact.is_some() || unit_contact.is_some())
})
.map_err(|error| format!("query projectile terrain: {error}"))?;
@@ -10809,11 +10837,19 @@ impl StaticVulkanApp {
fn update_projectile_geometry(&mut self) {
for draw in self
.projectile_draws
- .iter()
- .chain(&self.autodemo_draws)
+ .iter_mut()
+ .chain(&mut self.autodemo_draws)
.flatten()
{
- for instance in &draw.instances {
+ for instance in &mut draw.instances {
+ for trail in &mut instance.trails {
+ trail.sample(
+ &mut self.mesh.vertices,
+ instance.trail_segment,
+ self.view_camera.position,
+ self.view_camera.forward,
+ );
+ }
let Some(flight) = instance.flight else {
continue;
};
@@ -12033,6 +12069,16 @@ impl StaticVulkanApp {
.chain(&self.autodemo_draws)
.flatten()
{
+ for trail in draw
+ .instances
+ .iter()
+ .flat_map(|instance| &instance.trails)
+ .filter(|trail| !trail.visible)
+ {
+ renderer
+ .set_draw_range_index_count(trail.range.range_index, 0)
+ .map_err(|error| format!("hide inactive projectile trail: {error}"))?;
+ }
for instance in draw
.instances
.iter()
diff --git a/apps/fparkan-game/src/projectile_trail.rs b/apps/fparkan-game/src/projectile_trail.rs
new file mode 100644
index 00000000..3b1cdfda
--- /dev/null
+++ b/apps/fparkan-game/src/projectile_trail.rs
@@ -0,0 +1,133 @@
+use super::{
+ ambient_effect_pipeline_state, append_environment_range, environment_vertex,
+ fixed_effect_field, prepare_effect_material, quad_indices, EnvironmentGpuRange,
+ PreparedEffectMaterial, StaticPreviewScene,
+};
+use fparkan_assets::{load_resource_entry_bytes_from_root, PreparedControlEffectSection};
+use fparkan_fx::{decode_fxid, op5::TrailLayer, FxOpcode};
+use fparkan_runtime::ProjectileAssets;
+use std::{collections::HashMap, path::Path};
+
+pub(super) struct ProjectileTrail {
+ layer: TrailLayer,
+ pub(super) range: EnvironmentGpuRange,
+ pub(super) visible: bool,
+}
+
+pub(super) fn prepare(
+ root: &Path,
+ projectile: &ProjectileAssets,
+ name: &str,
+ preview: &mut StaticPreviewScene,
+ cache: &mut HashMap<String, PreparedEffectMaterial>,
+) -> Vec<(TrailLayer, usize, fparkan_render::LegacyPipelineState)> {
+ let Some(control) = projectile.assets.object_component_controls[0][0].as_ref() else {
+ return Vec::new();
+ };
+ let PreparedControlEffectSection::Parsed(rows) = &control.effect_section else {
+ return Vec::new();
+ };
+ let mut layers = Vec::new();
+ for row in rows.iter().filter(|row| {
+ Some(row.table_index) == control.initial_effect_group && row.raw_condition == [0; 3]
+ }) {
+ let archive = fixed_effect_field(&row.resource.archive);
+ let fx = fixed_effect_field(&row.resource.name);
+ let Ok(document) = load_resource_entry_bytes_from_root(root, &archive, &fx)
+ .map_err(|e| e.to_string())
+ .and_then(|bytes| decode_fxid(bytes).map_err(|e| e.to_string()))
+ else {
+ eprintln!("sim_coverage projectile_trail_resource_stub=1 projectile={name} effect={archive}/{fx}");
+ continue;
+ };
+ let header = document.header();
+ let [scale, sy, sz] = header.scale;
+ for command in document
+ .commands()
+ .iter()
+ .filter(|command| command.opcode == FxOpcode::Op5)
+ {
+ let prepared = (|| {
+ if header.time_mode != 0 || scale != sy || scale != sz {
+ return None;
+ }
+ let layer = TrailLayer::from_command(command, scale)?;
+ let reference = command.resource_refs.first()?;
+ let material = prepare_effect_material(
+ root,
+ std::str::from_utf8(reference.archive_name()).ok()?,
+ std::str::from_utf8(reference.resource_name()).ok()?,
+ &mut preview.materials,
+ cache,
+ )
+ .ok()?;
+ Some((
+ layer,
+ *material.phase_material_indices.first()?,
+ ambient_effect_pipeline_state(material.initial_pipeline_category)?,
+ ))
+ })();
+ if let Some(prepared) = prepared {
+ layers.push(prepared);
+ } else {
+ eprintln!("sim_coverage projectile_trail_command_stub=1 projectile={name} effect={archive}/{fx}");
+ }
+ }
+ }
+ if !layers.is_empty() {
+ eprintln!(
+ "sim_coverage projectile_trail_approx={} projectile={name}",
+ layers.len()
+ );
+ }
+ layers
+}
+
+impl ProjectileTrail {
+ pub(super) fn allocate(
+ mesh: &mut super::VulkanStaticMesh,
+ (layer, material, pipeline): (TrailLayer, usize, fparkan_render::LegacyPipelineState),
+ ) -> Result<Self, String> {
+ let range = append_environment_range(
+ mesh,
+ material,
+ vec![environment_vertex([0.0; 3], [1.0; 3], [0.0; 2]); 4],
+ quad_indices(1)?,
+ pipeline,
+ )?;
+ Ok(Self {
+ layer,
+ range,
+ visible: false,
+ })
+ }
+
+ pub(super) fn sample(
+ &mut self,
+ vertices: &mut [super::VulkanStaticVertex],
+ segment: Option<[[f32; 3]; 2]>,
+ camera_position: [f32; 3],
+ camera_forward: [f32; 3],
+ ) {
+ self.visible = false;
+ let Some([start, end]) = segment else { return };
+ let direction = std::array::from_fn(|i| (start[i] + end[i]) * 0.5 - camera_position[i]);
+ let Some((points, repeats)) = self
+ .layer
+ .ribbon([start, end], direction)
+ .or_else(|| self.layer.ribbon([start, end], camera_forward))
+ else {
+ return;
+ };
+ // shortcut: coalesce the native repeated segments into one textured ribbon.
+ let uvs = [[0.0, 0.0], [0.0, 1.0], [repeats, 0.0], [repeats, 1.0]];
+ let start = self.range.vertex_start;
+ for (vertex, (point, uv)) in vertices[start..start + 4]
+ .iter_mut()
+ .zip(points.into_iter().zip(uvs))
+ {
+ *vertex = environment_vertex(point, [1.0; 3], uv);
+ }
+ self.visible = true;
+ }
+}