nightshade 0.13.1

A cross-platform data-oriented game engine.
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
use crate::ecs::animation::components::{
    AnimationChannel, AnimationClip, AnimationInterpolation, AnimationProperty, AnimationSampler,
    AnimationSamplerOutput,
};
use crate::ecs::mesh::components::{Mesh, SkinData, SkinnedVertex, Vertex};
use crate::ecs::world::components::{LocalTransform, Material, Name, RenderMesh, Visibility};
use image::ImageReader;
use nalgebra_glm::{Mat4, Quat, Vec3, vec3};
use std::collections::HashMap;
use std::io::Cursor;

use super::super::components::{Prefab, PrefabComponents, PrefabNode};
use super::gltf_import::GltfSkin;

pub struct FbxLoadResult {
    pub prefabs: Vec<Prefab>,
    pub meshes: HashMap<String, Mesh>,
    pub materials: Vec<Material>,
    pub textures: HashMap<String, (Vec<u8>, u32, u32)>,
    pub animations: Vec<AnimationClip>,
    pub skins: Vec<GltfSkin>,
    pub node_to_skin: HashMap<usize, usize>,
    pub node_count: usize,
}

#[derive(Debug)]
pub struct FbxError(String);

impl std::fmt::Display for FbxError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "FBX error: {}", self.0)
    }
}

impl std::error::Error for FbxError {}

pub fn import_fbx_from_path(
    path: &std::path::Path,
) -> Result<FbxLoadResult, Box<dyn std::error::Error>> {
    let opts = ufbx::LoadOpts {
        target_axes: ufbx::CoordinateAxes::right_handed_y_up(),
        target_unit_meters: 1.0,
        ..Default::default()
    };

    let path_str = path
        .to_str()
        .ok_or_else(|| FbxError("Invalid path".into()))?;
    let scene = ufbx::load_file(path_str, opts).map_err(|e| FbxError(format!("{:?}", e)))?;

    let canonical_path = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
    let namespace = canonical_path
        .to_str()
        .unwrap_or("unnamed")
        .replace('\\', "/")
        .replace('/', "::");

    process_fbx_scene(&scene, &namespace)
}

pub fn import_fbx_animations_from_path(
    path: &std::path::Path,
) -> Result<Vec<AnimationClip>, Box<dyn std::error::Error>> {
    let opts = ufbx::LoadOpts {
        target_axes: ufbx::CoordinateAxes::right_handed_y_up(),
        target_unit_meters: 1.0,
        ..Default::default()
    };

    let path_str = path
        .to_str()
        .ok_or_else(|| FbxError("Invalid path".into()))?;
    let scene = ufbx::load_file(path_str, opts).map_err(|e| FbxError(format!("{:?}", e)))?;

    convert_animations(&scene)
}

