Skip to content

Commit 6227164

Browse files
committed
Merge branch 'master' of https://github.com/scpcbredux/rmesh
2 parents 4b76df0 + e3a59f3 commit 6227164

File tree

2 files changed

+178
-33
lines changed

2 files changed

+178
-33
lines changed

bevy_rmesh/examples/view.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ fn setup(
1616
// cube
1717
commands.spawn(PbrBundle {
1818
mesh: asset_server.load("cube.rmesh#Mesh0"),
19-
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
19+
material: materials.add(StandardMaterial {
20+
base_color: Color::rgb(0.8, 0.7, 0.6),
21+
..default()
22+
}),
2023
transform: Transform::from_xyz(0.0, 0.5, 0.0),
2124
..default()
2225
});

rmesh/src/lib.rs

+174-32
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,180 @@ pub struct TriggerBox {
131131
pub name: FixedLengthString,
132132
}
133133

134+
impl ExtMesh for SimpleMesh {
135+
fn bounding_box(&self) -> Bounds {
136+
let mut min_x = f32::INFINITY;
137+
let mut min_y = f32::INFINITY;
138+
let mut min_z = f32::INFINITY;
139+
let mut max_x = f32::NEG_INFINITY;
140+
let mut max_y = f32::NEG_INFINITY;
141+
let mut max_z = f32::NEG_INFINITY;
142+
143+
for vertex in &self.vertices {
144+
let [x, y, z] = *vertex;
145+
146+
// Update min values
147+
min_x = min_x.min(x);
148+
min_y = min_y.min(y);
149+
min_z = min_z.min(z);
150+
151+
// Update max values
152+
max_x = max_x.max(x);
153+
max_y = max_y.max(y);
154+
max_z = max_z.max(z);
155+
}
156+
157+
let min_point = [min_x, min_y, min_z];
158+
let max_point = [max_x, max_y, max_z];
159+
Bounds::new(min_point, max_point)
160+
}
161+
162+
fn calculate_normals(&self) -> Vec<[f32; 3]> {
163+
// Initialize vertex normals with zero vectors
164+
let mut vertex_normals = vec![[0.0, 0.0, 0.0]; self.vertices.len()];
165+
166+
// Calculate face normals and accumulate them to vertex normals
167+
for triangle in &self.triangles {
168+
let vertex0 = self.vertices[triangle[0] as usize];
169+
let vertex1 = self.vertices[triangle[1] as usize];
170+
let vertex2 = self.vertices[triangle[2] as usize];
171+
172+
let edge1 = [
173+
vertex1[0] - vertex0[0],
174+
vertex1[1] - vertex0[1],
175+
vertex1[2] - vertex0[2],
176+
];
177+
let edge2 = [
178+
vertex2[0] - vertex0[0],
179+
vertex2[1] - vertex0[1],
180+
vertex2[2] - vertex0[2],
181+
];
182+
183+
let normal = [
184+
edge1[1] * edge2[2] - edge1[2] * edge2[1],
185+
edge1[2] * edge2[0] - edge1[0] * edge2[2],
186+
edge1[0] * edge2[1] - edge1[1] * edge2[0],
187+
];
188+
189+
// Accumulate face normal to the vertices of the triangle
190+
for i in 0..3 {
191+
let vertex_index = triangle[i] as usize;
192+
vertex_normals[vertex_index][0] += normal[0];
193+
vertex_normals[vertex_index][1] += normal[1];
194+
vertex_normals[vertex_index][2] += normal[2];
195+
}
196+
}
197+
198+
// Normalize vertex normals
199+
for normal in &mut vertex_normals {
200+
let length = (normal[0].powi(2) + normal[1].powi(2) + normal[2].powi(2)).sqrt();
201+
if length != 0.0 {
202+
normal[0] /= length;
203+
normal[1] /= length;
204+
normal[2] /= length;
205+
}
206+
}
207+
208+
vertex_normals
209+
}
210+
}
211+
212+
impl ExtMesh for ComplexMesh {
213+
fn bounding_box(&self) -> Bounds {
214+
let mut min_x = f32::INFINITY;
215+
let mut min_y = f32::INFINITY;
216+
let mut min_z = f32::INFINITY;
217+
let mut max_x = f32::NEG_INFINITY;
218+
let mut max_y = f32::NEG_INFINITY;
219+
let mut max_z = f32::NEG_INFINITY;
220+
221+
for vertex in &self.vertices {
222+
let [x, y, z] = vertex.position;
223+
224+
// Update min values
225+
min_x = min_x.min(x);
226+
min_y = min_y.min(y);
227+
min_z = min_z.min(z);
228+
229+
// Update max values
230+
max_x = max_x.max(x);
231+
max_y = max_y.max(y);
232+
max_z = max_z.max(z);
233+
}
234+
235+
let min_point = [min_x, min_y, min_z];
236+
let max_point = [max_x, max_y, max_z];
237+
Bounds::new(min_point, max_point)
238+
}
239+
240+
fn calculate_normals(&self) -> Vec<[f32; 3]> {
241+
// Initialize vertex normals with zero vectors
242+
let mut vertex_normals = vec![[0.0, 0.0, 0.0]; self.vertices.len()];
243+
244+
// Calculate face normals and accumulate them to vertex normals
245+
for triangle in &self.triangles {
246+
let vertex0 = self.vertices[triangle[0] as usize].position;
247+
let vertex1 = self.vertices[triangle[1] as usize].position;
248+
let vertex2 = self.vertices[triangle[2] as usize].position;
249+
250+
let edge1 = [
251+
vertex1[0] - vertex0[0],
252+
vertex1[1] - vertex0[1],
253+
vertex1[2] - vertex0[2],
254+
];
255+
let edge2 = [
256+
vertex2[0] - vertex0[0],
257+
vertex2[1] - vertex0[1],
258+
vertex2[2] - vertex0[2],
259+
];
260+
261+
let normal = [
262+
edge1[1] * edge2[2] - edge1[2] * edge2[1],
263+
edge1[2] * edge2[0] - edge1[0] * edge2[2],
264+
edge1[0] * edge2[1] - edge1[1] * edge2[0],
265+
];
266+
267+
// Accumulate face normal to the vertices of the triangle
268+
for i in 0..3 {
269+
let vertex_index = triangle[i] as usize;
270+
vertex_normals[vertex_index][0] += normal[0];
271+
vertex_normals[vertex_index][1] += normal[1];
272+
vertex_normals[vertex_index][2] += normal[2];
273+
}
274+
}
275+
276+
// Normalize vertex normals
277+
for normal in &mut vertex_normals {
278+
let length = (normal[0].powi(2) + normal[1].powi(2) + normal[2].powi(2)).sqrt();
279+
if length != 0.0 {
280+
normal[0] /= length;
281+
normal[1] /= length;
282+
normal[2] /= length;
283+
}
284+
}
285+
286+
vertex_normals
287+
}
288+
}
289+
290+
pub trait ExtMesh {
291+
/// Used for aabb calc
292+
fn bounding_box(&self) -> Bounds;
293+
/// Calculate normals for the vertices based on the triangle faces.
294+
fn calculate_normals(&self) -> Vec<[f32; 3]>;
295+
}
296+
297+
pub struct Bounds {
298+
pub min: [f32; 3],
299+
pub max: [f32; 3],
300+
}
301+
302+
impl Bounds {
303+
pub fn new(min: [f32; 3], max: [f32; 3]) -> Self {
304+
Self { min, max }
305+
}
306+
}
307+
134308
#[binrw]
135309
#[derive(Debug)]
136310
pub struct EntityData {
@@ -173,35 +347,3 @@ pub fn write_rmesh(header: &Header) -> Result<Vec<u8>, RMeshError> {
173347

174348
Ok(bytes)
175349
}
176-
177-
/// Used for aabb calc
178-
pub fn calculate_bounds(vertices: &Vec<Vertex>) -> Option<([f32; 3], [f32; 3])> {
179-
if vertices.is_empty() {
180-
return None;
181-
}
182-
183-
let mut min_x = f32::INFINITY;
184-
let mut min_y = f32::INFINITY;
185-
let mut min_z = f32::INFINITY;
186-
let mut max_x = f32::NEG_INFINITY;
187-
let mut max_y = f32::NEG_INFINITY;
188-
let mut max_z = f32::NEG_INFINITY;
189-
190-
for vertex in vertices {
191-
let [x, y, z] = vertex.position;
192-
193-
// Update min values
194-
min_x = min_x.min(x);
195-
min_y = min_y.min(y);
196-
min_z = min_z.min(z);
197-
198-
// Update max values
199-
max_x = max_x.max(x);
200-
max_y = max_y.max(y);
201-
max_z = max_z.max(z);
202-
}
203-
204-
let min_point = [min_x, min_y, min_z];
205-
let max_point = [max_x, max_y, max_z];
206-
Some((min_point, max_point))
207-
}

0 commit comments

Comments
 (0)