2 unstable releases
0.2.0 | Oct 21, 2024 |
---|---|
0.1.0 | Mar 5, 2024 |
#628 in Algorithms
641 downloads per month
Used in 6 crates
(2 directly)
25KB
468 lines
uvgen
Triplanar texture coordinates generator and packer. This crate could be used to generate second texture coordinates for light maps and anywhere else where you need to automatically generate texture coordinates. This crate automatically packs everything into an atlas.
lib.rs
:
UV Map generator. Used to generate second texture coordinates for light maps.
Current implementation uses simple tri-planar mapping.
Example
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct Vertex {
pub position: Vector3<f32>,
pub tex_coord: Vector2<f32>,
}
impl Vertex {
fn new(x: f32, y: f32, z: f32) -> Self {
Self {
position: Vector3::new(x, y, z),
tex_coord: Default::default(),
}
}
}
// Create cube geometry.
let mut vertices = vec![
Vertex::new(-0.5, -0.5, 0.5),
Vertex::new(-0.5, 0.5, 0.5),
Vertex::new(0.5, 0.5, 0.5),
Vertex::new(0.5, -0.5, 0.5),
Vertex::new(-0.5, -0.5, -0.5),
Vertex::new(-0.5, 0.5, -0.5),
Vertex::new(0.5, 0.5, -0.5),
Vertex::new(0.5, -0.5, -0.5),
];
let mut triangles = vec![
// Front
[2, 1, 0],
[3, 2, 0],
// Back
[4, 5, 6],
[4, 6, 7],
// Right
[7, 6, 2],
[2, 3, 7],
// Left
[0, 1, 5],
[0, 5, 4],
// Top
[5, 1, 2],
[5, 2, 6],
// Bottom
[3, 0, 4],
[7, 3, 4],
];
let patch = uvgen::generate_uvs(
vertices.iter().map(|v| v.position),
triangles.iter().cloned(),
0.005,
).unwrap();
// Apply patch to the initial data.
triangles = patch.triangles;
for &vertex_index in &patch.additional_vertices {
let vertex = vertices[vertex_index as usize];
vertices.push(vertex);
}
// Assign generated texture coordinates.
for (vertex, tex_coord) in vertices.iter_mut().zip(&patch.second_tex_coords) {
vertex.tex_coord = *tex_coord;
}
Dependencies
~3MB
~59K SLoC