#uv #triplanar #lightmap #texcoord

uvgen

Triplanar texture coordinates generator and packer

2 unstable releases

0.2.0 Oct 21, 2024
0.1.0 Mar 5, 2024

#646 in Algorithms

Download history 90/week @ 2024-08-26 91/week @ 2024-09-02 88/week @ 2024-09-09 80/week @ 2024-09-16 107/week @ 2024-09-23 99/week @ 2024-09-30 71/week @ 2024-10-07 196/week @ 2024-10-14 348/week @ 2024-10-21 87/week @ 2024-10-28 99/week @ 2024-11-04 88/week @ 2024-11-11 126/week @ 2024-11-18 131/week @ 2024-11-25 106/week @ 2024-12-02 153/week @ 2024-12-09

526 downloads per month
Used in 6 crates (2 directly)

MIT license

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