1#![allow(dead_code)]
3
4use std::collections::HashMap;
5
6use bevy::prelude::*;
7use wde_animation::AnimationClip;
8
9use crate::material::GltfMaterial;
10
11#[derive(Debug, Clone)]
13pub struct GltfModel {
14 pub path: String,
15 pub filename: String,
16 pub buffers: Vec<GltfBuffer>,
17 pub meshes: Vec<GltfMesh>,
18 pub materials: Vec<GltfMaterial>,
19 pub properties: HashMap<String, (Vec3, Vec3)>,
22 pub bones: Vec<GltfBone>,
24 pub animations: Vec<AnimationClip>,
26 pub lights: Vec<GltfLight>
28}
29
30#[derive(Debug, Clone)]
32pub struct GltfLight {
33 pub name: String,
34 pub kind: GltfLightKind,
35 pub transform: Transform,
37 pub color: Vec3,
39 pub intensity: f32,
40 pub range: f32
42}
43
44#[derive(Debug, Clone, Copy, PartialEq)]
45pub enum GltfLightKind {
46 Point,
47 Spot {
49 inner_cone: f32,
50 outer_cone: f32
51 }
52}
53
54#[derive(Debug, Clone)]
56pub struct GltfBone {
57 pub name: String,
58 pub parent: Option<usize>,
60 pub rest: Transform
62}
63
64#[derive(Clone)]
66pub struct GltfBuffer {
67 pub data: Vec<u8>,
68 pub slices: Vec<BufferSliceData>
69}
70impl std::fmt::Debug for GltfBuffer {
71 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72 f.debug_struct("GltfBuffer")
73 .field("data_length", &self.data.len())
74 .field("slices", &self.slices)
75 .finish()
76 }
77}
78
79#[derive(Debug, Clone)]
81pub struct BufferSliceData {
82 pub byte_offset: usize
84}
85
86#[derive(Debug, Clone)]
88pub struct GltfMesh {
89 pub primitives: Vec<MeshPrimitive>
90}
91
92#[derive(Debug, Clone)]
94pub struct MeshPrimitive {
95 pub name: String,
97 pub vertex_indexed: Option<AccessorData>,
99 pub vertex_attributes: Vec<(String, AccessorData)>,
101 pub material_id: Option<u32>,
103 pub translation: [f32; 3],
105 pub rotation: [f32; 4],
107 pub scale: [f32; 3],
109 pub condition: Option<ModelCondition>,
111 pub bone: Option<usize>
113}
114
115#[derive(Debug, Clone, PartialEq, Eq)]
117pub struct ModelCondition {
118 pub anchor: String,
120 pub connected: bool
122}
123impl ModelCondition {
124 pub fn from_node_name(name: &str) -> Option<Self> {
126 let rest = name.strip_prefix("%V%")?;
127 let rest = &rest[..=rest.rfind(['+', '-'])?];
129 let (anchor, sign) = rest.split_at(rest.len() - 1);
130 Some(Self {
131 anchor: anchor.to_string(),
132 connected: sign == "+"
133 })
134 }
135}
136
137#[derive(Debug, Clone)]
139pub struct AccessorData {
140 pub buffer_view_index: i64,
142 pub byte_offset: usize,
144 pub component_type: GltfAccessorComponentType,
146 pub count: usize,
148 pub accessor_type: String,
150 pub min: Option<Vec<f32>>,
152 pub max: Option<Vec<f32>>
154}
155
156#[derive(Debug, Clone, Copy)]
158pub enum GltfAccessorComponentType {
159 Byte = 5120,
160 UnsignedByte = 5121,
161 Short = 5122,
162 UnsignedShort = 5123,
163 UnsignedInt = 5125,
164 Float = 5126
165}
166impl GltfAccessorComponentType {
167 pub fn from_i64(value: i64) -> Option<Self> {
169 match value {
170 5120 => Some(GltfAccessorComponentType::Byte),
171 5121 => Some(GltfAccessorComponentType::UnsignedByte),
172 5122 => Some(GltfAccessorComponentType::Short),
173 5123 => Some(GltfAccessorComponentType::UnsignedShort),
174 5125 => Some(GltfAccessorComponentType::UnsignedInt),
175 5126 => Some(GltfAccessorComponentType::Float),
176 _ => unreachable!("Unsupported component type: {}", value)
177 }
178 }
179}