refactor mesh construction
This commit is contained in:
parent
4fad56a7d1
commit
80433d5ed6
|
|
@ -1,6 +1,6 @@
|
||||||
use std::f32::consts::PI;
|
use std::f32::consts::PI;
|
||||||
|
|
||||||
use glam::{Vec3, vec3};
|
use glam::{Mat3A, Vec3, vec3};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
profile::Profile,
|
profile::Profile,
|
||||||
|
|
@ -27,104 +27,39 @@ impl CrownGear {
|
||||||
mesh.faces.reserve_exact(8 * sides);
|
mesh.faces.reserve_exact(8 * sides);
|
||||||
for k in 0..sides {
|
for k in 0..sides {
|
||||||
let t = k as f32 / sides as f32;
|
let t = k as f32 / sides as f32;
|
||||||
let phi = 2.0 * PI * t;
|
let rot = Mat3A::from_rotation_z(2.0 * PI * t);
|
||||||
let (s, c) = phi.sin_cos();
|
|
||||||
let tooth = t * teeth;
|
let tooth = t * teeth;
|
||||||
let y = self.profile.y_from_tooth(tooth);
|
let y = self.profile.y_from_tooth(tooth);
|
||||||
|
|
||||||
|
let n_top = {
|
||||||
let eps = 1e-3;
|
let eps = 1e-3;
|
||||||
let dy_dt = teeth
|
let dy_dt = teeth
|
||||||
* (self.profile.y_from_tooth(tooth + eps) - self.profile.y_from_tooth(tooth - eps))
|
* (self.profile.y_from_tooth(tooth + eps)
|
||||||
|
- self.profile.y_from_tooth(tooth - eps))
|
||||||
/ (2.0 * eps);
|
/ (2.0 * eps);
|
||||||
|
vec3(-2.0 * PI * y, -dy_dt, 2.0 * PI * self.pitch_radius)
|
||||||
|
};
|
||||||
|
|
||||||
let v_radial = vec3(c * self.pitch_radius, s * self.pitch_radius, y);
|
let vertex = |x, z, n| Vertex {
|
||||||
let v_tan = vec3(-2.0 * PI * s, 2.0 * PI * c, dy_dt / self.pitch_radius);
|
position: rot * vec3(x, 0.0, z),
|
||||||
let n_top = v_radial.cross(v_tan).normalize();
|
color,
|
||||||
|
normal: n,
|
||||||
let n_top = vec3(
|
};
|
||||||
s * dy_dt - 2.0 * PI * c * y,
|
mesh.vertices.extend([
|
||||||
-c * dy_dt - 2.0 * PI * s * y,
|
|
||||||
2.0 * PI * self.pitch_radius,
|
|
||||||
);
|
|
||||||
|
|
||||||
// top
|
// top
|
||||||
mesh.vertices.push(Vertex {
|
vertex(self.outer_radius, outer_scale * y, n_top),
|
||||||
position: vec3(
|
vertex(self.inner_radius, inner_scale * y, n_top),
|
||||||
c * self.outer_radius,
|
|
||||||
s * self.outer_radius,
|
|
||||||
outer_scale * y,
|
|
||||||
),
|
|
||||||
color,
|
|
||||||
normal: n_top,
|
|
||||||
});
|
|
||||||
mesh.vertices.push(Vertex {
|
|
||||||
position: vec3(
|
|
||||||
c * self.inner_radius,
|
|
||||||
s * self.inner_radius,
|
|
||||||
inner_scale * y,
|
|
||||||
),
|
|
||||||
color,
|
|
||||||
normal: n_top,
|
|
||||||
});
|
|
||||||
|
|
||||||
// bottom
|
// bottom
|
||||||
mesh.vertices.push(Vertex {
|
vertex(self.inner_radius, -self.base_thickness, -Vec3::Z),
|
||||||
position: vec3(
|
vertex(self.outer_radius, -self.base_thickness, -Vec3::Z),
|
||||||
c * self.inner_radius,
|
|
||||||
s * self.inner_radius,
|
|
||||||
-self.base_thickness,
|
|
||||||
),
|
|
||||||
color,
|
|
||||||
normal: vec3(0.0, 0.0, -1.0),
|
|
||||||
});
|
|
||||||
mesh.vertices.push(Vertex {
|
|
||||||
position: vec3(
|
|
||||||
c * self.outer_radius,
|
|
||||||
s * self.outer_radius,
|
|
||||||
-self.base_thickness,
|
|
||||||
),
|
|
||||||
color,
|
|
||||||
normal: vec3(0.0, 0.0, -1.0),
|
|
||||||
});
|
|
||||||
|
|
||||||
// inner
|
// inner
|
||||||
mesh.vertices.push(Vertex {
|
vertex(self.inner_radius, inner_scale * y, -Vec3::X),
|
||||||
position: vec3(
|
vertex(self.inner_radius, -self.base_thickness, -Vec3::X),
|
||||||
c * self.inner_radius,
|
|
||||||
s * self.inner_radius,
|
|
||||||
inner_scale * y,
|
|
||||||
),
|
|
||||||
color,
|
|
||||||
normal: vec3(-c, -s, 0.0),
|
|
||||||
});
|
|
||||||
mesh.vertices.push(Vertex {
|
|
||||||
position: vec3(
|
|
||||||
c * self.inner_radius,
|
|
||||||
s * self.inner_radius,
|
|
||||||
-self.base_thickness,
|
|
||||||
),
|
|
||||||
color,
|
|
||||||
normal: vec3(-c, -s, 0.0),
|
|
||||||
});
|
|
||||||
|
|
||||||
// outer
|
// outer
|
||||||
mesh.vertices.push(Vertex {
|
vertex(self.outer_radius, -self.base_thickness, Vec3::X),
|
||||||
position: vec3(
|
vertex(self.outer_radius, outer_scale * y, Vec3::X),
|
||||||
c * self.outer_radius,
|
]);
|
||||||
s * self.outer_radius,
|
|
||||||
-self.base_thickness,
|
|
||||||
),
|
|
||||||
color,
|
|
||||||
normal: vec3(c, s, 0.0),
|
|
||||||
});
|
|
||||||
mesh.vertices.push(Vertex {
|
|
||||||
position: vec3(
|
|
||||||
c * self.outer_radius,
|
|
||||||
s * self.outer_radius,
|
|
||||||
outer_scale * y,
|
|
||||||
),
|
|
||||||
color,
|
|
||||||
normal: vec3(c, s, 0.0),
|
|
||||||
});
|
|
||||||
|
|
||||||
let base1 = 8 * k;
|
let base1 = 8 * k;
|
||||||
let base2 = 8 * ((k + 1) % sides);
|
let base2 = 8 * ((k + 1) % sides);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user