fn process_fbx_scene(
    scene: &ufbx::Scene,
    namespace: &str,
) -> Result<FbxLoadResult, Box<dyn std::error::Error>> {
    let mut meshes = HashMap::new();
    let mut materials = Vec::new();
    let mut material_id_to_index: HashMap<usize, usize> = HashMap::new();
    let mut skins = Vec::new();
    let mut node_to_skin: HashMap<usize, usize> = HashMap::new();
    let mut node_index_to_mesh_name: HashMap<usize, String> = HashMap::new();

    for (mesh_index, mesh) in scene.meshes.iter().enumerate() {
        let mesh_name = if mesh.element.name.is_empty() {
            format!("{}::Mesh_{}", namespace, mesh_index)
        } else {
            format!("{}::{}", namespace, mesh.element.name)
        };

        let skin_deformer = mesh.skin_deformers.first();

        let converted_mesh = if let Some(skin) = skin_deformer {
            convert_skinned_mesh(mesh, skin)?
        } else {
            convert_static_mesh(mesh)?
        };

        meshes.insert(mesh_name.clone(), converted_mesh);

        for node in &scene.nodes {
            if let Some(ref node_mesh) = node.mesh
                && node_mesh.element.typed_id == mesh.element.typed_id
            {
                let node_index = node.element.typed_id as usize;
                node_index_to_mesh_name.insert(node_index, mesh_name.clone());

                if let Some(skin) = skin_deformer {
                    let skin_index = skins.len();
                    let gltf_skin = convert_skin(skin);
                    skins.push(gltf_skin);
                    node_to_skin.insert(node_index, skin_index);
                }
            }
        }
    }

    let mut textures: HashMap<String, (Vec<u8>, u32, u32)> = HashMap::new();
    let mut texture_id_to_name: HashMap<usize, String> = HashMap::new();

    for (texture_index, texture) in scene.textures.iter().enumerate() {
        if !texture.content.is_empty() {
            let texture_name = if texture.element.name.is_empty() {
                format!("{}::Texture_{}", namespace, texture_index)
            } else {
                format!("{}::{}", namespace, texture.element.name)
            };

            if let Some((rgba_data, width, height)) = decode_embedded_texture(&texture.content) {
                textures.insert(texture_name.clone(), (rgba_data, width, height));
                texture_id_to_name.insert(texture.element.typed_id as usize, texture_name);
            }
        }
    }

    for (index, mat) in scene.materials.iter().enumerate() {
        let material = convert_material(mat, &texture_id_to_name);
        material_id_to_index.insert(mat.element.typed_id as usize, index);
        materials.push(material);
    }

    let animations = convert_animations(scene)?;

    let prefabs = build_prefabs(
        scene,
        &node_index_to_mesh_name,
        &node_to_skin,
        &materials,
        &material_id_to_index,
    );

    Ok(FbxLoadResult {
        prefabs,
        meshes,
        materials,
        textures,
        animations,
        skins,
        node_to_skin,
        node_count: scene.nodes.len(),
    })
}

fn convert_static_mesh(mesh: &ufbx::Mesh) -> Result<Mesh, Box<dyn std::error::Error>> {
    let mut vertices = Vec::new();
    let mut indices = Vec::new();
    let mut tri_indices_buf = Vec::new();

    for face in &mesh.faces {
        let num_tris = ufbx::triangulate_face_vec(&mut tri_indices_buf, mesh, *face);

        for tri_idx in 0..(num_tris as usize) {
            for corner in 0..3 {
                let index = tri_indices_buf[tri_idx * 3 + corner] as usize;

                let position = mesh.vertex_position[index];
                let normal = if !mesh.vertex_normal.values.is_empty() {
                    mesh.vertex_normal[index]
                } else {
                    ufbx::Vec3 {
                        x: 0.0,
                        y: 1.0,
                        z: 0.0,
                    }
                };
                let uv = if !mesh.vertex_uv.values.is_empty() {
                    mesh.vertex_uv[index]
                } else {
                    ufbx::Vec2 { x: 0.0, y: 0.0 }
                };
                let tangent = if !mesh.vertex_tangent.values.is_empty() {
                    let t = mesh.vertex_tangent[index];
                    [t.x as f32, t.y as f32, t.z as f32, 1.0]
                } else {
                    [1.0, 0.0, 0.0, 1.0]
                };

                vertices.push(Vertex {
                    position: [position.x as f32, position.y as f32, position.z as f32],
                    normal: [normal.x as f32, normal.y as f32, normal.z as f32],
                    tex_coords: [uv.x as f32, 1.0 - uv.y as f32],
                    tex_coords_1: [0.0, 0.0],
                    tangent,
                    color: [1.0, 1.0, 1.0, 1.0],
                });

                indices.push(indices.len() as u32);
            }
        }
    }

    Ok(Mesh {
        vertices,
        indices,
        bounding_volume: None,
        skin_data: None,
        morph_targets: None,
    })
}

