 bdef86ea6e
			
		
	
	
		bdef86ea6e
		
	
	
	
	
		
			
			# Objective Models can be produced that do not have vertex tangents but do have normal map textures. The tangents can be generated. There is a way that the vertex tangents can be generated to be exactly invertible to avoid introducing error when recreating the normals in the fragment shader. ## Solution - After attempts to get https://github.com/gltf-rs/mikktspace to integrate simple glam changes and version bumps, and releases of that crate taking weeks / not being made (no offense intended to the authors/maintainers, bevy just has its own timelines and needs to take care of) it was decided to fork that repository. The following steps were taken: - mikktspace was forked to https://github.com/bevyengine/mikktspace in order to preserve the repository's history in case the original is ever taken down - The README in that repo was edited to add a note stating from where the repository was forked and explaining why - The repo was locked for changes as its only purpose is historical - The repo was integrated into the bevy repo using `git subtree add --prefix crates/bevy_mikktspace git@github.com:bevyengine/mikktspace.git master` - In `bevy_mikktspace`: - The travis configuration was removed - `cargo fmt` was run - The `Cargo.toml` was conformed to bevy's (just adding bevy to the keywords, changing the homepage and repository, changing the version to 0.7.0-dev - importantly the license is exactly the same) - Remove the features, remove `nalgebra` entirely, only use `glam`, suppress clippy. - This was necessary because our CI runs clippy with `--all-features` and the `nalgebra` and `glam` features are mutually exclusive, plus I don't want to modify this highly numerically-sensitive code just to appease clippy and diverge even more from upstream. - Rebase https://github.com/bevyengine/bevy/pull/1795 - @jakobhellermann said it was fine to copy and paste but it ended up being almost exactly the same with just a couple of adjustments when validating correctness so I decided to actually rebase it and then build on top of it. - Use the exact same fragment shader code to ensure correct normal mapping. - Tested with both https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/NormalTangentMirrorTest which has vertex tangents and https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/NormalTangentTest which requires vertex tangent generation Co-authored-by: alteous <alteous@outlook.com>
		
			
				
	
	
		
			260 lines
		
	
	
		
			6.9 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			260 lines
		
	
	
		
			6.9 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| #![allow(clippy::bool_assert_comparison, clippy::useless_conversion)]
 | |
| 
 | |
| use glam::{Vec2, Vec3};
 | |
| 
 | |
| pub type Face = [u32; 3];
 | |
| 
 | |
| #[derive(Debug)]
 | |
| struct Vertex {
 | |
|     position: Vec3,
 | |
|     normal: Vec3,
 | |
|     tex_coord: Vec2,
 | |
| }
 | |
| 
 | |
| struct Mesh {
 | |
|     faces: Vec<Face>,
 | |
|     vertices: Vec<Vertex>,
 | |
| }
 | |
| 
 | |
| fn vertex(mesh: &Mesh, face: usize, vert: usize) -> &Vertex {
 | |
|     let vs: &[u32; 3] = &mesh.faces[face];
 | |
|     &mesh.vertices[vs[vert] as usize]
 | |
| }
 | |
| 
 | |
| impl bevy_mikktspace::Geometry for Mesh {
 | |
|     fn num_faces(&self) -> usize {
 | |
|         self.faces.len()
 | |
|     }
 | |
| 
 | |
|     fn num_vertices_of_face(&self, _face: usize) -> usize {
 | |
|         3
 | |
|     }
 | |
| 
 | |
|     fn position(&self, face: usize, vert: usize) -> [f32; 3] {
 | |
|         vertex(self, face, vert).position.into()
 | |
|     }
 | |
| 
 | |
|     fn normal(&self, face: usize, vert: usize) -> [f32; 3] {
 | |
|         vertex(self, face, vert).normal.into()
 | |
|     }
 | |
| 
 | |
|     fn tex_coord(&self, face: usize, vert: usize) -> [f32; 2] {
 | |
|         vertex(self, face, vert).tex_coord.into()
 | |
|     }
 | |
| 
 | |
|     fn set_tangent_encoded(&mut self, tangent: [f32; 4], face: usize, vert: usize) {
 | |
|         println!(
 | |
|             "{face}-{vert}: v: {v:?}, vn: {vn:?}, vt: {vt:?}, vx: {vx:?}",
 | |
|             face = face,
 | |
|             vert = vert,
 | |
|             v = vertex(self, face, vert).position,
 | |
|             vn = vertex(self, face, vert).normal,
 | |
|             vt = vertex(self, face, vert).tex_coord,
 | |
|             vx = tangent,
 | |
|         );
 | |
|     }
 | |
| }
 | |
| 
 | |
