Skip to content

ProCraft for Android: An Android native painting app written in Rust

Rust

feat(3d): normal and AO maps, glTF export, frosted panels

Josh4a6cb698226f
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/ColorPanel.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/ColorPanel.kt
index 2ab4bd1..9ce4e02 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/ColorPanel.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/ColorPanel.kt
@@ -163,7 +163,8 @@ private class BrushColor(private val vm: CanvasViewModel) : ColorPick {
/** A colour of its own, starting at [rgb]; [onChange] hears every change. Greys keep their hue. */
class HeldColor(rgb: Int, private val onChange: (Int) -> Unit) : ColorPick {
- override var hue by mutableFloatStateOf(0f)
+ // a grey has no hue of its own: it opens at 180 degrees, as measured
+ override var hue by mutableFloatStateOf(0.5f)
private set
override var saturation by mutableFloatStateOf(0f)
private set
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/Popover.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/Popover.kt
index bdce76f..f3e5bf0 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/Popover.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/Popover.kt
@@ -100,16 +100,26 @@ fun Popover(
val paint = fill ?: if (backdrops != null) Pc.PopoverFrost else Pc.PopoverFill
val key = remember { Any() }
val density = LocalDensity.current
- if (backdrops != null) DisposableEffect(backdrops) { onDispose { backdrops.remove(key) } }
+ // the blur grows and fades with the panel: its body as drawn (already grown), and how much
+ // it shows
+ val look = LocalPanelLook.current
+ var body by remember { mutableStateOf<FloatArray?>(null) }
+ if (backdrops != null) {
+ DisposableEffect(backdrops) { onDispose { backdrops.remove(key) } }
+ // read here, so its being placed (or moved) comes back through here
+ val placed = body
+ SideEffect { placed?.let { backdrops.put(key, it + look.alpha) } }
+ }
val frost = if (backdrops == null) {
Modifier
} else {
Modifier.onGloballyPositioned { c ->
val at = c.positionInWindow()
+ val k = look.scale
val (arrowH, radius) = with(density) { Pc.ArrowHeight.toPx() to corner.toPx() }
val (l, t, r) = Triple(if (side == ArrowSide.LEFT) arrowH else 0f, if (side == ArrowSide.TOP) arrowH else 0f, if (side == ArrowSide.RIGHT) arrowH else 0f)
val b = if (side == ArrowSide.BOTTOM) arrowH else 0f
- backdrops.put(key, floatArrayOf(at.x + l, at.y + t, at.x + c.size.width - r, at.y + c.size.height - b, radius))
+ body = floatArrayOf(at.x + l * k, at.y + t * k, at.x + (c.size.width - r) * k, at.y + (c.size.height - b) * k, radius * k)
}
}
val sideways = side == ArrowSide.LEFT || side == ArrowSide.RIGHT
diff --git a/apps/procraft-android/src/scene3d.rs b/apps/procraft-android/src/scene3d.rs
index 50ac235..b8d7fbd 100644
--- a/apps/procraft-android/src/scene3d.rs
+++ b/apps/procraft-android/src/scene3d.rs
@@ -15,7 +15,7 @@ use std::sync::Arc;
use procraft_engine::document::{Document, LayerId, LayerKind};
use procraft_engine::geom::{IRect, Vec2};
use procraft_engine::input::Sample;
-use procraft_engine::mesh::{MAX_TRIANGLES, Mesh, MeshError, read_obj};
+use procraft_engine::mesh::{MAX_TRIANGLES, Mesh, MeshError, PartImages, read_obj};
use procraft_engine::stroke::StrokeBuilder;
use procraft_engine::usd::{read_usd, read_usdc, read_usdz};
use procraft_engine::view::View;
@@ -161,6 +161,10 @@ pub(crate) struct Scene {
channel: usize,
/// Each part's channel maps as last given to the view (as [`Scene::filtered`]).
channel_shown: Vec<[Option<(u64, bool)>; 2]>,
+ /// Each part's normal and occlusion maps from the model's file (RGBA8, the texture size
+ /// square), shown as they are; and whether the view has them as they are now.
+ surface_maps: Vec<[Option<Arc<Vec<u8>>>; 2]>,
+ surface_shown: bool,
/// A stroke is being carried onto the model each frame; `ending`: once more, then it ends.
pub(crate) stroking: bool,
ending: bool,
@@ -209,6 +213,8 @@ impl Scene {
channels: Vec::new(),
channel: 0,
channel_shown: Vec::new(),
+ surface_maps: Vec::new(),
+ surface_shown: false,
stroking: false,
ending: false,
}
@@ -407,7 +413,8 @@ impl CanvasSession {
}
/// The images the model's file brings for its texture sets (a USDZ's packed images): to be
- /// decoded and given back with [`CanvasSession::set_model_texture`].
+ /// decoded and given back with [`CanvasSession::set_model_texture`]. Its normal and occlusion
+ /// maps come as channels 3 and 4.
pub fn model_texture_files(&self) -> Vec<ModelTextureFile> {
let st = self.shared.lock();
let Some(scene) = st.scene.as_ref() else { return Vec::new() };
@@ -422,6 +429,8 @@ impl CanvasSession {
(0, m.color_map.as_deref(), None),
(1, m.roughness_map.as_deref(), m.roughness_component),
(2, m.metallic_map.as_deref(), m.metallic_component),
+ (3, m.normal_map.as_deref(), None),
+ (4, m.occlusion_map.as_deref(), m.occlusion_component),
];
for (channel, map, component) in maps {
let Some(bytes) = map.and_then(|f| packed_file(&zip, f)) else { continue };
@@ -437,17 +446,27 @@ impl CanvasSession {
}
/// Puts `rgba` (premultiplied, the texture size square) into texture set `texture_set`'s
- /// Base Layer in `channel` (0 colour, 1 roughness, 2 metalness), with no undo step; false if
- /// it doesn't fit.
+ /// Base Layer in `channel` (0 colour, 1 roughness, 2 metalness), with no undo step; or, as
+ /// channel 3 or 4, makes it the set's normal or occlusion map. False if it doesn't fit.
pub fn set_model_texture(&self, texture_set: u32, channel: u32, rgba: Vec<u8>) -> bool {
let mut guard = self.shared.lock();
let st = &mut *guard;
let (set, channel) = (texture_set as usize, channel as usize);
let Some(scene) = st.scene.as_mut() else { return false };
let side = scene.texture_size;
- if channel > 2 || set >= scene.mesh.parts.len() || rgba.len() != (side as usize) * (side as usize) * 4 {
+ if channel > 4 || set >= scene.mesh.parts.len() || rgba.len() != (side as usize) * (side as usize) * 4 {
return false;
}
+ // the maps shown as they are
+ if channel >= 3 {
+ scene.surface_maps.resize(scene.mesh.parts.len(), [None, None]);
+ if let Some(slot) = scene.surface_maps.get_mut(set).and_then(|m| m.get_mut(channel - 3)) {
+ *slot = Some(Arc::new(rgba));
+ }
+ scene.surface_shown = false;
+ self.shared.frame(st);
+ return true;
+ }
// the colour's layers say which is the Base Layer
let colour_is_session = set == scene.current && scene.channel == 0;
let structure = if colour_is_session {
@@ -659,52 +678,80 @@ impl CanvasSession {
pub fn export_model_obj(&self, path: String) -> Result<(), MobileError> {
let files = self.model_files()?;
let base = &files.base;
- let materials: Vec<(String, [String; 3])> =
- files.names.iter().map(|n| (n.clone(), MODEL_CHANNEL_FILES.map(|c| format!("{n}-{c}.png")))).collect();
+ let materials: Vec<(&str, &PartImages)> = files.names.iter().map(String::as_str).zip(&files.images).collect();
let mut zip = procraft_engine::zip::ZipWriter::new();
zip.add(
&format!("{base}.obj"),
procraft_engine::mesh::write_obj(&files.mesh, &format!("{base}.mtl"), &files.names).as_bytes(),
);
zip.add(&format!("{base}.mtl"), procraft_engine::mesh::write_mtl(&materials).as_bytes());
- for ((_, images), pngs) in materials.iter().zip(&files.pngs) {
- for (file, png) in images.iter().zip(pngs) {
- zip.add(file, png);
- }
+ for (file, png) in files.images.iter().flat_map(PartImages::files) {
+ zip.add(file, png);
}
let bytes = zip.finish();
write_atomically(Path::new(&path), |p| fs::write(p, &bytes).map_err(|e| file_error("exporting the model", e)))
}
+ /// Writes the model as a glTF binary at `path`, for a viewer that shows it in the room: as
+ /// for [`CanvasSession::export_model_usdz`], each set's roughness and metalness in one image
+ /// (green and blue).
+ pub fn export_model_glb(&self, path: String) -> Result<(), MobileError> {
+ let ModelPixels { mesh, names, pixels, maps, side, .. } = self.model_pixels()?;
+ let png = |px: &[u8]| crate::project::rgba_png(side, side, px);
+ let encoded: Vec<[Vec<u8>; 4]> = pixels
+ .iter()
+ .enumerate()
+ .map(|(part, [colour, roughness, metallic])| {
+ let mut both = roughness.clone();
+ for (p, m) in both.as_chunks_mut::<4>().0.iter_mut().zip(metallic.as_chunks::<4>().0) {
+ *p = [255, p[1], m[2], 255];
+ }
+ let [normal, occlusion] = maps.get(part).cloned().unwrap_or_default();
+ let map = |m: Option<Arc<Vec<u8>>>| m.map_or_else(Vec::new, |px| png(&px));
+ [png(colour), png(&both), map(normal), map(occlusion)]
+ })
+ .collect();
+ let parts: Vec<procraft_engine::gltf::GlbPart<'_>> = names
+ .iter()
+ .zip(&encoded)
+ .map(|(name, [colour, both, normal, occlusion])| procraft_engine::gltf::GlbPart {
+ name,
+ color: colour,
+ metallic_roughness: both,
+ normal: (!normal.is_empty()).then_some(normal.as_slice()),
+ occlusion: (!occlusion.is_empty()).then_some(occlusion.as_slice()),
+ })
+ .collect();
+ let bytes = procraft_engine::gltf::write_glb(&mesh, &parts);
+ write_atomically(Path::new(&path), |p| fs::write(p, &bytes).map_err(|e| file_error("exporting the model", e)))
+ }
+
/// Writes the model as a USDZ package at `path`: the mesh with a preview-surface material
/// for each texture set, reading its images (as for [`CanvasSession::export_model_obj`]).
pub fn export_model_usdz(&self, path: String) -> Result<(), MobileError> {
let files = self.model_files()?;
- let textures: Vec<[(String, Vec<u8>); 3]> = files
- .names
- .iter()
- .zip(files.pngs)
- .map(|(n, [color, roughness, metallic])| {
- let [c, r, m] = MODEL_CHANNEL_FILES.map(|c| format!("{n}-{c}.png"));
- [(c, color), (r, roughness), (m, metallic)]
- })
- .collect();
- let bytes = procraft_engine::usd::write_usdz(&files.mesh, &textures);
+ let bytes = procraft_engine::usd::write_usdz(&files.mesh, &files.images);
write_atomically(Path::new(&path), |p| fs::write(p, &bytes).map_err(|e| file_error("exporting the model", e)))
}
}
#[uniffi::export]
impl CanvasSession {
- /// Each texture set's colour, roughness and metalness as PNGs in folder `dir` (made if
- /// missing; as for [`CanvasSession::export_model_obj`]); returns the files' paths.
+ /// Each texture set's colour, roughness and metalness (and its normal and occlusion maps, if
+ /// it has them) as PNGs in folder `dir` (made if missing; as for
+ /// [`CanvasSession::export_model_obj`]); returns the files' paths.
pub fn export_model_textures(&self, dir: String) -> Result<Vec<String>, MobileError> {
let files = self.model_files()?;
let dir = Path::new(&dir);
fs::create_dir_all(dir).map_err(|e| file_error("making the folder", e))?;
let mut out = Vec::new();
- for (name, pngs) in files.names.iter().zip(&files.pngs) {
- for (channel, png) in SHARED_CHANNEL_NAMES.iter().zip(pngs) {
+ for (name, images) in files.names.iter().zip(&files.images) {
+ let [colour, roughness, metallic] = images.maps.each_ref().map(|(_, png)| png);
+ let named = [("Color", colour), ("Roughness", roughness), ("Metallic", metallic)]
+ .into_iter()
+ .chain(images.normal.iter().map(|(_, png)| ("Normal", png)))
+ .chain(images.occlusion.iter().map(|(_, png)| ("AmbientOcclusion", png)));
+ for (channel, png) in named {
let path = dir.join(format!("{name}-{channel}.png"));
write_atomically(&path, |p| fs::write(p, png).map_err(|e| file_error("exporting the textures", e)))?;
out.push(path.to_string_lossy().into_owned());
@@ -888,30 +935,35 @@ fn turntable_frames(turntable: Turntable, fps: f32) -> u32 {
}
/// What a model's exports are made of: the model, its texture sets' names (safe as file names,
-/// different from each other), each set's colour, roughness and metalness as PNGs (over the
-/// part's own, as the model shows them), and the artwork's name.
+/// different from each other), each set's images as PNGs (its colour, roughness and metalness
+/// over the part's own, as the model shows them, and its normal and occlusion maps) named as
+/// models name them, and the artwork's name.
struct ModelFiles {
mesh: Mesh,
names: Vec<String>,
- pngs: Vec<[Vec<u8>; 3]>,
+ images: Vec<PartImages>,
base: String,
}
-/// The channels as a model's exports name their images (`<set>-<channel>.png`), and as the
-/// shared textures name them.
+/// The images as a model's exports name them (`<set>-<channel>.png`).
const MODEL_CHANNEL_FILES: [&str; 3] = ["color", "roughness", "metallic"];
-const SHARED_CHANNEL_NAMES: [&str; 3] = ["Color", "Roughness", "Metallic"];
+const MODEL_NORMAL_FILE: &str = "normal";
+const MODEL_OCCLUSION_FILE: &str = "ambient_occlusion";
impl CanvasSession {
- fn model_files(&self) -> Result<ModelFiles, MobileError> {
- let (mesh, set_names, pictures, side, base) = {
+ /// The model's texture sets' pixels as exports take them: each set's colour, roughness and
+ /// metalness over the part's own (RGBA8, the texture size square), with its maps and the
+ /// sets' names safe as file names.
+ fn model_pixels(&self) -> Result<ModelPixels, MobileError> {
+ let (mesh, set_names, pictures, maps, side, base) = {
let mut guard = self.shared.lock();
let st = &mut *guard;
st.finish_stroke();
st.settle();
let pictures = st.channel_pictures()?;
let Some(scene) = st.scene.as_ref() else { return Err(file_error("exporting the model", "no model")) };
- (scene.mesh.clone(), scene.set_names(), pictures, scene.texture_size, file_stem(&st.name, "Model"))
+ let maps = scene.surface_maps.clone();
+ (scene.mesh.clone(), scene.set_names(), pictures, maps, scene.texture_size, file_stem(&st.name, "Model"))
};
let mut names: Vec<String> = Vec::new();
for name in set_names {
@@ -921,7 +973,7 @@ impl CanvasSession {
}
names.push(n);
}
- let pngs = pictures
+ let pixels = pictures
.into_iter()
.enumerate()
.map(|(part, [color, roughness, metallic])| {
@@ -933,13 +985,44 @@ impl CanvasSession {
]
})
.collect();
- Ok(ModelFiles { mesh, names, pngs, base })
+ Ok(ModelPixels { mesh, names, pixels, maps, side, base })
+ }
+
+ fn model_files(&self) -> Result<ModelFiles, MobileError> {
+ let ModelPixels { mesh, names, pixels, maps, side, base } = self.model_pixels()?;
+ let png = |px: &[u8]| crate::project::rgba_png(side, side, px);
+ let images = pixels
+ .iter()
+ .zip(&names)
+ .enumerate()
+ .map(|(part, ([cp, rp, mp], n))| {
+ let [c, r, m] = MODEL_CHANNEL_FILES.map(|k| format!("{n}-{k}.png"));
+ let [normal, occlusion] = maps.get(part).cloned().unwrap_or_default();
+ let map = |rgba: Option<Arc<Vec<u8>>>, k: &str| rgba.map(|px| (format!("{n}-{k}.png"), png(&px)));
+ PartImages {
+ maps: [(c, png(cp)), (r, png(rp)), (m, png(mp))],
+ normal: map(normal, MODEL_NORMAL_FILE),
+ occlusion: map(occlusion, MODEL_OCCLUSION_FILE),
+ }
+ })
+ .collect();
+ Ok(ModelFiles { mesh, names, images, base })
}
}
+/// What [`CanvasSession::model_pixels`] gives.
+struct ModelPixels {
+ mesh: Mesh,
+ names: Vec<String>,
+ pixels: Vec<[Vec<u8>; 3]>,
+ maps: Vec<[Option<Arc<Vec<u8>>>; 2]>,
+ side: u32,
+ base: String,
+}
+
/// A channel's picture (premultiplied RGBA8, `side` square; none if never painted) over what the
-/// part shows under it (`under`, 0 to 1), opaque, as a PNG: as it is, or as the grey the model
-/// reads from it.
+/// part shows under it (`under`, 0 to 1), opaque: as it is, or as the grey the model reads from
+/// it.
fn flattened(side: u32, picture: Option<Vec<u8>>, under: [f32; 3], grey: bool) -> Vec<u8> {
let under = under.map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u16);
let mut px = picture.unwrap_or_else(|| vec![0; (side as usize) * (side as usize) * 4]);
@@ -957,7 +1040,7 @@ fn flattened(side: u32, picture: Option<Vec<u8>>, under: [f32; 3], grey: bool) -
[r, g, b, 255]
};
}
- crate::project::rgba_png(side, side, &px)
+ px
}
/// A file packed in a USDZ by the path its layer gives (relative to the package; `./` and folders
@@ -1095,6 +1178,8 @@ pub(crate) struct ModelSnapshot {
/// The waiting texture sets' layers, by part, and the parts' channels' (part, channel 1 or 2).
layers: Vec<(usize, Pixels)>,
channel_layers: Vec<(usize, usize, Pixels)>,
+ /// The parts' normal and occlusion maps (part, its file's name, RGBA8).
+ maps: Vec<(usize, &'static str, Arc<Vec<u8>>)>,
/// The model as last shown (premultiplied RGBA8, its size), for the gallery.
pub(crate) picture: Option<(u32, u32, Vec<u8>)>,
}
@@ -1182,6 +1267,14 @@ impl State {
source: scene.source.clone(),
layers,
channel_layers,
+ maps: scene
+ .surface_maps
+ .iter()
+ .enumerate()
+ .flat_map(|(part, maps)| {
+ maps.iter().zip(MAP_FILES).filter_map(move |(m, name)| m.as_ref().map(|m| (part, name, m.clone())))
+ })
+ .collect(),
picture,
}))
}
@@ -1201,6 +1294,14 @@ pub(crate) fn write_model(saved: &mut SavedSets, dir: &Path, model: &ModelSnapsh
write_atomically(&mtl, |p| fs::write(p, text).map_err(|e| file_error("saving the model", e)))?;
}
let side = model.saved.texture_size;
+ // the maps, written once: they don't change
+ for (part, name, rgba) in &model.maps {
+ let path = dir.join("maps").join(format!("{part}-{name}.png"));
+ if !path.exists() {
+ fs::create_dir_all(dir.join("maps")).map_err(|e| file_error("making the folder", e))?;
+ write_atomically(&path, |p| write_png(p, side, side, rgba, &[]))?;
+ }
+ }
let colour = model.layers.iter().map(|(part, pixels)| (*part, 0, pixels));
let channels = model.channel_layers.iter().map(|(part, k, pixels)| (*part, *k, pixels));
for (part, channel, pixels) in colour.chain(channels) {
@@ -1239,6 +1340,10 @@ pub(crate) fn write_model(saved: &mut SavedSets, dir: &Path, model: &ModelSnapsh
Ok(())
}
+/// The names of a part's normal and occlusion maps' files in the artwork's `maps` folder
+/// (`<part>-<name>.png`).
+const MAP_FILES: [&str; 2] = ["normal", "occlusion"];
+
/// The layers of `doc` saved in `folder` (each `<id>.png`, `side` square); a layer with no file is
/// empty.
fn read_set_layers(folder: &Path, doc: &Document, side: u32) -> Pixels {
@@ -1308,6 +1413,14 @@ pub(crate) fn open_saved_model(dir: &Path, saved: &SavedModel) -> Result<Scene,
}
scene.hidden = (0..scene.mesh.submeshes.len()).map(|i| saved.hidden.contains(&i)).collect();
scene.channels = channels;
+ scene.surface_maps = (0..scene.mesh.parts.len())
+ .map(|part| {
+ MAP_FILES.map(|name| {
+ let path = dir.join("maps").join(format!("{part}-{name}.png"));
+ path.exists().then(|| read_png(&path, side, side).ok()).flatten().map(Arc::new)
+ })
+ })
+ .collect();
scene.orbit = saved.orbit.map(|o| Orbit {
target: o.target,
rotation: o.rotation.unwrap_or_else(|| Orbit::rotation_of(o.yaw, o.pitch)),
@@ -1594,6 +1707,7 @@ impl State {
// the surround behind the model until an environment comes
view.show_environment(gpu, false);
scene.view = Some(view);
+ scene.surface_shown = false;
}
// the waiting texture sets' canvases (and channels), with the pixels they opened with
for set in scene.sets.iter_mut().flatten().chain(scene.channels.iter_mut().flatten().flatten()) {
@@ -1674,19 +1788,41 @@ impl State {
}
}
}
+ // the model's own normal and occlusion maps, given again when they change
+ if !scene.surface_shown {
+ let side = scene.texture_size;
+ for (part, maps) in scene.surface_maps.iter().enumerate() {
+ for (map, kind) in
+ maps.iter().zip([procraft_gpu::SurfaceMap::Normal, procraft_gpu::SurfaceMap::Occlusion])
+ {
+ view.set_surface_map(gpu, part, kind, map.as_deref().map(|m| (side, m.as_slice())));
+ }
+ }
+ scene.surface_shown = true;
+ }
view.lights = scene.lights.iter().map(|l| l.to_gpu()).collect();
for i in 0..scene.mesh.submeshes.len() {
view.set_hidden(i, scene.hidden.get(i).copied().unwrap_or(false));
}
let s = crate::SURROUND;
let clear = [f64::from(s[0]), f64::from(s[1]), f64::from(s[2]), 1.0];
- if scene.studio_open {
- let orbit = *scene.studio.get_or_insert_with(|| view.studio_orbit());
- view.render_studio(gpu, &mut encoder, target, size.0, size.1, clear, orbit);
- } else {
- view.render(gpu, &mut encoder, target, size.0, size.1, clear);
+ let studio = scene.studio_open.then(|| *scene.studio.get_or_insert_with(|| view.studio_orbit()));
+ match studio {
+ Some(orbit) => view.render_studio(gpu, &mut encoder, target, size.0, size.1, clear, orbit),
+ None => view.render(gpu, &mut encoder, target, size.0, size.1, clear),
}
gpu.queue.submit([encoder.finish()]);
+ // frosted panels over the model: it drawn again, small, and blurred under each
+ if let Some(canvas) = self.canvas.as_mut().filter(|_| !self.backdrops.is_empty()) {
+ let backdrop = procraft_gpu::Backdrop {
+ regions: &self.backdrops,
+ sigma: crate::BACKDROP_SIGMA_PT * self.display_scale,
+ };
+ canvas.frost_over(gpu, target, format, size, s, backdrop, &mut |encoder, small, (w, h)| match studio {
+ Some(orbit) => view.render_studio(gpu, encoder, small, w, h, clear, orbit),
+ None => view.render(gpu, encoder, small, w, h, clear),
+ });
+ }
}
}
@@ -1817,7 +1953,8 @@ pub struct ModelInfo {
}
/// An image a model's file brings for a texture set's channel (0 colour, 1 roughness, 2
-/// metalness): its file's bytes, and which of its components to take (-1: as it is, or its grey).
+/// metalness, 3 its normal map, 4 its occlusion): its file's bytes, and which of its components
+/// to take (-1: as it is, or its grey).
#[derive(Clone, Debug, PartialEq, uniffi::Record)]
pub struct ModelTextureFile {
pub texture_set: u32,
@@ -1886,6 +2023,47 @@ mod tests {
assert_eq!(st.gpu.as_ref().unwrap().errors(), 0, "GPU validation errors");
}
+ /// A frame of `s`'s model, `side` square, read back (RGBA8).
+ fn frame_pixels(s: &CanvasSession, side: u32) -> Vec<u8> {
+ let mut guard = s.shared.lock();
+ let st = &mut *guard;
+ let format = wgpu::TextureFormat::Rgba8Unorm;
+ let target = st.gpu.as_ref().unwrap().device.create_texture(&wgpu::TextureDescriptor {
+ label: Some("test frame"),
+ size: wgpu::Extent3d { width: side, height: side, depth_or_array_layers: 1 },
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format,
+ usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
+ view_formats: &[],
+ });
+ let view = target.create_view(&wgpu::TextureViewDescriptor::default());
+ st.render_scene(&view, format, (side, side), None);
+ let gpu = st.gpu.as_ref().unwrap();
+ assert_eq!(gpu.errors(), 0, "GPU validation errors");
+ procraft_gpu::read_texture(gpu, &target, side, side).unwrap()
+ }
+
+ #[test]
+ fn panels_over_a_model_blur_it_under_them() {
+ let s = CanvasSession::open_model(CUBE.into(), "cube.obj".into(), None, "Cube".into(), 64).unwrap();
+ if !shown(&s) {
+ return;
+ }
+ frame(&s, 200);
+ let plain = frame_pixels(&s, 200);
+ // a panel over the left half
+ s.shared.lock().backdrops = vec![[0.0, 0.0, 100.0, 200.0, 0.0, 1.0]];
+ let frosted = frame_pixels(&s, 200);
+ let at = |p: &[u8], x: usize, y: usize| p[(y * 200 + x) * 4..][..4].to_vec();
+ // the model's edge softened under it, and nothing changed beside it
+ let under = (0..200).flat_map(|y| (0..100).map(move |x| (x, y)));
+ let changed = under.filter(|&(x, y)| at(&plain, x, y) != at(&frosted, x, y)).count();
+ assert!(changed > 200, "{changed}");
+ assert!((0..200).all(|y| (100..200).all(|x| at(&plain, x, y) == at(&frosted, x, y))));
+ }
+
/// How many of the current texture set's pixels have paint.
fn painted(s: &CanvasSession) -> usize {
let mut guard = s.shared.lock();
@@ -2304,6 +2482,85 @@ f 1/1 2/2",
frame(s, 200);
}
+ #[test]
+ fn a_models_normal_and_occlusion_maps_are_kept_and_exported() {
+ let s = CanvasSession::open_model(CUBE.into(), "cube.obj".into(), None, "Cube".into(), 64).unwrap();
+ // channels 3 and 4: the normal and occlusion maps, the texture size square
+ assert!(!s.set_model_texture(0, 3, vec![0; 8]));
+ assert!(!s.set_model_texture(0, 5, [128, 128, 255, 255].repeat(64 * 64)));
+ assert!(s.set_model_texture(0, 3, [128, 128, 255, 255].repeat(64 * 64)));
+ assert!(s.set_model_texture(0, 4, [200, 200, 200, 255].repeat(64 * 64)));
+ if !shown(&s) {
+ return;
+ }
+ frame(&s, 200);
+ let set = super::file_stem(&s.model_info().unwrap().texture_sets[0], "Material");
+ let dir = std::env::temp_dir().join(format!("procraft-maps-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ std::fs::create_dir_all(&dir).unwrap();
+ // in the OBJ's zip, named in its material file
+ let obj = dir.join("cube.zip");
+ s.export_model_obj(obj.to_string_lossy().into_owned()).unwrap();
+ let bytes = std::fs::read(&obj).unwrap();
+ let zip = procraft_engine::zip::ZipReader::new(&bytes).unwrap();
+ assert!(zip.read(&format!("{set}-normal.png")).is_some());
+ assert!(zip.read(&format!("{set}-ambient_occlusion.png")).is_some());
+ let mtl = String::from_utf8(zip.read("Cube.mtl").unwrap()).unwrap();
+ assert!(mtl.contains(&format!("map_tangentSpaceNormal {set}-normal.png")), "{mtl}");
+ assert!(mtl.contains(&format!("map_ao {set}-ambient_occlusion.png")), "{mtl}");
+ // shared as textures
+ let pngs = s.export_model_textures(dir.join("t").to_string_lossy().into_owned()).unwrap();
+ assert_eq!(pngs.len(), 5);
+ assert!(pngs.iter().any(|p| p.ends_with(&format!("{set}-AmbientOcclusion.png"))));
+ // kept with the artwork, and back when it opens
+ s.save(dir.join("a").to_string_lossy().into_owned()).unwrap();
+ assert!(dir.join("a").join("maps").join("0-normal.png").exists());
+ let again = CanvasSession::open(dir.join("a").to_string_lossy().into_owned()).unwrap();
+ let kept = again.shared.lock().scene.as_ref().unwrap().surface_maps.clone();
+ assert_eq!(kept[0][0].as_deref().map(|m| m[..4].to_vec()), Some(vec![128, 128, 255, 255]));
+ assert_eq!(kept[0][1].as_deref().map(|m| m[..4].to_vec()), Some(vec![200, 200, 200, 255]));
+ // a USDZ carries them, and brings them in again
+ let usdz = dir.join("cube.usdz");
+ s.export_model_usdz(usdz.to_string_lossy().into_owned()).unwrap();
+ let back =
+ CanvasSession::open_model(std::fs::read(&usdz).unwrap(), "cube.usdz".into(), None, "Back".into(), 64)
+ .unwrap();
+ let channels: Vec<u32> = back.model_texture_files().iter().map(|f| f.channel).collect();
+ assert_eq!(channels, [0, 1, 2, 3, 4]);
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
+ #[test]
+ fn a_model_exports_as_a_glb_for_viewers() {
+ let two = CUBE.replacen("f 1/1", "usemtl front\nf 1/1", 1).replacen("f 1/1 2/2", "usemtl sides\nf 1/1 2/2", 1);
+ let s = CanvasSession::open_model(two.into_bytes(), "two.obj".into(), None, "Two".into(), 64).unwrap();
+ if !shown(&s) {
+ return;
+ }
+ frame(&s, 200);
+ let path = std::env::temp_dir().join(format!("procraft-glb-{}.glb", std::process::id()));
+ s.export_model_glb(path.to_string_lossy().into_owned()).unwrap();
+ let glb = std::fs::read(&path).unwrap();
+ let _ = std::fs::remove_file(&path);
+ assert_eq!(&glb[0..4], b"glTF");
+ let json_len = u32::from_le_bytes(glb[12..16].try_into().unwrap()) as usize;
+ let doc: serde_json::Value = serde_json::from_slice(&glb[20..20 + json_len]).unwrap();
+ let bin = &glb[20 + json_len + 8..];
+ assert_eq!(doc["materials"].as_array().unwrap().len(), 2);
+ assert_eq!(doc["materials"][0]["name"], "front");
+ // the roughness (1, as nothing says otherwise) in green and the metalness (0) in blue
+ let texture =
+ doc["materials"][0]["pbrMetallicRoughness"]["metallicRoughnessTexture"]["index"].as_u64().unwrap();
+ let source = doc["textures"][texture as usize]["source"].as_u64().unwrap() as usize;
+ let view = &doc["bufferViews"][doc["images"][source]["bufferView"].as_u64().unwrap() as usize];
+ let offset = view["byteOffset"].as_u64().unwrap() as usize;
+ let png = &bin[offset..offset + view["byteLength"].as_u64().unwrap() as usize];
+ let mut reader = png::Decoder::new(std::io::Cursor::new(png)).read_info().unwrap();
+ let mut px = vec![0; reader.output_buffer_size().unwrap()];
+ reader.next_frame(&mut px).unwrap();
+ assert!(px.chunks(4).all(|p| p[1] == 255 && p[2] == 0), "{:?}", &px[..4]);
+ }
+
#[test]
fn a_saved_model_exports_once_opened_again() {
let s = CanvasSession::open_model(CUBE.into(), "cube.obj".into(), None, "Cube".into(), 64).unwrap();
diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml
index a7a951a..e889044 100644
--- a/crates/engine/Cargo.toml
+++ b/crates/engine/Cargo.toml
@@ -11,7 +11,5 @@ workspace = true
[dependencies]
serde = { workspace = true }
-miniz_oxide = { workspace = true }
-
-[dev-dependencies]
serde_json = { workspace = true }
+miniz_oxide = { workspace = true }
diff --git a/crates/engine/src/gltf.rs b/crates/engine/src/gltf.rs
new file mode 100644
index 0000000..97228f3
--- /dev/null
+++ b/crates/engine/src/gltf.rs
@@ -0,0 +1,219 @@
+//! Models as glTF 2.0 binary files (`.glb`), for viewers that show a model in the room: one node
+//! holding the model's mesh, a primitive per part, each with a metallic-roughness material
+//! reading the part's images (PNGs packed in the file).
+
+use serde_json::{Value, json};
+
+use crate::mesh::Mesh;
+
+/// A part's images for [`write_glb`], each a PNG: its colour, its roughness and metalness (in
+/// green and blue, as glTF reads them), and its normal and occlusion (red) maps if it has them.
+pub struct GlbPart<'a> {
+ pub name: &'a str,
+ pub color: &'a [u8],
+ pub metallic_roughness: &'a [u8],
+ pub normal: Option<&'a [u8]>,
+ pub occlusion: Option<&'a [u8]>,
+}
+
+const ARRAY_BUFFER: u32 = 34962;
+const ELEMENT_ARRAY_BUFFER: u32 = 34963;
+const FLOAT: u32 = 5126;
+const UNSIGNED_INT: u32 = 5125;
+
+/// The file's binary chunk as it is built: its bytes and the views into them.
+#[derive(Default)]
+struct Bin {
+ bytes: Vec<u8>,
+ views: Vec<Value>,
+}
+
+impl Bin {
+ /// `data` as a new view (on a 4-byte boundary), with what it holds for the GPU if anything;
+ /// returns the view's index.
+ fn view(&mut self, data: &[u8], target: Option<u32>) -> usize {
+ while !self.bytes.len().is_multiple_of(4) {
+ self.bytes.push(0);
+ }
+ let mut view = json!({ "buffer": 0, "byteOffset": self.bytes.len(), "byteLength": data.len() });
+ if let (Some(t), Some(v)) = (target, view.as_object_mut()) {
+ v.insert("target".into(), json!(t));
+ }
+ self.bytes.extend_from_slice(data);
+ self.views.push(view);
+ self.views.len() - 1
+ }
+}
+
+/// The model as a GLB: its mesh (texture coordinates turned to glTF's, which run down the image)
+/// and per part a primitive with its material and images (`parts`, by part).
+pub fn write_glb(mesh: &Mesh, parts: &[GlbPart<'_>]) -> Vec<u8> {
+ let mut bin = Bin::default();
+ let mut accessors: Vec<Value> = Vec::new();
+ let floats = |values: &mut dyn Iterator<Item = f32>| values.flat_map(f32::to_le_bytes).collect::<Vec<u8>>();
+ let count = mesh.vertices.len();
+ // the corners' places (with their bounds, as glTF asks), normals and texture coordinates
+ let (lo, hi) = mesh.bounds().unwrap_or(([0.0; 3], [0.0; 3]));
+ let positions = bin.view(&floats(&mut mesh.vertices.iter().flat_map(|v| v.position)), Some(ARRAY_BUFFER));
+ accessors.push(json!({
+ "bufferView": positions, "componentType": FLOAT, "count": count, "type": "VEC3", "min": lo, "max": hi
+ }));
+ let normals = bin.view(&floats(&mut mesh.vertices.iter().flat_map(|v| v.normal)), Some(ARRAY_BUFFER));
+ accessors.push(json!({ "bufferView": normals, "componentType": FLOAT, "count": count, "type": "VEC3" }));
+ let uvs = bin.view(&floats(&mut mesh.vertices.iter().flat_map(|v| [v.uv[0], 1.0 - v.uv[1]])), Some(ARRAY_BUFFER));
+ accessors.push(json!({ "bufferView": uvs, "componentType": FLOAT, "count": count, "type": "VEC2" }));
+
+ let mut images: Vec<Value> = Vec::new();
+ let mut textures: Vec<Value> = Vec::new();
+ let mut texture = |bin: &mut Bin, png: &[u8]| {
+ let view = bin.view(png, None);
+ images.push(json!({ "bufferView": view, "mimeType": "image/png" }));
+ textures.push(json!({ "sampler": 0, "source": images.len() - 1 }));
+ json!({ "index": textures.len() - 1 })
+ };
+ let mut materials: Vec<Value> = Vec::new();
+ let mut primitives: Vec<Value> = Vec::new();
+ for (part, images_of) in mesh.parts.iter().zip(parts) {
+ let triangles = mesh.triangles.get(part.first..part.first + part.count).unwrap_or(&[]);
+ if triangles.is_empty() {
+ continue;
+ }
+ let indices: Vec<u8> = triangles.iter().flatten().flat_map(|i| i.to_le_bytes()).collect();
+ let view = bin.view(&indices, Some(ELEMENT_ARRAY_BUFFER));
+ accessors.push(json!({
+ "bufferView": view, "componentType": UNSIGNED_INT, "count": triangles.len() * 3, "type": "SCALAR"
+ }));
+ let mut material = json!({
+ "name": images_of.name,
+ "pbrMetallicRoughness": {
+ "baseColorTexture": texture(&mut bin, images_of.color),
+ "metallicRoughnessTexture": texture(&mut bin, images_of.metallic_roughness),
+ "metallicFactor": 1.0,
+ "roughnessFactor": 1.0,
+ },
+ "doubleSided": true,
+ });
+ if let Some(m) = material.as_object_mut() {
+ if let Some(png) = images_of.normal {
+ m.insert("normalTexture".into(), texture(&mut bin, png));
+ }
+ if let Some(png) = images_of.occlusion {
+ m.insert("occlusionTexture".into(), texture(&mut bin, png));
+ }
+ }
+ materials.push(material);
+ primitives.push(json!({
+ "attributes": { "POSITION": 0, "NORMAL": 1, "TEXCOORD_0": 2 },
+ "indices": accessors.len() - 1,
+ "material": materials.len() - 1,
+ }));
+ }
+ while !bin.bytes.len().is_multiple_of(4) {
+ bin.bytes.push(0);
+ }
+ let document = json!({
+ "asset": { "version": "2.0", "generator": "ProCraft" },
+ "scene": 0,
+ "scenes": [{ "nodes": [0] }],
+ "nodes": [{ "mesh": 0, "name": "Model" }],
+ "meshes": [{ "primitives": primitives }],
+ "materials": materials,
+ "textures": textures,
+ "images": images,
+ "samplers": [{ "magFilter": 9729, "minFilter": 9987, "wrapS": 10497, "wrapT": 10497 }],
+ "accessors": accessors,
+ "bufferViews": bin.views,
+ "buffers": [{ "byteLength": bin.bytes.len() }],
+ });
+ let mut text = document.to_string().into_bytes();
+ while !text.len().is_multiple_of(4) {
+ text.push(b' ');
+ }
+ // the header, the JSON chunk, the binary chunk
+ let total = 12 + 8 + text.len() + 8 + bin.bytes.len();
+ let word = |n: usize| u32::try_from(n).unwrap_or(u32::MAX).to_le_bytes();
+ let mut out = Vec::with_capacity(total);
+ out.extend_from_slice(b"glTF");
+ out.extend_from_slice(&2u32.to_le_bytes());
+ out.extend_from_slice(&word(total));
+ out.extend_from_slice(&word(text.len()));
+ out.extend_from_slice(b"JSON");
+ out.extend_from_slice(&text);
+ out.extend_from_slice(&word(bin.bytes.len()));
+ out.extend_from_slice(b"BIN\0");
+ out.extend_from_slice(&bin.bytes);
+ out
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::mesh::read_obj;
+
+ const TWO: &str = "v 0 0 0\nv 1 0 0\nv 1 1 0\nv 0 1 0\nvt 0 0\nvt 1 0\nvt 1 1\nvt 0 1\n\
+ usemtl front\nf 1/1 2/2 3/3\nusemtl back\nf 1/1 3/3 4/4\n";
+
+ /// The file's JSON and binary chunks.
+ fn chunks(glb: &[u8]) -> (Value, Vec<u8>) {
+ let word = |at: usize| u32::from_le_bytes(glb[at..at + 4].try_into().unwrap()) as usize;
+ assert_eq!(&glb[0..4], b"glTF");
+ assert_eq!(word(4), 2);
+ assert_eq!(word(8), glb.len());
+ let json_len = word(12);
+ assert_eq!(&glb[16..20], b"JSON");
+ assert_eq!(json_len % 4, 0);
+ let doc: Value = serde_json::from_slice(&glb[20..20 + json_len]).unwrap();
+ let at = 20 + json_len;
+ let bin_len = word(at);
+ assert_eq!(&glb[at + 4..at + 8], b"BIN\0");
+ assert_eq!(at + 8 + bin_len, glb.len());
+ (doc, glb[at + 8..].to_vec())
+ }
+
+ #[test]
+ fn a_model_writes_as_a_glb_with_a_primitive_and_material_per_part() {
+ let mesh = read_obj(TWO, None).unwrap();
+ let (color, mr, normal) = (vec![1u8; 5], vec![2u8; 6], vec![3u8; 7]);
+ let parts = [
+ GlbPart { name: "front", color: &color, metallic_roughness: &mr, normal: Some(&normal), occlusion: None },
+ GlbPart { name: "back", color: &color, metallic_roughness: &mr, normal: None, occlusion: None },
+ ];
+ let (doc, bin) = chunks(&write_glb(&mesh, &parts));
+ assert_eq!(doc["asset"]["version"], "2.0");
+ assert_eq!(doc["buffers"][0]["byteLength"].as_u64().unwrap() as usize, bin.len());
+ // every view inside the binary chunk, on a 4-byte boundary
+ for v in doc["bufferViews"].as_array().unwrap() {
+ let (o, l) = (v["byteOffset"].as_u64().unwrap() as usize, v["byteLength"].as_u64().unwrap() as usize);
+ assert_eq!(o % 4, 0);
+ assert!(o + l <= bin.len());
+ }
+ let prims = doc["meshes"][0]["primitives"].as_array().unwrap();
+ assert_eq!(prims.len(), 2);
+ let accessor = |i: &Value| &doc["accessors"][i.as_u64().unwrap() as usize];
+ assert_eq!(accessor(&prims[0]["indices"])["count"], 3);
+ let position = accessor(&prims[0]["attributes"]["POSITION"]);
+ assert_eq!(position["count"].as_u64().unwrap() as usize, mesh.vertices.len());
+ assert_eq!(position["max"], json!([1.0, 1.0, 0.0]));
+ // the materials and their images: the front has a normal map, the back none
+ let front = &doc["materials"][0];
+ assert_eq!(front["name"], "front");
+ assert!(front["normalTexture"]["index"].is_u64());
+ assert!(doc["materials"][1].get("normalTexture").is_none());
+ assert_eq!(doc["images"].as_array().unwrap().len(), 5);
+ let image = |t: &Value| {
+ let source = doc["textures"][t["index"].as_u64().unwrap() as usize]["source"].as_u64().unwrap() as usize;
+ let view = &doc["bufferViews"][doc["images"][source]["bufferView"].as_u64().unwrap() as usize];
+ let o = view["byteOffset"].as_u64().unwrap() as usize;
+ bin[o..o + view["byteLength"].as_u64().unwrap() as usize].to_vec()
+ };
+ assert_eq!(image(&front["normalTexture"]), normal);
+ assert_eq!(image(&front["pbrMetallicRoughness"]["metallicRoughnessTexture"]), mr);
+ // texture coordinates running down the image: the first corner's v 0 is 1
+ let uv = accessor(&prims[0]["attributes"]["TEXCOORD_0"]);
+ let view = &doc["bufferViews"][uv["bufferView"].as_u64().unwrap() as usize];
+ let o = view["byteOffset"].as_u64().unwrap() as usize;
+ let v0 = f32::from_le_bytes(bin[o + 4..o + 8].try_into().unwrap());
+ let first = mesh.vertices[0].uv[1];
+ assert!((v0 - (1.0 - first)).abs() < 1e-6);
+ }
+}
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index 6d7089a..468942e 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -15,6 +15,7 @@ pub mod document;
pub mod environment;
pub mod formats;
pub mod geom;
+pub mod gltf;
pub mod guide;
pub mod icc;
pub mod input;
diff --git a/crates/engine/src/mesh.rs b/crates/engine/src/mesh.rs
index f5334a0..a1ab9dc 100644
--- a/crates/engine/src/mesh.rs
+++ b/crates/engine/src/mesh.rs
@@ -55,6 +55,24 @@ pub struct Material {
/// none, its grey).
pub roughness_component: Option<usize>,
pub metallic_component: Option<usize>,
+ /// Which of its image's components the occlusion is (none, its grey).
+ pub occlusion_component: Option<usize>,
+}
+
+/// A part's images in an export, each a file name and its bytes: its colour, roughness and
+/// metalness, and its normal map and ambient occlusion if it has them.
+#[derive(Clone, Debug, Default, PartialEq)]
+pub struct PartImages {
+ pub maps: [(String, Vec<u8>); 3],
+ pub normal: Option<(String, Vec<u8>)>,
+ pub occlusion: Option<(String, Vec<u8>)>,
+}
+
+impl PartImages {
+ /// Every image it has, colour first.
+ pub fn files(&self) -> impl Iterator<Item = &(String, Vec<u8>)> {
+ self.maps.iter().chain(&self.normal).chain(&self.occlusion)
+ }
}
#[derive(Clone, Debug, Default, PartialEq)]
@@ -402,32 +420,26 @@ pub fn write_obj(mesh: &Mesh, mtl: &str, names: &[String]) -> String {
out
}
-/// A material file for [`write_obj`]: each material (its name, and the images beside it for its
-/// colour, roughness and metalness) white and physically shaded, its properties indented.
-pub fn write_mtl(materials: &[(String, [String; 3])]) -> String {
+/// A material file for [`write_obj`]: each material (its name, and the images beside it) white
+/// and physically shaded, its properties indented.
+pub fn write_mtl(materials: &[(&str, &PartImages)]) -> String {
use std::fmt::Write as _;
let mut out = String::new();
- for (name, [color, roughness, metallic]) in materials {
+ for (name, images) in materials {
+ let [(color, _), (roughness, _), (metallic, _)] = &images.maps;
+ let _ = write!(out, "newmtl {name}\n\tmap_Kd {color}\n\tKd 1 1 1\n\tKa 0 0 0\n\tKs 0\n");
+ if let Some((occlusion, _)) = &images.occlusion {
+ let _ = writeln!(out, "\tmap_ao {occlusion}");
+ }
let _ = write!(
out,
- "newmtl {name}
-\tmap_Kd {color}
-\tKd 1 1 1
-\tKa 0 0 0
-\tKs 0
-\tao 0
-\tsubsurface 0
-\tmap_metallic {metallic}
-\tspecularTint 0
-\tmap_roughness {roughness}
-\tanisotropicRotation 0
-\tsheen 0.05
-\tsheenTint 0
-\tclearCoat 0
-\tclearCoatGloss 0
-
-"
+ "\tao 0\n\tsubsurface 0\n\tmap_metallic {metallic}\n\tspecularTint 0\n\tmap_roughness {roughness}\n\
+ \tanisotropicRotation 0\n\tsheen 0.05\n\tsheenTint 0\n\tclearCoat 0\n\tclearCoatGloss 0\n"
);
+ if let Some((normal, _)) = &images.normal {
+ let _ = writeln!(out, "\tmap_tangentSpaceNormal {normal}");
+ }
+ out.push('\n');
}
out
}
@@ -517,8 +529,15 @@ f -8/1 -4/2 -1/3 -5/4
let mesh = read_obj(CUBE, None).unwrap();
let names: Vec<String> = mesh.parts.iter().map(|p| p.material.clone()).collect();
let text = write_obj(&mesh, "cube.mtl", &names);
- let images = ["body-color.png".into(), "body-roughness.png".into(), "body-metallic.png".into()];
- let again = read_obj(&text, Some(&write_mtl(&[("body".into(), images)]))).unwrap();
+ let images = PartImages {
+ maps: ["color", "roughness", "metallic"].map(|c| (format!("body-{c}.png"), Vec::new())),
+ normal: Some(("body-normal.png".into(), Vec::new())),
+ occlusion: Some(("body-ambient_occlusion.png".into(), Vec::new())),
+ };
+ let mtl = write_mtl(&[("body", &images)]);
+ assert!(mtl.contains("\tKs 0\n\tmap_ao body-ambient_occlusion.png\n\tao 0\n"));
+ assert!(mtl.ends_with("\tclearCoatGloss 0\n\tmap_tangentSpaceNormal body-normal.png\n\n"));
+ let again = read_obj(&text, Some(&mtl)).unwrap();
assert_eq!(again.triangles.len(), mesh.triangles.len());
assert_eq!(again.parts, mesh.parts);
assert_eq!(again.vertices.len(), mesh.vertices.len());
@@ -535,6 +554,8 @@ f -8/1 -4/2 -1/3 -5/4
assert_eq!(m.color_map.as_deref(), Some("body-color.png"));
assert_eq!(m.roughness_map.as_deref(), Some("body-roughness.png"));
assert_eq!(m.metallic_map.as_deref(), Some("body-metallic.png"));
+ assert_eq!(m.normal_map.as_deref(), Some("body-normal.png"));
+ assert_eq!(m.occlusion_map.as_deref(), Some("body-ambient_occlusion.png"));
assert_eq!(m.color, Some([1.0; 3]));
}
diff --git a/crates/engine/src/studio.rs b/crates/engine/src/studio.rs
index c17d1ba..534e251 100644
--- a/crates/engine/src/studio.rs
+++ b/crates/engine/src/studio.rs
@@ -204,7 +204,6 @@ impl Brush {
| "brightness_tilt_angle"
| "secondary_tilt_angle"
| "jitter_lateral_tilt_angle"
- | "jitter_linear_tilt_angle"
| "count_tilt_angle"
| "grain_depth_tilt_angle"
| "stamp_hue_tilt_angle"
@@ -213,6 +212,11 @@ impl Brush {
| "stamp_darkness_tilt_angle"
| "stamp_secondary_tilt_angle"
| "attack_tilt_angle" => self.extra_or(key, self.tilt.angle / (PI / 2.0)),
+ // the linear jitter's tilt switch and angle, where a brush has none, are the lateral's
+ "jitter_linear_tilt" | "jitter_linear_tilt_angle" => {
+ let lateral = key.replace("linear", "lateral");
+ self.extra.get(key).copied().unwrap_or_else(|| self.setting(&lateral))
+ }
"tilt_opacity" => self.tilt.opacity,
"tilt_size" => self.tilt.size,
"max_size" => self.size_control(self.max_size),
@@ -342,6 +346,25 @@ impl Brush {
mod tests {
use super::*;
+ #[test]
+ fn the_linear_jitters_tilt_follows_the_laterals_unless_it_has_its_own() {
+ let mut b = Brush::round_pen();
+ b.set_setting("tilt_angle", 0.2);
+ // neither set: off, both angles the tilt angle
+ assert_eq!(b.setting("jitter_linear_tilt"), 0.0);
+ assert!((b.setting("jitter_linear_tilt_angle") - 0.2).abs() < 1e-6);
+ // the lateral's switch and angle carry over
+ b.set_setting("jitter_lateral_tilt", 1.0);
+ b.set_setting("jitter_lateral_tilt_angle", 0.6);
+ assert_eq!(b.setting("jitter_linear_tilt"), 1.0);
+ assert!((b.setting("jitter_linear_tilt_angle") - 0.6).abs() < 1e-6);
+ // until the linear has its own
+ b.set_setting("jitter_linear_tilt", 0.0);
+ b.set_setting("jitter_linear_tilt_angle", 0.3);
+ assert_eq!(b.setting("jitter_linear_tilt"), 0.0);
+ assert!((b.setting("jitter_linear_tilt_angle") - 0.3).abs() < 1e-6);
+ }
+
#[test]
fn controls_round_trip() {
let mut b = Brush::round_pen();
diff --git a/crates/engine/src/usd.rs b/crates/engine/src/usd.rs
index 7b6e5d9..c009966 100644
--- a/crates/engine/src/usd.rs
+++ b/crates/engine/src/usd.rs
@@ -12,7 +12,7 @@
use std::collections::HashMap;
use crate::applz4::raw_block;
-use crate::mesh::{Gathered, MAX_TRIANGLES, Material, Mesh, MeshError, Part};
+use crate::mesh::{Gathered, MAX_TRIANGLES, Material, Mesh, MeshError, Part, PartImages};
use crate::zip::{ZipReader, ZipWriter};
/// Reads a model from a `.usdz` package: its first layer (binary or text).
@@ -39,12 +39,12 @@ pub fn read_usdc(bytes: &[u8]) -> Result<Mesh, MeshError> {
/// Writes the model as a USDZ package: a text layer (`model.usda`) with the mesh, a subset of its
/// faces for each part bound to a preview-surface material, and each part's images beside it
-/// (`textures`, by part: its colour, roughness and metalness, each a file name and its PNG).
-pub fn write_usdz(mesh: &Mesh, textures: &[[(String, Vec<u8>); 3]]) -> Vec<u8> {
- let layer = write_usda(mesh, textures.iter().map(|t| t.each_ref().map(|(name, _)| name.as_str())));
+/// (`textures`, by part).
+pub fn write_usdz(mesh: &Mesh, textures: &[PartImages]) -> Vec<u8> {
+ let layer = write_usda(mesh, textures);
let mut zip = ZipWriter::new();
zip.add_aligned("model.usda", layer.as_bytes(), 64);
- for (name, png) in textures.iter().flatten() {
+ for (name, png) in textures.iter().flat_map(PartImages::files) {
zip.add_aligned(&format!("textures/{name}"), png, 64);
}
zip.finish()
@@ -63,9 +63,8 @@ fn identifier(name: &str, otherwise: &str) -> String {
}
/// The text layer of [`write_usdz`]: the mesh's corners as its points (each with its normal and
-/// texture coordinate), its triangles, and per part a subset and a material reading `textures`
-/// (its colour, roughness and metalness images).
-fn write_usda<'a>(mesh: &Mesh, textures: impl Iterator<Item = [&'a str; 3]>) -> String {
+/// texture coordinate), its triangles, and per part a subset and a material reading its images.
+fn write_usda(mesh: &Mesh, textures: &[PartImages]) -> String {
let mut names: Vec<String> = Vec::new();
for (i, part) in mesh.parts.iter().enumerate() {
let mut n = identifier(&part.material, "Material");
@@ -124,7 +123,8 @@ fn write_usda<'a>(mesh: &Mesh, textures: impl Iterator<Item = [&'a str; 3]>) ->
line(0, String::new());
line(1, "def Scope \"Materials\"".into());
line(1, "{".into());
- for (name, [color, roughness, metallic]) in names.iter().zip(textures) {
+ for (name, images) in names.iter().zip(textures) {
+ let [(color, _), (roughness, _), (metallic, _)] = &images.maps;
let m = format!("/Model/Materials/{name}");
line(2, format!("def Material \"{name}\""));
line(2, "{".into());
@@ -136,14 +136,23 @@ fn write_usda<'a>(mesh: &Mesh, textures: impl Iterator<Item = [&'a str; 3]>) ->
line(4, format!("color3f inputs:diffuseColor.connect = <{m}/Texture.outputs:rgb>"));
line(4, format!("float inputs:roughness.connect = <{m}/Roughness.outputs:r>"));
line(4, format!("float inputs:metallic.connect = <{m}/Metallic.outputs:r>"));
+ if images.normal.is_some() {
+ line(4, format!("normal3f inputs:normal.connect = <{m}/Normal.outputs:rgb>"));
+ }
+ if images.occlusion.is_some() {
+ line(4, format!("float inputs:occlusion.connect = <{m}/Occlusion.outputs:r>"));
+ }
line(4, "token outputs:surface".into());
line(3, "}".into());
- // the colour as it is, the greys as numbers (their red)
- for (shader, file, output) in [
- ("Texture", color, "float3 outputs:rgb"),
- ("Roughness", roughness, "float outputs:r"),
- ("Metallic", metallic, "float outputs:r"),
- ] {
+ // the colour as it is, the greys as numbers (their red), the normals from 0..1 to -1..1
+ let shaders = [
+ Some(("Texture", color, "float3 outputs:rgb")),
+ Some(("Roughness", roughness, "float outputs:r")),
+ Some(("Metallic", metallic, "float outputs:r")),
+ images.normal.as_ref().map(|(file, _)| ("Normal", file, "float3 outputs:rgb")),
+ images.occlusion.as_ref().map(|(file, _)| ("Occlusion", file, "float outputs:r")),
+ ];
+ for (shader, file, output) in shaders.into_iter().flatten() {
line(0, String::new());
line(3, format!("def Shader \"{shader}\""));
line(3, "{".into());
@@ -152,6 +161,10 @@ fn write_usda<'a>(mesh: &Mesh, textures: impl Iterator<Item = [&'a str; 3]>) ->
if shader != "Texture" {
line(4, "token inputs:sourceColorSpace = \"raw\"".into());
}
+ if shader == "Normal" {
+ line(4, "float4 inputs:scale = (2, 2, 2, 1)".into());
+ line(4, "float4 inputs:bias = (-1, -1, -1, 0)".into());
+ }
line(4, format!("float2 inputs:st.connect = <{m}/Coordinates.outputs:result>"));
line(4, output.into());
line(3, "}".into());
@@ -971,6 +984,10 @@ impl<'a> Stage<'a> {
Some((file, component)) => (m.metallic_map, m.metallic_component) = (Some(file), component),
None => m.metallic = self.value(shader, "inputs:metallic").and_then(|v| v.number()).map(|v| v as f32),
}
+ m.normal_map = self.connection(&input("normal")).and_then(|t| self.texture_file(&t)).map(|(file, _)| file);
+ if let Some((file, component)) = self.connection(&input("occlusion")).and_then(|t| self.texture_file(&t)) {
+ (m.occlusion_map, m.occlusion_component) = (Some(file), component);
+ }
m
}
@@ -1706,8 +1723,10 @@ mod tests {
parts: vec![Part { material: "1 coat".into(), first: 0, count: 1 }],
..Mesh::default()
};
- let images =
- [("coat.png".into(), vec![7; 5]), ("rough.png".into(), vec![8; 5]), ("metal.png".into(), vec![9; 5])];
+ let images = PartImages {
+ maps: [("coat.png".into(), vec![7; 5]), ("rough.png".into(), vec![8; 5]), ("metal.png".into(), vec![9; 5])],
+ ..PartImages::default()
+ };
let bytes = write_usdz(&mesh, &[images]);
let zip = ZipReader::new(&bytes).unwrap();
let names = ["model.usda", "textures/coat.png", "textures/rough.png", "textures/metal.png"];
diff --git a/crates/engine/tests/usd.rs b/crates/engine/tests/usd.rs
index dc3be77..7427d56 100644
--- a/crates/engine/tests/usd.rs
+++ b/crates/engine/tests/usd.rs
@@ -80,10 +80,19 @@ fn damaged_files_are_refused_without_panicking() {
#[test]
fn a_written_package_reads_back() {
let mesh = read_usdz(&model("cube.usdz")).unwrap();
- let textures: Vec<[(String, Vec<u8>); 3]> = mesh
+ // the lid has a normal map and ambient occlusion too
+ let textures: Vec<procraft_engine::mesh::PartImages> = mesh
.parts
.iter()
- .map(|p| ["color", "roughness", "metallic"].map(|c| (format!("{}-{c}.png", p.material), vec![0; 4])))
+ .map(|p| {
+ let file = |c: &str| (format!("{}-{c}.png", p.material), vec![0; 4]);
+ let extra = p.material == "Lid";
+ procraft_engine::mesh::PartImages {
+ maps: ["color", "roughness", "metallic"].map(file),
+ normal: extra.then(|| file("normal")),
+ occlusion: extra.then(|| file("ambient_occlusion")),
+ }
+ })
.collect();
let again = read_usdz(&procraft_engine::usd::write_usdz(&mesh, &textures)).unwrap();
assert_eq!(again.triangles.len(), mesh.triangles.len());
@@ -97,6 +106,11 @@ fn a_written_package_reads_back() {
assert_eq!(lid.roughness_map.as_deref(), Some("textures/Lid-roughness.png"));
assert_eq!(lid.metallic_map.as_deref(), Some("textures/Lid-metallic.png"));
assert_eq!((lid.roughness_component, lid.metallic_component), (Some(0), Some(0)));
+ assert_eq!(lid.normal_map.as_deref(), Some("textures/Lid-normal.png"));
+ assert_eq!(lid.occlusion_map.as_deref(), Some("textures/Lid-ambient_occlusion.png"));
+ assert_eq!(lid.occlusion_component, Some(0));
+ let body = &again.materials[0];
+ assert_eq!((body.normal_map.as_deref(), body.occlusion_map.as_deref()), (None, None));
assert_eq!(again.bounds(), mesh.bounds());
}
diff --git a/crates/gpu/src/canvas.rs b/crates/gpu/src/canvas.rs
index 2e46173..1e1dbb4 100644
--- a/crates/gpu/src/canvas.rs
+++ b/crates/gpu/src/canvas.rs
@@ -289,6 +289,10 @@ pub struct Backdrop<'a> {
pub sigma: f32,
}
+/// Draws a scene for [`Canvas::frost_over`] into the view given (cleared first), at the size
+/// given.
+pub type SmallScene<'s> = &'s mut dyn FnMut(&mut wgpu::CommandEncoder, &wgpu::TextureView, (u32, u32));
+
/// The backdrop's blur is this many texels wide (sigma) where it is worked out: the scene is
/// drawn that much smaller.
const BACKDROP_TEXELS: f32 = 6.0;
@@ -2619,7 +2623,7 @@ impl Canvas {
if let Some(target) = target {
self.display(device, &mut encoder, &mut uniforms, &target, predicted);
if let Some(backdrop) = target.backdrop.filter(|b| !b.regions.is_empty() && b.sigma > 0.0) {
- self.frost(device, &mut encoder, &mut uniforms, &target, backdrop);
+ self.frost(device, &mut encoder, &mut uniforms, &target, backdrop, None);
}
}
if !uniforms.data.is_empty() {
@@ -3768,8 +3772,46 @@ impl Canvas {
pass.draw(0..6, 0..predicted.end - predicted.start);
}
- /// Frosted panels: the canvas and its surround drawn again, small, blurred, and drawn into
- /// each region of the target.
+ /// Frosted panels over a picture something else drew on `target` (a model): `scene` draws it
+ /// again into the view it is given (of the target's format, cleared, the size given), small,
+ /// and that is blurred and drawn into each region of the target.
+ #[allow(clippy::too_many_arguments)]
+ pub fn frost_over(
+ &mut self,
+ gpu: &Gpu,
+ target: &wgpu::TextureView,
+ format: wgpu::TextureFormat,
+ size: (u32, u32),
+ surround: [f32; 3],
+ backdrop: Backdrop<'_>,
+ scene: SmallScene<'_>,
+ ) {
+ if backdrop.regions.is_empty() || backdrop.sigma <= 0.0 {
+ return;
+ }
+ let mut encoder = gpu.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("frost") });
+ let mut uniforms = UniformData::default();
+ let target = Target {
+ view: target,
+ format,
+ size,
+ to_screen: Affine::IDENTITY,
+ surround,
+ grid: None,
+ selection: None,
+ backdrop: None,
+ convert: None,
+ lut: None,
+ };
+ self.frost(&gpu.device, &mut encoder, &mut uniforms, &target, backdrop, Some(scene));
+ if !uniforms.data.is_empty() {
+ gpu.queue.write_buffer(&self.uniform_buffer, 0, &uniforms.data);
+ }
+ gpu.queue.submit([encoder.finish()]);
+ }
+
+ /// Frosted panels: the canvas and its surround (or what `other` draws) drawn again, small,
+ /// blurred, and drawn into each region of the target.
fn frost(
&mut self,
device: &wgpu::Device,
@@ -3777,6 +3819,7 @@ impl Canvas {
uniforms: &mut UniformData,
target: &Target<'_>,
backdrop: Backdrop<'_>,
+ other: Option<SmallScene<'_>>,
) {
let k = (backdrop.sigma / BACKDROP_TEXELS).floor().clamp(1.0, 16.0);
let size = ((target.size.0 as f32 / k).ceil() as u32, (target.size.1 as f32 / k).ceil() as u32);
@@ -3855,7 +3898,9 @@ impl Canvas {
let surround = target.surround.map(|c| if srgb { srgb_to_linear(c) } else { c });
let clear =
wgpu::Color { r: f64::from(surround[0]), g: f64::from(surround[1]), b: f64::from(surround[2]), a: 1.0 };
- {
+ if let Some(draw) = other {
+ draw(encoder, &small.views[0], size);
+ } else {
let mut pass = render_pass(encoder, "backdrop scene", &small.views[0], wgpu::LoadOp::Clear(clear));
pass.set_pipeline(display);
pass.set_bind_group(0, &self.display_bind, &[]);
diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs
index 1445f01..1e2136b 100644
--- a/crates/gpu/src/lib.rs
+++ b/crates/gpu/src/lib.rs
@@ -12,11 +12,11 @@ use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, OnceLock};
pub use canvas::{
- Backdrop, Canvas, DisplayLut, Filter, LiquifyMode, LiquifyStamp, Resample, SelectionLook, Stripes, StrokeStyle,
- StrokeTool, SurroundGrid, Target, Thumbnail, read_texture,
+ Backdrop, Canvas, DisplayLut, Filter, LiquifyMode, LiquifyStamp, Resample, SelectionLook, SmallScene, Stripes,
+ StrokeStyle, StrokeTool, SurroundGrid, Target, Thumbnail, read_texture,
};
pub use lighting::Lighting;
-pub use scene::{Light, MOST_LIGHTS, MeshView, Orbit};
+pub use scene::{Light, MOST_LIGHTS, MeshView, Orbit, SurfaceMap};
pub use wgpu;
#[derive(Debug, thiserror::Error)]
diff --git a/crates/gpu/src/scene.rs b/crates/gpu/src/scene.rs
index 41e2a5f..20ebcb1 100644
--- a/crates/gpu/src/scene.rs
+++ b/crates/gpu/src/scene.rs
@@ -102,10 +102,19 @@ struct ProjectUniform {
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct PartUniform {
base: [f32; 4],
- /// Roughness and metalness.
+ /// Roughness and metalness, and whether it has a normal map and an occlusion map (1 or 0).
material: [f32; 4],
}
+/// The maps a model's file brings for a part and that are shown as they are (not painted).
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum SurfaceMap {
+ /// Tangent-space normals (RGB, 0.5 the middle; green up the texture's v).
+ Normal,
+ /// Ambient occlusion: how much of the environment's light reaches each point (grey).
+ Occlusion,
+}
+
/// Where the camera is: turned by `rotation` about `target`, `distance` from it, seeing `fov`
/// (radians) across the screen's shorter side.
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -192,8 +201,11 @@ struct PartGpu {
texture: wgpu::Texture,
uniform: wgpu::Buffer,
bind: wgpu::BindGroup,
- /// What it shows: its albedo, and its roughness and metalness painted over the material's.
- views: [wgpu::TextureView; 3],
+ /// What it shows: its albedo, its roughness and metalness painted over the material's, and
+ /// its normal and occlusion maps.
+ views: [wgpu::TextureView; 5],
+ /// The normal and occlusion maps, once given.
+ maps: [Option<wgpu::Texture>; 2],
/// The albedo with its smaller levels, once given, so a far or slanting surface shows it
/// filtered rather than speckled.
mips: Option<(wgpu::Texture, Vec<wgpu::TextureView>, wgpu::TextureView)>,
@@ -207,7 +219,9 @@ pub struct MeshView {
pipeline: wgpu::RenderPipeline,
view_buffer: wgpu::Buffer,
view_bind: wgpu::BindGroup,
- depth: Option<(wgpu::Texture, wgpu::TextureView)>,
+ /// A depth buffer for each size the model was last drawn at (the window, and small under
+ /// frosted panels).
+ depths: Vec<(wgpu::Texture, wgpu::TextureView)>,
texture_size: u32,
part_layout: wgpu::BindGroupLayout,
sampler: wgpu::Sampler,
@@ -518,6 +532,8 @@ impl MeshView {
},
map_entry(3),
map_entry(4),
+ map_entry(5),
+ map_entry(6),
],
});
// nothing painted in a channel: the material's own value shows
@@ -595,7 +611,7 @@ impl MeshView {
}),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
- let views = [view, blank.clone(), blank.clone()];
+ let views = [view, blank.clone(), blank.clone(), blank.clone(), blank.clone()];
let bind = part_bind(device, &part_layout, &sampler, &views, &uniform);
PartGpu {
first: (p.first * 3) as u32,
@@ -604,6 +620,7 @@ impl MeshView {
uniform,
bind,
views,
+ maps: [None, None],
mips: None,
}
})
@@ -657,7 +674,7 @@ impl MeshView {
pipeline,
view_buffer,
view_bind,
- depth: None,
+ depths: Vec::new(),
texture_size: side,
part_layout,
sampler,
@@ -800,11 +817,50 @@ impl MeshView {
/// it covers (any size); none, the material's own everywhere.
pub fn set_channel_view(&mut self, gpu: &Gpu, part: usize, channel: usize, map: Option<&wgpu::TextureView>) {
let Some(p) = self.parts.get_mut(part) else { return };
- let Some(slot) = p.views.get_mut(channel).filter(|_| channel > 0) else { return };
+ let Some(slot) = p.views.get_mut(channel).filter(|_| channel == 1 || channel == 2) else { return };
*slot = map.unwrap_or(&self.blank).clone();
p.bind = part_bind(&gpu.device, &self.part_layout, &self.sampler, &p.views, &p.uniform);
}
+ /// Shades part `part` by its `kind` of map from now on: `rgba` (`side` square, RGBA8); none,
+ /// without one.
+ pub fn set_surface_map(&mut self, gpu: &Gpu, part: usize, kind: SurfaceMap, map: Option<(u32, &[u8])>) {
+ let Some(p) = self.parts.get_mut(part) else { return };
+ let k = match kind {
+ SurfaceMap::Normal => 0,
+ SurfaceMap::Occlusion => 1,
+ };
+ let texture =
+ map.filter(|(side, rgba)| *side > 0 && rgba.len() == (*side as usize).pow(2) * 4).map(|(side, rgba)| {
+ gpu.device.create_texture_with_data(
+ &gpu.queue,
+ &wgpu::TextureDescriptor {
+ label: Some("mesh surface map"),
+ size: wgpu::Extent3d { width: side, height: side, depth_or_array_layers: 1 },
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format: wgpu::TextureFormat::Rgba8Unorm,
+ usage: wgpu::TextureUsages::TEXTURE_BINDING,
+ view_formats: &[],
+ },
+ wgpu::util::TextureDataOrder::LayerMajor,
+ rgba,
+ )
+ });
+ let view = texture
+ .as_ref()
+ .map_or_else(|| self.blank.clone(), |t| t.create_view(&wgpu::TextureViewDescriptor::default()));
+ let shown = if texture.is_some() { 1.0f32 } else { 0.0 };
+ if let (Some(v), Some(slot)) = (p.views.get_mut(3 + k), p.maps.get_mut(k)) {
+ *v = view;
+ *slot = texture;
+ }
+ // its flag: the material's third or fourth number
+ gpu.queue.write_buffer(&p.uniform, 24 + 4 * k as u64, bytemuck::bytes_of(&shown));
+ p.bind = part_bind(&gpu.device, &self.part_layout, &self.sampler, &p.views, &p.uniform);
+ }
+
/// Shows `albedo` (premultiplied RGBA8, the texture size square) on part `part` through
/// smaller copies of itself made now, so a far or slanting surface shows it filtered. Called
/// again whenever the albedo changes.
@@ -883,7 +939,7 @@ impl MeshView {
/// Part `part`'s roughness and metalness (0..1).
pub fn set_material(&mut self, gpu: &Gpu, part: usize, roughness: f32, metallic: f32) {
if let Some(p) = self.parts.get(part) {
- let m = [roughness.clamp(0.0, 1.0), metallic.clamp(0.0, 1.0), 0.0, 0.0];
+ let m = [roughness.clamp(0.0, 1.0), metallic.clamp(0.0, 1.0)];
gpu.queue.write_buffer(&p.uniform, 16, bytemuck::bytes_of(&m));
}
}
@@ -1133,8 +1189,10 @@ impl MeshView {
let light_worlds: Vec<[f32; 3]> = self.lights.iter().take(MOST_LIGHTS).map(|l| self.light_world(l)).collect();
let shown_by_part: Vec<Vec<std::ops::Range<u32>>> =
(0..self.parts.len()).map(|i| self.shown_ranges(i)).collect();
- let stale = self.depth.as_ref().is_none_or(|(t, _)| t.width() != width || t.height() != height);
- if stale {
+ let found = self.depths.iter().position(|(t, _)| t.width() == width && t.height() == height);
+ let at = if let Some(i) = found {
+ i
+ } else {
let texture = gpu.device.create_texture(&wgpu::TextureDescriptor {
label: Some("mesh depth"),
size: wgpu::Extent3d { width, height, depth_or_array_layers: 1 },
@@ -1146,9 +1204,13 @@ impl MeshView {
view_formats: &[],
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
- self.depth = Some((texture, view));
- }
- let Some((_, depth_view)) = &self.depth else { return };
+ if self.depths.len() >= 2 {
+ self.depths.remove(0);
+ }
+ self.depths.push((texture, view));
+ self.depths.len() - 1
+ };
+ let Some((_, depth_view)) = self.depths.get(at) else { return };
let aspect = width as f32 / height as f32;
let view = look_at(orbit.eye(), orbit.target, orbit.up());
// the lights stand about the model in the world, seen from the camera
@@ -1270,10 +1332,10 @@ fn part_bind(
device: &wgpu::Device,
layout: &wgpu::BindGroupLayout,
sampler: &wgpu::Sampler,
- views: &[wgpu::TextureView; 3],
+ views: &[wgpu::TextureView; 5],
uniform: &wgpu::Buffer,
) -> wgpu::BindGroup {
- let [albedo, roughness, metallic] = views;
+ let [albedo, roughness, metallic, normal, occlusion] = views;
device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("mesh part"),
layout,
@@ -1283,6 +1345,8 @@ fn part_bind(
wgpu::BindGroupEntry { binding: 2, resource: uniform.as_entire_binding() },
wgpu::BindGroupEntry { binding: 3, resource: wgpu::BindingResource::TextureView(roughness) },
wgpu::BindGroupEntry { binding: 4, resource: wgpu::BindingResource::TextureView(metallic) },
+ wgpu::BindGroupEntry { binding: 5, resource: wgpu::BindingResource::TextureView(normal) },
+ wgpu::BindGroupEntry { binding: 6, resource: wgpu::BindingResource::TextureView(occlusion) },
],
})
}
diff --git a/crates/gpu/src/shaders/mesh.wgsl b/crates/gpu/src/shaders/mesh.wgsl
index 8bc4c25..44737f3 100644
--- a/crates/gpu/src/shaders/mesh.wgsl
+++ b/crates/gpu/src/shaders/mesh.wgsl
@@ -27,7 +27,7 @@ struct View {
struct Part {
base: vec4<f32>,
- // roughness and metallic
+ // roughness and metallic, and whether there are a normal map and an occlusion map
material: vec4<f32>,
}
@@ -42,6 +42,9 @@ struct Part {
// roughness and metalness painted over the material's (premultiplied greys)
@group(1) @binding(3) var roughness_map: texture_2d<f32>;
@group(1) @binding(4) var metallic_map: texture_2d<f32>;
+// the model's own normals (in the texture's tangent space) and ambient occlusion, if it has them
+@group(1) @binding(5) var normal_map: texture_2d<f32>;
+@group(1) @binding(6) var occlusion_map: texture_2d<f32>;
const PI: f32 = 3.14159265;
@@ -118,8 +121,30 @@ fn finish(color: vec3<f32>) -> vec4<f32> {
return vec4<f32>(rgb, 1.0);
}
+// The surface's normal bent by the normal map's `t` (0..1 RGB): along the texture's u and v as
+// they run across the surface here, from how position and texture coordinate change between
+// neighbouring pixels.
+fn bent(n: vec3<f32>, t: vec3<f32>, dp1: vec3<f32>, dp2: vec3<f32>, duv1: vec2<f32>, duv2: vec2<f32>) -> vec3<f32> {
+ let p2 = cross(dp2, n);
+ let p1 = cross(n, dp1);
+ let tu = p2 * duv1.x + p1 * duv2.x;
+ let tv = p2 * duv1.y + p1 * duv2.y;
+ let scale = inverseSqrt(max(max(dot(tu, tu), dot(tv, tv)), 1e-20));
+ let m = t * 2.0 - 1.0;
+ return normalize(tu * scale * m.x + tv * scale * m.y + n * m.z);
+}
+
@fragment
fn fs_main(i: Out) -> @location(0) vec4<f32> {
+ // how the surface and the file's texture coordinates run here (taken before any branch),
+ // across the target and up it (its rows run down)
+ let file_uv = vec2<f32>(i.uv.x, 1.0 - i.uv.y);
+ let dp1 = dpdx(i.eye);
+ let dp2 = -dpdy(i.eye);
+ let duv1 = dpdx(file_uv);
+ let duv2 = -dpdy(file_uv);
+ let bump = textureSample(normal_map, albedo_smp, i.uv).rgb;
+ let occlusion = textureSample(occlusion_map, albedo_smp, i.uv).r;
let painted = textureSample(albedo, albedo_smp, i.uv);
let color = painted.rgb + part.base.rgb * (1.0 - painted.a);
let albedo_lin = pow(max(color, vec3<f32>(0.0)), vec3<f32>(2.2));
@@ -133,6 +158,9 @@ fn fs_main(i: Out) -> @location(0) vec4<f32> {
// a normal turned away from the eye (the far side of an open surface, or a smooth one's
// outline) mirrored across the view plane: facing the eye, its slant kept
var n = normalize(i.normal);
+ if (part.material.z > 0.5) {
+ n = bent(n, bump, dp1, dp2, duv1, duv2);
+ }
let away = dot(n, v);
if (away < 0.0) {
n = normalize(n - 2.0 * away * v);
@@ -152,7 +180,9 @@ fn fs_main(i: Out) -> @location(0) vec4<f32> {
let split = textureSample(brdf, brdf_smp, vec2<f32>(nv, r)).rg;
let fr = f0 + (max(vec3<f32>(1.0 - r), f0) - f0) * pow(1.0 - nv, 5.0);
let ambient = (1.0 - fr) * (1.0 - metallic) * albedo_lin * irradiance(n) + reflected * (fr * split.x + split.y);
- return finish(lo + ambient);
+ // the environment's light kept out of creases
+ let reach = select(1.0, occlusion, part.material.w > 0.5);
+ return finish(lo + ambient * reach);
}
// ---- the backdrop: the environment's irradiance seen past the model -------------------------
diff --git a/crates/gpu/tests/mesh.rs b/crates/gpu/tests/mesh.rs
index 10b385a..1048ea6 100644
--- a/crates/gpu/tests/mesh.rs
+++ b/crates/gpu/tests/mesh.rs
@@ -505,3 +505,70 @@ fn a_metalness_painted_over_the_material_changes_how_it_shades() {
assert_eq!(pixel(&draw(&gpu, &mut view), SIDE / 2, SIDE / 2), a);
assert_eq!(gpu.errors(), 0);
}
+
+#[test]
+fn a_models_own_normal_and_occlusion_maps_shade_it() {
+ use procraft_gpu::SurfaceMap;
+ let Some(gpu) = gpu() else { return };
+ let square = "v -1 -1 0\nv 1 -1 0\nv 1 1 0\nv -1 1 0\nvt 0 0\nvt 1 0\nvt 1 1\nvt 0 1\nf 1/1 2/2 3/3 4/4\n";
+ let mesh = read_obj(square, None).unwrap();
+ let mut view = MeshView::new(&gpu, &mesh, 4, wgpu::TextureFormat::Rgba8Unorm);
+ view.orbit = Orbit { target: [0.0, 0.0, 0.0], rotation: Orbit::rotation_of(0.0, 0.0), distance: 6.0, fov: 0.9 };
+ let middle = |view: &mut MeshView| pixel(&draw(&gpu, view), SIDE / 2, SIDE / 2);
+ let near = |a: [u8; 4], b: [u8; 4]| a.iter().zip(&b).all(|(x, y)| x.abs_diff(*y) <= 3);
+ let plain = middle(&mut view);
+ let filled = |rgba: [u8; 4]| rgba.repeat(16);
+ // a flat normal map changes nothing; one leaning along u does
+ view.set_surface_map(&gpu, 0, SurfaceMap::Normal, Some((4, &filled([128, 128, 255, 255]))));
+ let flat = middle(&mut view);
+ assert!(near(flat, plain), "{flat:?} {plain:?}");
+ view.set_surface_map(&gpu, 0, SurfaceMap::Normal, Some((4, &filled([240, 128, 160, 255]))));
+ let leaning = middle(&mut view);
+ assert!(!near(leaning, plain), "{leaning:?} {plain:?}");
+ view.set_surface_map(&gpu, 0, SurfaceMap::Normal, None);
+ assert!(near(middle(&mut view), plain));
+ // full occlusion keeps the environment's light off; none leaves it as it was
+ view.set_surface_map(&gpu, 0, SurfaceMap::Occlusion, Some((4, &filled([0, 0, 0, 255]))));
+ let shut = middle(&mut view);
+ assert!(shut[0] + 10 < plain[0], "{shut:?} {plain:?}");
+ view.set_surface_map(&gpu, 0, SurfaceMap::Occlusion, Some((4, &filled([255, 255, 255, 255]))));
+ assert!(near(middle(&mut view), plain));
+ // a map of the wrong size is left out
+ view.set_surface_map(&gpu, 0, SurfaceMap::Occlusion, Some((4, &[0; 8])));
+ assert!(near(middle(&mut view), plain));
+ assert_eq!(gpu.errors(), 0);
+}
+
+#[test]
+fn a_normal_map_leans_the_surface_along_its_texture_coordinates() {
+ use procraft_gpu::{Light, SurfaceMap};
+ let Some(gpu) = gpu() else { return };
+ // a square facing the camera, u running to its right (+x) and v up (+y)
+ let square = "v -1 -1 0
+v 1 -1 0
+v 1 1 0
+v -1 1 0
+vt 0 0
+vt 1 0
+vt 1 1
+vt 0 1
+f 1/1 2/2 3/3 4/4
+";
+ let mesh = read_obj(square, None).unwrap();
+ let mut view = MeshView::new(&gpu, &mesh, 4, wgpu::TextureFormat::Rgba8Unorm);
+ view.set_material(&gpu, 0, 1.0, 0.0);
+ view.orbit = Orbit { target: [0.0, 0.0, 0.0], rotation: Orbit::rotation_of(0.0, 0.0), distance: 6.0, fov: 0.9 };
+ let mut lit = |place: [f32; 3], rgb: [u8; 3]| {
+ view.lights = vec![Light { place, color: [1.0; 3], intensity: 0.8 }];
+ let map = [rgb[0], rgb[1], rgb[2], 255].repeat(16);
+ view.set_surface_map(&gpu, 0, SurfaceMap::Normal, Some((4, &map)));
+ i32::from(pixel(&draw(&gpu, &mut view), SIDE / 2, SIDE / 2)[0])
+ };
+ // lit from the right (+x): leaning along +u turns it toward the light, along -u away
+ let (toward, away) = (lit([6.0, 0.0, 1.0], [218, 128, 218]), lit([6.0, 0.0, 1.0], [38, 128, 218]));
+ assert!(toward > away + 20, "lit from +x: {toward} leaning +u, {away} leaning -u");
+ // lit from above (+y): the same along v
+ let (toward, away) = (lit([0.0, 6.0, 1.0], [128, 218, 218]), lit([0.0, 6.0, 1.0], [128, 38, 218]));
+ assert!(toward > away + 20, "lit from +y: {toward} leaning +v, {away} leaning -v");
+ assert_eq!(gpu.errors(), 0);
+}