fn convert_skinned_mesh(
    mesh: &ufbx::Mesh,
    skin: &ufbx::SkinDeformer,
) -> Result<Mesh, Box<dyn std::error::Error>> {
    let mut skinned_vertices = Vec::new();
    let mut indices = Vec::new();
    let mut tri_indices_buf = Vec::new();

    for face in &mesh.faces {
        let num_tris = ufbx::triangulate_face_vec(&mut tri_indices_buf, mesh, *face);

        for tri_idx in 0..(num_tris as usize) {
            for corner in 0..3 {
                let index = tri_indices_buf[tri_idx * 3 + corner] as usize;
                let vertex_index = mesh.vertex_indices[index] as usize;

                let position = mesh.vertex_position[index];
                let normal = if !mesh.vertex_normal.values.is_empty() {
                    mesh.vertex_normal[index]
                } else {
                    ufbx::Vec3 {
                        x: 0.0,
                        y: 1.0,
                        z: 0.0,
                    }
                };
                let uv = if !mesh.vertex_uv.values.is_empty() {
                    mesh.vertex_uv[index]
                } else {
                    ufbx::Vec2 { x: 0.0, y: 0.0 }
                };
                let tangent = if !mesh.vertex_tangent.values.is_empty() {
                    let t = mesh.vertex_tangent[index];
                    [t.x as f32, t.y as f32, t.z as f32, 1.0]
                } else {
                    [1.0, 0.0, 0.0, 1.0]
                };

                let skin_vertex = &skin.vertices[vertex_index];
                let mut joint_indices = [0u32; 4];
                let mut joint_weights = [0.0f32; 4];
                let mut total_weight = 0.0f32;

                let weight_count = (skin_vertex.num_weights as usize).min(4);
                for weight_index in 0..weight_count {
                    let weight = &skin.weights[skin_vertex.weight_begin as usize + weight_index];
                    joint_indices[weight_index] = weight.cluster_index;
                    joint_weights[weight_index] = weight.weight as f32;
                    total_weight += weight.weight as f32;
                }

                if total_weight > 0.0 {
                    for weight in &mut joint_weights {
                        *weight /= total_weight;
                    }
                }

                skinned_vertices.push(SkinnedVertex {
                    position: [position.x as f32, position.y as f32, position.z as f32],
                    normal: [normal.x as f32, normal.y as f32, normal.z as f32],
                    tex_coords: [uv.x as f32, 1.0 - uv.y as f32],
                    tex_coords_1: [0.0, 0.0],
                    tangent,
                    color: [1.0, 1.0, 1.0, 1.0],
                    joint_indices,
                    joint_weights,
                });

                indices.push(indices.len() as u32);
            }
        }
    }

    Ok(Mesh {
        vertices: Vec::new(),
        indices,
        bounding_volume: None,
        skin_data: Some(SkinData {
            skinned_vertices,
            skin_index: None,
        }),
        morph_targets: None,
    })
}

fn convert_skin(skin: &ufbx::SkinDeformer) -> GltfSkin {
    let mut joints = Vec::new();
    let mut inverse_bind_matrices = Vec::new();

    for cluster in &skin.clusters {
        if let Some(ref bone_node) = cluster.bone_node {
            let node_index = bone_node.element.typed_id as usize;
            joints.push(node_index);

            let ibm = &cluster.geometry_to_bone;
            inverse_bind_matrices.push(ufbx_matrix_to_mat4(ibm));
        }
    }

    GltfSkin {
        name: if skin.element.name.is_empty() {
            None
        } else {
            Some(skin.element.name.to_string())
        },
        joints,
        inverse_bind_matrices,
    }
}

fn decode_embedded_texture(content: &ufbx::Blob) -> Option<(Vec<u8>, u32, u32)> {
    let bytes: &[u8] = content.as_ref();
    let cursor = Cursor::new(bytes);
    let reader = ImageReader::new(cursor).with_guessed_format().ok()?;
    let image = reader.decode().ok()?;
    let rgba = image.to_rgba8();
    let width = rgba.width();
    let height = rgba.height();
    Some((rgba.into_raw(), width, height))
}

fn get_texture_name(
    map: &ufbx::MaterialMap,
    texture_id_to_name: &HashMap<usize, String>,
) -> Option<String> {
    map.texture.as_ref().and_then(|tex| {
        texture_id_to_name
            .get(&(tex.element.typed_id as usize))
            .cloned()
    })
}