| fn make_cube() -> Mesh {
 | |
|     struct ControlPoint {
 | |
|         uv: [f32; 2],
 | |
|         dir: [f32; 3],
 | |
|     }
 | |
|     let mut faces = Vec::new();
 | |
|     let mut ctl_pts = Vec::new();
 | |
|     let mut vertices = Vec::new();
 | |
| 
 | |
|     // +x plane
 | |
|     {
 | |
|         let base = ctl_pts.len() as u32;
 | |
|         faces.push([base, base + 1, base + 4]);
 | |
|         faces.push([base + 1, base + 2, base + 4]);
 | |
|         faces.push([base + 2, base + 3, base + 4]);
 | |
|         faces.push([base + 3, base, base + 4]);
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 0.0],
 | |
|             dir: [1.0, -1.0, 1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 1.0],
 | |
|             dir: [1.0, -1.0, -1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [1.0, 1.0],
 | |
|             dir: [1.0, 1.0, -1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [1.0, 0.0],
 | |
|             dir: [1.0, 1.0, 1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.5, 0.5],
 | |
|             dir: [1.0, 0.0, 0.0],
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     // -x plane
 | |
|     {
 | |
|         let base = ctl_pts.len() as u32;
 | |
|         faces.push([base, base + 1, base + 4]);
 | |
|         faces.push([base + 1, base + 2, base + 4]);
 | |
|         faces.push([base + 2, base + 3, base + 4]);
 | |
|         faces.push([base + 3, base, base + 4]);
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [1.0, 0.0],
 | |
|             dir: [-1.0, 1.0, 1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [1.0, 1.0],
 | |
|             dir: [-1.0, 1.0, -1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 1.0],
 | |
|             dir: [-1.0, -1.0, -1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 0.0],
 | |
|             dir: [-1.0, -1.0, 1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.5, 0.5],
 | |
|             dir: [-1.0, 0.0, 0.0],
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     // +y plane
 | |
|     {
 | |
|         let base = ctl_pts.len() as u32;
 | |
|         faces.push([base, base + 1, base + 4]);
 | |
|         faces.push([base + 1, base + 2, base + 4]);
 | |
|         faces.push([base + 2, base + 3, base + 4]);
 | |
|         faces.push([base + 3, base, base + 4]);
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 0.0],
 | |
|             dir: [1.0, 1.0, 1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 1.0],
 | |
|             dir: [1.0, 1.0, -1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 1.0],
 | |
|             dir: [-1.0, 1.0, -1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 0.0],
 | |
|             dir: [-1.0, 1.0, 1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 0.5],
 | |
|             dir: [0.0, 1.0, 0.0],
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     // -y plane
 | |
|     {
 | |
|         let base = ctl_pts.len() as u32;
 | |
|         faces.push([base, base + 1, base + 4]);
 | |
|         faces.push([base + 1, base + 2, base + 4]);
 | |
|         faces.push([base + 2, base + 3, base + 4]);
 | |
|         faces.push([base + 3, base, base + 4]);
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 0.0],
 | |
|             dir: [-1.0, -1.0, 1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 1.0],
 | |
|             dir: [-1.0, -1.0, -1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 1.0],
 | |
|             dir: [1.0, -1.0, -1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 0.0],
 | |
|             dir: [1.0, -1.0, 1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 0.5],
 | |
|             dir: [0.0, -1.0, 0.0],
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     // +z plane
 | |
|     {
 | |
|         let base = ctl_pts.len() as u32;
 | |
|         faces.push([base, base + 1, base + 4]);
 | |
|         faces.push([base + 1, base + 2, base + 4]);
 | |
|         faces.push([base + 2, base + 3, base + 4]);
 | |
|         faces.push([base + 3, base, base + 4]);
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 0.0],
 | |
|             dir: [-1.0, 1.0, 1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 1.0],
 | |
|             dir: [-1.0, -1.0, 1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [1.0, 1.0],
 | |
|             dir: [1.0, -1.0, 1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [1.0, 0.0],
 | |
|             dir: [1.0, 1.0, 1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.5, 0.5],
 | |
|             dir: [0.0, 0.0, 1.0],
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     // -z plane
 | |
|     {
 | |
|         let base = ctl_pts.len() as u32;
 | |
|         faces.push([base, base + 1, base + 4]);
 | |
|         faces.push([base + 1, base + 2, base + 4]);
 | |
|         faces.push([base + 2, base + 3, base + 4]);
 | |
|         faces.push([base + 3, base, base + 4]);
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [1.0, 0.0],
 | |
|             dir: [1.0, 1.0, -1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [1.0, 1.0],
 | |
|             dir: [1.0, -1.0, -1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 1.0],
 | |
|             dir: [-1.0, -1.0, -1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.0, 0.0],
 | |
|             dir: [-1.0, 1.0, -1.0],
 | |
|         });
 | |
|         ctl_pts.push(ControlPoint {
 | |
|             uv: [0.5, 0.5],
 | |
|             dir: [0.0, 0.0, -1.0],
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     for pt in ctl_pts {
 | |
|         let p: Vec3 = pt.dir.into();
 | |
|         let n: Vec3 = p.normalize();
 | |
|         let t: Vec2 = pt.uv.into();
 | |
|         vertices.push(Vertex {
 | |
|             position: (p / 2.0).into(),
 | |
|             normal: n.into(),
 | |
|             tex_coord: t.into(),
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     Mesh { faces, vertices }
 | |
| }
 | |
| 
 | |
| fn main() {
 | |
|     let mut cube = make_cube();
 | |
|     let ret = bevy_mikktspace::generate_tangents(&mut cube);
 | |
|     assert_eq!(true, ret);
 | |
| }
 |