mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-28 00:59:29 +01:00
Polygon fill: the triangle grouping moves to geom, for generators to share
TrianglePartition and the UV edge key (with its corner grid) move from the engine into texturecraft-geom unchanged, beside the mesh they group, with a uv_islands constructor polygon fill now uses for its UV island mode. The compositor can then group texels by UV island for the UV island generators without depending on the engine.
This commit is contained in:
1 parent
f3c8103579
commit
59fd1bc705
6 files changed
+34
-26
No files matched your search
@@ -86,7 +86,6 @@ mod tile_transform_params;
|
||||
mod tool_availability;
|
||||
pub mod tool_cmds;
|
||||
pub mod tool_settings;
|
||||
mod triangle_partition;
|
||||
mod uv_stroke_state;
|
||||
mod uv_symmetry;
|
||||
pub mod uv_tile_cmds;
|
||||
|
||||
@@ -5,18 +5,15 @@ use std::collections::{BTreeSet, HashSet};
|
||||
|
||||
use texturecraft_compose::MaskGray;
|
||||
use texturecraft_geom::glam::Vec2;
|
||||
use texturecraft_geom::{Mesh, NO_TRI};
|
||||
use texturecraft_geom::triangle_partition::CORNER_GRID_STEPS;
|
||||
use texturecraft_geom::{Mesh, NO_TRI, TrianglePartition};
|
||||
use texturecraft_paint::{EyeDepth, fill_triangles, screen_point};
|
||||
|
||||
use crate::fill_mode::FillMode;
|
||||
use crate::polygon_fill_drag::PolygonFillDrag;
|
||||
use crate::session::Session;
|
||||
use crate::triangle_partition::TrianglePartition;
|
||||
use crate::{EngineError, Result};
|
||||
|
||||
/// Corners closer than one step of this grid (per unit of position or UV) count as the same corner
|
||||
const CORNER_GRID_STEPS: f32 = 1e5;
|
||||
|
||||
impl Session {
|
||||
/// Fill the triangle / polygon / UV island / connected mesh under viewport pixel `screen`
|
||||
pub fn polygon_fill(&mut self, screen: Vec2, viewport: Vec2) -> Result<usize> {
|
||||
@@ -178,7 +175,7 @@ impl Session {
|
||||
match self.tool.fill_mode {
|
||||
FillMode::Triangle => TrianglePartition::new(&self.mesh, material, |_, _| Vec::<u32>::new()),
|
||||
FillMode::Polygon => TrianglePartition::new(&self.mesh, material, polygon_edges),
|
||||
FillMode::UvIsland => TrianglePartition::new(&self.mesh, material, uv_edges),
|
||||
FillMode::UvIsland => TrianglePartition::uv_islands(&self.mesh, material),
|
||||
FillMode::Mesh => TrianglePartition::new(&self.mesh, material, position_edges),
|
||||
}
|
||||
}
|
||||
@@ -298,12 +295,6 @@ fn pixel_span(low: f32, high: f32, count: u32) -> std::ops::Range<usize> {
|
||||
first..end.max(first)
|
||||
}
|
||||
|
||||
/// Texture coordinates of `triangle`'s corners
|
||||
fn uv_corners(mesh: &Mesh, triangle: usize) -> Option<[Vec2; 3]> {
|
||||
let [first, second, third] = mesh.tri(triangle)?;
|
||||
Some([*mesh.uvs.get(first)?, *mesh.uvs.get(second)?, *mesh.uvs.get(third)?])
|
||||
}
|
||||
|
||||
/// The polygon diagonals of `triangle`: edges the importer added to split a polygon
|
||||
fn polygon_edges(mesh: &Mesh, triangle: usize) -> Vec<(u32, u32)> {
|
||||
let hidden = mesh.diagonals.get(triangle).copied().unwrap_or(0);
|
||||
@@ -318,14 +309,6 @@ fn polygon_edges(mesh: &Mesh, triangle: usize) -> Vec<(u32, u32)> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `triangle`'s edges in UV space: imports often split vertices along hard edges where the UVs run on, so vertex indices alone break islands apart
|
||||
fn uv_edges(mesh: &Mesh, triangle: usize) -> Vec<([i64; 2], [i64; 2])> {
|
||||
let corner = |uv: Vec2| (uv * CORNER_GRID_STEPS).round().to_array().map(|step| step as i64);
|
||||
let Some([first, second, third]) = uv_corners(mesh, triangle).map(|uvs| uvs.map(corner)) else { return Vec::new() };
|
||||
|
||||
[(first, second), (second, third), (third, first)].map(|(from, to)| (from.min(to), from.max(to))).to_vec()
|
||||
}
|
||||
|
||||
/// `triangle`'s edges by position: UV seams and hard edges split vertices but leave the geometry connected
|
||||
fn position_edges(mesh: &Mesh, triangle: usize) -> Vec<([i64; 3], [i64; 3])> {
|
||||
let corner = |vertex: usize| mesh.positions.get(vertex).map(|position| (*position * CORNER_GRID_STEPS).round().to_array().map(|step| step as i64));
|
||||
|
||||
@@ -9,7 +9,7 @@ use texturecraft_paint::EyeDepth;
|
||||
use texturecraft_texel::TileImage;
|
||||
|
||||
use crate::session::{Document, StackTarget};
|
||||
use crate::triangle_partition::TrianglePartition;
|
||||
use texturecraft_geom::TrianglePartition;
|
||||
|
||||
/// A polygon fill box being dragged: the fill shows live and becomes one undo step when the drag ends
|
||||
pub(crate) struct PolygonFillDrag {
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::collections::{BTreeSet, HashSet};
|
||||
use texturecraft_geom::glam::Mat3;
|
||||
|
||||
use crate::session::Session;
|
||||
use crate::triangle_partition::TrianglePartition;
|
||||
use texturecraft_geom::TrianglePartition;
|
||||
|
||||
/// How far a mirrored face centre may be from the surface and still count as on it, as a fraction of the mesh's bounding-box diagonal
|
||||
const MIRROR_TOLERANCE: f32 = 1e-3;
|
||||
|
||||
@@ -11,6 +11,7 @@ pub mod camera;
|
||||
pub mod gbuffer;
|
||||
pub mod mesh;
|
||||
pub mod primitives;
|
||||
pub mod triangle_partition;
|
||||
pub mod unwrap;
|
||||
|
||||
pub use bvh::{Bvh, Hit};
|
||||
@@ -18,6 +19,7 @@ pub use camera::{OrbitCamera, Ray};
|
||||
pub use gbuffer::{NO_TRI, Surface, UvGBuffer, surface_at};
|
||||
pub use glam;
|
||||
pub use mesh::{BaseColor, MAX_TRIANGLES, Mesh, SubMesh};
|
||||
pub use triangle_partition::TrianglePartition;
|
||||
|
||||
#[derive(Debug, thiserror::Error, Clone, PartialEq)]
|
||||
pub enum GeomError {
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
//! Triangles of one material grouped into the pieces polygon fill selects and fills.
|
||||
//! Triangles of one material grouped into pieces joined across shared edges: the UV islands, polygons or connected meshes polygon fill selects and the UV island generators colour
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
|
||||
use texturecraft_geom::Mesh;
|
||||
use glam::Vec2;
|
||||
|
||||
use crate::Mesh;
|
||||
|
||||
/// Corners closer than one step of this grid (per unit of position or UV) count as the same corner
|
||||
pub const CORNER_GRID_STEPS: f32 = 1e5;
|
||||
|
||||
/// A material's triangles split into groups joined across shared edges
|
||||
pub struct TrianglePartition {
|
||||
@@ -58,15 +63,34 @@ impl TrianglePartition {
|
||||
self.group_of.get(&triangle).copied()
|
||||
}
|
||||
|
||||
/// `material`'s UV islands: triangles joined where they share an edge in UV space
|
||||
pub fn uv_islands(mesh: &Mesh, material: usize) -> TrianglePartition {
|
||||
TrianglePartition::new(mesh, material, uv_edges)
|
||||
}
|
||||
|
||||
/// The group holding `triangle` (None for a triangle of another material)
|
||||
pub fn group(&self, triangle: u32) -> Option<&[u32]> {
|
||||
self.index_of(triangle).and_then(|group| self.groups.get(group)).map(Vec::as_slice)
|
||||
}
|
||||
}
|
||||
|
||||
/// Texture coordinates of `triangle`'s corners
|
||||
fn uv_corners(mesh: &Mesh, triangle: usize) -> Option<[Vec2; 3]> {
|
||||
let [first, second, third] = mesh.tri(triangle)?;
|
||||
Some([*mesh.uvs.get(first)?, *mesh.uvs.get(second)?, *mesh.uvs.get(third)?])
|
||||
}
|
||||
|
||||
/// `triangle`'s edges in UV space: imports often split vertices along hard edges where the UVs run on, so vertex indices alone break islands apart
|
||||
pub fn uv_edges(mesh: &Mesh, triangle: usize) -> Vec<([i64; 2], [i64; 2])> {
|
||||
let corner = |uv: Vec2| (uv * CORNER_GRID_STEPS).round().to_array().map(|step| step as i64);
|
||||
let Some([first, second, third]) = uv_corners(mesh, triangle).map(|uvs| uvs.map(corner)) else { return Vec::new() };
|
||||
|
||||
[(first, second), (second, third), (third, first)].map(|(from, to)| (from.min(to), from.max(to))).to_vec()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use texturecraft_geom::primitives;
|
||||
use crate::primitives;
|
||||
|
||||
use super::*;
|
||||
|
||||
Reference in new issue
Block a user