fn convert_material(mat: &ufbx::Material, texture_id_to_name: &HashMap<usize, String>) -> Material {
    let pbr_color = mat.pbr.base_color.value_vec4;
    let diffuse_color = mat.fbx.diffuse_color.value_vec4;

    let base_color = if pbr_color.x > 0.0 || pbr_color.y > 0.0 || pbr_color.z > 0.0 {
        [
            pbr_color.x as f32,
            pbr_color.y as f32,
            pbr_color.z as f32,
            pbr_color.w as f32,
        ]
    } else if diffuse_color.x > 0.0 || diffuse_color.y > 0.0 || diffuse_color.z > 0.0 {
        [
            diffuse_color.x as f32,
            diffuse_color.y as f32,
            diffuse_color.z as f32,
            1.0,
        ]
    } else {
        [1.0, 1.0, 1.0, 1.0]
    };

    let base_texture = get_texture_name(&mat.pbr.base_color, texture_id_to_name)
        .or_else(|| get_texture_name(&mat.fbx.diffuse_color, texture_id_to_name));

    let normal_texture = get_texture_name(&mat.pbr.normal_map, texture_id_to_name)
        .or_else(|| get_texture_name(&mat.fbx.normal_map, texture_id_to_name))
        .or_else(|| get_texture_name(&mat.fbx.bump, texture_id_to_name));

    let emissive_texture = get_texture_name(&mat.pbr.emission_color, texture_id_to_name)
        .or_else(|| get_texture_name(&mat.pbr.emission_factor, texture_id_to_name))
        .or_else(|| get_texture_name(&mat.fbx.emission_color, texture_id_to_name));

    let metallic_roughness_texture = get_texture_name(&mat.pbr.roughness, texture_id_to_name)
        .or_else(|| get_texture_name(&mat.pbr.metalness, texture_id_to_name));

    let occlusion_texture = get_texture_name(&mat.pbr.ambient_occlusion, texture_id_to_name);

    let pbr_emissive = mat.pbr.emission_color.value_vec4;
    let fbx_emissive = mat.fbx.emission_color.value_vec4;
    let emissive_factor = if pbr_emissive.x > 0.0 || pbr_emissive.y > 0.0 || pbr_emissive.z > 0.0 {
        [
            pbr_emissive.x as f32,
            pbr_emissive.y as f32,
            pbr_emissive.z as f32,
        ]
    } else if fbx_emissive.x > 0.0 || fbx_emissive.y > 0.0 || fbx_emissive.z > 0.0 {
        [
            fbx_emissive.x as f32,
            fbx_emissive.y as f32,
            fbx_emissive.z as f32,
        ]
    } else {
        [0.0, 0.0, 0.0]
    };

    let metallic = mat.pbr.metalness.value_vec4.x as f32;
    let roughness = if mat.pbr.roughness.value_vec4.x > 0.0 {
        mat.pbr.roughness.value_vec4.x as f32
    } else {
        let shininess = mat.fbx.specular_exponent.value_vec4.x as f32;
        if shininess > 0.0 {
            1.0 - (shininess / 100.0).min(1.0)
        } else {
            0.5
        }
    };

    Material {
        base_color,
        base_texture,
        normal_texture,
        emissive_texture,
        emissive_factor,
        metallic_roughness_texture,
        occlusion_texture,
        metallic,
        roughness,
        ..Material::default()
    }
}

