mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-30 10:03:56 +01:00
textureSet.resolution takes an optional height (default: square). Old projects open square as before; sets whose UV tiles have their own sizes stay square.
452 lines
20 KiB
Rust
452 lines
20 KiB
Rust
//! Texture set commands (Texture Set List / Texture Set Settings).
|
||
|
||
use serde_json::{Value, json};
|
||
use texturecraft_core::{AoMixing, Channel, ChannelBlend, ChannelFormat, HeightToNormalMethod, NormalMixing, UvPaddingMode};
|
||
|
||
use std::sync::Arc;
|
||
|
||
use texturecraft_geom::Mesh;
|
||
|
||
use crate::commands::{CommandSpec, always, bad, boolp, channel_param, f32p, normal_format_param, strp, u64p};
|
||
use crate::session::{Document, SetCache, StackTarget, check_resolution};
|
||
use crate::{Result, Session, cmd};
|
||
|
||
pub fn specs() -> Vec<CommandSpec> {
|
||
vec![
|
||
cmd!("textureSet.select", "Select texture set", [], None, "{ts}", always, select),
|
||
cmd!("textureSet.list", "List texture sets", [], None, "{}", always, |s, _| Ok(crate::inspect::texture_sets(s))),
|
||
cmd!(
|
||
"textureSet.inspect",
|
||
"Inspect texture set",
|
||
[],
|
||
None,
|
||
"{ts?} → {index, name, resolution, height, size: [w, h], uvTiles: [UDIM numbers] (empty: no UV tiles), tiles: [{udim, block: [x, y]}], channels, layers, baked, visible, active}",
|
||
always,
|
||
|s, p| Ok(crate::inspect::texture_set(s, ts_param(s, p, "textureSet.inspect")?))
|
||
),
|
||
cmd!(
|
||
"textureSet.resolution",
|
||
"Texture set size",
|
||
[],
|
||
None,
|
||
"{ts?, resolution: 128..8192 (power of two, texels across), height?: 128..8192 (texels down; default = resolution, a square set)}",
|
||
always,
|
||
resolution
|
||
),
|
||
cmd!("textureSet.channel.add", "Add channel", [], None, "{ts?, channel}", always, |s, p| channel(s, p, true)),
|
||
cmd!("textureSet.channel.remove", "Remove channel", [], None, "{ts?, channel}", always, |s, p| channel(s, p, false)),
|
||
cmd!("textureSet.visible", "Toggle texture set visibility", [], None, "{ts?, visible?}", always, visible),
|
||
cmd!(
|
||
"textureSet.visibility",
|
||
"Show or hide texture sets",
|
||
[],
|
||
None,
|
||
"{mode: showAll|hideAll|invert|solo, ts?} → {visible: [bool per set]}; solo shows only `ts` (the active set), or every set again when it already was the only one",
|
||
always,
|
||
visibility
|
||
),
|
||
cmd!("textureSet.rename", "Rename texture set", [], None, "{ts?, name}", always, rename),
|
||
cmd!(
|
||
"textureSet.settings",
|
||
"Texture set settings",
|
||
[],
|
||
None,
|
||
"{ts?, description?, heightStrength?, normalMixing?: combine|replace, aoMixing?: multiply|none, heightToNormal?: sharp|smooth, uvPadding?: 3d|2d, normalFormat?: openGl|directX}",
|
||
always,
|
||
settings
|
||
),
|
||
cmd!(
|
||
"textureSet.parts",
|
||
"List mesh parts",
|
||
[],
|
||
None,
|
||
"{ts?} — the mesh parts (submesh names) of a texture set, for geometry masks",
|
||
always,
|
||
|s, p| {
|
||
let ts = ts_param(s, p, "textureSet.parts")?;
|
||
Ok(json!(parts(s, ts)))
|
||
}
|
||
),
|
||
]
|
||
}
|
||
|
||
/// The mesh parts (submesh names, in mesh order, each once) of texture set `ts`.
|
||
pub fn parts(s: &Session, ts: usize) -> Vec<String> {
|
||
let Some(material) = s.doc.project.texture_sets.get(ts).map(|t| t.material) else { return Vec::new() };
|
||
let mut out: Vec<String> = Vec::new();
|
||
for sm in s.mesh.submeshes.iter().filter(|sm| sm.material == material) {
|
||
if !out.contains(&sm.name) {
|
||
out.push(sm.name.clone());
|
||
}
|
||
}
|
||
out
|
||
}
|
||
|
||
pub(crate) fn ts_param(s: &Session, p: &Value, cmd: &str) -> Result<usize> {
|
||
match u64p(p, "ts") {
|
||
Some(t) if (t as usize) < s.set_count() => Ok(t as usize),
|
||
Some(t) => Err(bad(cmd, format!("no texture set {t}"))),
|
||
None => Ok(s.ts()),
|
||
}
|
||
}
|
||
|
||
fn select(s: &mut Session, p: &Value) -> Result<Value> {
|
||
let ts = u64p(p, "ts")
|
||
.map(|index| index as usize)
|
||
.filter(|index| *index < s.set_count())
|
||
.ok_or_else(|| bad("textureSet.select", "valid `ts` index required"))?;
|
||
select_set(s, ts)?;
|
||
Ok(json!({ "ts": ts }))
|
||
}
|
||
|
||
/// Make texture set `ts` the active one, on its content stack
|
||
pub(crate) fn select_set(s: &mut Session, ts: usize) -> Result<()> {
|
||
s.cancel_stroke();
|
||
|
||
// Paths paint into the active set
|
||
if ts != s.ts() {
|
||
s.finish_path()?;
|
||
|
||
// The clone source is a point of the set it was picked in
|
||
s.tool.clone_source = None;
|
||
}
|
||
|
||
s.active_set = ts;
|
||
s.target = StackTarget::Content;
|
||
s.revision = s.revision.wrapping_add(1);
|
||
Ok(())
|
||
}
|
||
|
||
fn resolution(s: &mut Session, p: &Value) -> Result<Value> {
|
||
let ts = ts_param(s, p, "textureSet.resolution")?;
|
||
let r = u64p(p, "resolution").ok_or_else(|| bad("textureSet.resolution", "`resolution` is required"))?;
|
||
let height = u64p(p, "height").unwrap_or(r);
|
||
let task = s.resize_task_to(ts, u32::try_from(r).unwrap_or(0), u32::try_from(height).unwrap_or(0))?;
|
||
let out = task.run(&texturecraft_bake::Progress::new())?;
|
||
s.apply_resize(out)
|
||
}
|
||
|
||
/// A texture set size change prepared from a snapshot of the document, so the UI can run the
|
||
/// resampling and the new texel caches (seconds at 8K) on a worker thread:
|
||
/// [`Session::resize_task`], [`ResizeTask::run`], then [`Session::apply_resize`] (one undo step).
|
||
pub struct ResizeTask {
|
||
ts: usize,
|
||
resolution: u32,
|
||
height: u32,
|
||
doc: Document,
|
||
mesh: Arc<Mesh>,
|
||
/// The session's revision and mesh when the task was made: the result applies only to them.
|
||
from: (u64, u64),
|
||
}
|
||
|
||
/// What [`ResizeTask::run`] computed.
|
||
pub struct ResizeOutput {
|
||
ts: usize,
|
||
resolution: u32,
|
||
height: u32,
|
||
doc: Document,
|
||
/// The set's new caches (None when its UV tile layout moved: the session rebuilds them).
|
||
cache: Option<SetCache>,
|
||
log: crate::log::SessionLog,
|
||
from: (u64, u64),
|
||
}
|
||
|
||
impl ResizeTask {
|
||
/// Resample the set's painted pixels (bilinear; strokes are not replayed) and build its
|
||
/// caches. Progress goes from 0 to 1.
|
||
pub fn run(self, progress: &texturecraft_bake::Progress) -> Result<ResizeOutput> {
|
||
let ResizeTask { ts, resolution, height, mut doc, mesh, from } = self;
|
||
let set = doc.project.texture_set_mut(ts)?;
|
||
let old = set.clone();
|
||
set.set_size(resolution, height);
|
||
if set.resolution_height.is_some() && !set.tile_resolutions.is_empty() {
|
||
return Err(bad("textureSet.resolution", "a texture set whose UV tiles have their own sizes stays square"));
|
||
}
|
||
crate::session::check_size(set)?;
|
||
let size = set.size();
|
||
let mut log = crate::log::SessionLog::new();
|
||
|
||
// Tiles of their own resolution keep it, so the others move within their atlas blocks
|
||
if old.tile_resolutions.is_empty() {
|
||
crate::layer_ops::resample_set(&mut doc, ts, size);
|
||
} else {
|
||
crate::uv_tile_cmds::relayout(&mut doc, ts, &old, &mut log)?;
|
||
}
|
||
progress.done.store(3_000, std::sync::atomic::Ordering::Relaxed);
|
||
let set = doc.project.texture_set(ts)?;
|
||
let cache = (crate::session::uv_layout(&old) == crate::session::uv_layout(set)).then(|| SetCache::new(&mesh, set));
|
||
progress.done.store(10_000, std::sync::atomic::Ordering::Relaxed);
|
||
Ok(ResizeOutput { ts, resolution, height, doc, cache, log, from })
|
||
}
|
||
}
|
||
|
||
impl ResizeOutput {
|
||
/// The texture set and resolution this resize is for.
|
||
pub fn target(&self) -> (usize, u32) {
|
||
(self.ts, self.resolution)
|
||
}
|
||
}
|
||
|
||
impl Session {
|
||
/// Prepare resizing texture set `ts` to a square `resolution` (a power of two, 128..8192).
|
||
pub fn resize_task(&mut self, ts: usize, resolution: u32) -> Result<ResizeTask> {
|
||
self.resize_task_to(ts, resolution, resolution)
|
||
}
|
||
|
||
/// Prepare resizing texture set `ts` to `width × height` texels (powers of two, 128..8192).
|
||
pub fn resize_task_to(&mut self, ts: usize, width: u32, height: u32) -> Result<ResizeTask> {
|
||
let (resolution, height) = (check_resolution(width)?, check_resolution(height)?);
|
||
self.doc.project.texture_set(ts)?;
|
||
self.cancel_stroke();
|
||
Ok(ResizeTask { ts, resolution, height, doc: self.doc.clone(), mesh: self.mesh.clone(), from: (self.revision, self.mesh_id) })
|
||
}
|
||
|
||
/// Apply a finished resize as one undo step. When the document changed while it ran, the
|
||
/// resize runs again here on the current document.
|
||
pub fn apply_resize(&mut self, out: ResizeOutput) -> Result<Value> {
|
||
if out.from != (self.revision, self.mesh_id) || self.stroke_active() {
|
||
let task = self.resize_task_to(out.ts, out.resolution, out.height)?;
|
||
return self.apply_resize(task.run(&texturecraft_bake::Progress::new())?);
|
||
}
|
||
|
||
let ResizeOutput { ts, resolution, height, doc, cache, log, .. } = out;
|
||
let before = std::mem::replace(&mut self.doc, doc);
|
||
|
||
// Install the new caches before adding history to avoid rebuilding them
|
||
if let (Some(c), Some(slot)) = (cache, self.caches.get_mut(ts)) {
|
||
let old = std::mem::replace(slot, c);
|
||
|
||
// Release large caches without blocking the UI
|
||
#[cfg(not(target_arch = "wasm32"))]
|
||
let _ = std::thread::Builder::new().name("texturecraft-free".into()).spawn(move || drop(old));
|
||
#[cfg(target_arch = "wasm32")]
|
||
drop(old);
|
||
}
|
||
self.log.append(log);
|
||
self.push_history("Texture set size", before);
|
||
Ok(json!({ "ts": ts, "resolution": resolution, "height": height }))
|
||
}
|
||
}
|
||
|
||
fn channel(s: &mut Session, p: &Value, add: bool) -> Result<Value> {
|
||
let ts = ts_param(s, p, "textureSet.channel")?;
|
||
let c = channel_param("textureSet.channel", p, "channel")?.ok_or_else(|| bad("textureSet.channel", "`channel` is required"))?;
|
||
s.edit(if add { "Add channel" } else { "Remove channel" }, |s| {
|
||
if add {
|
||
return add_channel(s, ts, c);
|
||
}
|
||
if c == Channel::BaseColor {
|
||
return Err(bad("textureSet.channel.remove", "base color can't be removed"));
|
||
}
|
||
s.doc.project.texture_set_mut(ts)?.channels.retain(|(x, _)| *x != c);
|
||
Ok(())
|
||
})?;
|
||
Ok(Value::Null)
|
||
}
|
||
|
||
/// Add channel `c` to texture set `ts` (no-op when present) and enable it on the set's layers.
|
||
/// Call inside [`Session::edit`].
|
||
pub(crate) fn add_channel(s: &mut Session, ts: usize, c: Channel) -> Result<()> {
|
||
let set = s.doc.project.texture_set_mut(ts)?;
|
||
if !set.has_channel(c) {
|
||
set.channels.push((c, ChannelFormat::default_for(c)));
|
||
set.channels.sort_by_key(|(c, _)| c.index());
|
||
}
|
||
// Existing layers get the new channel enabled.
|
||
let ids: Vec<_> = s.doc.project.walk(ts).into_iter().map(|(id, _)| id).collect();
|
||
for id in ids {
|
||
if let Ok(l) = s.doc.project.layer_mut(id) {
|
||
let mode = if l.is_group() { texturecraft_core::BlendMode::Passthrough } else { texturecraft_core::BlendMode::Normal };
|
||
l.channels.entry(c).or_insert(ChannelBlend { mode, opacity: 1.0 });
|
||
}
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
fn visible(s: &mut Session, p: &Value) -> Result<Value> {
|
||
let ts = ts_param(s, p, "textureSet.visible")?;
|
||
let set = s.doc.project.texture_set_mut(ts)?;
|
||
set.visible = boolp(p, "visible").unwrap_or(!set.visible);
|
||
let v = set.visible;
|
||
s.revision = s.revision.wrapping_add(1);
|
||
Ok(json!({ "visible": v }))
|
||
}
|
||
|
||
fn visibility(s: &mut Session, p: &Value) -> Result<Value> {
|
||
let cmd = "textureSet.visibility";
|
||
let ts = ts_param(s, p, cmd)?;
|
||
let mode = strp(p, "mode").filter(|m| ["showAll", "hideAll", "invert", "solo"].contains(m));
|
||
let mode = mode.ok_or_else(|| bad(cmd, "`mode` must be showAll, hideAll, invert or solo"))?;
|
||
let sets = &mut s.doc.project.texture_sets;
|
||
let solo = sets.iter().enumerate().all(|(i, t)| t.visible == (i == ts));
|
||
for (i, t) in sets.iter_mut().enumerate() {
|
||
t.visible = match mode {
|
||
"showAll" => true,
|
||
"hideAll" => false,
|
||
"invert" => !t.visible,
|
||
_ => solo || i == ts,
|
||
};
|
||
}
|
||
s.revision = s.revision.wrapping_add(1);
|
||
Ok(json!({ "visible": s.doc.project.texture_sets.iter().map(|t| t.visible).collect::<Vec<_>>() }))
|
||
}
|
||
|
||
fn rename(s: &mut Session, p: &Value) -> Result<Value> {
|
||
let ts = ts_param(s, p, "textureSet.rename")?;
|
||
let name = strp(p, "name")
|
||
.map(str::trim)
|
||
.filter(|n| !n.is_empty() && n.chars().count() <= 128)
|
||
.ok_or_else(|| bad("textureSet.rename", "`name` is required"))?
|
||
.to_string();
|
||
s.edit("Rename texture set", |s| {
|
||
s.doc.project.texture_set_mut(ts)?.name = name;
|
||
Ok(())
|
||
})?;
|
||
Ok(Value::Null)
|
||
}
|
||
|
||
fn settings(s: &mut Session, p: &Value) -> Result<Value> {
|
||
let ts = ts_param(s, p, "textureSet.settings")?;
|
||
let nf = normal_format_param("textureSet.settings", p)?;
|
||
let ao_mixing_before = s.doc.project.texture_set(ts)?.ao_mixing;
|
||
let normal_mixing = match strp(p, "normalMixing") {
|
||
None => None,
|
||
Some("combine") => Some(NormalMixing::Combine),
|
||
Some("replace") => Some(NormalMixing::Replace),
|
||
Some(other) => return Err(bad("textureSet.settings", format!("unknown normal mixing {other:?} (combine or replace)"))),
|
||
};
|
||
let ao_mixing = match strp(p, "aoMixing") {
|
||
None => None,
|
||
Some("multiply") => Some(AoMixing::Multiply),
|
||
Some("none") => Some(AoMixing::None),
|
||
Some(other) => return Err(bad("textureSet.settings", format!("unknown ambient occlusion mixing {other:?} (multiply or none)"))),
|
||
};
|
||
let height_to_normal = match strp(p, "heightToNormal") {
|
||
None => None,
|
||
Some("sharp") => Some(HeightToNormalMethod::Sharp),
|
||
Some("smooth") => Some(HeightToNormalMethod::Smooth),
|
||
Some(other) => return Err(bad("textureSet.settings", format!("unknown height to normal method {other:?} (sharp or smooth)"))),
|
||
};
|
||
let uv_padding = match strp(p, "uvPadding") {
|
||
None => None,
|
||
Some("3d") => Some(UvPaddingMode::Space3d),
|
||
Some("2d") => Some(UvPaddingMode::Space2d),
|
||
Some(other) => return Err(bad("textureSet.settings", format!("unknown UV padding {other:?} (3d or 2d)"))),
|
||
};
|
||
s.edit("Texture set settings", |s| {
|
||
if let Some(n) = nf {
|
||
s.doc.project.normal_format = n;
|
||
}
|
||
let set = s.doc.project.texture_set_mut(ts)?;
|
||
if let Some(d) = strp(p, "description") {
|
||
set.description = d.chars().take(4096).collect();
|
||
}
|
||
if let Some(h) = f32p(p, "heightStrength") {
|
||
set.height_strength = h.clamp(0.0, 100.0);
|
||
}
|
||
if let Some(mixing) = normal_mixing {
|
||
set.normal_mixing = mixing;
|
||
}
|
||
if let Some(mixing) = ao_mixing {
|
||
set.ao_mixing = mixing;
|
||
}
|
||
if let Some(method) = height_to_normal {
|
||
set.height_to_normal = method;
|
||
}
|
||
if let Some(mode) = uv_padding {
|
||
set.uv_padding = mode;
|
||
}
|
||
Ok(())
|
||
})?;
|
||
|
||
// The viewport's occlusion texture mixes the baked map in: resend the set
|
||
if s.doc.project.texture_set(ts)?.ao_mixing != ao_mixing_before
|
||
&& let Some(cache) = s.caches.get_mut(ts)
|
||
{
|
||
cache.upload.extend(texturecraft_compose::all_tiles(cache.size.0, cache.size.1));
|
||
}
|
||
Ok(Value::Null)
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn visibility_shows_hides_inverts_and_solos() {
|
||
// The material ball has two texture sets.
|
||
let mut s = Session::new_project("t", texturecraft_geom::primitives::material_ball(), "builtin:material_ball", None, 128).unwrap();
|
||
let mut run = |p: Value| crate::execute(&mut s, "textureSet.visibility", &p).map(|r| r["visible"].clone());
|
||
assert_eq!(run(json!({"mode": "hideAll"})).unwrap(), json!([false, false]));
|
||
assert_eq!(run(json!({"mode": "solo", "ts": 1})).unwrap(), json!([false, true]));
|
||
assert_eq!(run(json!({"mode": "solo", "ts": 1})).unwrap(), json!([true, true]), "solo again shows every set");
|
||
assert_eq!(run(json!({"mode": "solo", "ts": 0})).unwrap(), json!([true, false]));
|
||
assert_eq!(run(json!({"mode": "invert"})).unwrap(), json!([false, true]));
|
||
assert_eq!(run(json!({"mode": "showAll"})).unwrap(), json!([true, true]));
|
||
assert!(run(json!({"mode": "sideways"})).is_err() && run(json!({"mode": "solo", "ts": 9})).is_err());
|
||
}
|
||
|
||
#[test]
|
||
fn normal_mixing_replace_keeps_height_out_of_the_normal_and_undoes() {
|
||
let mut s = Session::new_project("t", texturecraft_geom::primitives::material_ball(), "builtin:material_ball", None, 128).unwrap();
|
||
let mixing = |s: &Session| crate::inspect::texture_set(s, 0)["normalMixing"].clone();
|
||
assert_eq!(mixing(&s), json!("combine"));
|
||
|
||
crate::execute(&mut s, "textureSet.settings", &json!({"ts": 0, "normalMixing": "replace"})).unwrap();
|
||
assert_eq!(mixing(&s), json!("replace"));
|
||
assert!(!s.doc.project.texture_sets[0].height_in_normal() && s.doc.project.texture_sets[1].height_in_normal());
|
||
assert!(crate::execute(&mut s, "textureSet.settings", &json!({"normalMixing": "blend"})).is_err());
|
||
|
||
crate::execute(&mut s, "edit.undo", &Value::Null).unwrap();
|
||
assert_eq!(mixing(&s), json!("combine"));
|
||
}
|
||
|
||
#[test]
|
||
fn height_to_normal_method_is_set_by_command_and_undoes() {
|
||
let mut s = Session::new_project("t", texturecraft_geom::primitives::material_ball(), "builtin:material_ball", None, 128).unwrap();
|
||
let method = |s: &Session, ts: usize| crate::inspect::texture_set(s, ts)["heightToNormal"].clone();
|
||
assert_eq!(method(&s, 0), json!("smooth"));
|
||
|
||
crate::execute(&mut s, "textureSet.settings", &json!({"ts": 0, "heightToNormal": "sharp"})).unwrap();
|
||
assert_eq!((method(&s, 0), method(&s, 1)), (json!("sharp"), json!("smooth")));
|
||
assert!(crate::execute(&mut s, "textureSet.settings", &json!({"heightToNormal": "blurry"})).is_err());
|
||
|
||
crate::execute(&mut s, "edit.undo", &Value::Null).unwrap();
|
||
assert_eq!(method(&s, 0), json!("smooth"));
|
||
}
|
||
|
||
#[test]
|
||
fn uv_padding_rebuilds_the_viewport_padding_and_undoes() {
|
||
let mut s = Session::new_project("t", texturecraft_geom::primitives::material_ball(), "builtin:material_ball", None, 128).unwrap();
|
||
let mode = |s: &Session| crate::inspect::texture_set(s, 0)["uvPadding"].clone();
|
||
let crosses_seams = |s: &Session| s.caches[0].padding.flat.is_some();
|
||
assert_eq!((mode(&s), crosses_seams(&s)), (json!("3d"), true));
|
||
s.update();
|
||
|
||
crate::execute(&mut s, "textureSet.settings", &json!({"ts": 0, "uvPadding": "2d"})).unwrap();
|
||
assert_eq!((mode(&s), crosses_seams(&s)), (json!("2d"), false));
|
||
assert!(!s.caches[0].dirty.is_empty(), "the set recomposites with its new padding");
|
||
assert!(crate::execute(&mut s, "textureSet.settings", &json!({"uvPadding": "4d"})).is_err());
|
||
|
||
crate::execute(&mut s, "edit.undo", &Value::Null).unwrap();
|
||
assert_eq!((mode(&s), crosses_seams(&s)), (json!("3d"), true));
|
||
}
|
||
|
||
#[test]
|
||
fn ao_mixing_resends_the_set_to_the_viewport_and_undoes() {
|
||
let mut s = Session::new_project("t", texturecraft_geom::primitives::material_ball(), "builtin:material_ball", None, 128).unwrap();
|
||
s.update();
|
||
let _ = (s.take_uploads(0), s.take_uploads(1));
|
||
|
||
crate::execute(&mut s, "textureSet.settings", &json!({"ts": 0, "aoMixing": "none"})).unwrap();
|
||
assert_eq!(crate::inspect::texture_set(&s, 0)["aoMixing"], json!("none"));
|
||
assert!(!s.take_uploads(0).is_empty() && s.take_uploads(1).is_empty(), "only the changed set is resent");
|
||
assert!(crate::execute(&mut s, "textureSet.settings", &json!({"aoMixing": "screen"})).is_err());
|
||
|
||
crate::execute(&mut s, "edit.undo", &Value::Null).unwrap();
|
||
s.update();
|
||
assert_eq!(crate::inspect::texture_set(&s, 0)["aoMixing"], json!("multiply"));
|
||
assert!(!s.take_uploads(0).is_empty(), "undo resends the set too");
|
||
}
|
||
}
|