mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-30 18:14:00 +01:00
Auto: Integrate main and streamline History and Log panels
Name the scrollbars for assistive technology. Verify that resizing keeps the whole current History row visible and that a horizontally scrolled Log entry copies with its original line breaks. Borrow visible History labels and reuse formatted Log text to reduce temporary allocations. Validate with the focused Linux suite and the WebAssembly checks.
This commit is contained in:
commit
a020271842
218 files changed
+11627
-2234
No files matched your search
@@ -2,7 +2,7 @@ use std::sync::Arc;
|
||||
|
||||
use serde_json::json;
|
||||
use texturecraft_engine::bake::Progress;
|
||||
use texturecraft_engine::core::BakedMap;
|
||||
use texturecraft_engine::core::{AoMixing, BakedMap};
|
||||
use texturecraft_engine::geom::{NO_TRI, UvGBuffer, primitives};
|
||||
use texturecraft_engine::{Session, execute};
|
||||
|
||||
@@ -103,8 +103,11 @@ fn imported_mesh_maps_display_and_export_the_entire_overridden_tile() {
|
||||
let map = BakedMap::AmbientOcclusion;
|
||||
assert!((baked.scalar(map, 248, 120, (256, 128)).unwrap() - 240.0 / 255.0).abs() < 0.001);
|
||||
let composite = &session.caches[0].composite;
|
||||
let display = tile_bytes(composite, baked, DisplayKind::Orm, 5, 1);
|
||||
assert_eq!(display[((56 * 64 + 56) * 4) as usize], 240, "the viewport samples the far corner of the smaller working tile");
|
||||
let corner = ((56 * 64 + 56) * 4) as usize;
|
||||
let display = tile_bytes(composite, baked, AoMixing::Multiply, DisplayKind::Orm, 5, 1);
|
||||
assert_eq!(display[corner], 240, "the viewport samples the far corner of the smaller working tile");
|
||||
let unmixed = tile_bytes(composite, baked, AoMixing::None, DisplayKind::Orm, 5, 1);
|
||||
assert!(unmixed[corner] > display[corner], "ambient occlusion mixing None leaves the baked map out of the viewport");
|
||||
|
||||
let options = ExportOptions::new(ExportPreset {
|
||||
name: "Bake detail".into(),
|
||||
|
||||
@@ -39,6 +39,7 @@ fn in_memory_psd_export_only_renders_the_first_plan_for_each_name() {
|
||||
let small = options(SMALL_SIDE);
|
||||
let large = options(4096);
|
||||
let first = session.export_files(&[0], &small).unwrap();
|
||||
|
||||
assert!(first.len() > 1, "the mesh supplies multiple PSD tiles");
|
||||
|
||||
let jobs = std::iter::once((0, &small)).chain(std::iter::repeat_n((0, &large), 4));
|
||||
|
||||
@@ -70,6 +70,32 @@ fn planned_uv_tile_sizes_match_the_written_files() {
|
||||
std::fs::remove_dir_all(directory).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rectangular_map_overrides_plan_the_dimensions_written_by_png_and_psd() {
|
||||
let mut session = session();
|
||||
execute(&mut session, "textureSet.resolution", &json!({"ts": 0, "resolution": 128, "height": 256})).unwrap();
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let preset = json!({"name": "Mixed", "maps": [
|
||||
{"file": "one", "format": "Png", "bit_depth": 8, "channels": [["L", {"Channel": {"channel": "Roughness"}}]]},
|
||||
{"file": "two", "format": "Psd", "bit_depth": 8, "channels": [["L", {"Channel": {"channel": "Roughness"}}]]}
|
||||
]});
|
||||
let params = json!({"dir": directory.path(), "ts": 0, "preset": preset, "size": 256, "maps": [{"map": 0, "size": 128}, {"map": 1, "size": 128}]});
|
||||
|
||||
let files = execute(&mut session, "export.plan", ¶ms).unwrap();
|
||||
execute(&mut session, "file.exportTextures", ¶ms).unwrap();
|
||||
|
||||
let files = files.as_array().unwrap();
|
||||
assert_eq!(files.len(), 2, "{files:?}");
|
||||
for file in files {
|
||||
let bytes = std::fs::read(directory.path().join(file["file"].as_str().unwrap())).unwrap();
|
||||
let (width_bytes, height_bytes) = if file["format"] == "psd" { (&bytes[18..22], &bytes[14..18]) } else { (&bytes[16..20], &bytes[20..24]) };
|
||||
let (width, height) = (u32::from_be_bytes(width_bytes.try_into().unwrap()), u32::from_be_bytes(height_bytes.try_into().unwrap()));
|
||||
assert_eq!(height, 2 * width, "{file}");
|
||||
assert_eq!((file["size"].as_u64(), file["height"].as_u64()), (Some(u64::from(width)), Some(u64::from(height))), "{file}");
|
||||
assert_eq!(file["size"], file["sourceSize"], "both maps override the export width with the source width");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_file_name_an_earlier_texture_set_exports_is_not_overwritten() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -67,6 +67,7 @@ mod perf;
|
||||
mod polygon_fill;
|
||||
mod project_recovery;
|
||||
mod rebuilt_uv_layout;
|
||||
mod rename_project;
|
||||
mod resource_updater;
|
||||
mod resources_used;
|
||||
mod sample;
|
||||
|
||||
@@ -47,7 +47,7 @@ fn cooked(roughness: f32, wear: f32) -> CookedMaterial {
|
||||
CookedMaterial::new(serde_json::from_value(interface).unwrap(), maps, 4)
|
||||
}
|
||||
|
||||
fn install(session: &mut Session) -> LayerId {
|
||||
pub(crate) fn install(session: &mut Session) -> LayerId {
|
||||
session.add_cooked_material(&cooked(0.25, 0.5), Arc::new(b"package bytes".to_vec()), "mcmat", Place::Selection).unwrap()
|
||||
}
|
||||
|
||||
|
||||
@@ -523,19 +523,29 @@ fn sets_with_the_same_file_name_warn_instead_of_overwriting() {
|
||||
fn a_material_never_points_at_a_texture_kept_for_another_set() {
|
||||
let mut session = Session::new_project("t", primitives::material_ball(), "builtin:material_ball", None, 128).unwrap();
|
||||
|
||||
// The first set's name expands to the same base-colour filename as the second
|
||||
execute(&mut session, "textureSet.rename", &json!({"ts": 0, "name": "$channel"})).unwrap();
|
||||
execute(&mut session, "textureSet.rename", &json!({"ts": 1, "name": "baseColor"})).unwrap();
|
||||
execute(&mut session, "textureSet.rename", &json!({"ts": 0, "name": "body"})).unwrap();
|
||||
execute(&mut session, "textureSet.rename", &json!({"ts": 1, "name": "trim"})).unwrap();
|
||||
let directory = temp("shared-map");
|
||||
|
||||
let result = export(&mut session, &directory, OPENPBR, true);
|
||||
// Give the first set a map with the same filename as the second set's canonical base colour
|
||||
let mut preset = texturecraft_engine::formats::builtin_preset(OPENPBR).unwrap();
|
||||
let mut conflicting_map = preset.maps.iter().find(|map| map.file.ends_with("_base_color")).unwrap().clone();
|
||||
let conflicting_stem = "material_ball_trim_base_color";
|
||||
conflicting_map.file = conflicting_stem.into();
|
||||
preset.maps.push(conflicting_map);
|
||||
|
||||
let materials: Vec<_> = files(&result).into_iter().filter(|path| path.ends_with(".mtlx")).collect();
|
||||
assert!(materials.len() >= 2, "{result}");
|
||||
assert!(std::fs::read_to_string(&materials[0]).unwrap().contains("material_ball_baseColor_base_color.png"));
|
||||
for material in materials.iter().skip(1) {
|
||||
assert!(!std::fs::read_to_string(material).unwrap().contains("material_ball_baseColor_base_color.png"), "{material}");
|
||||
}
|
||||
let result =
|
||||
execute(&mut session, "file.exportTextures", &json!({"dir": directory.display().to_string(), "preset": preset, "ts": [0, 1], "materialX": true}))
|
||||
.unwrap();
|
||||
|
||||
let conflicting_file = format!("{conflicting_stem}.png");
|
||||
assert!(names(&result).contains(&conflicting_file), "{result}");
|
||||
assert!(warned(&result, &format!("\"{conflicting_file}\" was not written")), "{result}");
|
||||
let body = referenced(&std::fs::read_to_string(directory.join("material_ball_body.mtlx")).unwrap());
|
||||
let trim = referenced(&std::fs::read_to_string(directory.join("material_ball_trim.mtlx")).unwrap());
|
||||
assert!(body.iter().any(|file| file == "material_ball_body_base_color.png"), "{body:?}");
|
||||
assert!(trim.iter().any(|file| file.starts_with("material_ball_trim_")), "{trim:?}");
|
||||
assert!(!trim.contains(&conflicting_file), "{trim:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::time::{Duration, Instant};
|
||||
use rayon::prelude::*;
|
||||
use serde_json::{Value, json};
|
||||
use texturecraft_engine::compose::{DisplayKind, kinds_for, tile_bytes};
|
||||
use texturecraft_engine::core::Channel;
|
||||
use texturecraft_engine::core::{AoMixing, Channel};
|
||||
use texturecraft_engine::geom::glam::Vec2;
|
||||
use texturecraft_engine::geom::primitives;
|
||||
use texturecraft_engine::texel::TILE;
|
||||
@@ -64,7 +64,7 @@ fn upload_bytes(session: &mut Session) -> (usize, usize) {
|
||||
let selected_bytes = tiles
|
||||
.par_iter()
|
||||
.flat_map_iter(|&(tile_x, tile_y)| kinds.iter().map(move |&kind| (kind, tile_x, tile_y)))
|
||||
.map(|(kind, tile_x, tile_y)| tile_bytes(composite, baked, kind, tile_x, tile_y).len())
|
||||
.map(|(kind, tile_x, tile_y)| tile_bytes(composite, baked, AoMixing::Multiply, kind, tile_x, tile_y).len())
|
||||
.sum();
|
||||
let all_bytes_per_tile: usize = DisplayKind::ALL.iter().map(|kind| kind.bytes_per_texel() * (TILE * TILE) as usize).sum();
|
||||
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
//! `project.rename`: the name `$project` stands for in export file names.
|
||||
|
||||
use serde_json::json;
|
||||
use texturecraft_engine::geom::primitives;
|
||||
use texturecraft_engine::{Session, execute};
|
||||
|
||||
#[test]
|
||||
fn a_renamed_project_exports_under_its_new_name_until_undone() {
|
||||
let mut session = Session::new_project("hero", primitives::cube(2.0), "builtin:cube", None, 128).unwrap();
|
||||
let mut preset = execute(&mut session, "export.preset.get", &json!({"name": "Document channels + Normal + AO (No Alpha)"})).unwrap();
|
||||
preset["maps"][0]["file"] = json!("$project_$textureSet");
|
||||
|
||||
execute(&mut session, "project.rename", &json!({"name": " Knight final "})).unwrap();
|
||||
|
||||
let plan = execute(&mut session, "export.plan", &json!({"preset": preset})).unwrap();
|
||||
let renamed_file = plan[0]["file"].as_str().unwrap();
|
||||
assert!(renamed_file.starts_with("Knight_final_"), "{renamed_file}");
|
||||
|
||||
let reopened = Session::from_project_bytes(&session.to_project_bytes().unwrap()).unwrap();
|
||||
assert_eq!(reopened.doc.project.name, "Knight final");
|
||||
|
||||
execute(&mut session, "edit.undo", &json!({})).unwrap();
|
||||
let plan = execute(&mut session, "export.plan", &json!({"preset": preset})).unwrap();
|
||||
|
||||
assert_eq!(session.doc.project.name, "hero");
|
||||
let restored_file = plan[0]["file"].as_str().unwrap();
|
||||
assert!(restored_file.starts_with("hero_"), "{restored_file}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unusable_or_unchanged_project_name_edits_nothing() {
|
||||
use texturecraft_engine::project_cmds::MAX_PROJECT_NAME_CHARS;
|
||||
|
||||
let mut session = Session::new_project("hero", primitives::cube(2.0), "builtin:cube", None, 128).unwrap();
|
||||
|
||||
for name in [json!(null), json!(7), json!(" "), json!("a\nb"), json!("界".repeat(MAX_PROJECT_NAME_CHARS + 1))] {
|
||||
assert!(execute(&mut session, "project.rename", &json!({"name": name})).is_err(), "{name}");
|
||||
}
|
||||
|
||||
execute(&mut session, "project.rename", &json!({"name": "hero"})).unwrap();
|
||||
|
||||
assert_eq!(session.doc.project.name, "hero");
|
||||
assert!(!session.modified, "nothing changed");
|
||||
assert!(!session.can_undo());
|
||||
|
||||
let at_limit = "界".repeat(MAX_PROJECT_NAME_CHARS);
|
||||
let result = execute(&mut session, "project.rename", &json!({"name": format!(" {at_limit} ")})).unwrap();
|
||||
|
||||
assert_eq!(result["name"], at_limit);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_renamed_project_is_inserted_into_the_template_as_literal_text() {
|
||||
let mut session = Session::new_project("hero", primitives::cube(2.0), "builtin:cube", None, 128).unwrap();
|
||||
let mut preset = execute(&mut session, "export.preset.get", &json!({"name": "Document channels + Normal + AO (No Alpha)"})).unwrap();
|
||||
preset["maps"][0]["file"] = json!("$project/$mesh");
|
||||
|
||||
execute(&mut session, "project.rename", &json!({"name": "Knight/$mesh"})).unwrap();
|
||||
let plan = execute(&mut session, "export.plan", &json!({"preset": preset})).unwrap();
|
||||
|
||||
let file = plan[0]["file"].as_str().unwrap();
|
||||
assert!(file.starts_with("Knight__mesh/cube."), "{file}");
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
//! The images a project reads beyond fill sources: a colour selection's ID map, a filter's image input, the mask brush and its paths
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use texturecraft_engine::core::{Channel, LayerId};
|
||||
use texturecraft_engine::core::{Channel, EffectKind, LayerId, LayerKind};
|
||||
use texturecraft_engine::geom::primitives;
|
||||
use texturecraft_engine::{Session, execute};
|
||||
|
||||
@@ -53,6 +53,24 @@ fn removing_unused_resources_keeps_the_images_a_selection_a_filter_input_the_mas
|
||||
assert_eq!(reply["removed"], json!([unused]), "only the image nothing reads goes");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removing_unused_resources_keeps_the_package_and_maps_of_a_material_linked_in_a_content_effect() {
|
||||
let mut session = session();
|
||||
let unused = session.import_resource_bytes("unused.png", white_png()).unwrap();
|
||||
let linked = crate::materialcraft_link::install(&mut session);
|
||||
let LayerKind::Fill(fill) = session.doc.project.layer(linked).unwrap().kind.clone() else { panic!("not a fill") };
|
||||
|
||||
// The linked material moves into a content effect of another layer, the only place that uses it
|
||||
let layer = execute(&mut session, "layer.add.fill", &Value::Null).unwrap()["layer"].as_u64().unwrap();
|
||||
let effect = LayerId(execute(&mut session, "effect.add.fill", &json!({"layer": layer, "target": "content"})).unwrap()["effect"].as_u64().unwrap());
|
||||
session.doc.project.effect_mut(effect).unwrap().kind = EffectKind::Material(fill);
|
||||
execute(&mut session, "layer.remove", &json!({"layer": linked.0})).unwrap();
|
||||
|
||||
let reply = execute(&mut session, "file.removeUnused", &Value::Null).unwrap();
|
||||
|
||||
assert_eq!(reply["removed"], json!([unused]), "the linked package and its maps stay");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_smart_material_carries_the_images_its_selection_and_filter_input_read() {
|
||||
let mut session = session();
|
||||
|
||||
@@ -148,3 +148,26 @@ fn restored_stencil_and_smudge_settings_use_the_tool_limits() {
|
||||
let look = session.tool.stencil_look;
|
||||
assert_eq!((look.center, look.scale, look.opacity, look.rotation), ([-4.0, 4.0], 0.01, 0.0, 345.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_brush_sets_and_reports_the_projection_bands_settings() {
|
||||
let mut session = another_project();
|
||||
let defaults = session.tool.brush.clone();
|
||||
let reported = execute(&mut session, "tool.brush", &json!({"angleFade": [20, 40], "stampDepth": 2.5, "screenSpace": !defaults.screen_space})).unwrap();
|
||||
|
||||
let brush = &session.tool.brush;
|
||||
assert_ne!(brush.angle_fade, defaults.angle_fade, "Angle fade from / to changes through the command");
|
||||
assert_ne!(brush.depth, defaults.depth, "Stamp depth changes through the command");
|
||||
assert_eq!(reported["angleFade"], json!([brush.angle_fade.0, brush.angle_fade.1]));
|
||||
assert_eq!(reported["stampDepth"], json!(brush.depth));
|
||||
assert_eq!(reported["screenSpace"], json!(brush.screen_space));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_malformed_angle_fade_leaves_the_brush_unchanged() {
|
||||
let mut session = another_project();
|
||||
let before = session.tool.brush.clone();
|
||||
|
||||
assert!(execute(&mut session, "tool.brush", &json!({"size": 50, "angleFade": "steep"})).is_err());
|
||||
assert_eq!(session.tool.brush, before);
|
||||
}
|
||||
@@ -410,14 +410,14 @@ fn a_mesh_without_materials_gets_a_material_bound_to_the_default_texture_set() {
|
||||
|
||||
#[test]
|
||||
fn a_psd_of_layers_is_left_out_of_the_materials_and_said_so() {
|
||||
let mut s = ball();
|
||||
let mut session = ball();
|
||||
let dir = temp("psd");
|
||||
|
||||
let r = export(&mut s, &dir, json!({"ts": 0, "format": "psd", "usdAsset": true}));
|
||||
let result = export(&mut session, &dir, json!({"ts": 0, "format": "psd", "usdAsset": true}));
|
||||
|
||||
assert!(files(&r).iter().any(|f| f.ends_with(".psd")), "{r}");
|
||||
let materials = read_back(&s, &dir);
|
||||
assert!(files(&result).iter().any(|f| f.ends_with(".psd")), "{result}");
|
||||
let materials = read_back(&session, &dir);
|
||||
|
||||
assert!(materials[material_of(&s, 0)].0.is_empty(), "no input reads the PSD's merged image: {:?}", materials[material_of(&s, 0)]);
|
||||
assert!(r["warnings"].to_string().contains("doesn't read PSD files"), "{r}");
|
||||
assert!(materials[material_of(&session, 0)].0.is_empty(), "no input reads the PSD's merged image: {:?}", materials[material_of(&session, 0)]);
|
||||
assert!(result["warnings"].to_string().contains("doesn't read PSD files"), "{result}");
|
||||
}
|
||||
@@ -103,3 +103,19 @@ fn decreasing_pressure_does_not_stamp_behind_a_turn() {
|
||||
assert!(red_at(&session, 0.33, 0.55), "the new segment is painted");
|
||||
assert!(!red_at(&session, 0.33, 0.45), "a smaller step must not stamp behind the turn");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_light_pen_paints_at_full_size_with_size_pressure_off() {
|
||||
let painted_across = |size_pressure: bool| {
|
||||
let mut session = red_brush_session(json!({"size": 10.0, "minSize": 0.0, "sizePressure": size_pressure, "hardness": 90}));
|
||||
session.begin_stroke_uv([0.5, 0.5].into(), 0.2).unwrap();
|
||||
session.continue_stroke_uv([0.5, 0.5001].into(), 0.2);
|
||||
session.end_stroke().unwrap();
|
||||
session.update();
|
||||
(0..RESOLUTION).filter(|x| red_at(&session, *x as f32 / RESOLUTION as f32, 0.5)).count()
|
||||
};
|
||||
|
||||
let (pressed, ignored) = (painted_across(true), painted_across(false));
|
||||
assert!(pressed > 0, "a light pen still paints");
|
||||
assert!(ignored > pressed, "with size pressure off a light pen paints wider ({ignored} vs {pressed} texels)");
|
||||
}
|
||||
+363
-11
@@ -1,8 +1,9 @@
|
||||
use serde_json::{Value, json};
|
||||
use texturecraft_engine::compose::PixelStore;
|
||||
use texturecraft_engine::core::{Channel, MIN_RESOLUTION};
|
||||
use texturecraft_engine::geom::glam::Vec2;
|
||||
use texturecraft_engine::geom::primitives;
|
||||
use texturecraft_engine::{Session, command_specs, execute};
|
||||
use texturecraft_engine::{Document, Session, command_specs, execute};
|
||||
|
||||
fn session() -> Session {
|
||||
session_at(256)
|
||||
@@ -17,6 +18,23 @@ fn session_at(texture_size: u32) -> Session {
|
||||
s
|
||||
}
|
||||
|
||||
/// Whether two documents hold the same content (`Document` has no `PartialEq`). Destructured so
|
||||
/// a new public field must be compared here too (crate-private fields, such as `painted_regions`,
|
||||
/// can't be reached from an integration test).
|
||||
fn same_document(a: &Document, b: &Document) -> bool {
|
||||
let Document { project, pixels: PixelStore { layers, masks }, baked, bake_paint, resources, bake_meshes, .. } = a;
|
||||
// Bake meshes have no equality (each holds a mesh and its BVH): the same high polys and cage
|
||||
// stand in for them.
|
||||
let same_meshes = bake_meshes.high.len() == b.bake_meshes.high.len() && bake_meshes.cage.is_some() == b.bake_meshes.cage.is_some();
|
||||
*project == b.project
|
||||
&& *layers == b.pixels.layers
|
||||
&& *masks == b.pixels.masks
|
||||
&& *baked == b.baked
|
||||
&& *bake_paint == b.bake_paint
|
||||
&& *resources == b.resources
|
||||
&& same_meshes
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scripted_stroke_paints_and_undo_restores() {
|
||||
let mut s = session();
|
||||
@@ -500,6 +518,92 @@ fn an_srgb_hex_colour_matches_its_linear_rgb_value() {
|
||||
assert!(digits[0] - hex[0] > 0.1, "{digits:?} {hex:?}");
|
||||
}
|
||||
|
||||
/// A command that panics halfway through an edit leaves the document and history as they were.
|
||||
#[test]
|
||||
fn a_panic_inside_an_edit_restores_the_document() {
|
||||
let mut s = session();
|
||||
let before = s.doc.clone();
|
||||
let history = s.history_labels();
|
||||
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||
let _ = s.edit("Half done", |s| -> texturecraft_engine::Result<()> {
|
||||
s.doc.project.name = "changed".into();
|
||||
s.doc.pixels.mask_mut(texturecraft_engine::core::LayerId(u64::MAX), (64, 64));
|
||||
s.doc.resources.insert("half.png".into(), std::sync::Arc::new(vec![1, 2, 3]));
|
||||
panic!("boom");
|
||||
});
|
||||
}));
|
||||
assert!(r.is_err(), "the panic still reaches the caller");
|
||||
assert!(same_document(&s.doc, &before), "the half-made change was rolled back");
|
||||
assert_eq!(s.history_labels(), history, "no undo step was recorded");
|
||||
}
|
||||
|
||||
/// An edit that recomposites the discarded document and then fails or panics: the display shows the
|
||||
/// restored document again after the next `update`, not the discarded one.
|
||||
#[test]
|
||||
fn a_failed_or_panicking_edit_recomposites_the_restored_document() {
|
||||
for panics in [false, true] {
|
||||
let mut s = session();
|
||||
let hit = s.pick(texturecraft_engine::geom::glam::Vec2::new(200.0, 200.0), s.viewport).unwrap();
|
||||
let (x, y) = ((hit.uv.x * 256.0) as u32, (hit.uv.y * 256.0) as u32);
|
||||
let red_at = |s: &mut Session| {
|
||||
s.update();
|
||||
s.caches[0].composite.value(Channel::BaseColor, x, y)[0]
|
||||
};
|
||||
execute(&mut s, "tool.material", &json!({"channel": "baseColor", "color": [1.0, 0.0, 0.0]})).unwrap();
|
||||
execute(&mut s, "tool.brush", &json!({"size": 20, "hardness": 90})).unwrap();
|
||||
execute(&mut s, "tool.stroke", &json!({"points": [[180, 200], [220, 200]]})).unwrap();
|
||||
let painted = red_at(&mut s);
|
||||
assert!(painted > 0.9, "the stroke painted: {painted}");
|
||||
|
||||
let edit = |s: &mut Session| {
|
||||
let _ = s.edit("Hide everything", |s| -> texturecraft_engine::Result<()> {
|
||||
s.doc.project.texture_sets[0].stack.clear();
|
||||
s.touched();
|
||||
s.update();
|
||||
assert!(s.caches[0].composite.value(Channel::BaseColor, x, y)[0] < 0.9, "the edit shows the empty stack");
|
||||
if panics {
|
||||
panic!("boom");
|
||||
}
|
||||
Err(texturecraft_engine::EngineError::Other("nope".into()))
|
||||
});
|
||||
};
|
||||
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| edit(&mut s)));
|
||||
assert_eq!(r.is_err(), panics);
|
||||
let shown = red_at(&mut s);
|
||||
assert!(shown > 0.9, "panics={panics}: the restored paint is shown again: {shown}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A command that fails leaves the document and history as they were (commands are atomic).
|
||||
#[test]
|
||||
fn failed_commands_leave_the_document_unchanged() {
|
||||
let hostile = [
|
||||
Value::Null,
|
||||
json!({}),
|
||||
json!({"layer": u64::MAX, "ts": 99, "channel": "nope", "mode": "???", "opacity": f64::MAX}),
|
||||
json!({"layer": 1, "index": -5, "delta": i64::MIN, "above": "x", "points": [[f64::MAX, -1e300]], "size": -1e30}),
|
||||
json!({"resolution": 0, "maps": ["x"], "rays": u64::MAX, "antialiasing": 3, "color": [1], "value": "nan"}),
|
||||
json!({"effect": 0, "x": 1e30, "y": -1e30, "viewport": [0, 0], "mesh": "../../etc", "material": ""}),
|
||||
];
|
||||
let mut offenders = Vec::new();
|
||||
for spec in command_specs() {
|
||||
if spec.id == "project.sample" {
|
||||
continue; // slow (bakes); covered elsewhere
|
||||
}
|
||||
for p in &hostile {
|
||||
let mut s = session_at(MIN_RESOLUTION);
|
||||
for texture_set in &mut s.doc.project.texture_sets {
|
||||
texture_set.bake.common.resolution = texturecraft_engine::core::BakeResolution::R64;
|
||||
}
|
||||
let (before, history) = (s.doc.clone(), s.history_labels());
|
||||
if execute(&mut s, spec.id, p).is_err() && (!same_document(&s.doc, &before) || s.history_labels() != history) {
|
||||
offenders.push(format!("{} {p}", spec.id));
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(offenders.is_empty(), "failed commands changed the document:\n{}", offenders.join("\n"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copy_paste_flatten_and_resize_keep_the_look() {
|
||||
let mut s = Session::new_project("t", primitives::material_ball(), "builtin:material_ball", None, 256).unwrap();
|
||||
@@ -697,6 +801,224 @@ fn projection_tool_paints_the_stencil_colors() {
|
||||
assert!(right[1] > 0.8 && right[0] < 0.2, "right {right:?}");
|
||||
}
|
||||
|
||||
/// The stencil tests' texture size, to turn a picked UV into a texel
|
||||
const TEXTURE_SIZE: u32 = 256;
|
||||
|
||||
/// Side of the stencil black on its left half and white on its right
|
||||
const STENCIL_SIZE: u32 = 16;
|
||||
|
||||
/// The view's row over the sphere the stencil tests paint along
|
||||
const DAB_Y: f32 = 200.0;
|
||||
|
||||
/// A dab behind the stencil's black half
|
||||
const BLACK_DAB_X: f32 = 150.0;
|
||||
|
||||
/// A dab behind the stencil's white half
|
||||
const WHITE_DAB_X: f32 = 250.0;
|
||||
|
||||
/// Imports a stencil black on its left half and white on its right, returning its resource name
|
||||
fn half_black_stencil(session: &mut Session) -> String {
|
||||
let stencil = image::RgbaImage::from_fn(STENCIL_SIZE, STENCIL_SIZE, |x, _| {
|
||||
if x < STENCIL_SIZE / 2 { image::Rgba([0, 0, 0, 255]) } else { image::Rgba([255, 255, 255, 255]) }
|
||||
});
|
||||
let mut png = Vec::new();
|
||||
stencil.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
|
||||
session.import_resource_bytes("half.png", png).unwrap()
|
||||
}
|
||||
|
||||
/// The composite base color under the view's point at `x` on the dab row
|
||||
fn base_color_at(session: &Session, x: f32) -> [f32; 3] {
|
||||
let hit = session.pick(Vec2::new(x, DAB_Y), session.viewport).unwrap();
|
||||
let texel = |coordinate: f32| (coordinate * TEXTURE_SIZE as f32) as u32;
|
||||
|
||||
session.caches[0].composite.value(Channel::BaseColor, texel(hit.uv.x), texel(hit.uv.y))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_paint_tool_paints_its_own_color_through_the_stencils_gray() {
|
||||
let mut session = session_at(TEXTURE_SIZE);
|
||||
let name = half_black_stencil(&mut session);
|
||||
execute(&mut session, "tool.paint", &Value::Null).unwrap();
|
||||
execute(&mut session, "tool.material", &json!({"channel": "baseColor", "color": [1.0, 0.0, 0.0]})).unwrap();
|
||||
execute(&mut session, "tool.stencil", &json!({"image": name, "scale": 1.0, "tile": true})).unwrap();
|
||||
execute(&mut session, "tool.brush", &json!({"size": 6, "hardness": 90})).unwrap();
|
||||
let left_before = base_color_at(&session, BLACK_DAB_X);
|
||||
|
||||
for x in [BLACK_DAB_X, WHITE_DAB_X] {
|
||||
execute(&mut session, "tool.stroke", &json!({"points": [[x, DAB_Y]]})).unwrap();
|
||||
}
|
||||
|
||||
let (left, right) = (base_color_at(&session, BLACK_DAB_X), base_color_at(&session, WHITE_DAB_X));
|
||||
assert_eq!(left, left_before, "black stencil blocks the paint");
|
||||
assert!(right[0] > 0.8 && right[1] < 0.2 && right[2] < 0.2, "white stencil lets the tool's red through: {right:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stroke_blocked_by_a_stencil_keeps_pixels_and_history() {
|
||||
for (tool, alpha) in [("paint", 255), ("projection", 0)] {
|
||||
let mut session = session_at(TEXTURE_SIZE);
|
||||
let stencil = image::RgbaImage::from_pixel(STENCIL_SIZE, STENCIL_SIZE, image::Rgba([0, 0, 0, alpha]));
|
||||
let mut png = Vec::new();
|
||||
stencil.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
let name = session.import_resource_bytes("blocked.png", png).unwrap();
|
||||
execute(&mut session, "tool.select", &json!({"tool": tool})).unwrap();
|
||||
execute(&mut session, "tool.brush", &json!({"size": 6, "hardness": 90})).unwrap();
|
||||
execute(&mut session, "tool.stencil", &json!({"image": name, "scale": 1.0, "tile": true})).unwrap();
|
||||
let layer = session.selected_layer_id().unwrap();
|
||||
execute(&mut session, "layer.add.paint", &Value::Null).unwrap();
|
||||
execute(&mut session, "edit.undo", &Value::Null).unwrap();
|
||||
execute(&mut session, "layer.select", &json!({"layer": layer.0})).unwrap();
|
||||
let history = session.history_labels();
|
||||
let redo = session.redo_labels();
|
||||
let revision = session.revision;
|
||||
let tile_bytes = session.doc.pixels.bytes();
|
||||
let tiles = session.doc.pixels.layers.values().map(texturecraft_texel::TileImage::allocated_count).sum::<usize>();
|
||||
let color = base_color_at(&session, BLACK_DAB_X);
|
||||
|
||||
let stroke = execute(&mut session, "tool.stroke", &json!({"points": [[BLACK_DAB_X, DAB_Y]]})).unwrap();
|
||||
let after_tiles = session.doc.pixels.layers.values().map(texturecraft_texel::TileImage::allocated_count).sum::<usize>();
|
||||
let after_tile_bytes = session.doc.pixels.bytes();
|
||||
println!("{tool}: {tiles} -> {after_tiles} retained tiles, {tile_bytes} -> {after_tile_bytes} retained tile bytes");
|
||||
|
||||
assert!(stroke["stamps"].as_u64().unwrap() > 0);
|
||||
assert_eq!(base_color_at(&session, BLACK_DAB_X), color);
|
||||
assert_eq!(after_tile_bytes, tile_bytes, "{tool} stored empty tiles");
|
||||
assert_eq!(after_tiles, tiles);
|
||||
assert_eq!(session.history_labels(), history);
|
||||
assert_eq!(session.redo_labels(), redo);
|
||||
assert_eq!(session.revision, revision);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stroke_blocked_by_a_stencil_drops_its_unused_mask_effect() {
|
||||
let mut session = session_at(TEXTURE_SIZE);
|
||||
let name = half_black_stencil(&mut session);
|
||||
execute(&mut session, "tool.paint", &Value::Null).unwrap();
|
||||
execute(&mut session, "tool.brush", &json!({"size": 6, "hardness": 90})).unwrap();
|
||||
execute(&mut session, "tool.stencil", &json!({"image": name, "scale": 1.0, "tile": true})).unwrap();
|
||||
execute(&mut session, "tool.material", &json!({"channel": "baseColor", "color": [1.0, 0.0, 0.0]})).unwrap();
|
||||
execute(&mut session, "tool.stroke", &json!({"points": [[WHITE_DAB_X, DAB_Y]]})).unwrap();
|
||||
let painted_color = base_color_at(&session, WHITE_DAB_X);
|
||||
let id = session.selected_layer_id().unwrap();
|
||||
session.target = texturecraft_engine::StackTarget::Mask;
|
||||
let history = session.history_labels();
|
||||
let next_id = session.doc.project.next_id;
|
||||
assert!(session.doc.project.layers[&id].mask.is_none());
|
||||
|
||||
session.begin_stroke(Vec2::new(BLACK_DAB_X, DAB_Y), session.viewport, 1.0).unwrap();
|
||||
|
||||
assert!(session.stroke_tiles() > 0);
|
||||
session.caches[0].invalidate_all();
|
||||
session.update();
|
||||
assert_ne!(base_color_at(&session, WHITE_DAB_X), painted_color);
|
||||
session.end_stroke().unwrap();
|
||||
session.update();
|
||||
|
||||
assert!(session.doc.project.layers[&id].mask.is_none());
|
||||
assert_eq!(session.doc.project.next_id, next_id);
|
||||
assert!(session.doc.pixels.masks.is_empty());
|
||||
assert_eq!(session.history_labels(), history);
|
||||
assert_eq!(base_color_at(&session, WHITE_DAB_X), painted_color);
|
||||
|
||||
session.begin_stroke(Vec2::new(BLACK_DAB_X, DAB_Y), session.viewport, 1.0).unwrap();
|
||||
|
||||
assert!(session.stroke_tiles() > 0);
|
||||
execute(&mut session, "layer.rename", &json!({"layer": id.0, "name": "Renamed while painting"})).unwrap();
|
||||
let history_after_rename = session.history_labels();
|
||||
|
||||
session.end_stroke().unwrap();
|
||||
|
||||
assert_eq!(session.doc.project.layers[&id].name, "Renamed while painting");
|
||||
assert!(session.doc.project.layers[&id].mask.is_some());
|
||||
assert_eq!(session.history_labels(), history_after_rename);
|
||||
assert!(session.doc.pixels.masks.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_eraser_erases_only_through_the_stencils_gray() {
|
||||
let mut session = session_at(TEXTURE_SIZE);
|
||||
let name = half_black_stencil(&mut session);
|
||||
execute(&mut session, "tool.paint", &Value::Null).unwrap();
|
||||
execute(&mut session, "tool.material", &json!({"channel": "baseColor", "color": [1.0, 0.0, 0.0]})).unwrap();
|
||||
execute(&mut session, "tool.brush", &json!({"size": 6, "hardness": 90})).unwrap();
|
||||
for x in [BLACK_DAB_X, WHITE_DAB_X] {
|
||||
execute(&mut session, "tool.stroke", &json!({"points": [[x, DAB_Y]]})).unwrap();
|
||||
}
|
||||
let (left_painted, right_painted) = (base_color_at(&session, BLACK_DAB_X), base_color_at(&session, WHITE_DAB_X));
|
||||
|
||||
execute(&mut session, "tool.eraser", &Value::Null).unwrap();
|
||||
execute(&mut session, "tool.stencil", &json!({"image": name, "scale": 1.0, "tile": true})).unwrap();
|
||||
for x in [BLACK_DAB_X, WHITE_DAB_X] {
|
||||
execute(&mut session, "tool.stroke", &json!({"points": [[x, DAB_Y]]})).unwrap();
|
||||
}
|
||||
|
||||
assert_eq!(base_color_at(&session, BLACK_DAB_X), left_painted, "black stencil keeps the paint from the eraser");
|
||||
assert_ne!(base_color_at(&session, WHITE_DAB_X), right_painted, "white stencil lets the eraser through");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_mask_stroke_shows_through_the_stencil_while_painting_as_it_commits() {
|
||||
let mut session = session_at(TEXTURE_SIZE);
|
||||
let name = half_black_stencil(&mut session);
|
||||
|
||||
// A red fill under a white mask the stroke paints black
|
||||
execute(&mut session, "layer.add.fill", &json!({"color": [1.0, 0.0, 0.0]})).unwrap();
|
||||
execute(&mut session, "mask.add.white", &Value::Null).unwrap();
|
||||
session.target = texturecraft_engine::StackTarget::Mask;
|
||||
session.tool.mask_value = 0.0;
|
||||
execute(&mut session, "tool.paint", &Value::Null).unwrap();
|
||||
execute(&mut session, "tool.stencil", &json!({"image": name, "scale": 1.0, "tile": true})).unwrap();
|
||||
execute(&mut session, "tool.brush", &json!({"size": 6, "hardness": 90})).unwrap();
|
||||
session.update();
|
||||
let (left_before, right_before) = (base_color_at(&session, BLACK_DAB_X), base_color_at(&session, WHITE_DAB_X));
|
||||
|
||||
// A stroke from behind the stencil's black half to its white half
|
||||
let viewport = session.viewport;
|
||||
session.begin_stroke(Vec2::new(BLACK_DAB_X, DAB_Y), viewport, 1.0).unwrap();
|
||||
session.continue_stroke(Vec2::new(WHITE_DAB_X, DAB_Y), 1.0);
|
||||
session.update();
|
||||
let (left_preview, right_preview) = (base_color_at(&session, BLACK_DAB_X), base_color_at(&session, WHITE_DAB_X));
|
||||
assert_eq!(left_preview, left_before, "black stencil hides the live stroke");
|
||||
assert_ne!(right_preview, right_before, "white stencil shows the live stroke");
|
||||
|
||||
session.end_stroke().unwrap();
|
||||
session.update();
|
||||
let (left_committed, right_committed) = (base_color_at(&session, BLACK_DAB_X), base_color_at(&session, WHITE_DAB_X));
|
||||
assert_eq!(left_committed, left_before, "black stencil blocks the committed stroke");
|
||||
assert_ne!(right_committed, right_before, "white stencil lets the committed stroke through");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_live_paint_stroke_shows_through_the_stencil_on_every_channel_it_paints() {
|
||||
let mut session = session_at(TEXTURE_SIZE);
|
||||
let name = half_black_stencil(&mut session);
|
||||
execute(&mut session, "tool.paint", &Value::Null).unwrap();
|
||||
execute(&mut session, "tool.material", &json!({"channel": "baseColor", "color": [1.0, 0.0, 0.0]})).unwrap();
|
||||
execute(&mut session, "tool.material", &json!({"channel": "roughness", "value": 0.0})).unwrap();
|
||||
execute(&mut session, "tool.stencil", &json!({"image": name, "scale": 1.0, "tile": true})).unwrap();
|
||||
execute(&mut session, "tool.brush", &json!({"size": 6, "hardness": 90})).unwrap();
|
||||
let channel_at = |session: &Session, channel: Channel, x: f32| {
|
||||
let hit = session.pick(Vec2::new(x, DAB_Y), session.viewport).unwrap();
|
||||
let texel = |coordinate: f32| (coordinate * TEXTURE_SIZE as f32) as u32;
|
||||
session.caches[0].composite.value(channel, texel(hit.uv.x), texel(hit.uv.y))
|
||||
};
|
||||
let before =
|
||||
[Channel::BaseColor, Channel::Roughness].map(|channel| (channel_at(&session, channel, BLACK_DAB_X), channel_at(&session, channel, WHITE_DAB_X)));
|
||||
|
||||
// A stroke from behind the stencil's black half to its white half, shown before it commits
|
||||
let viewport = session.viewport;
|
||||
session.begin_stroke(Vec2::new(BLACK_DAB_X, DAB_Y), viewport, 1.0).unwrap();
|
||||
session.continue_stroke(Vec2::new(WHITE_DAB_X, DAB_Y), 1.0);
|
||||
session.update();
|
||||
|
||||
for (channel, (left_before, right_before)) in [Channel::BaseColor, Channel::Roughness].into_iter().zip(before) {
|
||||
assert_eq!(channel_at(&session, channel, BLACK_DAB_X), left_before, "black stencil hides the live {channel:?}");
|
||||
assert_ne!(channel_at(&session, channel, WHITE_DAB_X), right_before, "white stencil shows the live {channel:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn smart_materials_save_and_load_with_pixels() {
|
||||
let mut s = Session::new_project("t", primitives::material_ball(), "builtin:material_ball", None, 256).unwrap();
|
||||
@@ -771,6 +1093,36 @@ fn committed_strokes_settle_to_the_full_composite() {
|
||||
assert_eq!(full(&mut s), original);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn synchronizing_a_saved_channel_order_preserves_the_composite() {
|
||||
let mut session = texturecraft_engine::sample_session(128, 64).unwrap();
|
||||
session.doc.project.texture_sets[0].channels.reverse();
|
||||
let bytes = session.to_project_bytes().unwrap();
|
||||
let mut reopened = Session::from_project_bytes(&bytes).unwrap();
|
||||
reopened.update();
|
||||
reopened.caches[0].upload.clear();
|
||||
|
||||
reopened.sync_caches();
|
||||
|
||||
assert!(reopened.caches[0].dirty.is_empty(), "the unchanged set should keep its composited tiles");
|
||||
assert!(reopened.caches[0].upload.is_empty(), "the unchanged set should require no uploads");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_pending_resize_cannot_change_a_replacement_project() {
|
||||
let mut session = session_at(128);
|
||||
let task = session.resize_task_to(0, 512, 256).unwrap();
|
||||
execute(&mut session, "project.new", &json!({"mesh": "cube", "resolution": 128})).unwrap();
|
||||
let before = session.doc.clone();
|
||||
|
||||
let output = task.run(&texturecraft_engine::bake::Progress::new()).unwrap();
|
||||
let result = session.apply_resize(output);
|
||||
|
||||
assert!(result.is_err(), "a resize from the previous project was accepted");
|
||||
assert!(same_document(&session.doc, &before));
|
||||
assert!(session.undo_label().is_none());
|
||||
}
|
||||
|
||||
/// A resize prepared from a snapshot and applied later matches the command, keeps edits made
|
||||
/// while it ran, and undoes in one step.
|
||||
#[test]
|
||||
@@ -1159,21 +1511,21 @@ fn interleaved_sessions_render_depth_from_their_own_mesh() {
|
||||
/// A full history keeps its length and may repeat its newest name, so views follow the count of steps added
|
||||
#[test]
|
||||
fn a_full_history_still_counts_each_new_step() {
|
||||
let mut s = session();
|
||||
let mut session = session();
|
||||
for _ in 0..texturecraft_engine::session::MAX_HISTORY {
|
||||
execute(&mut s, "tool.stroke", &json!({"points": [[180, 200], [220, 200]]})).unwrap();
|
||||
execute(&mut session, "tool.stroke", &json!({"points": [[180, 200], [220, 200]]})).unwrap();
|
||||
}
|
||||
let (labels, added) = (s.history_labels(), s.steps_added());
|
||||
let (labels, added) = (session.history_labels(), session.steps_added());
|
||||
|
||||
execute(&mut s, "tool.stroke", &json!({"points": [[180, 200], [220, 200]]})).unwrap();
|
||||
execute(&mut session, "tool.stroke", &json!({"points": [[180, 200], [220, 200]]})).unwrap();
|
||||
|
||||
assert_eq!(s.history_labels(), labels);
|
||||
assert!(s.steps_added() > added);
|
||||
assert_eq!(session.history_labels(), labels);
|
||||
assert!(session.steps_added() > added);
|
||||
|
||||
// A path preview paints through strokes of its own, none of which is a step
|
||||
let added = s.steps_added();
|
||||
execute(&mut s, "tool.path", &json!({"uvPoints": [[0.2, 0.5], [0.8, 0.5]]})).unwrap();
|
||||
let added = session.steps_added();
|
||||
execute(&mut session, "tool.path", &json!({"uvPoints": [[0.2, 0.5], [0.8, 0.5]]})).unwrap();
|
||||
|
||||
assert_eq!(s.history_labels(), labels);
|
||||
assert_eq!(s.steps_added(), added);
|
||||
assert_eq!(session.history_labels(), labels);
|
||||
assert_eq!(session.steps_added(), added);
|
||||
}
|
||||
Reference in new issue
Block a user