@@ -131,7 +131,7 @@ pub struct TriggerBox {
131
131
pub name : FixedLengthString ,
132
132
}
133
133
134
- impl CalcBoundBox for SimpleMesh {
134
+ impl ExtMesh for SimpleMesh {
135
135
fn bounding_box ( & self ) -> Bounds {
136
136
let mut min_x = f32:: INFINITY ;
137
137
let mut min_y = f32:: INFINITY ;
@@ -158,9 +158,58 @@ impl CalcBoundBox for SimpleMesh {
158
158
let max_point = [ max_x, max_y, max_z] ;
159
159
Bounds :: new ( min_point, max_point)
160
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
+ }
161
210
}
162
211
163
- impl CalcBoundBox for ComplexMesh {
212
+ impl ExtMesh for ComplexMesh {
164
213
fn bounding_box ( & self ) -> Bounds {
165
214
let mut min_x = f32:: INFINITY ;
166
215
let mut min_y = f32:: INFINITY ;
@@ -187,11 +236,62 @@ impl CalcBoundBox for ComplexMesh {
187
236
let max_point = [ max_x, max_y, max_z] ;
188
237
Bounds :: new ( min_point, max_point)
189
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
+ }
190
288
}
191
289
192
- pub trait CalcBoundBox {
290
+ pub trait ExtMesh {
193
291
/// Used for aabb calc
194
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 ] > ;
195
295
}
196
296
197
297
pub struct Bounds {
0 commit comments