mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-20 06:20:50 +01:00
Assets: a dots badge on procedurals whose result depends on the random seed
This commit is contained in:
1 parent
73e15193de
commit
e8fd3b0074
2 files changed
+69
-1
No files matched your search
@@ -33,6 +33,14 @@ const SELECTED_TILE: &str = "shelf_selected_tile";
|
||||
/// The outline widths of the selected tile and of the one in use (the brush's alpha, the shown environment), in points
|
||||
const SELECTED_OUTLINE_WIDTH: f32 = 1.5;
|
||||
const IN_USE_OUTLINE_WIDTH: f32 = 1.0;
|
||||
/// The randomness badge's dot radius, dot spacing and inset from the thumbnail's corner, in points
|
||||
const BADGE_DOT_RADIUS: f32 = 1.3;
|
||||
const BADGE_DOT_SPACING: f32 = 3.6;
|
||||
const BADGE_INSET: f32 = 2.5;
|
||||
/// The randomness badge's dot colour and its rim's width (points) and colour
|
||||
const BADGE_DOT_FILL: Color32 = Color32::from_gray(205);
|
||||
const BADGE_DOT_RIM: f32 = 0.8;
|
||||
const BADGE_DOT_RIM_COLOR: Color32 = Color32::from_black_alpha(170);
|
||||
/// The thumbnail's size in a tile and its center below the tile's top, in points
|
||||
const THUMBNAIL_SIZE: f32 = 60.0;
|
||||
const THUMBNAIL_CENTER_Y: f32 = 31.0;
|
||||
@@ -408,6 +416,10 @@ fn entry_tile(app: &mut TextureCraftApp, ui: &mut Ui, ctx: &egui::Context, entry
|
||||
ui.painter().rect_stroke(response.rect, TILE_ROUNDING, Stroke::new(width, tk.accent), egui::StrokeKind::Inside);
|
||||
}
|
||||
|
||||
if entry.includes_randomness() {
|
||||
randomness_badge(ui.painter(), response.rect);
|
||||
}
|
||||
|
||||
if response.clicked() || response.secondary_clicked() {
|
||||
ctx.data_mut(|data| data.insert_temp(selected_id, key));
|
||||
}
|
||||
@@ -452,7 +464,7 @@ pub(crate) fn tile(
|
||||
if resp.hovered() || resp.dragged() {
|
||||
painter.rect_filled(rect, TILE_ROUNDING, tk.row_hover);
|
||||
}
|
||||
let image_rect = egui::Rect::from_center_size(rect.center_top() + vec2(0.0, THUMBNAIL_CENTER_Y), vec2(THUMBNAIL_SIZE, THUMBNAIL_SIZE));
|
||||
let image_rect = thumbnail_rect(rect);
|
||||
thumbnail(painter, tex, image_rect, &tk);
|
||||
let galley = painter.layout_job(one_line(label, rect.width() - LABEL_MARGIN, tk.text));
|
||||
painter.galley(egui::pos2(rect.center().x - galley.size().x * 0.5, image_rect.bottom() + LABEL_GAP), galley, tk.text);
|
||||
@@ -461,6 +473,19 @@ pub(crate) fn tile(
|
||||
resp.on_hover_ui(|ui| hover_preview(ui, tex, big.as_ref(), label))
|
||||
}
|
||||
|
||||
/// Where a tile's thumbnail goes in the tile's `rect`
|
||||
fn thumbnail_rect(rect: egui::Rect) -> egui::Rect {
|
||||
egui::Rect::from_center_size(rect.center_top() + vec2(0.0, THUMBNAIL_CENTER_Y), vec2(THUMBNAIL_SIZE, THUMBNAIL_SIZE))
|
||||
}
|
||||
|
||||
/// Three light, dark-rimmed dots at the thumbnail's bottom-right corner: the asset includes randomness
|
||||
fn randomness_badge(painter: &egui::Painter, tile: egui::Rect) {
|
||||
let corner = thumbnail_rect(tile).right_bottom() - vec2(BADGE_INSET, BADGE_INSET);
|
||||
for offset in [vec2(0.0, 0.0), vec2(-BADGE_DOT_SPACING, 0.0), vec2(-BADGE_DOT_SPACING * 0.5, -BADGE_DOT_SPACING)] {
|
||||
painter.circle(corner + offset, BADGE_DOT_RADIUS, BADGE_DOT_FILL, Stroke::new(BADGE_DOT_RIM, BADGE_DOT_RIM_COLOR));
|
||||
}
|
||||
}
|
||||
|
||||
/// A tile label on one line, cut with "…" past `width` points (the hover popup shows the whole name)
|
||||
fn one_line(label: &str, width: f32, color: Color32) -> egui::text::LayoutJob {
|
||||
let mut job = egui::text::LayoutJob::single_section(label.to_string(), egui::TextFormat::simple(egui::FontId::proportional(LABEL_FONT_SIZE), color));
|
||||
|
||||
@@ -19,6 +19,12 @@ use crate::shelf_preview::ShelfPreview;
|
||||
/// The name of a saved brush whose file name isn't text
|
||||
const SAVED_BRUSH_NAME: &str = "brush";
|
||||
|
||||
/// Probes per side of the grid that compares a procedural under two seeds, off the pattern's grid lines
|
||||
const SEED_PROBES: usize = 4;
|
||||
const SEED_PROBE_OFFSET: f32 = 0.13;
|
||||
/// A smaller difference between the two seeds' samples is rounding
|
||||
const SEED_TOLERANCE: f32 = 1e-4;
|
||||
|
||||
/// A shelf tile's asset
|
||||
#[derive(Clone)]
|
||||
pub(crate) enum ShelfEntry {
|
||||
@@ -170,6 +176,14 @@ impl ShelfEntry {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether applying the asset gives a different result per random seed (the tile's dots badge)
|
||||
pub(crate) fn includes_randomness(&self) -> bool {
|
||||
match self {
|
||||
ShelfEntry::Procedural(kind) => SEEDED_PROCEDURALS.contains(kind),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// What a click on the tile does
|
||||
pub(crate) fn activate(&self, app: &mut TextureCraftApp) {
|
||||
match self {
|
||||
@@ -194,3 +208,32 @@ impl ShelfEntry {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The procedurals whose pattern depends on the seed, worked out once
|
||||
static SEEDED_PROCEDURALS: std::sync::LazyLock<Vec<ProceduralKind>> =
|
||||
std::sync::LazyLock::new(|| ProceduralKind::ALL.into_iter().filter(|kind| seed_changes(*kind)).collect());
|
||||
|
||||
/// Whether procedural `kind` samples differently under two seeds
|
||||
fn seed_changes(kind: ProceduralKind) -> bool {
|
||||
use texturecraft_engine::compose::pattern::{Coords, procedural};
|
||||
use texturecraft_engine::core::{Procedural, Projection};
|
||||
use texturecraft_engine::geom::glam::{Vec2, Vec3};
|
||||
|
||||
let sample = |seed: u32, i: usize| {
|
||||
let uv = Vec2::new((i % SEED_PROBES) as f32 / SEED_PROBES as f32, (i / SEED_PROBES) as f32 / SEED_PROBES as f32) + SEED_PROBE_OFFSET;
|
||||
procedural(&Procedural { seed, ..Procedural::new(kind) }, &Projection::default(), &Coords { uv, pos: Vec3::ZERO, nrm: Vec3::Y })
|
||||
};
|
||||
(0..SEED_PROBES * SEED_PROBES).any(|i| (sample(1, i) - sample(2, i)).abs() > SEED_TOLERANCE)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn only_seeded_procedurals_include_randomness() {
|
||||
let seeded = &*SEEDED_PROCEDURALS;
|
||||
assert!(seeded.contains(&ProceduralKind::Clouds), "{seeded:?}");
|
||||
assert!(!seeded.contains(&ProceduralKind::Checker), "{seeded:?}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user