fn convert_animations(
    scene: &ufbx::Scene,
) -> Result<Vec<AnimationClip>, Box<dyn std::error::Error>> {
    let mut clips = Vec::new();

    tracing::trace!(
        "Converting {} animation stacks from FBX",
        scene.anim_stacks.len()
    );

    for anim_stack in &scene.anim_stacks {
        let stack_duration = (anim_stack.time_end - anim_stack.time_begin) as f32;
        tracing::trace!(
            "  Animation stack '{}': time_begin={}, time_end={}, duration={}s",
            anim_stack.element.name,
            anim_stack.time_begin,
            anim_stack.time_end,
            stack_duration
        );

        let bake_opts = ufbx::BakeOpts {
            resample_rate: 30.0,
            ..Default::default()
        };

        let baked = ufbx::bake_anim(scene, &anim_stack.anim, bake_opts)
            .map_err(|e| FbxError(format!("{:?}", e)))?;

        tracing::trace!(
            "  Baked animation '{}': {} nodes",
            anim_stack.element.name,
            baked.nodes.len()
        );

        let mut channels = Vec::new();
        let mut max_time = 0.0f32;

        for baked_node in &baked.nodes {
            let node_index = baked_node.typed_id as usize;

            let node_name = scene
                .nodes
                .iter()
                .find(|n| n.element.typed_id == baked_node.typed_id)
                .map(|n| n.element.name.to_string());

            if !baked_node.translation_keys.is_empty() {
                let times: Vec<f32> = baked_node
                    .translation_keys
                    .iter()
                    .map(|k| k.time as f32)
                    .collect();
                let values: Vec<Vec3> = baked_node
                    .translation_keys
                    .iter()
                    .map(|k| vec3(k.value.x as f32, k.value.y as f32, k.value.z as f32))
                    .collect();

                if let Some(&last_time) = times.last() {
                    max_time = max_time.max(last_time);
                }

                channels.push(AnimationChannel {
                    target_node: node_index,
                    target_bone_name: node_name.clone(),
                    target_property: AnimationProperty::Translation,
                    sampler: AnimationSampler {
                        input: times,
                        output: AnimationSamplerOutput::Vec3(values),
                        interpolation: AnimationInterpolation::Linear,
                    },
                });
            }

            if !baked_node.rotation_keys.is_empty() {
                let times: Vec<f32> = baked_node
                    .rotation_keys
                    .iter()
                    .map(|k| k.time as f32)
                    .collect();
                let values: Vec<Quat> = baked_node
                    .rotation_keys
                    .iter()
                    .map(|k| {
                        Quat::new(
                            k.value.w as f32,
                            k.value.x as f32,
                            k.value.y as f32,
                            k.value.z as f32,
                        )
                    })
                    .collect();

                if let Some(&last_time) = times.last() {
                    max_time = max_time.max(last_time);
                }

                channels.push(AnimationChannel {
                    target_node: node_index,
                    target_bone_name: node_name.clone(),
                    target_property: AnimationProperty::Rotation,
                    sampler: AnimationSampler {
                        input: times,
                        output: AnimationSamplerOutput::Quat(values),
                        interpolation: AnimationInterpolation::Linear,
                    },
                });
            }

            if !baked_node.scale_keys.is_empty() {
                let times: Vec<f32> = baked_node
                    .scale_keys
                    .iter()
                    .map(|k| k.time as f32)
                    .collect();
                let values: Vec<Vec3> = baked_node
                    .scale_keys
                    .iter()
                    .map(|k| vec3(k.value.x as f32, k.value.y as f32, k.value.z as f32))
                    .collect();

                if let Some(&last_time) = times.last() {
                    max_time = max_time.max(last_time);
                }

                channels.push(AnimationChannel {
                    target_node: node_index,
                    target_bone_name: node_name,
                    target_property: AnimationProperty::Scale,
                    sampler: AnimationSampler {
                        input: times,
                        output: AnimationSamplerOutput::Vec3(values),
                        interpolation: AnimationInterpolation::Linear,
                    },
                });
            }
        }

        let final_duration = if stack_duration > max_time {
            stack_duration
        } else {
            max_time
        };

        tracing::info!(
            "  Created {} channels, baked_max_time: {}s, stack_duration: {}s, final: {}s",
            channels.len(),
            max_time,
            stack_duration,
            final_duration
        );

        if !channels.is_empty() {
            let clip_name = if anim_stack.element.name.is_empty() {
                format!("Animation_{}", clips.len())
            } else {
                anim_stack.element.name.to_string()
            };

            clips.push(AnimationClip {
                name: clip_name,
                duration: final_duration,
                channels,
            });
        }
    }

    Ok(clips)
}

