scena 1.1.0

A Rust-native scene-graph renderer with typed scene state, glTF assets, and explicit prepare/render lifecycles.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use crate::geometry::GeometryVertex;
use crate::scene::{Transform, Vec3};

use super::transforms::{transform_normal, transform_position};

#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct TangentFrame {
    pub(super) tangent: Vec3,
    pub(super) handedness: f32,
}

/// Generate per-vertex tangent frames for a glTF mesh using the canonical
/// MikkTSpace algorithm. Phase 1E plan-line-856 closure: production
/// arbitrary-glTF tangent generation now goes through the same algorithm
/// the parity oracle uses, so authored normal maps from any glTF source
/// resolve to the tangent basis MikkTSpace defines.
pub(super) fn accumulate_vertex_tangents(
    vertices: &[GeometryVertex],
    indices: &[u32],
    tex_coords0: &[[f32; 2]],
    transform: Transform,
    origin_shift: Vec3,
) -> Vec<TangentFrame> {
    let face_count = indices.len() / 3;
    let mut adapter = MikktspaceAdapter {
        positions: vertices
            .iter()
            .map(|vertex| transform_position(vertex.position, transform, origin_shift))
            .collect(),
        normals: vertices
            .iter()
            .map(|vertex| transform_normal(vertex.normal, transform))
            .collect(),
        tex_coords0,
        indices,
        output: vec![
            TangentFrame {
                tangent: Vec3::new(1.0, 0.0, 0.0),
                handedness: 1.0,
            };
            vertices.len()
        ],
        face_count,
    };
    if face_count > 0 {
        let result = bevy_mikktspace::generate_tangents(&mut adapter);
        debug_assert!(
            result.is_ok(),
            "bevy_mikktspace exposes an uninhabited tangent-generation error"
        );
    }
    // Re-orthogonalize against the per-vertex normal so a downstream
    // tangent-space basis stays orthonormal even when the geometry's
    // authored normals do not exactly match MikkTSpace's per-face normal
    // averaging. Authored-tangent path applies the same step; preserving
    // it for generated tangents keeps both paths under the same
    // contract.
    for (frame, vertex) in adapter.output.iter_mut().zip(vertices) {
        let normal = transform_normal(vertex.normal, transform);
        let orthogonal = subtract_vec3(
            frame.tangent,
            scale_vec3(normal, dot_vec3(frame.tangent, normal)),
        );
        frame.tangent = normalize_or(orthogonal, fallback_tangent(normal));
    }
    adapter.output
}

struct MikktspaceAdapter<'a> {
    positions: Vec<Vec3>,
    normals: Vec<Vec3>,
    tex_coords0: &'a [[f32; 2]],
    indices: &'a [u32],
    output: Vec<TangentFrame>,
    face_count: usize,
}

impl<'a> MikktspaceAdapter<'a> {
    fn vertex_index(&self, face: usize, vert: usize) -> usize {
        self.indices[face * 3 + vert] as usize
    }
}

impl<'a> bevy_mikktspace::Geometry for MikktspaceAdapter<'a> {
    fn num_faces(&self) -> usize {
        self.face_count
    }

    fn num_vertices_of_face(&self, _face: usize) -> usize {
        3
    }

    fn position(&self, face: usize, vert: usize) -> [f32; 3] {
        let position = self.positions[self.vertex_index(face, vert)];
        [position.x, position.y, position.z]
    }

    fn normal(&self, face: usize, vert: usize) -> [f32; 3] {
        let normal = self.normals[self.vertex_index(face, vert)];
        [normal.x, normal.y, normal.z]
    }

    fn tex_coord(&self, face: usize, vert: usize) -> [f32; 2] {
        self.tex_coords0[self.vertex_index(face, vert)]
    }

    fn set_tangent(
        &mut self,
        tangent_space: Option<bevy_mikktspace::TangentSpace>,
        face: usize,
        vert: usize,
    ) {
        let Some(tangent_space) = tangent_space else {
            return;
        };
        let tangent = tangent_space.tangent_encoded();
        let index = self.vertex_index(face, vert);
        self.output[index] = TangentFrame {
            tangent: Vec3::new(tangent[0], tangent[1], tangent[2]),
            handedness: if tangent[3] < 0.0 { -1.0 } else { 1.0 },
        };
    }
}

