mirror of
https://github.com/storytold/texturecraft.git
synced 2026-12-01 10:34:00 +01:00
Send each texture set to PhotoCraft as one layered PSD
The hand-off now writes one 16-bit PSD per texture set instead of one OpenRaster file per channel, so every map is in one document and a mask edited once shapes all of them: - Folders become groups; every layer becomes a pass-through group with its real mask as the group's editable layer mask, holding one pixel layer per channel (Base color, Roughness, ...) with the channel's blend mode and opacity. - One layer comp per channel shows only that channel's layers. - When PhotoCraft saves, every channel is recomposited from the saved layers with TextureCraft's blend math and shown through `PhotoCraft: <channel>` fills, all in one undo step. The PSD reader and writer (formats::psd) are written from the public file format specification. OpenRaster stays available as format "ora". compose gains flatten_layer_masked_apart, which keeps a layer's content and mask apart.
This commit is contained in:
1 parent
fa30e824c7
commit
f723d68e5e
9 files changed
+1916
-69
No files matched your search
@@ -2200,10 +2200,25 @@ pub fn effect_histogram(inp: &Inputs, effect: LayerId, channel: Channel, bins: u
|
||||
/// One flattened tile of one channel: (channel, tile, premultiplied value + coverage data).
|
||||
pub type FlatTile = (Channel, (u32, u32), Vec<u16>);
|
||||
|
||||
/// One tile of a layer's mask (with its geometry mask and mask with height), u16 per texel.
|
||||
pub type MaskTile = ((u32, u32), Vec<u16>);
|
||||
|
||||
/// Flatten one paint or fill layer: its content (with content effects) times its mask, as
|
||||
/// premultiplied (value, coverage) tiles per enabled channel, ready to become a plain paint
|
||||
/// layer. Groups are not supported (None).
|
||||
pub fn flatten_layer(inputs: &Inputs, id: LayerId, tiles: &[(u32, u32)]) -> Option<Vec<FlatTile>> {
|
||||
flatten(inputs, id, tiles, false).map(|(content, _)| content)
|
||||
}
|
||||
|
||||
/// [`flatten_layer`] with the mask kept apart: the content tiles hold the content alone
|
||||
/// (premultiplied by its own coverage), and the mask tiles the layer's mask as it shapes the
|
||||
/// content (geometry mask and mask with height included), over the same tiles. For an image
|
||||
/// editor's layer mask. Groups are not supported (None).
|
||||
pub fn flatten_layer_masked_apart(inputs: &Inputs, id: LayerId, tiles: &[(u32, u32)]) -> Option<(Vec<FlatTile>, Vec<MaskTile>)> {
|
||||
flatten(inputs, id, tiles, true)
|
||||
}
|
||||
|
||||
fn flatten(inputs: &Inputs, id: LayerId, tiles: &[(u32, u32)], apart: bool) -> Option<(Vec<FlatTile>, Vec<MaskTile>)> {
|
||||
let set = inputs.project.texture_set(inputs.ts).ok()?;
|
||||
if inputs.project.content_layer(id)?.is_group() {
|
||||
return None;
|
||||
@@ -2219,11 +2234,12 @@ pub fn flatten_layer(inputs: &Inputs, id: LayerId, tiles: &[(u32, u32)]) -> Opti
|
||||
let visible_inputs = Inputs { project: &solo, preview: None, block_scales: inputs.block_scales.clone(), ..*inputs };
|
||||
let fields = neighbourhood_fields(&visible_inputs, &channels, tiles);
|
||||
|
||||
let out: Vec<Vec<FlatTile>> = tiles
|
||||
let out: Vec<(Vec<FlatTile>, Option<MaskTile>)> = tiles
|
||||
.par_iter()
|
||||
.map(|&(tx, ty)| {
|
||||
let nodes = plan(&visible_inputs, &[id], &channels, tx, ty, 0, Some(&fields));
|
||||
let Some(n) = nodes.first() else { return Vec::new() };
|
||||
let Some(n) = nodes.first() else { return (Vec::new(), None) };
|
||||
let mut mask = apart.then(|| vec![0u16; TILE_AREA]);
|
||||
let full = anchor_plan(&visible_inputs, id, &channels, tx, ty, Some(&fields));
|
||||
let mut buffers: Vec<(Channel, Vec<u16>, bool)> =
|
||||
n.chans.iter().filter_map(|c| Channel::ALL.get(c.ch).map(|ch| (*ch, vec![0u16; TILE_AREA * (c.comps + 1)], false))).collect();
|
||||
@@ -2241,6 +2257,11 @@ pub fn flatten_layer(inputs: &Inputs, id: LayerId, tiles: &[(u32, u32)]) -> Opti
|
||||
tex.anchors_below(&full, id);
|
||||
}
|
||||
let m = tex.node_mask(n);
|
||||
if let Some(slot) = mask.as_mut().and_then(|t| t.get_mut(o)) {
|
||||
*slot = to_u16(m);
|
||||
}
|
||||
// Kept apart, the mask leaves the content's own coverage alone.
|
||||
let m = if apart { 1.0 } else { m };
|
||||
for (c, (ch, buffer, any)) in n.chans.iter().zip(buffers.iter_mut()) {
|
||||
let Some((v, cov)) = tex.content(&n.content, c) else { continue };
|
||||
let a = (cov * m).clamp(0.0, 1.0);
|
||||
@@ -2260,11 +2281,18 @@ pub fn flatten_layer(inputs: &Inputs, id: LayerId, tiles: &[(u32, u32)]) -> Opti
|
||||
}
|
||||
}
|
||||
|
||||
buffers.into_iter().filter(|(_, _, any)| *any).map(|(ch, buffer, _)| (ch, (tx, ty), buffer)).collect()
|
||||
let content = buffers.into_iter().filter(|(_, _, any)| *any).map(|(ch, buffer, _)| (ch, (tx, ty), buffer)).collect();
|
||||
(content, mask.map(|m| ((tx, ty), m)))
|
||||
})
|
||||
.collect();
|
||||
|
||||
Some(out.into_iter().flatten().collect())
|
||||
let mut content = Vec::new();
|
||||
let mut masks = Vec::new();
|
||||
for (c, m) in out {
|
||||
content.extend(c);
|
||||
masks.extend(m);
|
||||
}
|
||||
Some((content, masks))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -20,6 +20,6 @@ pub use below::{BelowCache, StackKey};
|
||||
pub use display::{DisplayKind, tile_bytes};
|
||||
pub use eval::{
|
||||
Composite, Inputs, PaintTarget, Stencil, StrokePreview, all_tiles, composite_tiles, composite_tiles_cached, covered_tiles, effect_histogram, flatten_layer,
|
||||
mask_at, mask_image, pad_tiles,
|
||||
flatten_layer_masked_apart, mask_at, mask_image, pad_tiles,
|
||||
};
|
||||
pub use store::{BakedMaps, ImageRes, ImageStore, PixelStore};
|
||||
@@ -39,6 +39,7 @@ pub mod orb_preview;
|
||||
pub mod painting;
|
||||
pub mod path_tool;
|
||||
pub mod photocraft_cmds;
|
||||
pub mod photocraft_psd;
|
||||
mod polygon_fill;
|
||||
mod polygon_fill_drag;
|
||||
// Mesh reimport reads files: native only.
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
//! PhotoCraft hand-off: a texture set's layer stack as layered OpenRaster documents (one per
|
||||
//! channel) opened in PhotoCraft, and PhotoCraft's saves shown live.
|
||||
//! PhotoCraft hand-off: a texture set's layer stack opened in PhotoCraft, and PhotoCraft's
|
||||
//! saves shown live.
|
||||
//!
|
||||
//! Every layer is rendered alone (content with its fill sources, generators, filters and
|
||||
//! content effects, times its mask: [`Session::flatten`]) into a layer of the same name, so the
|
||||
//! mask becomes the layer's transparency; folders become groups. Each sent file is remembered
|
||||
//! as a [`Link`]: when PhotoCraft saves it, its merged image becomes the image source of a
|
||||
//! `PhotoCraft: <channel>` fill at the top of the set, which leaves the layers below untouched.
|
||||
//! The apps share the file format and the command line, not code.
|
||||
//! By default every texture set goes as one layered PSD holding all its channels, each layer
|
||||
//! a group with its real mask (see [`crate::photocraft_psd`]). The `ora` format instead writes
|
||||
//! one OpenRaster document per channel, where every layer is rendered alone (content with its
|
||||
//! fill sources, generators, filters and content effects, times its mask:
|
||||
//! [`Session::flatten`]) so the mask becomes the layer's transparency; folders become groups.
|
||||
//!
|
||||
//! Each sent file is remembered as a [`Link`]: when PhotoCraft saves it, every channel it holds
|
||||
//! becomes the image source of a `PhotoCraft: <channel>` fill at the top of the set (one undo
|
||||
//! step), which leaves the layers below untouched. The apps share the file format and the
|
||||
//! command line, not code.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use texturecraft_core::{BlendMode, Channel, ChannelKind, LayerId, LayerKind};
|
||||
@@ -29,11 +35,13 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Send layers to PhotoCraft...",
|
||||
["File"],
|
||||
None,
|
||||
"{dir, ts?: index|\"all\" (default: the active set), channels?: [channel ids] (default: the set's channels), open?: true} → {files, warnings} — writes <mesh>_<set>_<channel>.ora per set and channel (every layer rendered with its mask as transparency, folders as groups) and opens them in PhotoCraft; saves made there show live (see photocraft.links)",
|
||||
"{dir, ts?: index|\"all\" (default: the active set), channels?: [channel ids] (default: the set's channels), format?: \"psd\"|\"ora\" (default psd), open?: true} → {files, warnings} — psd: writes <mesh>_<set>.psd per set, every layer a group with its editable mask and one layer per channel inside, a layer comp per channel; ora: writes <mesh>_<set>_<channel>.ora per set and channel, every layer rendered with its mask as transparency. Opens them in PhotoCraft; saves made there show live (see photocraft.links)",
|
||||
always,
|
||||
send
|
||||
),
|
||||
cmd!("photocraft.links", "List PhotoCraft links", [], None, "{} → [{path, ts, channel, layer?}]", always, |s, _| Ok(links_json(s))),
|
||||
cmd!("photocraft.links", "List PhotoCraft links", [], None, "{} → [{path, ts, format, channels, layers: {channel: layer id}}]", always, |s, _| Ok(
|
||||
links_json(s)
|
||||
)),
|
||||
cmd!(
|
||||
"photocraft.sync",
|
||||
"Update from PhotoCraft",
|
||||
@@ -47,16 +55,36 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
]
|
||||
}
|
||||
|
||||
/// How a texture set is sent.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Format {
|
||||
/// One layered PSD per texture set with every channel.
|
||||
Psd,
|
||||
/// One OpenRaster document per texture set and channel.
|
||||
Ora,
|
||||
}
|
||||
|
||||
impl Format {
|
||||
pub fn id(self) -> &'static str {
|
||||
match self {
|
||||
Format::Psd => "psd",
|
||||
Format::Ora => "ora",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A file sent to PhotoCraft, watched for its saves.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Link {
|
||||
pub path: std::path::PathBuf,
|
||||
pub ts: usize,
|
||||
pub channel: Channel,
|
||||
pub format: Format,
|
||||
/// The channels the file holds (one for OpenRaster).
|
||||
pub channels: Vec<Channel>,
|
||||
/// The file's modification time when it was last read or written.
|
||||
pub modified: Option<std::time::SystemTime>,
|
||||
/// The `PhotoCraft: <channel>` fill showing the file's merged image, once there is one.
|
||||
pub layer: Option<LayerId>,
|
||||
/// The `PhotoCraft: <channel>` fills showing what the file holds, once there are some.
|
||||
pub layers: BTreeMap<Channel, LayerId>,
|
||||
}
|
||||
|
||||
/// The session's PhotoCraft links (not saved with the project).
|
||||
@@ -73,7 +101,13 @@ pub(crate) fn links_json(s: &Session) -> Value {
|
||||
s.photocraft
|
||||
.links
|
||||
.iter()
|
||||
.map(|l| json!({"path": l.path.display().to_string(), "ts": l.ts, "channel": l.channel.id(), "layer": l.layer.map(|id| id.0)}))
|
||||
.map(|l| json!({
|
||||
"path": l.path.display().to_string(),
|
||||
"ts": l.ts,
|
||||
"format": l.format.id(),
|
||||
"channels": l.channels.iter().map(|c| c.id()).collect::<Vec<_>>(),
|
||||
"layers": l.layers.iter().map(|(c, id)| (c.id().to_string(), json!(id.0))).collect::<serde_json::Map<_, _>>(),
|
||||
}))
|
||||
.collect::<Vec<_>>()
|
||||
)
|
||||
}
|
||||
@@ -129,7 +163,7 @@ fn texel(c: Channel, px: &[u16]) -> Option<[u16; 4]> {
|
||||
}
|
||||
|
||||
/// The canvas-sized image of channel `c` in flattened tiles.
|
||||
fn flat_image(c: Channel, (w, h): (u32, u32), tiles: &[texturecraft_compose::eval::FlatTile]) -> Rgba16 {
|
||||
pub(crate) fn flat_image(c: Channel, (w, h): (u32, u32), tiles: &[texturecraft_compose::eval::FlatTile]) -> Rgba16 {
|
||||
let mut img = Rgba16::transparent(w, h);
|
||||
let stride = c.components() + 1;
|
||||
for (_, (tx, ty), buf) in tiles.iter().filter(|(ch, _, _)| *ch == c) {
|
||||
@@ -148,8 +182,19 @@ fn flat_image(c: Channel, (w, h): (u32, u32), tiles: &[texturecraft_compose::eva
|
||||
img
|
||||
}
|
||||
|
||||
/// Channel value `v` as an opaque hand-off pixel (sRGB colour, signed values around 0.5).
|
||||
pub(crate) fn encode_value(c: Channel, v: [f32; 3]) -> [u16; 4] {
|
||||
let rgb = match c.kind() {
|
||||
ChannelKind::Color => v.map(|v| linear_to_srgb(v.clamp(0.0, 1.0))),
|
||||
ChannelKind::NormalMap => v,
|
||||
ChannelKind::Scalar => [v[0]; 3],
|
||||
ChannelKind::Signed => [v[0] * 0.5 + 0.5; 3],
|
||||
};
|
||||
[to_u16(rgb[0]), to_u16(rgb[1]), to_u16(rgb[2]), 65535]
|
||||
}
|
||||
|
||||
/// The set's composite of channel `c`: opaque where the mesh covers the texture.
|
||||
fn merged_image(s: &Session, ts: usize, c: Channel) -> Result<Rgba16> {
|
||||
pub(crate) fn merged_image(s: &Session, ts: usize, c: Channel) -> Result<Rgba16> {
|
||||
let cache = s.caches.get(ts).ok_or_else(|| EngineError::Other(format!("no texture set {ts}")))?;
|
||||
let (w, h) = cache.size;
|
||||
let coverage = cache.gbuf.coverage_mask();
|
||||
@@ -160,15 +205,8 @@ fn merged_image(s: &Session, ts: usize, c: Channel) -> Result<Rgba16> {
|
||||
if !coverage.get(i).copied().unwrap_or(false) {
|
||||
continue;
|
||||
}
|
||||
let v = cache.composite.value(c, x, y);
|
||||
let rgb = match c.kind() {
|
||||
ChannelKind::Color => v.map(|v| linear_to_srgb(v.clamp(0.0, 1.0))),
|
||||
ChannelKind::NormalMap => v,
|
||||
ChannelKind::Scalar => [v[0]; 3],
|
||||
ChannelKind::Signed => [v[0] * 0.5 + 0.5; 3],
|
||||
};
|
||||
if let Some(dst) = img.data.get_mut(i * 4..i * 4 + 4) {
|
||||
dst.copy_from_slice(&[to_u16(rgb[0]), to_u16(rgb[1]), to_u16(rgb[2]), 65535]);
|
||||
dst.copy_from_slice(&encode_value(c, cache.composite.value(c, x, y)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -289,10 +327,19 @@ fn send(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let dir = std::path::PathBuf::from(dir);
|
||||
let sets = sets_param(s, p)?;
|
||||
let open = p.get("open").and_then(Value::as_bool).unwrap_or(true);
|
||||
let mut jobs = Vec::new();
|
||||
let format = match p.get("format") {
|
||||
None | Some(Value::Null) => Format::Psd,
|
||||
Some(v) if v.as_str() == Some("psd") => Format::Psd,
|
||||
Some(v) if v.as_str() == Some("ora") => Format::Ora,
|
||||
Some(v) => return Err(bad(SEND, format!("unknown format {v}: use \"psd\" or \"ora\""))),
|
||||
};
|
||||
let mut jobs: Vec<(usize, Vec<Channel>)> = Vec::new();
|
||||
for &ts in &sets {
|
||||
for c in channels_param(s, ts, p)? {
|
||||
jobs.push((ts, c));
|
||||
let channels = channels_param(s, ts, p)?;
|
||||
match format {
|
||||
Format::Psd if !channels.is_empty() => jobs.push((ts, channels)),
|
||||
Format::Psd => {}
|
||||
Format::Ora => jobs.extend(channels.into_iter().map(|c| (ts, vec![c]))),
|
||||
}
|
||||
}
|
||||
if jobs.is_empty() {
|
||||
@@ -302,21 +349,34 @@ fn send(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let mesh = std::path::Path::new(s.doc.project.mesh.trim_start_matches("builtin:")).file_stem().and_then(|n| n.to_str()).unwrap_or("Untitled").to_string();
|
||||
let mut files = Vec::new();
|
||||
let mut warnings = Vec::new();
|
||||
for (ts, c) in jobs {
|
||||
for (ts, channels) in jobs {
|
||||
let set_name = s.doc.project.texture_set(ts).map(|t| t.name.clone()).unwrap_or_default();
|
||||
let stem = texturecraft_formats::export::sanitize_file_stem(&format!("{mesh}_{set_name}_{}", c.id()));
|
||||
let path = dir.join(format!("{stem}.{}", ora::ORA_EXT));
|
||||
let (stem, ext) = match (format, channels.first()) {
|
||||
(Format::Ora, Some(c)) => (format!("{mesh}_{set_name}_{}", c.id()), ora::ORA_EXT),
|
||||
_ => (format!("{mesh}_{set_name}"), texturecraft_formats::psd::PSD_EXT),
|
||||
};
|
||||
let path = dir.join(format!("{}.{ext}", texturecraft_formats::export::sanitize_file_stem(&stem)));
|
||||
if files.contains(&path) {
|
||||
warnings.push(format!("texture set \"{set_name}\" sends as {} like an earlier set, so it was skipped; rename one of them", path.display()));
|
||||
continue;
|
||||
}
|
||||
let (bytes, w) = s.photocraft_document(ts, c)?;
|
||||
warnings.extend(w.into_iter().map(|w| format!("{set_name} {}: {w}", c.label())));
|
||||
let (bytes, w) = match (format, channels.first()) {
|
||||
(Format::Ora, Some(&c)) => {
|
||||
let (bytes, w) = s.photocraft_document(ts, c)?;
|
||||
(bytes, w.into_iter().map(|w| format!("{set_name} {}: {w}", c.label())).collect::<Vec<_>>())
|
||||
}
|
||||
_ => {
|
||||
let (bytes, w) = s.photocraft_psd(ts, &channels)?;
|
||||
(bytes, w.into_iter().map(|w| format!("{set_name}: {w}")).collect())
|
||||
}
|
||||
};
|
||||
warnings.extend(w);
|
||||
texturecraft_formats::write_atomic(&path, &bytes).map_err(|e| bad(SEND, format!("cannot write {}: {e}", path.display())))?;
|
||||
let modified = std::fs::metadata(&path).and_then(|m| m.modified()).ok();
|
||||
let layer = s.photocraft.links.iter().find(|l| l.path == path).and_then(|l| l.layer);
|
||||
// Sending again keeps the fills an earlier save made.
|
||||
let layers = s.photocraft.links.iter().find(|l| l.path == path && l.ts == ts).map(|l| l.layers.clone()).unwrap_or_default();
|
||||
s.photocraft.links.retain(|l| l.path != path);
|
||||
s.photocraft.links.push(Link { path: path.clone(), ts, channel: c, modified, layer });
|
||||
s.photocraft.links.push(Link { path: path.clone(), ts, format, channels, modified, layers });
|
||||
files.push(path);
|
||||
}
|
||||
s.log.push(format!("Sent {} documents to {}", files.len(), dir.display()));
|
||||
@@ -387,9 +447,9 @@ impl Session {
|
||||
l.modified = Some(modified);
|
||||
}
|
||||
match self.show_photocraft_file(&link) {
|
||||
Ok(layer) => {
|
||||
Ok(layers) => {
|
||||
if let Some(l) = self.photocraft.links.get_mut(i) {
|
||||
l.layer = Some(layer);
|
||||
l.layers = layers;
|
||||
}
|
||||
self.log.push(format!("Updated {} from PhotoCraft", link.path.display()));
|
||||
updated.push(link.path.clone());
|
||||
@@ -400,10 +460,10 @@ impl Session {
|
||||
updated
|
||||
}
|
||||
|
||||
/// Put the merged image of `link`'s file into its `PhotoCraft: <channel>` fill (created at the
|
||||
/// top of the set when missing), as one undo step.
|
||||
/// Put every channel `link`'s file holds into its `PhotoCraft: <channel>` fill (created at
|
||||
/// the top of the set when missing), as one undo step.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn show_photocraft_file(&mut self, link: &Link) -> Result<LayerId> {
|
||||
fn show_photocraft_file(&mut self, link: &Link) -> Result<BTreeMap<Channel, LayerId>> {
|
||||
use std::io::Read;
|
||||
let io = |e: std::io::Error| EngineError::Other(e.to_string());
|
||||
let file = std::fs::File::open(&link.path).map_err(io)?;
|
||||
@@ -412,17 +472,66 @@ impl Session {
|
||||
if bytes.len() as u64 > MAX_LINKED_BYTES {
|
||||
return Err(EngineError::Other(format!("larger than {} MB", MAX_LINKED_BYTES >> 20)));
|
||||
}
|
||||
let png = ora::read_merged(&bytes).map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
self.show_photocraft_image(link.ts, link.channel, link.layer, &png)
|
||||
let err = |e: texturecraft_formats::FormatError| EngineError::Other(e.to_string());
|
||||
let images = match link.format {
|
||||
Format::Ora => {
|
||||
let c = link.channels.first().copied().ok_or_else(|| EngineError::Other("the link holds no channel".into()))?;
|
||||
vec![(c, ora::read_merged(&bytes).map_err(err)?)]
|
||||
}
|
||||
Format::Psd => {
|
||||
let doc = texturecraft_formats::psd::read_psd(&bytes).map_err(err)?;
|
||||
let cache = self.caches.get(link.ts).ok_or_else(|| EngineError::Other(format!("no texture set {}", link.ts)))?;
|
||||
if (doc.width, doc.height) != cache.size {
|
||||
return Err(EngineError::Other(format!(
|
||||
"the document is {}×{} but the texture set is {}×{}; resize it back to bring it in",
|
||||
doc.width, doc.height, cache.size.0, cache.size.1
|
||||
)));
|
||||
}
|
||||
let coverage = cache.gbuf.coverage_mask();
|
||||
let mut notes = Vec::new();
|
||||
let mut images = Vec::new();
|
||||
for &c in &link.channels {
|
||||
let img = crate::photocraft_psd::composite(&doc, c, &coverage, &mut notes);
|
||||
images.push((c, ora::png16(&img).map_err(err)?));
|
||||
}
|
||||
self.log.extend(notes.into_iter().map(|n| format!("PhotoCraft: {}: {n}", link.path.display())));
|
||||
images
|
||||
}
|
||||
};
|
||||
self.show_photocraft_images(link.ts, &images, &link.layers)
|
||||
}
|
||||
|
||||
/// Show `png` as channel `c` of texture set `ts` through the fill `layer` (when it is still a
|
||||
/// fill of that set) or a new `PhotoCraft: <channel>` fill at the top. The previous image
|
||||
/// resource is removed when nothing else uses it.
|
||||
pub fn show_photocraft_image(&mut self, ts: usize, c: Channel, layer: Option<LayerId>, png: &[u8]) -> Result<LayerId> {
|
||||
let layers: BTreeMap<Channel, LayerId> = layer.map(|id| (c, id)).into_iter().collect();
|
||||
self.show_photocraft_images(ts, &[(c, png.to_vec())], &layers)?.get(&c).copied().ok_or_else(|| EngineError::Other("no layer was made".into()))
|
||||
}
|
||||
|
||||
/// [`Session::show_photocraft_image`] for several channels in one undo step.
|
||||
pub fn show_photocraft_images(
|
||||
&mut self,
|
||||
ts: usize,
|
||||
images: &[(Channel, Vec<u8>)],
|
||||
layers: &BTreeMap<Channel, LayerId>,
|
||||
) -> Result<BTreeMap<Channel, LayerId>> {
|
||||
let (w, h) = self.caches.get(ts).map(|c| c.size).ok_or_else(|| EngineError::Other(format!("no texture set {ts}")))?;
|
||||
let set_name = self.doc.project.texture_set(ts).map_err(|e| EngineError::Other(e.to_string()))?.name.clone();
|
||||
self.edit(UPDATE, |s| {
|
||||
let mut out = BTreeMap::new();
|
||||
for (c, png) in images {
|
||||
let id = s.put_photocraft_image(ts, &set_name, (w, h), *c, layers.get(c).copied(), png)?;
|
||||
out.insert(*c, id);
|
||||
}
|
||||
Ok(out)
|
||||
})
|
||||
}
|
||||
|
||||
/// The body of [`Session::show_photocraft_images`] for one channel, inside its edit.
|
||||
fn put_photocraft_image(&mut self, ts: usize, set_name: &str, (w, h): (u32, u32), c: Channel, layer: Option<LayerId>, png: &[u8]) -> Result<LayerId> {
|
||||
let s = self;
|
||||
{
|
||||
let resource = texturecraft_formats::export::sanitize_file_stem(&format!("photocraft_{set_name}_{}", c.id()));
|
||||
let name = s.add_image_resource(&format!("{resource}.png"), png.to_vec(), u64::from(w) * u64::from(h) * 4)?;
|
||||
let source = texturecraft_core::Source::image(name);
|
||||
@@ -452,7 +561,7 @@ impl Session {
|
||||
}
|
||||
};
|
||||
Ok(id)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Drop image resource `name` when no fill source uses it any more.
|
||||
@@ -474,6 +583,7 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::execute;
|
||||
use std::io::Read;
|
||||
use texturecraft_formats::psd;
|
||||
use texturecraft_geom::primitives;
|
||||
|
||||
/// A fresh folder under the system temp dir, removed when dropped.
|
||||
@@ -528,7 +638,9 @@ mod tests {
|
||||
execute(&mut s, "layer.rename", &json!({"layer": fill.0, "name": "Red"})).unwrap();
|
||||
execute(&mut s, "mask.add.black", &Value::Null).unwrap();
|
||||
execute(&mut s, "layer.group", &json!({"layer": fill.0})).unwrap();
|
||||
let r = execute(&mut s, SEND, &json!({"dir": dir.path().display().to_string(), "channels": ["baseColor", "roughness"], "open": false})).unwrap();
|
||||
let r =
|
||||
execute(&mut s, SEND, &json!({"dir": dir.path().display().to_string(), "channels": ["baseColor", "roughness"], "format": "ora", "open": false}))
|
||||
.unwrap();
|
||||
let files: Vec<String> = r["files"].as_array().unwrap().iter().map(|f| f.as_str().unwrap().to_string()).collect();
|
||||
assert_eq!(files.len(), 2, "{r}");
|
||||
let base = std::path::PathBuf::from(files.iter().find(|f| f.ends_with("_baseColor.ora")).unwrap());
|
||||
@@ -552,7 +664,7 @@ mod tests {
|
||||
let dir = tempdir("srgb");
|
||||
let mut s = session();
|
||||
execute(&mut s, "layer.add.fill", &json!({"color": [0.5, 0.5, 0.5]})).unwrap();
|
||||
let r = execute(&mut s, SEND, &json!({"dir": dir.path().display().to_string(), "channels": ["baseColor"], "open": false})).unwrap();
|
||||
let r = execute(&mut s, SEND, &json!({"dir": dir.path().display().to_string(), "channels": ["baseColor"], "format": "ora", "open": false})).unwrap();
|
||||
let path = std::path::PathBuf::from(r["files"][0].as_str().unwrap());
|
||||
let xml = stack_xml(&path);
|
||||
let src = xml.split("src=\"").nth(1).unwrap().split('"').next().unwrap().to_string();
|
||||
@@ -565,7 +677,7 @@ mod tests {
|
||||
execute(&mut s, "mask.add.black", &Value::Null).unwrap();
|
||||
execute(&mut s, "effect.add.generator", &json!({"generator": "position"})).unwrap();
|
||||
s.update();
|
||||
let r = execute(&mut s, SEND, &json!({"dir": dir.path().display().to_string(), "channels": ["baseColor"], "open": false})).unwrap();
|
||||
let r = execute(&mut s, SEND, &json!({"dir": dir.path().display().to_string(), "channels": ["baseColor"], "format": "ora", "open": false})).unwrap();
|
||||
let path = std::path::PathBuf::from(r["files"][0].as_str().unwrap());
|
||||
let xml = stack_xml(&path);
|
||||
let src = xml.split("src=\"").nth(1).unwrap().split('"').next().unwrap().to_string();
|
||||
@@ -588,7 +700,7 @@ mod tests {
|
||||
fn a_save_in_photocraft_shows_live_and_undoes() {
|
||||
let dir = tempdir("live");
|
||||
let mut s = session();
|
||||
let r = execute(&mut s, SEND, &json!({"dir": dir.path().display().to_string(), "channels": ["baseColor"], "open": false})).unwrap();
|
||||
let r = execute(&mut s, SEND, &json!({"dir": dir.path().display().to_string(), "channels": ["baseColor"], "format": "ora", "open": false})).unwrap();
|
||||
let path = std::path::PathBuf::from(r["files"][0].as_str().unwrap());
|
||||
// Nothing changed since the send.
|
||||
assert!(s.sync_photocraft().is_empty());
|
||||
@@ -605,12 +717,12 @@ mod tests {
|
||||
s.update();
|
||||
let v = s.caches[0].composite.value(Channel::BaseColor, w / 2, h / 2);
|
||||
assert!(v[1] > 0.9 && v[0] < 0.1, "{v:?}");
|
||||
let layer = s.photocraft.links[0].layer.unwrap();
|
||||
let layer = s.photocraft.links[0].layers[&Channel::BaseColor];
|
||||
assert!(s.doc.project.layer(layer).unwrap().name.starts_with("PhotoCraft"));
|
||||
// A second save reuses the layer and its old image resource goes.
|
||||
std::fs::File::options().write(true).open(&path).unwrap().set_modified(later + std::time::Duration::from_secs(5)).unwrap();
|
||||
assert_eq!(s.sync_photocraft().len(), 1);
|
||||
assert_eq!(s.photocraft.links[0].layer, Some(layer));
|
||||
assert_eq!(s.photocraft.links[0].layers.get(&Channel::BaseColor), Some(&layer));
|
||||
assert_eq!(s.doc.resources.keys().filter(|k| k.contains("photocraft")).count(), 1, "{:?}", s.doc.resources.keys().collect::<Vec<_>>());
|
||||
// A broken file is logged, not fatal.
|
||||
std::fs::write(&path, b"broken").unwrap();
|
||||
@@ -627,6 +739,179 @@ mod tests {
|
||||
assert!(s.photocraft.links.is_empty());
|
||||
}
|
||||
|
||||
fn read_psd(path: &std::path::Path) -> psd::ReadDocument {
|
||||
psd::read_psd(&std::fs::read(path).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
fn find<'a>(layers: &'a [psd::ReadLayer], name: &str) -> Option<&'a psd::ReadLayer> {
|
||||
layers.iter().find_map(|l| {
|
||||
if l.name == name {
|
||||
return Some(l);
|
||||
}
|
||||
match &l.kind {
|
||||
psd::ReadKind::Group(children) => find(children, name),
|
||||
psd::ReadKind::Pixels { .. } => None,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn children(l: &psd::ReadLayer) -> &[psd::ReadLayer] {
|
||||
match &l.kind {
|
||||
psd::ReadKind::Group(c) => c,
|
||||
psd::ReadKind::Pixels { .. } => panic!("{} is not a group", l.name),
|
||||
}
|
||||
}
|
||||
|
||||
fn pixel(l: &psd::ReadLayer, x: i32, y: i32) -> [u16; 4] {
|
||||
let psd::ReadKind::Pixels { x: x0, y: y0, image } = &l.kind else { panic!("{} is a group", l.name) };
|
||||
let i = ((y - y0) as usize * image.width as usize + (x - x0) as usize) * 4;
|
||||
image.data[i..i + 4].try_into().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sends_one_psd_per_set_with_a_group_per_layer_its_mask_and_a_layer_per_channel() {
|
||||
let dir = tempdir("psd");
|
||||
let mut s = session();
|
||||
// A dab in a corner of the paint layer: its other tiles hold no paint.
|
||||
execute(&mut s, "tool.strokeUv", &json!({"points": [[0.1, 0.1], [0.12, 0.1]]})).unwrap();
|
||||
execute(&mut s, "layer.add.fill", &json!({"color": [1.0, 0.0, 0.0], "roughness": 0.2})).unwrap();
|
||||
let fill = s.selected_layer().unwrap();
|
||||
execute(&mut s, "layer.rename", &json!({"layer": fill.0, "name": "Red"})).unwrap();
|
||||
execute(&mut s, "mask.add.black", &Value::Null).unwrap();
|
||||
execute(&mut s, "layer.group", &json!({"layer": fill.0})).unwrap();
|
||||
let r = execute(&mut s, SEND, &json!({"dir": dir.path().display().to_string(), "channels": ["baseColor", "roughness"], "open": false})).unwrap();
|
||||
let files = r["files"].as_array().unwrap();
|
||||
assert_eq!(files.len(), 1, "{r}");
|
||||
let path = std::path::PathBuf::from(files[0].as_str().unwrap());
|
||||
assert_eq!(path.extension().unwrap(), "psd");
|
||||
let doc = read_psd(&path);
|
||||
let (w, h) = s.caches[0].size;
|
||||
assert_eq!((doc.width, doc.height), (w, h));
|
||||
// Top-first: the folder holding "Red", then the project's first paint layer.
|
||||
assert_eq!(doc.layers.len(), 2, "{:?}", doc.layers.iter().map(|l| &l.name).collect::<Vec<_>>());
|
||||
assert!(find(children(&doc.layers[0]), "Red").is_some());
|
||||
assert_eq!(doc.layers[1].name, "Layer 1");
|
||||
// A paint layer without a mask hides nothing, so its group has no mask (also where it
|
||||
// holds no paint).
|
||||
assert!(doc.layers[1].mask.is_none());
|
||||
let red = find(&doc.layers, "Red").unwrap();
|
||||
assert_eq!(&red.blend, b"pass");
|
||||
// The black mask is the group's own mask; the content inside stays whole.
|
||||
let (cx, cy) = (w as i32 / 2, h as i32 / 2);
|
||||
assert_eq!(red.mask.as_ref().map(|m| m.at(cx, cy)), Some(0));
|
||||
let inside = children(red);
|
||||
assert_eq!(inside.iter().map(|l| l.name.as_str()).collect::<Vec<_>>(), ["Base color", "Roughness"]);
|
||||
assert!(inside[0].visible && !inside[1].visible, "the first comp shows only Base color");
|
||||
assert_eq!(pixel(&inside[0], cx, cy), [65535, 0, 0, 65535]);
|
||||
let rough = pixel(&inside[1], cx, cy);
|
||||
assert!((rough[0] as f32 / 65535.0 - 0.2).abs() < 1e-3 && rough[3] == 65535, "{rough:?}");
|
||||
// One link holds both channels.
|
||||
assert_eq!(s.photocraft.links.len(), 1);
|
||||
assert_eq!(links_json(&s)[0]["channels"], json!(["baseColor", "roughness"]));
|
||||
assert_eq!(links_json(&s)[0]["format"], "psd");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_psd_sent_back_unchanged_rebuilds_every_channel() {
|
||||
let dir = tempdir("psd_same");
|
||||
let mut s = session();
|
||||
execute(&mut s, "layer.add.fill", &json!({"color": [0.2, 0.6, 0.9], "roughness": 0.7, "metallic": 1.0})).unwrap();
|
||||
execute(&mut s, "mask.add.black", &Value::Null).unwrap();
|
||||
execute(&mut s, "effect.add.generator", &json!({"generator": "position"})).unwrap();
|
||||
execute(&mut s, "layer.add.fill", &json!({"roughness": 0.1})).unwrap();
|
||||
let top = s.selected_layer().unwrap();
|
||||
execute(&mut s, "layer.blend", &json!({"layer": top.0, "channel": "roughness", "mode": "multiply", "opacity": 0.5})).unwrap();
|
||||
s.update();
|
||||
let channels = [Channel::BaseColor, Channel::Roughness, Channel::Metallic];
|
||||
let r = execute(&mut s, SEND, &json!({"dir": dir.path().display().to_string(), "channels": ["baseColor", "roughness", "metallic"], "open": false}))
|
||||
.unwrap();
|
||||
let doc = read_psd(std::path::Path::new(r["files"][0].as_str().unwrap()));
|
||||
let coverage = s.caches[0].gbuf.coverage_mask();
|
||||
let (w, h) = s.caches[0].size;
|
||||
let mut notes = Vec::new();
|
||||
for c in channels {
|
||||
let img = crate::photocraft_psd::composite(&doc, c, &coverage, &mut notes);
|
||||
let mut worst = 0i32;
|
||||
for (x, y) in [(3, 5), (w / 4, h / 3), (w / 2, h / 2), (w - 7, h - 9), (w / 3, h - 20)] {
|
||||
let i = (y as usize * w as usize + x as usize) * 4;
|
||||
let want = encode_value(c, s.caches[0].composite.value(c, x, y));
|
||||
for k in 0..4 {
|
||||
worst = worst.max((img.data[i + k] as i32 - want[k] as i32).abs());
|
||||
}
|
||||
}
|
||||
assert!(worst < 300, "{c:?} differs by {worst}");
|
||||
}
|
||||
assert!(notes.is_empty(), "{notes:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_psd_save_brings_every_channel_back_in_one_step_through_its_group_mask() {
|
||||
let dir = tempdir("psd_live");
|
||||
let mut s = session();
|
||||
let r = execute(&mut s, SEND, &json!({"dir": dir.path().display().to_string(), "channels": ["baseColor", "roughness"], "open": false})).unwrap();
|
||||
let path = std::path::PathBuf::from(r["files"][0].as_str().unwrap());
|
||||
assert!(s.sync_photocraft().is_empty());
|
||||
// PhotoCraft saves: one group whose mask shows only the right half, holding a green
|
||||
// base colour, a roughness of 0.8 (hidden, as a comp would leave it) and a stray layer.
|
||||
let (w, h) = s.caches[0].size;
|
||||
let solid = |px: [u16; 4]| {
|
||||
let mut img = Rgba16::transparent(w, h);
|
||||
for p in img.data.as_chunks_mut::<4>().0 {
|
||||
*p = px;
|
||||
}
|
||||
psd::encode_pixels(&img).unwrap()
|
||||
};
|
||||
let mut mask = psd::Mask16 { width: w, height: h, data: vec![0; (w * h) as usize] };
|
||||
for (i, v) in mask.data.iter_mut().enumerate() {
|
||||
if i as u32 % w >= w / 2 {
|
||||
*v = 65535;
|
||||
}
|
||||
}
|
||||
let layer = |name: &str, visible: bool, px: [u16; 4]| psd::Layer {
|
||||
name: name.into(),
|
||||
visible,
|
||||
opacity: 1.0,
|
||||
blend: *b"norm",
|
||||
mask: None,
|
||||
comps: Vec::new(),
|
||||
kind: psd::Kind::Pixels(solid(px)),
|
||||
};
|
||||
let r8 = (0.8f32 * 65535.0) as u16;
|
||||
let group = psd::Layer {
|
||||
name: "Paint".into(),
|
||||
visible: true,
|
||||
opacity: 1.0,
|
||||
blend: *b"pass",
|
||||
mask: Some(psd::encode_mask(&mask).unwrap()),
|
||||
comps: Vec::new(),
|
||||
kind: psd::Kind::Group(vec![
|
||||
layer("Notes", true, [65535, 65535, 65535, 65535]),
|
||||
layer("Base color", true, [0, 65535, 0, 65535]),
|
||||
layer("Roughness", false, [r8, r8, r8, 65535]),
|
||||
]),
|
||||
};
|
||||
let doc = psd::Document { layers: vec![group], merged: Rgba16::transparent(w, h), comps: vec!["Base color".into(), "Roughness".into()] };
|
||||
std::fs::write(&path, psd::write_psd(&doc).unwrap()).unwrap();
|
||||
let later = std::time::SystemTime::now() + std::time::Duration::from_secs(5);
|
||||
std::fs::File::options().write(true).open(&path).unwrap().set_modified(later).unwrap();
|
||||
let undo_before = s.undo.len();
|
||||
assert_eq!(s.sync_photocraft(), vec![path.clone()]);
|
||||
assert_eq!(s.undo.len(), undo_before + 1, "both channels are one undo step");
|
||||
s.update();
|
||||
let comp = &s.caches[0].composite;
|
||||
let (right, left) = ((w * 3 / 4, h / 2), (w / 4, h / 2));
|
||||
let base = comp.value(Channel::BaseColor, right.0, right.1);
|
||||
assert!(base[1] > 0.9 && base[0] < 0.1, "{base:?}");
|
||||
assert!((comp.value(Channel::Roughness, right.0, right.1)[0] - 0.8).abs() < 1e-2);
|
||||
// Where the group mask hides the paint, the channels keep their defaults.
|
||||
let d = Channel::Roughness.default_value()[0];
|
||||
assert!((comp.value(Channel::Roughness, left.0, left.1)[0] - d).abs() < 1e-2);
|
||||
assert_eq!(s.photocraft.links[0].layers.len(), 2);
|
||||
assert!(s.log.iter().any(|l| l.contains("\"Notes\"")), "{:?}", s.log);
|
||||
execute(&mut s, "edit.undo", &Value::Null).unwrap();
|
||||
assert!(s.doc.project.walk(0).iter().all(|(id, _)| !s.doc.project.layer(*id).unwrap().name.starts_with("PhotoCraft")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_params_are_errors() {
|
||||
let mut s = session();
|
||||
@@ -638,6 +923,7 @@ mod tests {
|
||||
assert!(execute(&mut s, SEND, &json!({"dir": d, "channels": ["nope"], "open": false})).is_err());
|
||||
assert!(execute(&mut s, SEND, &json!({"dir": d, "channels": "baseColor", "open": false})).is_err());
|
||||
assert!(execute(&mut s, SEND, &json!({"dir": d, "channels": ["coatNormal"], "open": false})).is_err());
|
||||
assert!(execute(&mut s, SEND, &json!({"dir": d, "format": "tiff", "open": false})).is_err());
|
||||
assert!(s.show_photocraft_image(0, Channel::BaseColor, None, b"not a png").is_err());
|
||||
assert!(s.doc.project.walk(0).iter().all(|(id, _)| !s.doc.project.layer(*id).unwrap().name.starts_with("PhotoCraft")));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,379 @@
|
||||
//! The PhotoCraft hand-off as one layered PSD per texture set.
|
||||
//!
|
||||
//! The stack keeps its shape: every folder is a group and every layer a pass-through group
|
||||
//! named after it, carrying the layer's mask as an editable group mask; inside it, one pixel
|
||||
//! layer per channel (`Base color`, `Roughness`, ...) holds the layer's content for that
|
||||
//! channel with the channel's blend mode and opacity. One layer comp per channel shows only
|
||||
//! that channel's layers, so the whole material is one document and a mask edited once
|
||||
//! shapes every channel.
|
||||
//!
|
||||
//! When PhotoCraft saves the file, [`composite`] rebuilds each channel from the saved layers:
|
||||
//! a pixel layer counts for the channel it is named after, through every group mask, opacity
|
||||
//! and visibility above it, blended with TextureCraft's own blend math.
|
||||
|
||||
use texturecraft_core::{BlendMode, Channel, ChannelKind, LayerId, LayerKind, blend1, blend3, mix3};
|
||||
use texturecraft_formats::ora::Rgba16;
|
||||
use texturecraft_formats::psd::{self, EncodedMask, Kind, Mask16, ReadDocument, ReadKind, ReadLayer};
|
||||
use texturecraft_texel::{TILE, TILE_AREA, from_u16, srgb_to_linear};
|
||||
|
||||
use crate::photocraft_cmds::{encode_value, flat_image, merged_image};
|
||||
use crate::{EngineError, Result, Session};
|
||||
|
||||
/// The PSD blend mode key for `mode`, with a warning when it is only close or missing.
|
||||
pub(crate) fn psd_key(mode: BlendMode) -> ([u8; 4], Option<&'static str>) {
|
||||
use BlendMode as B;
|
||||
match mode {
|
||||
B::Normal => (*b"norm", None),
|
||||
B::Passthrough => (*b"pass", None),
|
||||
B::Multiply => (*b"mul ", None),
|
||||
B::Screen => (*b"scrn", None),
|
||||
B::Overlay => (*b"over", None),
|
||||
B::Darken => (*b"dark", None),
|
||||
B::Lighten => (*b"lite", None),
|
||||
B::ColorDodge => (*b"div ", None),
|
||||
B::ColorBurn => (*b"idiv", None),
|
||||
B::HardLight => (*b"hLit", None),
|
||||
B::SoftLight => (*b"sLit", None),
|
||||
B::Difference => (*b"diff", None),
|
||||
B::Exclusion => (*b"smud", None),
|
||||
B::LinearDodge => (*b"lddg", None),
|
||||
B::LinearBurn => (*b"lbrn", None),
|
||||
B::VividLight => (*b"vLit", None),
|
||||
B::LinearLight => (*b"lLit", None),
|
||||
B::PinLight => (*b"pLit", None),
|
||||
B::Subtract => (*b"fsub", None),
|
||||
B::Divide => (*b"fdiv", None),
|
||||
B::Saturation => (*b"sat ", None),
|
||||
B::Color => (*b"colr", None),
|
||||
B::Value => (*b"lum ", Some("Value is sent as Luminosity")),
|
||||
_ => (*b"norm", Some("this blend mode has no PSD equivalent; it is sent as Normal")),
|
||||
}
|
||||
}
|
||||
|
||||
/// The blend mode a PSD key stands for (unknown keys blend as Normal).
|
||||
fn mode_of(key: &[u8; 4]) -> BlendMode {
|
||||
use BlendMode as B;
|
||||
match key {
|
||||
b"mul " => B::Multiply,
|
||||
b"scrn" => B::Screen,
|
||||
b"over" => B::Overlay,
|
||||
b"dark" => B::Darken,
|
||||
b"lite" => B::Lighten,
|
||||
b"div " => B::ColorDodge,
|
||||
b"idiv" => B::ColorBurn,
|
||||
b"hLit" => B::HardLight,
|
||||
b"sLit" => B::SoftLight,
|
||||
b"diff" => B::Difference,
|
||||
b"smud" => B::Exclusion,
|
||||
b"lddg" => B::LinearDodge,
|
||||
b"lbrn" => B::LinearBurn,
|
||||
b"vLit" => B::VividLight,
|
||||
b"lLit" => B::LinearLight,
|
||||
b"pLit" => B::PinLight,
|
||||
b"fsub" => B::Subtract,
|
||||
b"fdiv" => B::Divide,
|
||||
b"sat " => B::Saturation,
|
||||
b"colr" => B::Color,
|
||||
b"lum " => B::Value,
|
||||
_ => B::Normal,
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds the PSD tree of one texture set.
|
||||
struct Tree<'s> {
|
||||
s: &'s Session,
|
||||
ts: usize,
|
||||
size: (u32, u32),
|
||||
channels: Vec<Channel>,
|
||||
covered: Vec<(u32, u32)>,
|
||||
warnings: Vec<String>,
|
||||
}
|
||||
|
||||
impl Tree<'_> {
|
||||
fn warn(&mut self, layer: &str, msg: &str) {
|
||||
let line = format!("\"{layer}\": {msg}");
|
||||
if !self.warnings.contains(&line) {
|
||||
self.warnings.push(line);
|
||||
}
|
||||
}
|
||||
|
||||
/// `ids` (bottom → top) as PSD layers top-first.
|
||||
fn nodes(&mut self, ids: &[LayerId], depth: usize) -> Result<Vec<psd::Layer>> {
|
||||
if depth > psd::MAX_DEPTH {
|
||||
return Err(EngineError::Other(format!("folders are nested more than {} deep", psd::MAX_DEPTH)));
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
for &id in ids.iter().rev() {
|
||||
let layer = self.s.doc.project.layer(id).map_err(|e| EngineError::Other(e.to_string()))?.clone();
|
||||
let enabled: Vec<(Channel, texturecraft_core::ChannelBlend)> =
|
||||
self.channels.iter().filter_map(|c| layer.blend(*c).filter(|b| b.mode != BlendMode::Disable).map(|b| (*c, b))).collect();
|
||||
if enabled.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let node = match &layer.kind {
|
||||
LayerKind::Group { children, .. } => {
|
||||
// A folder has one blend mode and opacity in the PSD; TextureCraft has one per channel.
|
||||
let first = enabled.first().map(|(_, b)| *b).unwrap_or_default();
|
||||
let uniform = enabled.iter().all(|(_, b)| b.mode == first.mode && (b.opacity - first.opacity).abs() < 1e-4);
|
||||
let (blend, opacity) = if uniform {
|
||||
let (key, note) = psd_key(first.mode);
|
||||
if let Some(note) = note {
|
||||
self.warn(&layer.name, note);
|
||||
}
|
||||
(key, first.opacity)
|
||||
} else {
|
||||
self.warn(&layer.name, "the folder blends its channels differently; it is sent as a pass-through group at 100%");
|
||||
(*b"pass", 1.0)
|
||||
};
|
||||
let children = self.nodes(children, depth + 1)?;
|
||||
psd::Layer {
|
||||
name: layer.name.clone(),
|
||||
visible: layer.visible,
|
||||
opacity,
|
||||
blend,
|
||||
mask: self.mask(id, &[])?,
|
||||
comps: Vec::new(),
|
||||
kind: Kind::Group(children),
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let (content, masks) = self.s.flatten_apart(self.ts, id).unwrap_or_default();
|
||||
let mask = self.mask(id, &masks)?;
|
||||
let mut children = Vec::new();
|
||||
for (c, b) in &enabled {
|
||||
let (key, note) = psd_key(b.mode);
|
||||
if let Some(note) = note {
|
||||
self.warn(&format!("{} › {}", layer.name, c.label()), note);
|
||||
}
|
||||
let image = psd::encode_pixels(&flat_image(*c, self.size, &content)).map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
let comps: Vec<bool> = self.channels.iter().map(|x| x == c).collect();
|
||||
let first_comp = self.channels.first() == Some(c);
|
||||
children.push(psd::Layer {
|
||||
name: c.label().to_string(),
|
||||
visible: first_comp,
|
||||
opacity: b.opacity,
|
||||
blend: key,
|
||||
mask: None,
|
||||
comps,
|
||||
kind: Kind::Pixels(image),
|
||||
});
|
||||
}
|
||||
psd::Layer {
|
||||
name: layer.name.clone(),
|
||||
visible: layer.visible,
|
||||
opacity: 1.0,
|
||||
blend: *b"pass",
|
||||
mask,
|
||||
comps: Vec::new(),
|
||||
kind: Kind::Group(children),
|
||||
}
|
||||
}
|
||||
};
|
||||
out.push(node);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The mask of layer or folder `id` over the covered texels, when it hides any of them.
|
||||
/// `tiles` are the mask tiles flattening gave (a layer); the texels they leave out, such as
|
||||
/// tiles where a paint layer is empty, are evaluated on their own.
|
||||
fn mask(&self, id: LayerId, tiles: &[texturecraft_compose::eval::MaskTile]) -> Result<Option<EncodedMask>> {
|
||||
let (w, h) = self.size;
|
||||
let index = |x: u32, y: u32| (x < w && y < h).then(|| y as usize * w as usize + x as usize);
|
||||
let mut data: Vec<Option<u16>> = vec![None; w as usize * h as usize];
|
||||
for ((tx, ty), values) in tiles {
|
||||
for (o, v) in values.iter().enumerate().take(TILE_AREA) {
|
||||
if let Some(d) = index(tx * TILE + o as u32 % TILE, ty * TILE + o as u32 / TILE).and_then(|i| data.get_mut(i)) {
|
||||
*d = Some(*v);
|
||||
}
|
||||
}
|
||||
}
|
||||
let missing: Vec<(u32, u32)> =
|
||||
self.covered.iter().copied().filter(|&(x, y)| index(x, y).and_then(|i| data.get(i)).is_some_and(Option::is_none)).collect();
|
||||
if !missing.is_empty() {
|
||||
let values = self.s.with_inputs(self.ts, |inp| texturecraft_compose::mask_at(inp, id, &missing)).unwrap_or_default();
|
||||
for (&(x, y), v) in missing.iter().zip(values) {
|
||||
if let Some(d) = index(x, y).and_then(|i| data.get_mut(i)) {
|
||||
*d = Some(texturecraft_texel::to_u16(v));
|
||||
}
|
||||
}
|
||||
}
|
||||
let hides = self.covered.iter().any(|&(x, y)| index(x, y).and_then(|i| data.get(i).copied().flatten()).is_some_and(|v| v < 65535));
|
||||
if !hides {
|
||||
return Ok(None);
|
||||
}
|
||||
let m = Mask16 { width: w, height: h, data: data.into_iter().map(|v| v.unwrap_or(0)).collect() };
|
||||
psd::encode_mask(&m).map(Some).map_err(|e| EngineError::Other(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl Session {
|
||||
/// Layer `id` of texture set `ts` alone, its content and its mask apart (see
|
||||
/// [`texturecraft_compose::flatten_layer_masked_apart`]).
|
||||
pub(crate) fn flatten_apart(
|
||||
&self,
|
||||
ts: usize,
|
||||
id: LayerId,
|
||||
) -> Option<(Vec<texturecraft_compose::eval::FlatTile>, Vec<texturecraft_compose::eval::MaskTile>)> {
|
||||
let tiles: Vec<(u32, u32)> = self.caches.get(ts)?.covered_tiles.iter().copied().collect();
|
||||
self.with_inputs(ts, |inp| texturecraft_compose::flatten_layer_masked_apart(inp, id, &tiles))
|
||||
}
|
||||
|
||||
/// Texture set `ts` as one layered PSD of `channels` (see the module docs), plus what it
|
||||
/// couldn't keep.
|
||||
pub fn photocraft_psd(&mut self, ts: usize, channels: &[Channel]) -> Result<(Vec<u8>, Vec<String>)> {
|
||||
self.update();
|
||||
let set = self.doc.project.texture_set(ts).map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
let channels: Vec<Channel> = channels.iter().copied().filter(|c| set.channel_list().contains(c)).collect();
|
||||
let Some(&first) = channels.first() else {
|
||||
return Err(EngineError::Other(format!("texture set \"{}\" has none of the channels asked for", set.name)));
|
||||
};
|
||||
let stack = set.stack.clone();
|
||||
let cache = self.caches.get(ts).ok_or_else(|| EngineError::Other(format!("no texture set {ts}")))?;
|
||||
let size = cache.size;
|
||||
let covered: Vec<(u32, u32)> = cache
|
||||
.gbuf
|
||||
.coverage_mask()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, c)| **c)
|
||||
.map(|(i, _)| ((i % size.0 as usize) as u32, (i / size.0 as usize) as u32))
|
||||
.collect();
|
||||
let mut tree = Tree { s: self, ts, size, channels: channels.clone(), covered, warnings: Vec::new() };
|
||||
let layers = tree.nodes(&stack, 1)?;
|
||||
let mut warnings = tree.warnings;
|
||||
if channels.iter().any(|c| c.kind() == ChannelKind::NormalMap) {
|
||||
warnings.push("normal channels: PhotoCraft blends normal layers as colours, not as normals".into());
|
||||
}
|
||||
let doc = psd::Document { layers, merged: merged_image(self, ts, first)?, comps: channels.iter().map(|c| c.label().to_string()).collect() };
|
||||
let bytes = psd::write_psd(&doc).map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
Ok((bytes, warnings))
|
||||
}
|
||||
}
|
||||
|
||||
/// A channel value from a saved pixel (straight, 0..65535).
|
||||
fn decode(c: Channel, px: &[u16]) -> [f32; 3] {
|
||||
let v = |k: usize| px.get(k).map_or(0.0, |v| from_u16(*v));
|
||||
match c.kind() {
|
||||
ChannelKind::Color => [0, 1, 2].map(|k| srgb_to_linear(v(k))),
|
||||
ChannelKind::NormalMap => [v(0), v(1), v(2)],
|
||||
ChannelKind::Scalar => [v(0); 3],
|
||||
ChannelKind::Signed => [v(0) * 2.0 - 1.0; 3],
|
||||
}
|
||||
}
|
||||
|
||||
/// Channel `c` of a saved document, `w`×`h`: the channel's default value with every layer named
|
||||
/// after `c` blended over it (see the module docs). Encoded as the hand-off writes channels;
|
||||
/// opaque where `coverage` is set.
|
||||
pub(crate) fn composite(doc: &ReadDocument, c: Channel, coverage: &[bool], notes: &mut Vec<String>) -> Rgba16 {
|
||||
let (w, h) = (doc.width as usize, doc.height as usize);
|
||||
let mut acc = vec![c.default_value(); w * h];
|
||||
walk(&doc.layers, c, &mut acc, (w, h), None, 0, notes);
|
||||
let mut img = Rgba16::transparent(doc.width, doc.height);
|
||||
for (i, v) in acc.iter().enumerate() {
|
||||
if coverage.get(i).copied().unwrap_or(false)
|
||||
&& let Some(dst) = img.data.get_mut(i * 4..i * 4 + 4)
|
||||
{
|
||||
dst.copy_from_slice(&encode_value(c, *v));
|
||||
}
|
||||
}
|
||||
img
|
||||
}
|
||||
|
||||
fn is_channel_name(name: &str) -> bool {
|
||||
Channel::ALL.iter().any(|c| c.label() == name.trim())
|
||||
}
|
||||
|
||||
/// Blend `nodes` (top-first) into `acc`, each weighted by `weight` (the masks, opacities and
|
||||
/// visibility of the groups above; None: 1).
|
||||
fn walk(nodes: &[ReadLayer], c: Channel, acc: &mut [[f32; 3]], (w, h): (usize, usize), weight: Option<&[f32]>, depth: usize, notes: &mut Vec<String>) {
|
||||
if depth > psd::MAX_DEPTH {
|
||||
return;
|
||||
}
|
||||
for node in nodes.iter().rev() {
|
||||
match &node.kind {
|
||||
ReadKind::Group(children) => {
|
||||
if !node.visible || node.opacity <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
let mut wgt: Vec<f32> = weight.map_or_else(|| vec![1.0; w * h], <[f32]>::to_vec);
|
||||
for (i, v) in wgt.iter_mut().enumerate() {
|
||||
let m = node.mask.as_ref().map_or(1.0, |m| from_u16(m.at((i % w) as i32, (i / w) as i32)));
|
||||
*v *= m * node.opacity;
|
||||
}
|
||||
walk(children, c, acc, (w, h), Some(&wgt), depth + 1, notes);
|
||||
}
|
||||
ReadKind::Pixels { x, y, image } => {
|
||||
// Channel layers are shown one comp at a time, so their own visibility is ignored.
|
||||
if node.name.trim() != c.label() {
|
||||
if node.visible && !is_channel_name(&node.name) {
|
||||
let note = format!("\"{}\" is not named after a channel (such as \"{}\"), so it isn't brought back", node.name, c.label());
|
||||
if !notes.contains(¬e) {
|
||||
notes.push(note);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let mode = mode_of(&node.blend);
|
||||
let (iw, ih) = (image.width as usize, image.height as usize);
|
||||
for ly in 0..ih {
|
||||
let cy = i64::from(*y) + ly as i64;
|
||||
if cy < 0 || cy >= h as i64 {
|
||||
continue;
|
||||
}
|
||||
for lx in 0..iw {
|
||||
let cx = i64::from(*x) + lx as i64;
|
||||
if cx < 0 || cx >= w as i64 {
|
||||
continue;
|
||||
}
|
||||
let i = cy as usize * w + cx as usize;
|
||||
let Some(px) = image.data.get((ly * iw + lx) * 4..(ly * iw + lx) * 4 + 4) else { continue };
|
||||
let mask = node.mask.as_ref().map_or(1.0, |m| from_u16(m.at(cx as i32, cy as i32)));
|
||||
let a = from_u16(px[3]) * node.opacity * mask * weight.and_then(|wg| wg.get(i)).copied().unwrap_or(1.0);
|
||||
if a <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
let Some(dst) = acc.get_mut(i) else { continue };
|
||||
let v = decode(c, px);
|
||||
let blended = if c.components() == 3 { blend3(mode, *dst, v) } else { [blend1(mode, dst[0], v[0]); 3] };
|
||||
*dst = mix3(*dst, blended, a.min(1.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::execute;
|
||||
use serde_json::Value;
|
||||
use texturecraft_geom::primitives;
|
||||
|
||||
fn tree(s: &Session) -> Tree<'_> {
|
||||
let size = s.caches[0].size;
|
||||
let covered = s.caches[0]
|
||||
.gbuf
|
||||
.coverage_mask()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, c)| **c)
|
||||
.map(|(i, _)| ((i % size.0 as usize) as u32, (i / size.0 as usize) as u32))
|
||||
.collect();
|
||||
Tree { s, ts: 0, size, channels: vec![Channel::BaseColor], covered, warnings: Vec::new() }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn texels_flattening_leaves_out_are_masked_as_the_layer_is() {
|
||||
let mut s = Session::new_project("t", primitives::plane(2.0, 2), "builtin:plane", None, 128).unwrap();
|
||||
s.update();
|
||||
let paint = s.selected_layer().unwrap();
|
||||
// No mask tiles at all (an empty paint layer flattens to nothing): no mask either.
|
||||
assert!(tree(&s).mask(paint, &[]).unwrap().is_none());
|
||||
// A black mask hides every texel, tiles or not.
|
||||
execute(&mut s, "mask.add.black", &Value::Null).unwrap();
|
||||
s.update();
|
||||
assert!(tree(&s).mask(paint, &[]).unwrap().is_some());
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,8 @@
|
||||
//! MaterialCraft graph inputs.
|
||||
//! - [`ora`]: layered OpenRaster (`.ora`) documents for an image editor, and their merged image
|
||||
//! read back.
|
||||
//! - [`psd`]: layered 16-bit PSD documents (groups, layer masks, layer comps) for an image
|
||||
//! editor, and their layers read back.
|
||||
//! - [`presets`]: the built-in export presets and JSON (de)serialization of user presets.
|
||||
//! - [`project_file`]: the `.tcraft` project zip (project JSON, sparse lossless pixel tiles,
|
||||
//! baked maps, the original mesh and a thumbnail).
|
||||
@@ -40,6 +42,7 @@ pub mod mesh_import;
|
||||
pub mod ora;
|
||||
pub mod presets;
|
||||
pub mod project_file;
|
||||
pub mod psd;
|
||||
|
||||
pub use export::{
|
||||
Dest, ExportInput, ExportOptions, ExportPreset, ExportedFile, FileFormat, MapSpec, Padding, PlannedFile, SourceRef, VirtualMap, list_outputs, render_maps,
|
||||
|
||||
@@ -191,7 +191,8 @@ fn escape(s: &str) -> String {
|
||||
out
|
||||
}
|
||||
|
||||
fn png16(img: &Rgba16) -> Result<Vec<u8>> {
|
||||
/// `img` as a 16-bit RGBA PNG.
|
||||
pub fn png16(img: &Rgba16) -> Result<Vec<u8>> {
|
||||
use image::ImageEncoder;
|
||||
let bytes: Vec<u8> = img.data.iter().flat_map(|v| v.to_ne_bytes()).collect();
|
||||
let mut out = Cursor::new(Vec::new());
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
+39
-15
@@ -338,25 +338,49 @@ OpenGL convention, `roughness`, `metallic`, `height`, `ambientOcclusion`, `emiss
|
||||
|
||||
## PhotoCraft hand-off
|
||||
|
||||
`file.sendToPhotoCraft` (File › Send layers to PhotoCraft...) writes the texture set's stack as layered
|
||||
OpenRaster documents (`formats::ora`), one per texture set and channel, named
|
||||
`<mesh>_<set>_<channel>.ora`, and opens them in PhotoCraft (`$PHOTOCRAFT`, else `photocraft` next to the
|
||||
TextureCraft executable, else on `PATH`). Each layer is rendered alone into a layer of the same name:
|
||||
its fill sources, generators, filters and content effects, times its mask, so the mask becomes the
|
||||
layer's transparency. Folders become groups (pass-through folders `isolation="auto"`), with the
|
||||
channel's visibility, opacity and blend mode (`svg:*` names; modes OpenRaster lacks are sent as
|
||||
Normal with a warning). Colour channels are sRGB, data channels as stored (height `0.5 + 0.5·h`,
|
||||
normals OpenGL), 16-bit with straight alpha; `mergedimage.png` is the set's composite.
|
||||
`file.sendToPhotoCraft` (File › Send layers to PhotoCraft...) writes texture sets as layered documents
|
||||
and opens them in PhotoCraft (`$PHOTOCRAFT`, else `photocraft` next to the TextureCraft executable,
|
||||
else on `PATH`). The menu item sends every texture set.
|
||||
|
||||
Each sent file is a link. When PhotoCraft saves it, its `mergedimage.png` becomes the image source of a
|
||||
`PhotoCraft: <channel>` fill at the top of the set (one undo step per save; the layers below are
|
||||
untouched). The desktop app looks at the files' modification times about twice a second and never
|
||||
during a stroke. Links last for the session and are not saved with the project.
|
||||
**PSD (the default).** One 16-bit document per texture set, `<mesh>_<set>.psd` (`formats::psd`,
|
||||
written from the public file format specification), holding every channel so that a mask edited
|
||||
once shapes all of them:
|
||||
|
||||
- Every folder is a group, with the folder's mask and, when all its channels blend alike, their
|
||||
blend mode and opacity (otherwise a pass-through group at 100%, with a warning).
|
||||
- Every layer is a pass-through group of the same name. The layer's mask (painted, generators,
|
||||
filters, geometry and height masks included) is the group's own editable layer mask; it is left
|
||||
out when it hides nothing.
|
||||
- Inside each layer group, one pixel layer per enabled channel, named after it (`Base color`,
|
||||
`Roughness`, `Metallic`, `Height`, ...), holds the layer's content for that channel (fill sources,
|
||||
generators, filters and content effects, not masked), with the channel's opacity and blend mode
|
||||
(modes the format lacks are sent as Normal with a warning).
|
||||
- One layer comp per channel shows only that channel's layers; the document opens on the first.
|
||||
The merged image is the first channel's composite.
|
||||
|
||||
**OpenRaster** (`format: "ora"`). One document per texture set and channel,
|
||||
`<mesh>_<set>_<channel>.ora` (`formats::ora`). Each layer is rendered alone, times its mask, so the
|
||||
mask becomes the layer's transparency; folders become groups (pass-through folders
|
||||
`isolation="auto"`) with the channel's visibility, opacity and blend mode (`svg:*` names).
|
||||
|
||||
In both, colour channels are sRGB and data channels as stored (height `0.5 + 0.5·h`, normals
|
||||
OpenGL), 16-bit with straight alpha.
|
||||
|
||||
Each sent file is a link. When PhotoCraft saves it, TextureCraft reads it back and every channel it
|
||||
holds becomes the image source of a `PhotoCraft: <channel>` fill at the top of the set, all in one
|
||||
undo step (the layers below are untouched). An OpenRaster file brings back its `mergedimage.png`. A
|
||||
PSD is recomposited per channel with TextureCraft's blend math: starting from the channel's default
|
||||
value, every pixel layer named after the channel is blended in through the masks, opacity and
|
||||
visibility of the groups above it; the pixel layers' own visibility is ignored, since layer comps
|
||||
toggle it. Layers not named after a channel are skipped with a note in the log. A PSD whose size no
|
||||
longer matches the texture set is refused. The desktop app looks at the files' modification times
|
||||
about twice a second and never during a stroke. Links last for the session and are not saved with
|
||||
the project.
|
||||
|
||||
| Command | Params |
|
||||
|---|---|
|
||||
| `file.sendToPhotoCraft` | `{dir, ts?: index\|"all" (default: the active set), channels?: [ids], open?: true}` → `{files, warnings}` |
|
||||
| `photocraft.links` | `{}` → `[{path, ts, channel, layer?}]` |
|
||||
| `file.sendToPhotoCraft` | `{dir, ts?: index\|"all" (default: the active set), channels?: [ids], format?: "psd"\|"ora" (default psd), open?: true}` → `{files, warnings}` |
|
||||
| `photocraft.links` | `{}` → `[{path, ts, format, channels, layers: {channel: layer id}}]` |
|
||||
| `photocraft.sync` | `{}` → `{updated}`: read the linked files saved since the last look now |
|
||||
| `photocraft.unlink` | `{path?}`: stop watching one file, or all |
|
||||
|
||||
|
||||
Reference in new issue
Block a user