fn build_prefabs(
    scene: &ufbx::Scene,
    node_index_to_mesh_name: &HashMap<usize, String>,
    node_to_skin: &HashMap<usize, usize>,
    materials: &[Material],
    material_id_to_index: &HashMap<usize, usize>,
) -> Vec<Prefab> {
    let root_nodes: Vec<_> = scene
        .nodes
        .iter()
        .filter(|n| n.parent.is_none() || n.parent.as_ref().map(|p| p.is_root).unwrap_or(false))
        .filter(|n| !n.is_root)
        .collect();

    if root_nodes.is_empty() {
        return Vec::new();
    }

    let mut prefab_root_nodes = Vec::new();
    for root_node in &root_nodes {
        let prefab_node = build_single_prefab_node(
            root_node,
            node_index_to_mesh_name,
            node_to_skin,
            materials,
            material_id_to_index,
        );
        prefab_root_nodes.push(prefab_node);
    }

    let prefab_name = if root_nodes.len() == 1 {
        let first = &root_nodes[0];
        if first.element.name.is_empty() {
            "FBX_Import".to_string()
        } else {
            first.element.name.to_string()
        }
    } else {
        "FBX_Import".to_string()
    };

    vec![Prefab {
        name: prefab_name,
        root_nodes: prefab_root_nodes,
    }]
}

fn build_single_prefab_node(
    node: &ufbx::Node,
    node_index_to_mesh_name: &HashMap<usize, String>,
    node_to_skin: &HashMap<usize, usize>,
    materials: &[Material],
    material_id_to_index: &HashMap<usize, usize>,
) -> PrefabNode {
    let node_index = node.element.typed_id as usize;

    let local_transform = LocalTransform {
        translation: vec3(
            node.local_transform.translation.x as f32,
            node.local_transform.translation.y as f32,
            node.local_transform.translation.z as f32,
        ),
        rotation: Quat::new(
            node.local_transform.rotation.w as f32,
            node.local_transform.rotation.x as f32,
            node.local_transform.rotation.y as f32,
            node.local_transform.rotation.z as f32,
        ),
        scale: vec3(
            node.local_transform.scale.x as f32,
            node.local_transform.scale.y as f32,
            node.local_transform.scale.z as f32,
        ),
    };

    let mut components = PrefabComponents::default();

    if !node.element.name.is_empty() {
        components.name = Some(Name(node.element.name.to_string()));
    }

    if let Some(mesh_name) = node_index_to_mesh_name.get(&node_index) {
        components.render_mesh = Some(RenderMesh::new(mesh_name.clone()));
        components.visibility = Some(Visibility { visible: true });

        if let Some(&skin_index) = node_to_skin.get(&node_index) {
            components.skin_index = Some(skin_index);
        }

        if let Some(ref mesh) = node.mesh
            && let Some(mat) = mesh.materials.first()
            && let Some(&mat_index) = material_id_to_index.get(&(mat.element.typed_id as usize))
            && mat_index < materials.len()
        {
            components.material = Some(materials[mat_index].clone());
        }
    }

    let mut children = Vec::new();
    for child in &node.children {
        let child_node = build_single_prefab_node(
            child,
            node_index_to_mesh_name,
            node_to_skin,
            materials,
            material_id_to_index,
        );
        children.push(child_node);
    }

    PrefabNode {
        local_transform,
        components,
        children,
        node_index: Some(node_index),
    }
}

fn ufbx_matrix_to_mat4(m: &ufbx::Matrix) -> Mat4 {
    Mat4::new(
        m.m00 as f32,
        m.m01 as f32,
        m.m02 as f32,
        m.m03 as f32,
        m.m10 as f32,
        m.m11 as f32,
        m.m12 as f32,
        m.m13 as f32,
        m.m20 as f32,
        m.m21 as f32,
        m.m22 as f32,
        m.m23 as f32,
        0.0,
        0.0,
        0.0,
        1.0,
    )
}