pub(super) fn authored_vertex_tangents(
    tangents: Option<&[[f32; 4]]>,
    vertices: &[GeometryVertex],
    transform: Transform,
) -> Option<Vec<TangentFrame>> {
    let tangents = tangents?;
    Some(
        vertices
            .iter()
            .zip(tangents.iter().copied())
            .map(|(vertex, tangent)| {
                let normal = transform_normal(vertex.normal, transform);
                let transformed =
                    transform_normal(Vec3::new(tangent[0], tangent[1], tangent[2]), transform);
                let orthogonal = subtract_vec3(
                    transformed,
                    scale_vec3(normal, dot_vec3(transformed, normal)),
                );
                TangentFrame {
                    tangent: normalize_or(orthogonal, fallback_tangent(normal)),
                    handedness: if tangent[3] < 0.0 { -1.0 } else { 1.0 },
                }
            })
            .collect(),
    )
}

#[cfg(test)]
fn triangle_tangent(
    position_a: Vec3,
    position_b: Vec3,
    position_c: Vec3,
    uv_a: [f32; 2],
    uv_b: [f32; 2],
    uv_c: [f32; 2],
    normal: Vec3,
) -> Vec3 {
    let raw = raw_triangle_tangent_frame(position_a, position_b, position_c, uv_a, uv_b, uv_c)
        .map(|(tangent, _)| tangent)
        .unwrap_or_else(|| fallback_tangent(normal));
    let orthogonal = subtract_vec3(raw, scale_vec3(normal, dot_vec3(raw, normal)));
    normalize_or(orthogonal, fallback_tangent(normal))
}

#[cfg(test)]
fn raw_triangle_tangent_frame(
    position_a: Vec3,
    position_b: Vec3,
    position_c: Vec3,
    uv_a: [f32; 2],
    uv_b: [f32; 2],
    uv_c: [f32; 2],
) -> Option<(Vec3, Vec3)> {
    let edge_ab = subtract_vec3(position_b, position_a);
    let edge_ac = subtract_vec3(position_c, position_a);
    let delta_uv_ab = [uv_b[0] - uv_a[0], uv_b[1] - uv_a[1]];
    let delta_uv_ac = [uv_c[0] - uv_a[0], uv_c[1] - uv_a[1]];
    let determinant = delta_uv_ab[0] * delta_uv_ac[1] - delta_uv_ac[0] * delta_uv_ab[1];
    if determinant.abs() <= f32::EPSILON || !determinant.is_finite() {
        return None;
    }
    let inverse = determinant.recip();
    let tangent = scale_vec3(
        subtract_vec3(
            scale_vec3(edge_ab, delta_uv_ac[1]),
            scale_vec3(edge_ac, delta_uv_ab[1]),
        ),
        inverse,
    );
    let bitangent = scale_vec3(
        subtract_vec3(
            scale_vec3(edge_ac, delta_uv_ab[0]),
            scale_vec3(edge_ab, delta_uv_ac[0]),
        ),
        inverse,
    );
    Some((tangent, bitangent))
}

fn fallback_tangent(normal: Vec3) -> Vec3 {
    let axis = if normal.x.abs() < 0.9 {
        Vec3::new(1.0, 0.0, 0.0)
    } else {
        Vec3::new(0.0, 1.0, 0.0)
    };
    normalize_or(cross_vec3(axis, normal), Vec3::new(1.0, 0.0, 0.0))
}

fn subtract_vec3(left: Vec3, right: Vec3) -> Vec3 {
    Vec3::new(left.x - right.x, left.y - right.y, left.z - right.z)
}

fn scale_vec3(value: Vec3, scale: f32) -> Vec3 {
    Vec3::new(value.x * scale, value.y * scale, value.z * scale)
}

fn dot_vec3(left: Vec3, right: Vec3) -> f32 {
    left.x * right.x + left.y * right.y + left.z * right.z
}

fn cross_vec3(left: Vec3, right: Vec3) -> Vec3 {
    Vec3::new(
        left.y * right.z - left.z * right.y,
        left.z * right.x - left.x * right.z,
        left.x * right.y - left.y * right.x,
    )
}

