Skip to main content

euv_core/game/engine3d/asset/
struct.rs

1use super::*;
2
3/// Decoded 64-byte `.euv3d` package header metadata.
4#[derive(Clone, Copy, Data, Debug, PartialEq)]
5pub struct Euv3dHeader {
6    /// Format version (`EUV3D_FORMAT_VERSION`).
7    #[get(type(copy))]
8    pub(crate) version: u32,
9    /// Bitmask describing optional pre-baked sections (`BVH4`, `Meshlets`, `LODs`).
10    #[get(type(copy))]
11    pub(crate) flags: u32,
12    /// Number of entries stored in the section directory.
13    #[get(type(copy))]
14    pub(crate) section_count: u32,
15    /// Byte offset of the section directory table (typically `64`).
16    #[get(type(copy))]
17    pub(crate) table_offset: u64,
18    /// CRC32 checksum computed over payload bytes `[64..total_bytes]`.
19    #[get(type(copy))]
20    pub(crate) content_crc32: u32,
21    /// Total file size in bytes.
22    #[get(type(copy))]
23    pub(crate) total_bytes: u64,
24    /// 64-bit FNV-1a content digest for deterministic asset cache keying.
25    #[get(type(copy))]
26    pub(crate) digest_fnv1a: u64,
27}
28
29/// Single 32-byte section directory entry inside an `.euv3d` package.
30#[derive(Clone, Copy, Data, Debug, Eq, Hash, PartialEq)]
31pub struct Euv3dSectionEntry {
32    /// Semantic payload type of this section.
33    #[get(type(copy))]
34    pub(crate) kind: Euv3dSectionKind,
35    /// 64-byte-aligned starting byte offset inside the `.euv3d` buffer.
36    #[get(type(copy))]
37    pub(crate) offset: u64,
38    /// Exact payload length in bytes (excluding trailing alignment padding).
39    #[get(type(copy))]
40    pub(crate) byte_length: u64,
41    /// Number of logical elements stored inside this section.
42    #[get(type(copy))]
43    pub(crate) element_count: u32,
44    /// Section-specific bit flags.
45    #[get(type(copy))]
46    pub(crate) flags: u32,
47}
48
49/// Configuration controlling Garland-Heckbert QEM LOD generation, spatial meshlets, and BVH4 baking.
50#[derive(Clone, Copy, Data, Debug, PartialEq)]
51pub struct Euv3dBakeConfig {
52    /// Target number of LOD levels (`1..=4`, default `4`).
53    #[get(type(copy))]
54    pub(crate) lod_levels: usize,
55    /// Whether to cluster triangles into spatially coherent vertex-cache-friendly meshlets.
56    #[get(type(copy))]
57    pub(crate) build_meshlets: bool,
58    /// Whether to pre-bake per-mesh SIMD BVH4 acceleration structures into the package.
59    #[get(type(copy))]
60    pub(crate) bake_bvh4: bool,
61    /// Whether to preserve/generate full texture mipmap chains before serializing.
62    #[get(type(copy))]
63    pub(crate) generate_mipmaps: bool,
64}
65
66/// Zero-copy validated view over a 64-byte-aligned `.euv3d` binary buffer.
67#[derive(Clone, Data, Debug)]
68pub struct Euv3dPackageView<'a> {
69    /// Borrowed backing `.euv3d` byte slice.
70    pub(crate) raw_bytes: &'a [u8],
71    /// Parsed and CRC-verified 64-byte file header.
72    #[get(type(copy))]
73    pub(crate) header: Euv3dHeader,
74    /// Validated 64-byte-aligned section directory entries.
75    pub(crate) sections: Vec<Euv3dSectionEntry>,
76}
77
78/// Decoded `.euv3d` asset bundle containing the full scene, QEM LOD chains, and zero-rebuild BVH4 trees.
79#[derive(Clone, Data, Debug, Default)]
80pub struct BakedAssetBundle3D {
81    /// Restored Blender/glTF scene with hierarchy, materials, textures, rigs, animations, cameras, and lights.
82    pub(crate) scene: ImportedScene3D,
83    /// Pre-baked Garland-Heckbert QEM LOD chains aligned 1:1 with `scene.meshes`.
84    pub(crate) lod_chains: Vec<MeshLodChain>,
85    /// Pre-baked local-space SIMD BVH4 acceleration trees aligned 1:1 with `scene.meshes` (zero SAH build on load).
86    pub(crate) local_bvhs: Vec<Bvh3D>,
87    /// Total number of sections stored in the source `.euv3d` archive.
88    #[get(type(copy))]
89    pub(crate) section_count: usize,
90    /// Total byte size of the source `.euv3d` archive.
91    #[get(type(copy))]
92    pub(crate) total_bytes: usize,
93    /// Deterministic 64-bit FNV-1a digest of the archive contents.
94    #[get(type(copy))]
95    pub(crate) digest_fnv1a: u64,
96}
97
98/// Stateless serializer and zero-copy loader for `.euv3d` 64-byte-aligned binary 3D asset packages.
99#[derive(Clone, Copy, Debug, Default)]
100pub struct Euv3dPackage {}
101
102/// Symmetric 4x4 error quadric matrix stored as 10 upper-triangular `f64` coefficients for QEM simplification.
103#[derive(Clone, Copy, Debug, Default)]
104pub(crate) struct QemMatrix {
105    /// Upper-triangular entries `[a00, a01, a02, a03, a11, a12, a13, a22, a23, a33]`.
106    pub(crate) coeffs: [f64; 10],
107}
108
109/// Candidate undirected edge collapse entry queued in the QEM binary min-heap.
110#[derive(Clone, Copy, Debug)]
111pub(crate) struct QemEdgeCandidate {
112    /// Quadric error cost `v^T Q v` (smaller collapses first).
113    pub(crate) cost: f64,
114    /// First vertex index.
115    pub(crate) vert_a: usize,
116    /// Second vertex index.
117    pub(crate) vert_b: usize,
118    /// Revision stamp of `vert_a` when queued (for lazy invalidation).
119    pub(crate) ver_a: u32,
120    /// Revision stamp of `vert_b` when queued (for lazy invalidation).
121    pub(crate) ver_b: u32,
122    /// Optimal contracted 3D position.
123    pub(crate) target_pos: [f64; 3],
124    /// Normalized interpolation factor in `[0.0, 1.0]` along `vert_a -> vert_b`.
125    pub(crate) interp_t: f32,
126}
127
128/// Serializable metadata record describing one baked LOD level inside the shared binary geometry buffers.
129#[derive(Clone, Debug, Deserialize, Serialize)]
130pub(crate) struct Euv3dLodRecord {
131    /// Starting vertex index inside the `Vertices` section.
132    pub(crate) first_vertex: u32,
133    /// Number of vertices in this LOD level.
134    pub(crate) vertex_count: u32,
135    /// Starting triangle index inside the `Indices` section.
136    pub(crate) first_tri: u32,
137    /// Number of triangles in this LOD level.
138    pub(crate) tri_count: u32,
139    /// Starting meshlet index inside the `Meshlets` section.
140    pub(crate) first_meshlet: u32,
141    /// Number of meshlets belonging to this LOD level.
142    pub(crate) meshlet_count: u32,
143    /// Local bounding box minimum corner.
144    pub(crate) bounds_min: [f32; 3],
145    /// Local bounding box maximum corner.
146    pub(crate) bounds_max: [f32; 3],
147}
148
149/// Serializable metadata record describing one morph target inside the `MorphDeltas` section.
150#[derive(Clone, Debug, Deserialize, Serialize)]
151pub(crate) struct Euv3dMorphRecord {
152    /// Shape key / morph target name.
153    pub(crate) name: String,
154    /// Starting `[f32; 3]` element index for position deltas.
155    pub(crate) first_pos: u32,
156    /// Count of position deltas.
157    pub(crate) pos_count: u32,
158    /// Starting `[f32; 3]` element index for normal deltas.
159    pub(crate) first_norm: u32,
160    /// Count of normal deltas.
161    pub(crate) norm_count: u32,
162    /// Starting `[f32; 3]` element index for tangent deltas.
163    pub(crate) first_tan: u32,
164    /// Count of tangent deltas.
165    pub(crate) tan_count: u32,
166}
167
168/// Serializable PBR material payload matching `PbrMaterial` for `.euv3d` legacy material slots.
169#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
170pub(crate) struct Euv3dLegacyPbrRecord {
171    /// Base linear albedo RGB.
172    pub(crate) base_color: [f32; 3],
173    /// Metallic factor.
174    pub(crate) metallic: f32,
175    /// Roughness factor.
176    pub(crate) roughness: f32,
177    /// Emissive HDR RGB.
178    pub(crate) emissive: [f32; 3],
179    /// Reflectivity factor.
180    pub(crate) reflectivity: f32,
181    /// Encoded `SurfaceDetailMode` discriminant.
182    pub(crate) surface_detail: u8,
183}
184
185/// Serializable mesh record storing LOD slices, material links, skin sidecar slice, and morph targets.
186#[derive(Clone, Debug, Deserialize, Serialize)]
187pub(crate) struct Euv3dMeshRecord {
188    /// Mesh asset name.
189    pub(crate) name: String,
190    /// Default fallback `PbrMaterial`.
191    pub(crate) material: Euv3dLegacyPbrRecord,
192    /// Sub-material table.
193    pub(crate) sub_materials: Vec<Euv3dLegacyPbrRecord>,
194    /// Per-triangle sub-material slot indices for `LOD0`.
195    pub(crate) tri_materials: Vec<u16>,
196    /// Optional index into the scene `PrincipledMaterial3D` table.
197    pub(crate) material_index: Option<u32>,
198    /// Whether this mesh is a closed solid volume.
199    pub(crate) is_closed: bool,
200    /// Ordered LOD level records (`LOD0` first).
201    pub(crate) lods: Vec<Euv3dLodRecord>,
202    /// Starting index inside `SkinInfluences` for `LOD0`.
203    pub(crate) first_skin: u32,
204    /// Number of `MeshSkinInfluence3D` records for `LOD0`.
205    pub(crate) skin_count: u32,
206    /// Morph targets belonging to `LOD0`.
207    pub(crate) morphs: Vec<Euv3dMorphRecord>,
208}
209
210/// Serializable metadata record describing one pre-baked local BVH4 tree inside the BVH sections.
211#[derive(Clone, Debug, Deserialize, Serialize)]
212pub(crate) struct Euv3dBvhRecord {
213    /// First `Bvh4Node` index inside `Bvh4Nodes`.
214    pub(crate) first_node: u32,
215    /// Number of `Bvh4Node` entries.
216    pub(crate) node_count: u32,
217    /// First `BvhTri4` packet index inside `BvhTri4Packets`.
218    pub(crate) first_packet: u32,
219    /// Number of `BvhTri4` packets.
220    pub(crate) packet_count: u32,
221    /// First `BvhTriangle` index inside `BvhTriangles`.
222    pub(crate) first_tri: u32,
223    /// Number of `BvhTriangle` records.
224    pub(crate) tri_count: u32,
225    /// Cached root bounding box minimum corner.
226    pub(crate) root_min: [f32; 3],
227    /// Cached root bounding box maximum corner.
228    pub(crate) root_max: [f32; 3],
229    /// Baseline SAH build cost.
230    pub(crate) build_sah_cost: f32,
231}
232
233/// Serializable `TextureSlot3D` record for `PrincipledMaterial3D`.
234#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
235pub(crate) struct Euv3dTextureSlotRecord {
236    /// Index into scene texture array.
237    pub(crate) texture_index: u32,
238    /// UV set index (`0` or `1`).
239    pub(crate) tex_coord: u8,
240    /// UV offset.
241    pub(crate) offset: [f32; 2],
242    /// UV rotation in radians.
243    pub(crate) rotation: f32,
244    /// UV scale.
245    pub(crate) uv_scale: [f32; 2],
246}
247
248/// Serializable `PrincipledMaterial3D` record preserving every Blender Principled BSDF and KHR extension parameter.
249#[derive(Clone, Debug, Deserialize, Serialize)]
250pub(crate) struct Euv3dMaterialRecord {
251    /// Material name.
252    pub(crate) name: String,
253    /// Base color RGBA.
254    pub(crate) base_color_factor: [f32; 4],
255    /// Metallic factor.
256    pub(crate) metallic_factor: f32,
257    /// Roughness factor.
258    pub(crate) roughness_factor: f32,
259    /// Normal map scale.
260    pub(crate) normal_scale: f32,
261    /// Occlusion strength.
262    pub(crate) occlusion_strength: f32,
263    /// Emissive factor RGB.
264    pub(crate) emissive_factor: [f32; 3],
265    /// Emissive strength multiplier.
266    pub(crate) emissive_strength: f32,
267    /// Index of refraction.
268    pub(crate) ior: f32,
269    /// Specular factor.
270    pub(crate) specular_factor: f32,
271    /// Specular color tint.
272    pub(crate) specular_color_factor: [f32; 3],
273    /// Clearcoat weight.
274    pub(crate) clearcoat_factor: f32,
275    /// Clearcoat roughness.
276    pub(crate) clearcoat_roughness_factor: f32,
277    /// Transmission weight.
278    pub(crate) transmission_factor: f32,
279    /// Volume thickness.
280    pub(crate) thickness_factor: f32,
281    /// Volume attenuation distance.
282    pub(crate) attenuation_distance: f32,
283    /// Volume attenuation color.
284    pub(crate) attenuation_color: [f32; 3],
285    /// Sheen color factor.
286    pub(crate) sheen_color_factor: [f32; 3],
287    /// Sheen roughness.
288    pub(crate) sheen_roughness_factor: f32,
289    /// Unlit flag.
290    pub(crate) unlit: bool,
291    /// Alpha mode discriminant (`0=Opaque`, `1=Mask`, `2=Blend`).
292    pub(crate) alpha_mode: u8,
293    /// Alpha cutoff threshold.
294    pub(crate) alpha_cutoff: f32,
295    /// Double-sided flag.
296    pub(crate) double_sided: bool,
297    /// Base color texture slot.
298    pub(crate) base_color_texture: Option<Euv3dTextureSlotRecord>,
299    /// Metallic-roughness texture slot.
300    pub(crate) metallic_roughness_texture: Option<Euv3dTextureSlotRecord>,
301    /// Normal map texture slot.
302    pub(crate) normal_texture: Option<Euv3dTextureSlotRecord>,
303    /// Occlusion texture slot.
304    pub(crate) occlusion_texture: Option<Euv3dTextureSlotRecord>,
305    /// Emissive texture slot.
306    pub(crate) emissive_texture: Option<Euv3dTextureSlotRecord>,
307    /// Specular factor texture slot.
308    pub(crate) specular_texture: Option<Euv3dTextureSlotRecord>,
309    /// Specular color texture slot.
310    pub(crate) specular_color_texture: Option<Euv3dTextureSlotRecord>,
311    /// Clearcoat texture slot.
312    pub(crate) clearcoat_texture: Option<Euv3dTextureSlotRecord>,
313    /// Clearcoat roughness texture slot.
314    pub(crate) clearcoat_roughness_texture: Option<Euv3dTextureSlotRecord>,
315    /// Clearcoat normal texture slot.
316    pub(crate) clearcoat_normal_texture: Option<Euv3dTextureSlotRecord>,
317    /// Transmission texture slot.
318    pub(crate) transmission_texture: Option<Euv3dTextureSlotRecord>,
319    /// Sheen color texture slot.
320    pub(crate) sheen_color_texture: Option<Euv3dTextureSlotRecord>,
321    /// Sheen roughness texture slot.
322    pub(crate) sheen_roughness_texture: Option<Euv3dTextureSlotRecord>,
323}
324
325/// Serializable texture mip level byte range inside `TexturesBlob`.
326#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
327pub(crate) struct Euv3dTexMipRecord {
328    /// Mip width in pixels.
329    pub(crate) width: u32,
330    /// Mip height in pixels.
331    pub(crate) height: u32,
332    /// Byte offset inside `TexturesBlob`.
333    pub(crate) byte_offset: u64,
334    /// Byte length inside `TexturesBlob`.
335    pub(crate) byte_length: u64,
336}
337
338/// Serializable texture descriptor referencing RGBA8 mip slices inside `TexturesBlob`.
339#[derive(Clone, Debug, Deserialize, Serialize)]
340pub(crate) struct Euv3dTextureRecord {
341    /// Texture name.
342    pub(crate) name: String,
343    /// Color space (`0=Srgb`, `1=Linear`, `2=NormalTangent`).
344    pub(crate) color_space: u8,
345    /// Min filter (`0=Nearest`, `1=Linear`).
346    pub(crate) min_filter: u8,
347    /// Mag filter (`0=Nearest`, `1=Linear`).
348    pub(crate) mag_filter: u8,
349    /// Wrap S (`0=Repeat`, `1=MirroredRepeat`, `2=ClampToEdge`).
350    pub(crate) wrap_s: u8,
351    /// Wrap T (`0=Repeat`, `1=MirroredRepeat`, `2=ClampToEdge`).
352    pub(crate) wrap_t: u8,
353    /// Encoded mip levels.
354    pub(crate) mips: Vec<Euv3dTexMipRecord>,
355}
356
357/// Serializable scene node descriptor matching `SceneNode3D`.
358#[derive(Clone, Debug, Deserialize, Serialize)]
359pub(crate) struct Euv3dNodeRecord {
360    /// Node name.
361    pub(crate) name: String,
362    /// Parent index.
363    pub(crate) parent: Option<u32>,
364    /// Local translation.
365    pub(crate) translation: [f32; 3],
366    /// Local rotation quaternion `[x, y, z, w]`.
367    pub(crate) rotation: [f32; 4],
368    /// Local scale.
369    pub(crate) scale: [f32; 3],
370    /// Optional mesh reference.
371    pub(crate) mesh_id: Option<u32>,
372    /// Optional skin reference.
373    pub(crate) skin_id: Option<u32>,
374    /// Optional camera reference.
375    pub(crate) camera_id: Option<u32>,
376    /// Optional light reference.
377    pub(crate) light_id: Option<u32>,
378    /// Collection membership bitmask.
379    pub(crate) collection_mask: u64,
380    /// Node visibility flag.
381    pub(crate) visible: bool,
382    /// Initial morph weights.
383    pub(crate) morph_weights: Vec<f32>,
384    /// `extras.euv` collider kind (`0=None, 1=Box, 2=Sphere, 3=ConvexHull, 4=Mesh`).
385    pub(crate) collider_shape: u8,
386    /// Fluid buoyancy density ratio.
387    pub(crate) buoyancy_density: f32,
388    /// Trigger volume flag.
389    pub(crate) is_trigger: bool,
390    /// Cast shadow flag.
391    pub(crate) cast_shadows: bool,
392    /// Raw custom JSON payload.
393    pub(crate) custom_json: Option<String>,
394}
395
396/// Serializable collection entry matching `CollectionEntry3D`.
397#[derive(Clone, Debug, Deserialize, Serialize)]
398pub(crate) struct Euv3dCollectionRecord {
399    /// Collection name.
400    pub(crate) name: String,
401    /// Bit index (`0..64`).
402    pub(crate) bit_index: u8,
403    /// Viewport visibility flag.
404    pub(crate) visible_viewport: bool,
405    /// Render visibility flag.
406    pub(crate) visible_render: bool,
407}
408
409/// Serializable armature skin record matching `ImportedSkin3D`.
410#[derive(Clone, Debug, Deserialize, Serialize)]
411pub(crate) struct Euv3dSkinRecord {
412    /// Skin name.
413    pub(crate) name: String,
414    /// Joint node indices.
415    pub(crate) joints: Vec<u32>,
416    /// Optional skeleton root node index.
417    pub(crate) skeleton_root: Option<u32>,
418    /// Flattened column-major inverse bind matrices (`16` floats each).
419    pub(crate) inverse_bind_matrices: Vec<[f32; 16]>,
420}
421
422/// Serializable animation sampler record referencing `f32` ranges inside `AnimationsBlob`.
423#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
424pub(crate) struct Euv3dAnimSamplerRecord {
425    /// Starting `f32` element index for timestamps inside `AnimationsBlob`.
426    pub(crate) first_time: u32,
427    /// Count of keyframe timestamps.
428    pub(crate) time_count: u32,
429    /// Starting `f32` element index for values inside `AnimationsBlob`.
430    pub(crate) first_value: u32,
431    /// Count of flattened value floats.
432    pub(crate) value_count: u32,
433    /// Float stride per keyframe.
434    pub(crate) value_stride: u32,
435    /// Interpolation mode (`0=Step`, `1=Linear`, `2=CubicSpline`).
436    pub(crate) interpolation: u8,
437}
438
439/// Serializable animation channel record.
440#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
441pub(crate) struct Euv3dAnimChannelRecord {
442    /// Target node index.
443    pub(crate) target_node: u32,
444    /// Animated property (`0=Translation`, `1=Rotation`, `2=Scale`, `3=MorphWeights`).
445    pub(crate) path: u8,
446    /// Sampler index within the owning clip.
447    pub(crate) sampler_index: u32,
448}
449
450/// Serializable animation clip record.
451#[derive(Clone, Debug, Deserialize, Serialize)]
452pub(crate) struct Euv3dAnimClipRecord {
453    /// Clip name.
454    pub(crate) name: String,
455    /// Total duration in seconds.
456    pub(crate) duration_secs: f32,
457    /// Samplers belonging to this clip.
458    pub(crate) samplers: Vec<Euv3dAnimSamplerRecord>,
459    /// Channels belonging to this clip.
460    pub(crate) channels: Vec<Euv3dAnimChannelRecord>,
461}
462
463/// Serializable camera record matching `ImportedCamera3D`.
464#[derive(Clone, Debug, Deserialize, Serialize)]
465pub(crate) struct Euv3dCameraRecord {
466    /// Camera label.
467    pub(crate) name: String,
468    /// `true` for perspective, `false` for orthographic.
469    pub(crate) is_perspective: bool,
470    /// Vertical FOV (perspective) or X magnification (orthographic).
471    pub(crate) param_a: f32,
472    /// Aspect ratio (perspective, optional encoded via `> 0.0`) or Y magnification (orthographic).
473    pub(crate) param_b: f32,
474    /// Near clipping plane.
475    pub(crate) znear: f32,
476    /// Optional far clipping plane.
477    pub(crate) zfar: Option<f32>,
478}
479
480/// Serializable light record matching `ImportedLight3D`.
481#[derive(Clone, Debug, Deserialize, Serialize)]
482pub(crate) struct Euv3dLightRecord {
483    /// Light label.
484    pub(crate) name: String,
485    /// Linear RGB color.
486    pub(crate) color: [f32; 3],
487    /// Intensity scalar.
488    pub(crate) intensity: f32,
489    /// Kind tag (`0=Directional`, `1=Point`, `2=Spot`, `3=Area`).
490    pub(crate) kind: u8,
491    /// Optional attenuation range (Point/Spot).
492    pub(crate) range: Option<f32>,
493    /// Inner cone angle (Spot) or Width (Area).
494    pub(crate) param_a: f32,
495    /// Outer cone angle (Spot) or Height (Area).
496    pub(crate) param_b: f32,
497}
498
499/// Top-level JSON metadata structure stored in the `.euv3d` `SceneMetadataJson` section.
500#[derive(Clone, Debug, Deserialize, Serialize)]
501pub(crate) struct Euv3dSceneMetadata {
502    /// Scene name.
503    pub(crate) name: String,
504    /// Source generator string.
505    pub(crate) generator: String,
506    /// Scene collections.
507    pub(crate) collections: Vec<Euv3dCollectionRecord>,
508    /// Scene graph nodes.
509    pub(crate) nodes: Vec<Euv3dNodeRecord>,
510    /// Baked meshes and LOD chains.
511    pub(crate) meshes: Vec<Euv3dMeshRecord>,
512    /// Pre-baked local BVH4 tree records aligned with `meshes`.
513    pub(crate) bvhs: Vec<Euv3dBvhRecord>,
514    /// Principled BSDF materials.
515    pub(crate) materials: Vec<Euv3dMaterialRecord>,
516    /// Mipmapped textures.
517    pub(crate) textures: Vec<Euv3dTextureRecord>,
518    /// Armature skins.
519    pub(crate) skins: Vec<Euv3dSkinRecord>,
520    /// Animation clips.
521    pub(crate) animations: Vec<Euv3dAnimClipRecord>,
522    /// Cameras.
523    pub(crate) cameras: Vec<Euv3dCameraRecord>,
524    /// Punctual & area lights.
525    pub(crate) lights: Vec<Euv3dLightRecord>,
526}