burn_synth 0.2.0

Shared utilities and pipeline re-exports for burn_synth
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
use std::path::{Path, PathBuf};

#[derive(Clone, Debug)]
pub enum ImageSource {
    Path(PathBuf),
    Bytes(Vec<u8>),
}

impl ImageSource {
    pub fn from_path(path: impl Into<PathBuf>) -> Self {
        Self::Path(path.into())
    }

    pub fn from_bytes(bytes: impl Into<Vec<u8>>) -> Self {
        Self::Bytes(bytes.into())
    }

    pub fn path(&self) -> Option<&Path> {
        match self {
            Self::Path(path) => Some(path.as_path()),
            Self::Bytes(_) => None,
        }
    }

    pub fn bytes(&self) -> Option<&[u8]> {
        match self {
            Self::Path(_) => None,
            Self::Bytes(bytes) => Some(bytes),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TextPrompt(pub String);

impl From<String> for TextPrompt {
    fn from(value: String) -> Self {
        Self(value)
    }
}

impl From<&str> for TextPrompt {
    fn from(value: &str) -> Self {
        Self(value.to_string())
    }
}

impl TextPrompt {
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

#[cfg(any(feature = "runtime", feature = "wasm-api"))]
use std::borrow::Cow;
#[cfg(any(feature = "runtime", feature = "wasm-api"))]
use std::fs;
#[cfg(any(feature = "runtime", feature = "wasm-api"))]
use std::io;

#[cfg(any(feature = "runtime", feature = "wasm-api"))]
use crate::mesh::{Mesh, MeshTexture};
#[cfg(any(feature = "runtime", feature = "wasm-api"))]
use image::ImageEncoder;
#[cfg(any(feature = "runtime", feature = "wasm-api"))]
use serde_json::{Value, json};

#[cfg(any(feature = "runtime", feature = "wasm-api"))]
#[derive(Clone, Debug)]
struct MeshBinaryLayout {
    buffer: Vec<u8>,
    positions_byte_offset: usize,
    positions_byte_length: usize,
    indices_byte_offset: usize,
    indices_byte_length: usize,
    uvs_byte_offset: Option<usize>,
    uvs_byte_length: Option<usize>,
    base_color_image_view: Option<(usize, usize)>,
    metallic_roughness_image_view: Option<(usize, usize)>,
    normal_image_view: Option<(usize, usize)>,
    emissive_image_view: Option<(usize, usize)>,
    occlusion_image_view: Option<(usize, usize)>,
    min: [f32; 3],
    max: [f32; 3],
}

#[cfg(any(feature = "runtime", feature = "wasm-api"))]
pub fn write_glb_mesh(path: &Path, mesh: &Mesh) -> Result<(), String> {
    ensure_parent_dir(path).map_err(|err| err.to_string())?;
    let bytes = mesh_to_glb_bytes(mesh)?;
    fs::write(path, bytes).map_err(|err| format!("failed to write {}: {err}", path.display()))
}

#[cfg(any(feature = "runtime", feature = "wasm-api"))]
pub fn mesh_to_glb_bytes(mesh: &Mesh) -> Result<Vec<u8>, String> {
    let layout = build_mesh_binary_layout(mesh)?;
    let gltf = gltf_json(mesh, &layout);
    let json_bytes = serde_json::to_vec(&gltf)
        .map_err(|err| format!("failed to serialize glTF json chunk: {err}"))?;
    gltf::Glb {
        header: gltf::binary::Header {
            magic: *b"glTF",
            version: 2,
            length: 0,
        },
        json: Cow::Owned(json_bytes),
        bin: Some(Cow::Owned(layout.buffer)),
    }
    .to_vec()
    .map_err(|err| format!("failed to build GLB: {err}"))
}

#[cfg(any(feature = "runtime", feature = "wasm-api"))]
fn build_mesh_binary_layout(mesh: &Mesh) -> Result<MeshBinaryLayout, String> {
    if mesh.vertices.is_empty() {
        return Err("cannot export empty mesh".to_string());
    }

    let mut min = [f32::INFINITY; 3];
    let mut max = [f32::NEG_INFINITY; 3];
    for vertex in &mesh.vertices {
        for axis in 0..3 {
            min[axis] = min[axis].min(vertex[axis]);
            max[axis] = max[axis].max(vertex[axis]);
        }
    }

    let mut buffer = Vec::with_capacity(mesh.vertices.len() * 12 + mesh.faces.len() * 12 + 8192);
    let positions_byte_offset = buffer.len();
    for vertex in &mesh.vertices {
        for component in vertex {
            buffer.extend_from_slice(&component.to_le_bytes());
        }
    }
    let positions_byte_length = buffer.len() - positions_byte_offset;

    let mut uvs_byte_offset = None;
    let mut uvs_byte_length = None;
    if mesh.uvs.len() == mesh.vertices.len() && !mesh.uvs.is_empty() {
        pad_buffer_4(&mut buffer);
        let offset = buffer.len();
        for uv in &mesh.uvs {
            buffer.extend_from_slice(&uv[0].to_le_bytes());
            buffer.extend_from_slice(&uv[1].to_le_bytes());
        }
        uvs_byte_offset = Some(offset);
        uvs_byte_length = Some(buffer.len() - offset);
    }

    pad_buffer_4(&mut buffer);
    let indices_byte_offset = buffer.len();
    for face in &mesh.faces {
        for index in face {
            buffer.extend_from_slice(&index.to_le_bytes());
        }
    }
    let indices_byte_length = buffer.len() - indices_byte_offset;

    let mut base_color_image_view = None;
    let mut metallic_roughness_image_view = None;
    let mut normal_image_view = None;
    let mut emissive_image_view = None;
    let mut occlusion_image_view = None;
    if let Some(pbr) = mesh.pbr_textures.as_ref() {
        let base_png = encode_rgba_texture_png(&pbr.base_color)?;
        let mr_png = encode_rgba_texture_png(&pbr.metallic_roughness)?;
        pad_buffer_4(&mut buffer);
        let base_offset = buffer.len();
        buffer.extend_from_slice(base_png.as_slice());
        base_color_image_view = Some((base_offset, base_png.len()));
        pad_buffer_4(&mut buffer);
        let mr_offset = buffer.len();
        buffer.extend_from_slice(mr_png.as_slice());
        metallic_roughness_image_view = Some((mr_offset, mr_png.len()));

        if let Some(normal) = pbr.normal.as_ref() {
            let png = encode_rgba_texture_png(normal)?;
            pad_buffer_4(&mut buffer);
            let offset = buffer.len();
            buffer.extend_from_slice(png.as_slice());
            normal_image_view = Some((offset, png.len()));
        }
        if let Some(emissive) = pbr.emissive.as_ref() {
            let png = encode_rgba_texture_png(emissive)?;
            pad_buffer_4(&mut buffer);
            let offset = buffer.len();
            buffer.extend_from_slice(png.as_slice());
            emissive_image_view = Some((offset, png.len()));
        }
        if let Some(occlusion) = pbr.occlusion.as_ref() {
            let png = encode_rgba_texture_png(occlusion)?;
            pad_buffer_4(&mut buffer);
            let offset = buffer.len();
            buffer.extend_from_slice(png.as_slice());
            occlusion_image_view = Some((offset, png.len()));
        }
    }

    Ok(MeshBinaryLayout {
        buffer,
        positions_byte_offset,
        positions_byte_length,
        indices_byte_offset,
        indices_byte_length,
        uvs_byte_offset,
        uvs_byte_length,
        base_color_image_view,
        metallic_roughness_image_view,
        normal_image_view,
        emissive_image_view,
        occlusion_image_view,
        min,
        max,
    })
}

#[cfg(any(feature = "runtime", feature = "wasm-api"))]
fn gltf_json(mesh: &Mesh, layout: &MeshBinaryLayout) -> Value {
    let mut primitive = json!({
        "attributes": {
            "POSITION": 0
        },
        "indices": 1,
        "mode": 4
    });
    if mesh.uvs.len() == mesh.vertices.len() && !mesh.uvs.is_empty() {
        primitive["attributes"]["TEXCOORD_0"] = json!(2);
    }

    let buffers = vec![json!({
        "byteLength": layout.buffer.len(),
    })];

    let mut buffer_views = Vec::new();
    buffer_views.push(json!({
        "buffer": 0,
        "byteOffset": layout.positions_byte_offset,
        "byteLength": layout.positions_byte_length,
        "target": 34962
    }));
    buffer_views.push(json!({
        "buffer": 0,
        "byteOffset": layout.indices_byte_offset,
        "byteLength": layout.indices_byte_length,
        "target": 34963
    }));
    if let (Some(uv_offset), Some(uv_len)) = (layout.uvs_byte_offset, layout.uvs_byte_length) {
        buffer_views.push(json!({
            "buffer": 0,
            "byteOffset": uv_offset,
            "byteLength": uv_len,
            "target": 34962
        }));
    }

    let mut accessors = Vec::new();
    accessors.push(json!({
        "bufferView": 0,
        "componentType": 5126,
        "count": mesh.vertices.len(),
        "type": "VEC3",
        "min": layout.min,
        "max": layout.max
    }));
    accessors.push(json!({
        "bufferView": 1,
        "componentType": 5125,
        "count": mesh.faces.len() * 3,
        "type": "SCALAR"
    }));
    if mesh.uvs.len() == mesh.vertices.len() && !mesh.uvs.is_empty() {
        accessors.push(json!({
            "bufferView": 2,
            "componentType": 5126,
            "count": mesh.uvs.len(),
            "type": "VEC2"
        }));
    }

    let mut images = Vec::new();
    let mut textures = Vec::new();
    let mut materials = Vec::new();
    let mut pbr_mr = json!({});
    let mut push_texture_image = |byte_offset: usize, byte_length: usize| -> usize {
        let view_index = buffer_views.len();
        buffer_views.push(json!({
            "buffer": 0,
            "byteOffset": byte_offset,
            "byteLength": byte_length
        }));
        let image_index = images.len();
        images.push(json!({
            "bufferView": view_index,
            "mimeType": "image/png"
        }));
        let texture_index = textures.len();
        textures.push(json!({ "source": image_index }));
        texture_index
    };

    if let Some(material) = mesh.material {
        pbr_mr = json!({
            "baseColorFactor": [
                material.base_color[0],
                material.base_color[1],
                material.base_color[2],
                material.alpha.clamp(0.0, 1.0)
            ],
            "metallicFactor": material.metallic.clamp(0.0, 1.0),
            "roughnessFactor": material.roughness.clamp(0.0, 1.0)
        });
    }
    if let Some((base_offset, base_len)) = layout.base_color_image_view {
        let texture_index = push_texture_image(base_offset, base_len);
        pbr_mr["baseColorTexture"] = json!({ "index": texture_index });
    }
    if let Some((mr_offset, mr_len)) = layout.metallic_roughness_image_view {
        let texture_index = push_texture_image(mr_offset, mr_len);
        pbr_mr["metallicRoughnessTexture"] = json!({ "index": texture_index });
    }

    if mesh.material.is_some() || mesh.pbr_textures.is_some() {
        let alpha = mesh
            .material
            .map(|value| value.alpha)
            .unwrap_or(1.0)
            .clamp(0.0, 1.0);
        let material_index = materials.len();
        let mut material = json!({
            "pbrMetallicRoughness": pbr_mr,
            "alphaMode": if alpha < 0.995 { "BLEND" } else { "OPAQUE" },
            "doubleSided": true
        });
        if let Some((normal_offset, normal_len)) = layout.normal_image_view {
            let texture_index = push_texture_image(normal_offset, normal_len);
            material["normalTexture"] = json!({ "index": texture_index });
        }
        if let Some((emissive_offset, emissive_len)) = layout.emissive_image_view {
            let texture_index = push_texture_image(emissive_offset, emissive_len);
            material["emissiveTexture"] = json!({ "index": texture_index });
            material["emissiveFactor"] = json!([1.0, 1.0, 1.0]);
        }
        if let Some((occlusion_offset, occlusion_len)) = layout.occlusion_image_view {
            let texture_index = push_texture_image(occlusion_offset, occlusion_len);
            material["occlusionTexture"] = json!({ "index": texture_index });
        }
        materials.push(material);
        primitive["material"] = json!(material_index);
    }

    let mut gltf = json!({
        "asset": {
            "version": "2.0",
            "generator": "burn_synth"
        },
        "scene": 0,
        "scenes": [
            { "nodes": [0] }
        ],
        "nodes": [
            { "mesh": 0 }
        ],
        "meshes": [
            {
                "primitives": [
                    primitive
                ]
            }
        ],
        "buffers": buffers,
        "bufferViews": buffer_views,
        "accessors": accessors
    });
    if !materials.is_empty() {
        gltf["materials"] = Value::Array(materials);
    }
    if !images.is_empty() {
        gltf["images"] = Value::Array(images);
    }
    if !textures.is_empty() {
        gltf["textures"] = Value::Array(textures);
    }
    gltf
}

#[cfg(any(feature = "runtime", feature = "wasm-api"))]
fn pad_buffer_4(buffer: &mut Vec<u8>) {
    while !buffer.len().is_multiple_of(4) {
        buffer.push(0);
    }
}

#[cfg(any(feature = "runtime", feature = "wasm-api"))]
fn encode_rgba_texture_png(texture: &MeshTexture) -> Result<Vec<u8>, String> {
    let expected = texture.width as usize * texture.height as usize * 4;
    if texture.rgba8.len() != expected {
        return Err(format!(
            "texture byte length mismatch: expected {}, got {}",
            expected,
            texture.rgba8.len()
        ));
    }
    let mut out = Vec::new();
    let encoder = image::codecs::png::PngEncoder::new(&mut out);
    encoder
        .write_image(
            texture.rgba8.as_slice(),
            texture.width,
            texture.height,
            image::ColorType::Rgba8.into(),
        )
        .map_err(|err| format!("failed to encode texture png: {err}"))?;
    Ok(out)
}

#[cfg(any(feature = "runtime", feature = "wasm-api"))]
fn ensure_parent_dir(path: &Path) -> io::Result<()> {
    if let Some(parent) = path.parent()
        && !parent.as_os_str().is_empty()
    {
        fs::create_dir_all(parent)?;
    }
    Ok(())
}

#[cfg(all(test, feature = "runtime"))]
mod tests {
    use super::*;
    use crate::mesh::{Mesh, MeshMaterial, MeshPbrTextures, MeshTexture};

    fn test_texture(width: u32, height: u32, rgba: [u8; 4]) -> MeshTexture {
        let mut bytes = Vec::with_capacity(width as usize * height as usize * 4);
        for _ in 0..(width as usize * height as usize) {
            bytes.extend_from_slice(&rgba);
        }
        MeshTexture {
            width,
            height,
            rgba8: bytes,
        }
    }

    fn sample_mesh_with_pbr() -> Mesh {
        Mesh {
            vertices: vec![[-0.5, 0.0, 0.0], [0.5, 0.0, 0.0], [0.0, 0.8, 0.0]],
            faces: vec![[0, 1, 2]],
            uvs: vec![[0.0, 0.0], [1.0, 0.0], [0.5, 1.0]],
            material: Some(MeshMaterial {
                base_color: [1.0, 1.0, 1.0],
                metallic: 1.0,
                roughness: 1.0,
                alpha: 1.0,
            }),
            pbr_textures: Some(MeshPbrTextures {
                base_color: test_texture(2, 2, [200, 180, 160, 255]),
                metallic_roughness: test_texture(2, 2, [0, 128, 64, 255]),
                normal: None,
                emissive: None,
                occlusion: None,
            }),
        }
    }

    #[test]
    fn glb_embeds_pbr_textures_when_present() {
        let mesh = sample_mesh_with_pbr();
        let bytes = mesh_to_glb_bytes(&mesh).expect("glb export");
        let glb = gltf::Glb::from_slice(bytes.as_slice()).expect("parse glb");
        let json: Value = serde_json::from_slice(glb.json.as_ref()).expect("parse glb json");

        let materials = json["materials"].as_array().expect("materials array");
        assert_eq!(materials.len(), 1);
        let pbr = &materials[0]["pbrMetallicRoughness"];
        assert!(pbr.get("baseColorTexture").is_some());
        assert!(pbr.get("metallicRoughnessTexture").is_some());
        assert!(
            json["textures"]
                .as_array()
                .is_some_and(|value| !value.is_empty())
        );
        assert!(
            json["images"]
                .as_array()
                .is_some_and(|value| !value.is_empty())
        );
    }

    #[test]
    fn glb_writes_material_when_only_textures_are_present() {
        let mut mesh = sample_mesh_with_pbr();
        mesh.material = None;

        let bytes = mesh_to_glb_bytes(&mesh).expect("glb export");
        let glb = gltf::Glb::from_slice(bytes.as_slice()).expect("parse glb");
        let json: Value = serde_json::from_slice(glb.json.as_ref()).expect("parse glb json");

        let materials = json["materials"].as_array().expect("materials array");
        assert_eq!(materials.len(), 1);
        assert_eq!(materials[0]["alphaMode"], "OPAQUE");
        let pbr = &materials[0]["pbrMetallicRoughness"];
        assert!(pbr.get("baseColorTexture").is_some());
        assert!(pbr.get("metallicRoughnessTexture").is_some());
    }
}