fn normalize_or(value: Vec3, fallback: Vec3) -> Vec3 {
    let length = dot_vec3(value, value).sqrt();
    if length <= f32::EPSILON || !length.is_finite() {
        fallback
    } else {
        scale_vec3(value, length.recip())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn generated_triangle_tangent_follows_texcoord_u_axis() {
        let tangent = triangle_tangent(
            Vec3::new(0.0, 0.0, 0.0),
            Vec3::new(0.0, 1.0, 0.0),
            Vec3::new(1.0, 0.0, 0.0),
            [0.0, 0.0],
            [1.0, 0.0],
            [0.0, 1.0],
            Vec3::new(0.0, 0.0, 1.0),
        );

        assert_vec3_near(tangent, Vec3::new(0.0, 1.0, 0.0));
        assert!(
            dot_vec3(tangent, Vec3::new(0.0, 0.0, 1.0)).abs() < 0.0001,
            "generated tangent must stay orthogonal to the geometric normal"
        );
    }

    #[test]
    fn generated_triangle_tangent_falls_back_for_degenerate_uvs() {
        let normal = Vec3::new(0.0, 0.0, 1.0);
        let tangent = triangle_tangent(
            Vec3::new(0.0, 0.0, 0.0),
            Vec3::new(1.0, 0.0, 0.0),
            Vec3::new(0.0, 1.0, 0.0),
            [0.5, 0.5],
            [0.5, 0.5],
            [0.5, 0.5],
            normal,
        );

        assert!(
            tangent.x.is_finite() && tangent.y.is_finite() && tangent.z.is_finite(),
            "degenerate UV fallback tangent must be finite"
        );
        assert!(
            dot_vec3(tangent, normal).abs() < 0.0001,
            "degenerate UV fallback tangent must stay orthogonal to the normal"
        );
    }

    #[test]
    fn accumulated_vertex_tangents_resolve_shared_triangle_through_mikktspace() {
        // Phase 1E plan-line-856 closure: production tangents now go
        // through the MikkTSpace algorithm instead of the prior in-tree
        // weighted-average. Vertex 0 is shared by two triangles with
        // different UV mappings (the first puts the U axis along +X, the
        // second along +Y). MikkTSpace resolves shared vertices per UV
        // island rather than averaging across face boundaries, so the
        // returned tangent matches one of the two per-face tangents
        // (specifically the +X mapping from the first face) and stays
        // orthogonal to the vertex normal. This is the canonical glTF
        // tangent-space contract.
        let vertices = [
            GeometryVertex {
                position: Vec3::new(0.0, 0.0, 0.0),
                normal: Vec3::new(0.0, 0.0, 1.0),
            },
            GeometryVertex {
                position: Vec3::new(0.0, 1.0, 0.0),
                normal: Vec3::new(0.0, 0.0, 1.0),
            },
            GeometryVertex {
                position: Vec3::new(1.0, 0.0, 0.0),
                normal: Vec3::new(0.0, 0.0, 1.0),
            },
            GeometryVertex {
                position: Vec3::new(1.0, 0.0, 0.0),
                normal: Vec3::new(0.0, 0.0, 1.0),
            },
            GeometryVertex {
                position: Vec3::new(0.0, -1.0, 0.0),
                normal: Vec3::new(0.0, 0.0, 1.0),
            },
        ];
        let tex_coords = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 0.0], [0.0, 1.0]];

        let tangents = accumulate_vertex_tangents(
            &vertices,
            &[0, 1, 2, 0, 3, 4],
            &tex_coords,
            Transform::IDENTITY,
            Vec3::ZERO,
        );

        assert_vec3_near(tangents[0].tangent, Vec3::new(1.0, 0.0, 0.0));
        assert!(
            dot_vec3(tangents[0].tangent, vertices[0].normal).abs() < 0.0001,
            "MikkTSpace tangent must stay orthogonal to the vertex normal"
        );
    }

    #[test]
    fn accumulated_vertex_tangents_preserve_mirrored_uv_handedness() {
        let vertices = [
            GeometryVertex {
                position: Vec3::new(0.0, 0.0, 0.0),
                normal: Vec3::new(0.0, 0.0, 1.0),
            },
            GeometryVertex {
                position: Vec3::new(1.0, 0.0, 0.0),
                normal: Vec3::new(0.0, 0.0, 1.0),
            },
            GeometryVertex {
                position: Vec3::new(0.0, 1.0, 0.0),
                normal: Vec3::new(0.0, 0.0, 1.0),
            },
        ];
        let mirrored_uvs = [[0.0, 0.0], [1.0, 0.0], [0.0, -1.0]];

        let tangents = accumulate_vertex_tangents(
            &vertices,
            &[0, 1, 2],
            &mirrored_uvs,
            Transform::IDENTITY,
            Vec3::ZERO,
        );

        assert_vec3_near(tangents[0].tangent, Vec3::new(1.0, 0.0, 0.0));
        assert_eq!(
            tangents[0].handedness, -1.0,
            "mirrored UV islands must flip tangent-space bitangent handedness"
        );
    }

    #[test]
    fn authored_vertex_tangents_preserve_handedness_and_orthogonalize() {
        let vertices = [GeometryVertex {
            position: Vec3::ZERO,
            normal: Vec3::new(0.0, 0.0, 1.0),
        }];
        let authored = [[1.0, 0.0, 0.5, -1.0]];

        let tangents = authored_vertex_tangents(Some(&authored), &vertices, Transform::IDENTITY)
            .expect("authored tangents are present");

        assert_vec3_near(tangents[0].tangent, Vec3::new(1.0, 0.0, 0.0));
        assert_eq!(tangents[0].handedness, -1.0);
        assert!(
            dot_vec3(tangents[0].tangent, vertices[0].normal).abs() < 0.0001,
            "authored tangent must be re-orthogonalized against the prepared normal"
        );
    }

    #[test]
    fn accumulated_vertex_tangents_generate_stable_indexed_quad_basis() {
        // Two-triangle quad with shared corners: the simplest non-trivial
        // mesh that exercises shared-vertex tangent generation. Every
        // vertex sees identical (position, normal, uv) across both
        // incident triangles, so the MikkTSpace basis must be the +U
        // axis with preserved handedness at every corner.
        let vertices = [
            GeometryVertex {
                position: Vec3::new(-1.0, -1.0, 0.0),
                normal: Vec3::new(0.0, 0.0, 1.0),
            },
            GeometryVertex {
                position: Vec3::new(1.0, -1.0, 0.0),
                normal: Vec3::new(0.0, 0.0, 1.0),
            },
            GeometryVertex {
                position: Vec3::new(1.0, 1.0, 0.0),
                normal: Vec3::new(0.0, 0.0, 1.0),
            },
            GeometryVertex {
                position: Vec3::new(-1.0, 1.0, 0.0),
                normal: Vec3::new(0.0, 0.0, 1.0),
            },
        ];
        let tex_coords = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]];
        let indices = [0u32, 1, 2, 0, 2, 3];

        let in_tree = accumulate_vertex_tangents(
            &vertices,
            &indices,
            &tex_coords,
            Transform::IDENTITY,
            Vec3::ZERO,
        );

        assert_eq!(in_tree.len(), vertices.len());
        for (vertex_index, frame) in in_tree.iter().enumerate() {
            assert_vec3_near(frame.tangent, Vec3::new(1.0, 0.0, 0.0));
            assert_eq!(
                frame.handedness, 1.0,
                "vertex {vertex_index}: indexed quad handedness must be preserved",
            );
        }
    }

    #[test]
    fn accumulated_vertex_tangents_fall_back_for_degenerate_uvs() {
        let vertices = [
            GeometryVertex {
                position: Vec3::new(0.0, 0.0, 0.0),
                normal: Vec3::new(0.0, 0.0, 1.0),
            },
            GeometryVertex {
                position: Vec3::new(1.0, 0.0, 0.0),
                normal: Vec3::new(0.0, 0.0, 1.0),
            },
            GeometryVertex {
                position: Vec3::new(0.0, 1.0, 0.0),
                normal: Vec3::new(0.0, 0.0, 1.0),
            },
        ];
        let collapsed_uvs = [[0.5, 0.5], [0.5, 0.5], [0.5, 0.5]];

        let tangents = accumulate_vertex_tangents(
            &vertices,
            &[0, 1, 2],
            &collapsed_uvs,
            Transform::IDENTITY,
            Vec3::ZERO,
        );

        for frame in tangents {
            assert!(
                frame.tangent.x.is_finite()
                    && frame.tangent.y.is_finite()
                    && frame.tangent.z.is_finite(),
                "degenerate UV fallback tangent must be finite"
            );
            assert!(
                dot_vec3(frame.tangent, vertices[0].normal).abs() < 0.0001,
                "degenerate UV fallback tangent must stay orthogonal to the normal"
            );
        }
    }

    fn assert_vec3_near(actual: Vec3, expected: Vec3) {
        assert!(
            (actual.x - expected.x).abs() < 0.0001
                && (actual.y - expected.y).abs() < 0.0001
                && (actual.z - expected.z).abs() < 0.0001,
            "expected {expected:?}, got {actual:?}"
        );
    }
}