mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-30 10:03:56 +01:00
Preserve Resources Updater links and bake provenance when replacing or clearing mesh maps. Keep unrelated maps on unchanged UV atlases, discard partial results from failed workers, and reject overflowing texture-set indices. Track vertex colors, part assignments, tangent frames and the packed file used by material-color bakes. Share the common source fingerprint across each task's selected maps. Add reimport, cancellation and atlas regressions, update the control protocol, and clarify the affected code.
247 lines
9.7 KiB
Rust
247 lines
9.7 KiB
Rust
//! Live bake preview: while the app bakes, the 3D view shows each mesh map on the model as soon
|
|
//! as it is finished ([`crate::BakeTask::run_streaming`]). The streamed maps live here, not in
|
|
//! the document: nothing enters the undo history until a texture set's bake is applied
|
|
//! ([`Session::apply_streamed_bake`]). The UI can apply eligible finished maps on cancellation.
|
|
//! Already applied sets keep their results and undo steps. The preview shows one raw map at a time.
|
|
//! Maps and texture sets that are still pending show their neutral value.
|
|
|
|
mod preview_set;
|
|
mod preview_show;
|
|
mod stage;
|
|
|
|
pub use preview_set::PreviewSet;
|
|
pub use preview_show::PreviewShow;
|
|
pub use stage::Stage;
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use serde_json::{Value, json};
|
|
use texturecraft_compose::BakedMaps;
|
|
use texturecraft_core::BakedMap;
|
|
|
|
use crate::bake::StreamedMap;
|
|
use crate::commands::{CommandSpec, always, bad, strp};
|
|
use crate::session::ViewMode;
|
|
use crate::{Result, Session, cmd};
|
|
|
|
/// What painting says while a bake preview shows (the app translates it).
|
|
pub const PAINTING_PAUSED: &str = "Painting is paused while a bake is shown. Cancel the bake or wait for it to finish.";
|
|
|
|
pub fn specs() -> Vec<CommandSpec> {
|
|
vec![cmd!(
|
|
"bake.preview",
|
|
"Bake preview",
|
|
[],
|
|
None,
|
|
"{show?: \"follow\" (the newest finished map) | \"material\" | \"map:<id>\" (pin a map)} → {active, show, shown, newest, stage, sets}. While the app bakes, the 3D view shows the maps as they finish; `show` picks what it shows. In Bake mode the app's map preview also shows here while Mesh Map Settings is in front. With neither {active: false} (setting `show` is then an error)",
|
|
always,
|
|
preview
|
|
)]
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct BakePreview {
|
|
/// The view mode to put back when the preview ends.
|
|
pub restore: ViewMode,
|
|
pub show: PreviewShow,
|
|
pub sets: BTreeMap<usize, PreviewSet>,
|
|
/// The texture set being baked.
|
|
pub current: Option<usize>,
|
|
/// The last map that finished.
|
|
pub newest: Option<BakedMap>,
|
|
}
|
|
|
|
impl BakePreview {
|
|
/// The map the view shows, None for the material.
|
|
pub fn shown(&self) -> Option<BakedMap> {
|
|
match self.show {
|
|
PreviewShow::Material => None,
|
|
PreviewShow::Pin(map) => Some(map),
|
|
PreviewShow::Follow => self
|
|
.newest
|
|
.or_else(|| self.current_set().and_then(|set| set.order.first().copied()))
|
|
.or_else(|| self.sets.values().find_map(|set| set.order.first().copied())),
|
|
}
|
|
}
|
|
|
|
pub fn current_set(&self) -> Option<&PreviewSet> {
|
|
self.sets.get(&self.current?)
|
|
}
|
|
|
|
/// The map being baked now (None between sets and once the set's maps are all in).
|
|
pub fn stage(&self) -> Option<Stage> {
|
|
let texture_set = self.current?;
|
|
let set = self.sets.get(&texture_set)?;
|
|
let done = set.baked.maps.len();
|
|
Some(Stage { texture_set, map: *set.order.get(done)?, position: done + 1, total: set.order.len() })
|
|
}
|
|
}
|
|
|
|
impl Session {
|
|
/// Start a preview from each selected texture set's enabled maps, in the same order as
|
|
/// [`crate::BakeTask::maps`]. Invalid indices return the project's texture-set error before
|
|
/// changing the preview or view mode. This reads settings without preparing bake geometry.
|
|
pub fn bake_preview_begin_for(&mut self, sets: &[usize]) -> Result<()> {
|
|
let orders = self.bake_preview_orders(sets, None)?;
|
|
self.bake_preview_begin(orders);
|
|
Ok(())
|
|
}
|
|
|
|
/// Validate the entire selection before a bake takes the view or starts its first worker.
|
|
pub fn bake_preview_orders(&self, sets: &[usize], only: Option<BakedMap>) -> Result<Vec<(usize, Vec<BakedMap>)>> {
|
|
sets.iter()
|
|
.map(|&texture_set| {
|
|
let set = self.doc.project.texture_set(texture_set)?;
|
|
let maps = match only {
|
|
Some(map) => vec![map],
|
|
None => set.bake.maps.enabled().collect(),
|
|
};
|
|
Ok((texture_set, maps))
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// Start showing a bake of `sets` (each with the maps it makes, in order): the 3D view
|
|
/// follows the newest map and every set shows neutral values until its maps arrive. The
|
|
/// view mode in use comes back with [`Session::bake_preview_end`]. An active stroke is
|
|
/// discarded so a later pointer release cannot commit paint during the preview.
|
|
pub fn bake_preview_begin(&mut self, sets: Vec<(usize, Vec<BakedMap>)>) {
|
|
self.cancel_stroke();
|
|
|
|
let restore = self.bake_preview.as_ref().map_or(self.view, |preview| preview.restore);
|
|
let sets = sets.into_iter().map(|(texture_set, order)| (texture_set, PreviewSet { baked: BakedMaps::default(), order })).collect();
|
|
|
|
self.bake_preview = Some(BakePreview { restore, show: PreviewShow::Follow, sets, current: None, newest: None });
|
|
self.bake_preview_refresh();
|
|
}
|
|
|
|
/// The texture set whose maps stream in now.
|
|
pub fn bake_preview_set_current(&mut self, texture_set: usize) {
|
|
if let Some(preview) = &mut self.bake_preview {
|
|
preview.current = Some(texture_set);
|
|
}
|
|
|
|
self.bake_preview_refresh();
|
|
}
|
|
|
|
/// Install one finished map of set `texture_set`. Returns false when no preview shows.
|
|
pub fn bake_preview_map(&mut self, texture_set: usize, streamed: StreamedMap) -> bool {
|
|
let Some(preview) = &mut self.bake_preview else { return false };
|
|
|
|
let set = preview.sets.entry(texture_set).or_default();
|
|
|
|
// A new bake of the set starts from nothing (the baker never repeats a map)
|
|
if streamed.index == 0 {
|
|
set.baked = BakedMaps::default();
|
|
}
|
|
|
|
set.baked.resolution = streamed.resolution;
|
|
set.baked.maps.insert(streamed.map, streamed.data);
|
|
preview.newest = Some(streamed.map);
|
|
self.bake_preview_refresh();
|
|
true
|
|
}
|
|
|
|
/// Choose what the view shows (Follow, a pinned map or the material).
|
|
pub fn bake_preview_show(&mut self, show: PreviewShow) -> Result<()> {
|
|
let Some(preview) = &mut self.bake_preview else { return Err(bad("bake.preview", "no bake is running")) };
|
|
|
|
preview.show = show;
|
|
self.bake_preview_refresh();
|
|
Ok(())
|
|
}
|
|
|
|
/// The user picked a view mode during a bake: a mesh map pins it, any other mode shows
|
|
/// the material and is what comes back when the bake ends.
|
|
pub(crate) fn bake_preview_user_view(&mut self, view: ViewMode) {
|
|
let Some(preview) = &mut self.bake_preview else { return };
|
|
|
|
match view {
|
|
ViewMode::MeshMap(map) => preview.show = PreviewShow::Pin(map),
|
|
other => {
|
|
preview.show = PreviewShow::Material;
|
|
preview.restore = other;
|
|
}
|
|
}
|
|
|
|
self.bake_preview_refresh();
|
|
if !matches!(view, ViewMode::MeshMap(_)) {
|
|
self.view = view;
|
|
}
|
|
}
|
|
|
|
/// Drop the displayed maps and restore the previous view mode. This does not stop a
|
|
/// worker or undo any applied results; the UI also uses it when leaving Bake mode.
|
|
pub fn bake_preview_end(&mut self) {
|
|
if let Some(preview) = self.bake_preview.take() {
|
|
self.view = preview.restore;
|
|
|
|
// The image behind a deferred "Other..." view may have been removed during the bake
|
|
self.drop_missing_image_view();
|
|
self.revision = self.revision.wrapping_add(1);
|
|
}
|
|
}
|
|
|
|
pub fn bake_preview_active(&self) -> bool {
|
|
self.bake_preview.is_some()
|
|
}
|
|
|
|
/// The streamed maps of set `texture_set` while a preview shows (empty for a set not baked
|
|
/// yet: neutral values), None when the document's baked maps are what shows.
|
|
pub fn bake_preview_baked(&self, texture_set: usize) -> Option<&BakedMaps> {
|
|
self.bake_preview.as_ref()?.sets.get(&texture_set).map(|set| &set.baked)
|
|
}
|
|
|
|
/// Why painting is not possible now, when a bake preview shows.
|
|
pub fn painting_blocked(&self) -> Option<&'static str> {
|
|
self.bake_preview.as_ref().map(|_| PAINTING_PAUSED)
|
|
}
|
|
|
|
fn bake_preview_refresh(&mut self) {
|
|
let Some(preview) = &self.bake_preview else { return };
|
|
|
|
self.view = match preview.shown() {
|
|
Some(map) => ViewMode::MeshMap(map),
|
|
None => ViewMode::Material,
|
|
};
|
|
self.revision = self.revision.wrapping_add(1);
|
|
}
|
|
}
|
|
|
|
/// The preview as JSON, for `bake.preview`, `session.inspect` and `ui.inspect`.
|
|
pub fn preview_json(session: &Session) -> Value {
|
|
let Some(preview) = &session.bake_preview else { return json!({ "active": false }) };
|
|
|
|
let stage = preview.stage().map(|stage| json!({ "set": stage.texture_set, "map": stage.map.id(), "n": stage.position, "total": stage.total }));
|
|
let sets: Vec<Value> = preview
|
|
.sets
|
|
.iter()
|
|
.map(|(texture_set, set)| {
|
|
json!({
|
|
"set": texture_set,
|
|
"order": set.order.iter().map(|map| map.id()).collect::<Vec<_>>(),
|
|
"done": set.baked.maps.keys().map(|map| map.id()).collect::<Vec<_>>(),
|
|
})
|
|
})
|
|
.collect();
|
|
|
|
json!({
|
|
"active": true,
|
|
"show": preview.show.id(),
|
|
"shown": preview.shown().map(|map| map.id()),
|
|
"newest": preview.newest.map(|map| map.id()),
|
|
"restore": preview.restore.label(),
|
|
"stage": stage,
|
|
"sets": sets,
|
|
})
|
|
}
|
|
|
|
fn preview(session: &mut Session, params: &Value) -> Result<Value> {
|
|
if let Some(show) = strp(params, "show") {
|
|
let show = PreviewShow::parse(show).ok_or_else(|| bad("bake.preview", format!("unknown `show` {show:?}: follow, material or map:<id>")))?;
|
|
session.bake_preview_show(show)?;
|
|
}
|
|
|
|
Ok(preview_json(session))
|
|
}
|