Crate uvgen

source
Expand description

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;
}

Structs§

  • A patch for surface data that contains secondary texture coordinates and new topology for data. It is needed for serialization: during the UV generation, generator could multiply vertices to make seams, it adds new data to existing vertices. The problem is that we do not serialize surface data - we store only a “link” to resource from which we’ll load surface data on deserialization. But freshly loaded resource is not suitable for generated lightmap - in most cases it just does not have secondary texture coordinates. So we have to patch data after loading somehow with required data, this is where SurfaceDataPatch comes into play.

Functions§

  • Generates UV map for the given vertices and triangles.