mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-28 09:09:25 +01:00
Chasse's repairs to this part's code, carried over from the #698 head (5644b7d3).
231 lines
11 KiB
Rust
231 lines
11 KiB
Rust
//! Cheap shelf previews on a unit sphere: plain materials and library filters (smart ones use [`crate::orb_preview`])
|
|
|
|
use rayon::prelude::*;
|
|
use texturecraft_compose::pattern::{Coords, procedural};
|
|
use texturecraft_core::{
|
|
Channel, Effect, EffectKind, Fill, Filter, Layer, LayerKind, Procedural, ProceduralKind, Project, Projection, SIZE_REFERENCE_RES, Source, TextureSet,
|
|
TileTransform,
|
|
};
|
|
use texturecraft_geom::glam::{Vec2, Vec3};
|
|
|
|
use crate::materials::{Builder, build};
|
|
|
|
/// Linear material values of one preview pixel.
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub struct Sample {
|
|
pub base: [f32; 3],
|
|
pub metallic: f32,
|
|
pub roughness: f32,
|
|
pub height: f32,
|
|
}
|
|
|
|
/// The view-space normal (= position on the unit sphere) of each pixel of a `size²` image,
|
|
/// row-major, None outside the sphere.
|
|
pub fn sphere_normals(size: usize) -> impl Iterator<Item = Option<Vec3>> {
|
|
(0..size * size).map(sphere_normal_at(size))
|
|
}
|
|
|
|
fn sphere_normal_at(size: usize) -> impl Fn(usize) -> Option<Vec3> {
|
|
let center_px = size as f32 * 0.5;
|
|
let radius_px = (center_px - 1.0).max(0.5);
|
|
move |pixel_index| {
|
|
let (x, y) = ((pixel_index % size) as f32 + 0.5 - center_px, center_px - (pixel_index / size) as f32 - 0.5);
|
|
let (dx, dy) = (x / radius_px, y / radius_px);
|
|
let length_squared = dx * dx + dy * dy;
|
|
(length_squared <= 1.0).then(|| Vec3::new(dx, dy, (1.0 - length_squared).max(0.0).sqrt()))
|
|
}
|
|
}
|
|
|
|
fn coords(n: Vec3) -> Coords {
|
|
let uv = Vec2::new(n.z.atan2(n.x) / std::f32::consts::TAU + 0.5, n.y.clamp(-1.0, 1.0).acos() / std::f32::consts::PI);
|
|
Coords { uv, pos: n, nrm: n }
|
|
}
|
|
|
|
fn source(s: &Source, proj: &Projection, c: &Coords) -> Option<[f32; 3]> {
|
|
match s {
|
|
Source::Uniform(v) => Some(*v),
|
|
Source::Procedural { pattern, low, high, .. } => {
|
|
let t = procedural(pattern, proj, c);
|
|
Some([0, 1, 2].map(|k| low[k] + (high[k] - low[k]) * t))
|
|
}
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
const CHANNELS: [Channel; 4] = [Channel::BaseColor, Channel::Metallic, Channel::Roughness, Channel::Height];
|
|
|
|
/// Preview of plain material `id` (None for an unknown id or a smart material, whose masks need mesh maps)
|
|
pub fn material(id: &str, size: usize) -> Option<Vec<Option<Sample>>> {
|
|
let plain = crate::materials::find(id).filter(|found| !found.smart)?;
|
|
let mut project = Project::new("preview", "");
|
|
let layers = build(&mut Builder { p: &mut project, channels: CHANNELS.to_vec() }, plain.id).ok()?;
|
|
|
|
// Pixels evaluate independently and big previews take long on one core
|
|
let normal_at = sphere_normal_at(size);
|
|
let samples = (0..size * size)
|
|
.into_par_iter()
|
|
.map(|pixel_index| {
|
|
let at = coords(normal_at(pixel_index)?);
|
|
let mut values = CHANNELS.map(Channel::default_value);
|
|
for layer in layers.iter().filter(|layer| layer.visible) {
|
|
// Plain materials are unmasked fills
|
|
let LayerKind::Fill(fill) = &layer.kind else { continue };
|
|
|
|
for (value, channel) in values.iter_mut().zip(CHANNELS) {
|
|
let layer_value = fill.sources.get(&channel).and_then(|channel_source| source(channel_source, &fill.projection, &at));
|
|
if let (Some(blend), Some(layer_value)) = (layer.blend(channel), layer_value) {
|
|
let weight = blend.opacity.clamp(0.0, 1.0);
|
|
*value = [0, 1, 2].map(|component| value[component] + (layer_value[component] - value[component]) * weight);
|
|
}
|
|
}
|
|
}
|
|
|
|
let [base, metallic, roughness, height] = values;
|
|
Some(Sample { base, metallic: metallic[0], roughness: roughness[0], height: height[0] })
|
|
})
|
|
.collect();
|
|
|
|
Some(samples)
|
|
}
|
|
|
|
/// `f` with its reach in texels multiplied by `k` (previews show at a fraction of 2K).
|
|
fn magnified(f: Filter, k: f32) -> Filter {
|
|
match f {
|
|
Filter::Blur { radius, wrap } => Filter::Blur { radius: radius * k, wrap },
|
|
Filter::DirectionalBlur { radius, angle } => Filter::DirectionalBlur { radius: radius * k, angle },
|
|
Filter::SlopeBlur { radius, scale, seed, divider } => Filter::SlopeBlur { radius: radius * k, scale, seed, divider },
|
|
Filter::Warp { intensity, scale, seed, divider } => Filter::Warp { intensity: intensity * k, scale, seed, divider },
|
|
Filter::HighPass { radius, space, alpha } => Filter::HighPass { radius: radius * k, space, alpha },
|
|
Filter::EdgeDetect { width } => Filter::EdgeDetect { width: width * k },
|
|
Filter::Bevel { width, wrap } => Filter::Bevel { width: width * k, wrap },
|
|
Filter::Mosaic { size, alpha } => Filter::Mosaic { size: size * k, alpha },
|
|
Filter::Glow { radius, amount } => Filter::Glow { radius: radius * k, amount },
|
|
Filter::DirectionalWarp { intensity, angle, scale, seed, divider } => Filter::DirectionalWarp { intensity: intensity * k, angle, scale, seed, divider },
|
|
f => f,
|
|
}
|
|
}
|
|
|
|
/// Preview of library filter `id` (see [`crate::filter_lib`]): a sample material (colorful
|
|
/// clouds, checker roughness, cell bumps) with the filter on its content, composited by the
|
|
/// compositor on a plane and laid over the sphere (None for an unknown id).
|
|
pub fn filter(id: &str, size: usize) -> Option<Vec<Option<Sample>>> {
|
|
let res = u32::try_from(size).ok()?.max(1);
|
|
let kind = match crate::filter_lib::build(id, &serde_json::Value::Null).ok()? {
|
|
// Transform changes nothing as the library adds it: its picture shows it scaled and turned
|
|
EffectKind::Filter(Filter::Transform(transform)) => {
|
|
EffectKind::Filter(Filter::Transform(TileTransform { scaling: [2.0, 2.0], rotation: 30.0, ..transform }))
|
|
}
|
|
EffectKind::Filter(f) => EffectKind::Filter(magnified(f, SIZE_REFERENCE_RES / res as f32)),
|
|
k => k,
|
|
};
|
|
|
|
// The filters that shape a mask's edge read where values cross 0.5: they get black and white shapes, where the clouds would leave them black
|
|
let shapes = matches!(kind, EffectKind::Filter(Filter::BevelSmooth(_) | Filter::DirectionalDistance(_)));
|
|
render_filter_preview(kind, size, res, shapes)
|
|
}
|
|
|
|
fn render_filter_preview(kind: EffectKind, size: usize, res: u32, shapes: bool) -> Option<Vec<Option<Sample>>> {
|
|
let mesh = texturecraft_geom::primitives::plane(2.0, 2);
|
|
let gbuf = texturecraft_geom::UvGBuffer::new(&mesh, 0, res, res);
|
|
let mut p = Project::new("preview", "");
|
|
p.texture_sets.push(TextureSet::new("preview", 0, res));
|
|
let procedural =
|
|
|kind, scale, low: [f32; 3], high: [f32; 3]| Source::Procedural { pattern: Procedural { scale, ..Procedural::new(kind) }, low, high, ramp: Vec::new() };
|
|
let mut fill = Fill::default();
|
|
let base = if shapes {
|
|
procedural(ProceduralKind::Checker, 3.0, [0.0; 3], [1.0; 3])
|
|
} else {
|
|
procedural(ProceduralKind::Clouds, 2.0, [0.05, 0.2, 0.55], [0.95, 0.55, 0.1])
|
|
};
|
|
fill.sources.insert(Channel::BaseColor, base);
|
|
fill.sources.insert(Channel::Roughness, procedural(ProceduralKind::Checker, 4.0, [0.25; 3], [0.7; 3]));
|
|
fill.sources.insert(Channel::Height, procedural(ProceduralKind::Cells, 3.0, [-0.12; 3], [0.12; 3]));
|
|
let mut layer = Layer::new(p.alloc_id(), "sample", LayerKind::Fill(fill), &CHANNELS);
|
|
layer.content_effects.push(Effect::new(p.alloc_id(), kind));
|
|
p.insert_layer(0, layer, None, false).ok()?;
|
|
let (pixels, baked, images) = (Default::default(), Default::default(), Default::default());
|
|
let inp = texturecraft_compose::Inputs {
|
|
project: &p,
|
|
ts: 0,
|
|
mesh: &mesh,
|
|
gbuf: &gbuf,
|
|
pixels: &pixels,
|
|
baked: &baked,
|
|
block_scales: p.texture_set(0).map(|set| set.block_scales()).unwrap_or_default(),
|
|
preview: None,
|
|
images: &images,
|
|
bounds: mesh.bounds(),
|
|
};
|
|
let mut out = texturecraft_compose::Composite::new(&CHANNELS, res);
|
|
texturecraft_compose::composite_tiles(&inp, &mut out, &texturecraft_compose::all_tiles(res, res));
|
|
Some(
|
|
sphere_normals(size)
|
|
.enumerate()
|
|
.map(|(i, n)| {
|
|
n?;
|
|
let (x, y) = ((i % size) as u32, (i / size) as u32);
|
|
let [base, metal, rough, height] = CHANNELS.map(|c| out.value(c, x, y));
|
|
Some(Sample { base, metallic: metal[0], roughness: rough[0], height: height[0] })
|
|
})
|
|
.collect(),
|
|
)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
/// Whether every value of `sample` is finite
|
|
fn finite(sample: &Sample) -> bool {
|
|
sample.base.into_iter().chain([sample.metallic, sample.roughness, sample.height]).all(f32::is_finite)
|
|
}
|
|
|
|
#[test]
|
|
fn previews_cover_the_sphere_with_finite_values() {
|
|
let size = 16;
|
|
for built_in in crate::materials::MATERIALS {
|
|
let id = built_in.id;
|
|
if built_in.smart {
|
|
assert!(material(id, size).is_none(), "{id}: smart materials preview on the orb");
|
|
continue;
|
|
}
|
|
|
|
let mut project = Project::new("preview", "");
|
|
let layers = build(&mut Builder { p: &mut project, channels: CHANNELS.to_vec() }, id).unwrap();
|
|
assert!(layers.iter().all(|layer| layer.mask.is_none()), "{id}: the sphere preview shows no masks");
|
|
|
|
let pixels = material(id, size).unwrap_or_else(|| panic!("{id}"));
|
|
let center = pixels[size / 2 * size + size / 2].unwrap_or_else(|| panic!("{id}"));
|
|
assert!(finite(¢er), "{id}");
|
|
assert!(pixels[0].is_none(), "corners are outside the sphere");
|
|
}
|
|
assert!(material("nope", size).is_none() && filter("nope", size).is_none());
|
|
|
|
let size = 24;
|
|
for library_filter in crate::filter_lib::FILTERS {
|
|
let id = library_filter.id;
|
|
let pixels = filter(id, size).unwrap_or_else(|| panic!("{id}"));
|
|
let center = pixels[size / 2 * size + size / 2].unwrap_or_else(|| panic!("{id}"));
|
|
assert!(finite(¢er), "{id}");
|
|
}
|
|
let (plain, warped) = (filter("levels", size).unwrap(), filter("warp", size).unwrap());
|
|
assert!(plain != warped, "the warp shows in its preview");
|
|
}
|
|
|
|
#[test]
|
|
fn the_edge_shaping_filters_show_their_shapes_not_black() {
|
|
const SIDE_PX: u32 = 32;
|
|
let size = SIDE_PX as usize;
|
|
|
|
// Use the same checker input with an inactive filter
|
|
let checker = render_filter_preview(EffectKind::Filter(Filter::InvertOff), size, SIDE_PX, true).unwrap();
|
|
for id in ["bevelSmooth", "directionalDistance"] {
|
|
let pixels = filter(id, size).unwrap();
|
|
assert!(pixels != checker, "{id}: the preview shows the unchanged checker");
|
|
let lit: Vec<f32> = pixels.into_iter().flatten().map(|sample| sample.base.iter().sum::<f32>() / 3.0).collect();
|
|
let bright = lit.iter().filter(|value| **value > 0.25).count();
|
|
assert!(bright * 5 > lit.len(), "{id}: {bright} of {} texels lit", lit.len());
|
|
}
|
|
}
|
|
}
|