|
| 1 | +use bevy_math::{primitives::Triangle3d, Vec3}; |
| 2 | +use wgpu::PrimitiveTopology; |
| 3 | + |
| 4 | +use crate::{ |
| 5 | + mesh::{Indices, Mesh, Meshable}, |
| 6 | + render_asset::RenderAssetUsages, |
| 7 | +}; |
| 8 | + |
| 9 | +impl Meshable for Triangle3d { |
| 10 | + type Output = Mesh; |
| 11 | + |
| 12 | + fn mesh(&self) -> Self::Output { |
| 13 | + let positions: Vec<_> = self.vertices.into(); |
| 14 | + let uvs: Vec<_> = uv_coords(self).into(); |
| 15 | + |
| 16 | + // Every vertex has the normal of the face of the triangle (or zero if the triangle is degenerate). |
| 17 | + let normal: Vec3 = self.normal().map_or(Vec3::ZERO, |n| n.into()); |
| 18 | + let normals = vec![normal; 3]; |
| 19 | + |
| 20 | + let indices = Indices::U32(vec![0, 1, 2]); |
| 21 | + |
| 22 | + Mesh::new( |
| 23 | + PrimitiveTopology::TriangleList, |
| 24 | + RenderAssetUsages::default(), |
| 25 | + ) |
| 26 | + .with_inserted_indices(indices) |
| 27 | + .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions) |
| 28 | + .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals) |
| 29 | + .with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/// Unskewed uv-coordinates for a [`Triangle3d`]. |
| 34 | +#[inline] |
| 35 | +pub(crate) fn uv_coords(triangle: &Triangle3d) -> [[f32; 2]; 3] { |
| 36 | + let [a, b, c] = triangle.vertices; |
| 37 | + |
| 38 | + let main_length = a.distance(b); |
| 39 | + let Some(x) = (b - a).try_normalize() else { |
| 40 | + return [[0., 0.], [1., 0.], [0., 1.]]; |
| 41 | + }; |
| 42 | + let y = c - a; |
| 43 | + |
| 44 | + // `x` corresponds to one of the axes in uv-coordinates; |
| 45 | + // to uv-map the triangle without skewing, we use the orthogonalization |
| 46 | + // of `y` with respect to `x` as the second direction and construct a rectangle that |
| 47 | + // contains `triangle`. |
| 48 | + let y_proj = y.project_onto_normalized(x); |
| 49 | + |
| 50 | + // `offset` represents the x-coordinate of the point `c`; note that x has been shrunk by a |
| 51 | + // factor of `main_length`, so `offset` follows it. |
| 52 | + let offset = y_proj.dot(x) / main_length; |
| 53 | + |
| 54 | + // Obtuse triangle leaning to the left => x direction extends to the left, shifting a from 0. |
| 55 | + if offset < 0. { |
| 56 | + let total_length = 1. - offset; |
| 57 | + let a_uv = [offset.abs() / total_length, 0.]; |
| 58 | + let b_uv = [1., 0.]; |
| 59 | + let c_uv = [0., 1.]; |
| 60 | + |
| 61 | + [a_uv, b_uv, c_uv] |
| 62 | + } |
| 63 | + // Obtuse triangle leaning to the right => x direction extends to the right, shifting b from 1. |
| 64 | + else if offset > 1. { |
| 65 | + let a_uv = [0., 0.]; |
| 66 | + let b_uv = [1. / offset, 0.]; |
| 67 | + let c_uv = [1., 1.]; |
| 68 | + |
| 69 | + [a_uv, b_uv, c_uv] |
| 70 | + } |
| 71 | + // Acute triangle => no extending necessary; a remains at 0 and b remains at 1. |
| 72 | + else { |
| 73 | + let a_uv = [0., 0.]; |
| 74 | + let b_uv = [1., 0.]; |
| 75 | + let c_uv = [offset, 1.]; |
| 76 | + |
| 77 | + [a_uv, b_uv, c_uv] |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl From<Triangle3d> for Mesh { |
| 82 | + fn from(triangle: Triangle3d) -> Self { |
| 83 | + triangle.mesh() |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(test)] |
| 88 | +mod tests { |
| 89 | + use super::uv_coords; |
| 90 | + use bevy_math::primitives::Triangle3d; |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn uv_test() { |
| 94 | + use bevy_math::vec3; |
| 95 | + let mut triangle = Triangle3d::new(vec3(0., 0., 0.), vec3(2., 0., 0.), vec3(-1., 1., 0.)); |
| 96 | + |
| 97 | + let [a_uv, b_uv, c_uv] = uv_coords(&triangle); |
| 98 | + assert_eq!(a_uv, [1. / 3., 0.]); |
| 99 | + assert_eq!(b_uv, [1., 0.]); |
| 100 | + assert_eq!(c_uv, [0., 1.]); |
| 101 | + |
| 102 | + triangle.vertices[2] = vec3(3., 1., 0.); |
| 103 | + let [a_uv, b_uv, c_uv] = uv_coords(&triangle); |
| 104 | + assert_eq!(a_uv, [0., 0.]); |
| 105 | + assert_eq!(b_uv, [2. / 3., 0.]); |
| 106 | + assert_eq!(c_uv, [1., 1.]); |
| 107 | + |
| 108 | + triangle.vertices[2] = vec3(2., 1., 0.); |
| 109 | + let [a_uv, b_uv, c_uv] = uv_coords(&triangle); |
| 110 | + assert_eq!(a_uv, [0., 0.]); |
| 111 | + assert_eq!(b_uv, [1., 0.]); |
| 112 | + assert_eq!(c_uv, [1., 1.]); |
| 113 | + } |
| 114 | +} |
0 commit comments