feat: some stuff
This commit is contained in:
@ -12,3 +12,4 @@ serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
|
||||
console_error_panic_hook = { version = "0.1.7", optional = true }
|
||||
gl_matrix = "0.0.2"
|
||||
glam = "0.27.0"
|
||||
|
50
packages/utils/src/geometry/calculate_normals.rs
Normal file
50
packages/utils/src/geometry/calculate_normals.rs
Normal file
@ -0,0 +1,50 @@
|
||||
use crate::{decode_float, encode_float};
|
||||
use glam::Vec3;
|
||||
|
||||
pub fn calculate_normals(geometry: &mut [i32]) {
|
||||
let vertex_count = geometry[1] as usize;
|
||||
let face_count = geometry[2] as usize;
|
||||
let index_start = 3;
|
||||
let indices_end = index_start + face_count * 3;
|
||||
let positions_start = indices_end;
|
||||
let positions_end = positions_start + vertex_count * 3;
|
||||
let normals_start = positions_end;
|
||||
|
||||
let mut last_normal = Vec3::ZERO;
|
||||
|
||||
for i in 0..face_count {
|
||||
let base_idx1 = geometry[index_start + i * 3] as usize * 3;
|
||||
let base_idx2 = geometry[index_start + i * 3 + 1] as usize * 3;
|
||||
let base_idx3 = geometry[index_start + i * 3 + 2] as usize * 3;
|
||||
|
||||
let v1 = Vec3::new(
|
||||
decode_float(geometry[positions_start + base_idx1]),
|
||||
decode_float(geometry[positions_start + base_idx1 + 1]),
|
||||
decode_float(geometry[positions_start + base_idx1 + 2]),
|
||||
);
|
||||
let v2 = Vec3::new(
|
||||
decode_float(geometry[positions_start + base_idx2]) - v1.x,
|
||||
decode_float(geometry[positions_start + base_idx2 + 1]) - v1.y,
|
||||
decode_float(geometry[positions_start + base_idx2 + 2]) - v1.z,
|
||||
);
|
||||
let v3 = Vec3::new(
|
||||
decode_float(geometry[positions_start + base_idx3]) - v1.x,
|
||||
decode_float(geometry[positions_start + base_idx3 + 1]) - v1.y,
|
||||
decode_float(geometry[positions_start + base_idx3 + 2]) - v1.z,
|
||||
);
|
||||
|
||||
let mut normal = v2.cross(v3).normalize();
|
||||
if normal.length_squared() == 0.0 {
|
||||
normal = last_normal;
|
||||
} else {
|
||||
last_normal = normal;
|
||||
}
|
||||
|
||||
for j in 0..3 {
|
||||
let idx = geometry[index_start + i * 3 + j] as usize * 3;
|
||||
geometry[normals_start + idx] = encode_float(normal.x);
|
||||
geometry[normals_start + idx + 1] = encode_float(normal.y);
|
||||
geometry[normals_start + idx + 2] = encode_float(normal.z);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,2 +1 @@
|
||||
pub fn extrude_path(path: &[i32]) {}
|
||||
|
||||
|
3
packages/utils/src/geometry/mod.rs
Normal file
3
packages/utils/src/geometry/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod calculate_normals;
|
||||
pub mod extrude_path;
|
||||
|
@ -5,6 +5,7 @@ mod tree;
|
||||
pub use encoding::*;
|
||||
pub use helpers::*;
|
||||
pub use tree::*;
|
||||
pub mod geometry;
|
||||
|
||||
pub fn set_panic_hook() {
|
||||
// When the `console_error_panic_hook` feature is enabled, we can call the
|
||||
|
Reference in New Issue
Block a user