voxelify 0.1.0

Convert a 2D pixel art image into GLTF 3D object
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
use gltf::json::validation::USize64;
use gltf::json::{self, Index};
use gltf::json::{buffer, validation::Checked::Valid};
use image::GenericImageView;
use nalgebra::Vector2;
use std::borrow::Cow;

use self::error::VoxelifyError;

mod error;

const ALPHA_CHANNEL_INDEX: usize = 3;
const RGB_MAX_VALUE: f32 = 255.0;

#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct Vertex {
    position: [f32; 3],
    normal: [f32; 3],
    color: [f32; 3],
}

/// Creates a GLB object that can be written to a file
pub fn create_glb<'a>(
    root: &gltf::json::Root,
    vertices: &[Vertex],
) -> Result<gltf::binary::Glb<'a>, VoxelifyError> {
    let json_string = json::serialize::to_string(&root)?;

    let mut json_offset = json_string.len();
    align_to_multiple_of_four(&mut json_offset);

    let glb = gltf::binary::Glb {
        header: gltf::binary::Header {
            magic: *b"glTF",
            version: 2,
            // N.B., the size of binary glTF file is limited to range of `u32`.
            length: (json_offset + calculate_buffer_length(vertices)).try_into()?,
        },
        bin: Some(Cow::Owned(to_padded_byte_vector(vertices.to_vec()))),
        json: Cow::Owned(json_string.into_bytes()),
    };

    Ok(glb)
}

/// Creates the root JSON object for GLTF
pub fn create_gltf_root(vertices: &[Vertex], uri: Option<String>) -> gltf::json::Root {
    let mut root = gltf::json::Root::default();

    let vertex_buffer_length = calculate_buffer_length(vertices);

    let buffer = root.push(gltf::json::Buffer {
        byte_length: USize64::from(vertex_buffer_length),
        extensions: Default::default(),
        extras: Default::default(),
        name: None,
        uri,
    });

    let vertex_buffer_view = root.push(buffer::View {
        buffer,
        byte_length: USize64::from(vertex_buffer_length),
        byte_offset: None,
        byte_stride: Some(buffer::Stride(std::mem::size_of::<Vertex>())),
        extensions: Default::default(),
        extras: Default::default(),
        name: None,
        target: Some(Valid(buffer::Target::ArrayBuffer)),
    });

    let VoxelAccessors {
        positions,
        normals,
        colors,
    } = create_accessors(&mut root, &vertex_buffer_view, vertices);

    let primitive = json::mesh::Primitive {
        attributes: {
            let mut map = std::collections::BTreeMap::new();
            map.insert(Valid(json::mesh::Semantic::Positions), positions);
            map.insert(Valid(json::mesh::Semantic::Normals), normals);
            map.insert(Valid(json::mesh::Semantic::Colors(0)), colors);
            map
        },
        extensions: Default::default(),
        extras: Default::default(),
        indices: None,
        material: None,
        mode: Valid(json::mesh::Mode::Triangles),
        targets: None,
    };

    let mesh = root.push(json::Mesh {
        extensions: Default::default(),
        extras: Default::default(),
        name: None,
        primitives: vec![primitive],
        weights: None,
    });

    let node = root.push(json::Node {
        mesh: Some(mesh),
        ..Default::default()
    });

    root.push(json::Scene {
        extensions: Default::default(),
        extras: Default::default(),
        name: None,
        nodes: vec![node],
    });

    root
}

/// Converts an image to list of vertices that can be used to create a mesh
/// Each pixel will be converted to a voxel with height determined by the height argument
/// The faces of the voxel will be culled if they are not visible
pub fn image_to_vertices(image: &image::DynamicImage, height: f32) -> Vec<Vertex> {
    let (image_width, image_height) = image.dimensions();
    let mut vertices = Vec::new();

    for y in 0..image_height {
        for x in 0..image_width {
            let pixel = image.get_pixel(x, y);
            if is_empty_pixel(pixel) {
                continue;
            }

            let faces = cull_faces(image, Vector2::new(x, y));

            for face in faces {
                vertices.extend(create_pixel_verticies_face(
                    [x as f32, y as f32],
                    pixel,
                    height,
                    &face,
                ));
            }
        }
    }

    vertices
}

/// Creates a padded byte vector from a generic vector
/// Needed for creating the GLB/GLTF file
pub fn to_padded_byte_vector<T>(vec: Vec<T>) -> Vec<u8> {
    let byte_length = vec.len() * std::mem::size_of::<T>();
    let byte_capacity = vec.capacity() * std::mem::size_of::<T>();

    // Ensure the capacity of the vector is rounded up to the nearest multiple of four bytes.
    let padding = (4 - byte_length % 4) % 4;
    let padded_capacity = byte_capacity + padding;

    let alloc = vec.into_boxed_slice();
    let ptr = Box::into_raw(alloc) as *mut u8;

    // Assumes the content is properly aligned as u8.
    let mut padded_vec = unsafe { Vec::from_raw_parts(ptr, byte_length, padded_capacity) };

    // Add zero-padding to ensure the length is a multiple of four bytes.
    padded_vec.resize(padded_vec.len() + padding, 0);

    padded_vec
}

/// Struct for storing accessors for the GLTF vertex buffer
struct VoxelAccessors {
    positions: Index<json::Accessor>,
    normals: Index<json::Accessor>,
    colors: Index<json::Accessor>,
}

// Enum for the faces of a voxel
// Used for culling faces that are not visible
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
enum Face {
    Up,
    Down,
    Left,
    Right,
    Forward,
    Back,
}

/// Returns the faces that should be visible for a given pixel
fn cull_faces(image: &image::DynamicImage, pos: Vector2<u32>) -> Vec<Face> {
    let (image_width, image_height) = image.dimensions();

    // Initialize with Up and Down faces as they're always visible in a 2D image
    let mut faces = vec![Face::Up, Face::Down];

    match pos.x {
        0 => faces.push(Face::Left),
        x if x == image_width - 1 => faces.push(Face::Right),
        _ => {
            // Check on the x-axis for adjacent pixels that are empty.
            if is_empty_pixel(image.get_pixel(pos.x + 1, pos.y)) {
                faces.push(Face::Right);
            }
            if is_empty_pixel(image.get_pixel(pos.x - 1, pos.y)) {
                faces.push(Face::Left);
            }
        }
    }

    match pos.y {
        0 => faces.push(Face::Forward),
        y if y == image_height - 1 => faces.push(Face::Back),
        _ => {
            // Check on the y-axis for adjacent pixels that are empty.
            if is_empty_pixel(image.get_pixel(pos.x, pos.y + 1)) {
                faces.push(Face::Back);
            }
            if is_empty_pixel(image.get_pixel(pos.x, pos.y - 1)) {
                faces.push(Face::Forward);
            }
        }
    }

    faces
}

fn create_accessors(
    root: &mut gltf::json::Root,
    buffer_view: &Index<buffer::View>,
    vertices: &[Vertex],
) -> VoxelAccessors {
    let (min, max) = bounding_coords(vertices);

    let positions = root.push(json::Accessor {
        buffer_view: Some(*buffer_view),
        byte_offset: Some(USize64(0)),
        count: USize64::from(vertices.len()),
        component_type: Valid(json::accessor::GenericComponentType(
            json::accessor::ComponentType::F32,
        )),
        extensions: Default::default(),
        extras: Default::default(),
        type_: Valid(json::accessor::Type::Vec3),
        min: Some(json::Value::from(Vec::from(min))),
        max: Some(json::Value::from(Vec::from(max))),
        name: None,
        normalized: false,
        sparse: None,
    });

    let normals = root.push(json::Accessor {
        buffer_view: Some(*buffer_view),
        byte_offset: Some(USize64::from(3 * std::mem::size_of::<f32>())),
        count: USize64::from(vertices.len()),
        component_type: Valid(json::accessor::GenericComponentType(
            json::accessor::ComponentType::F32,
        )),
        extensions: Default::default(),
        extras: Default::default(),
        type_: Valid(json::accessor::Type::Vec3),
        min: Some(json::Value::from(Vec::from([-1.0, -1.0, -1.0]))),
        max: Some(json::Value::from(Vec::from([1.0, 1.0, 1.0]))),
        name: None,
        normalized: false,
        sparse: None,
    });

    let colors = root.push(json::Accessor {
        buffer_view: Some(*buffer_view),
        byte_offset: Some(USize64::from(6 * std::mem::size_of::<f32>())),
        count: USize64::from(vertices.len()),
        component_type: Valid(json::accessor::GenericComponentType(
            json::accessor::ComponentType::F32,
        )),
        extensions: Default::default(),
        extras: Default::default(),
        type_: Valid(json::accessor::Type::Vec3),
        min: None,
        max: None,
        name: None,
        normalized: false,
        sparse: None,
    });

    VoxelAccessors {
        positions,
        normals,
        colors,
    }
}

#[inline]
fn is_empty_pixel(pixel: image::Rgba<u8>) -> bool {
    pixel.0[ALPHA_CHANNEL_INDEX] == 0
}

#[rustfmt::skip]
/// Creates the two triangles for a face of a voxel
fn create_pixel_verticies_face(
    pos: [f32; 2],
    color: image::Rgba<u8>,
    height: f32,
    face: &Face,
) -> Vec<Vertex> {
    let color = [
        color.0[0] as f32 / RGB_MAX_VALUE,
        color.0[1] as f32 / RGB_MAX_VALUE,
        color.0[2] as f32 / RGB_MAX_VALUE,
    ];

    match face {
        // Top face (z = 1)
        Face::Up => {
            vec![
                Vertex { position: [pos[0], pos[1], height], normal: [0.0, 0.0, 1.0], color },
                Vertex { position: [pos[0] + 1.0, pos[1], height], normal: [0.0, 0.0, 1.0], color },
                Vertex { position: [pos[0], pos[1] + 1.0, height], normal: [0.0, 0.0, 1.0], color },

                Vertex { position: [pos[0], pos[1] + 1.0, height], normal: [0.0, 0.0, 1.0], color },
                Vertex { position: [pos[0] + 1.0, pos[1], height], normal: [0.0, 0.0, 1.0], color },
                Vertex { position: [pos[0] + 1.0, pos[1] + 1.0, height], normal: [0.0, 0.0, 1.0], color },
            ]
        }
        // Bottom face (z = 0)
        Face::Down => {
            vec![
                Vertex { position: [pos[0], pos[1], 0.0], normal: [0.0, 0.0, -1.0], color },
                Vertex { position: [pos[0], pos[1] + 1.0, 0.0], normal: [0.0, 0.0, -1.0], color },
                Vertex { position: [pos[0] + 1.0, pos[1], 0.0], normal: [0.0, 0.0, -1.0], color },

                Vertex { position: [pos[0], pos[1] + 1.0, 0.0], normal: [0.0, 0.0, -1.0], color },
                Vertex { position: [pos[0] + 1.0, pos[1] + 1.0, 0.0], normal: [0.0, 0.0, -1.0], color },
                Vertex { position: [pos[0] + 1.0, pos[1], 0.0], normal: [0.0, 0.0, -1.0], color },
            ]
        }
        Face::Forward => {
            vec![
                Vertex { position: [pos[0], pos[1], 0.0], normal: [0.0, -1.0, 0.0], color },
                Vertex { position: [pos[0] + 1.0, pos[1], 0.0], normal: [0.0, -1.0, 0.0], color },
                Vertex { position: [pos[0], pos[1], height], normal: [0.0, -1.0, 0.0], color },

                Vertex { position: [pos[0], pos[1], height], normal: [0.0, -1.0, 0.0], color },
                Vertex { position: [pos[0] + 1.0, pos[1], 0.0], normal: [0.0, -1.0, 0.0], color },
                Vertex { position: [pos[0] + 1.0, pos[1], height], normal: [0.0, -1.0, 0.0], color },
            ]
        }
        Face::Back => {
            vec![
                Vertex { position: [pos[0], pos[1] + 1.0, 0.0], normal: [0.0, 1.0, 0.0], color },
                Vertex { position: [pos[0], pos[1] + 1.0, height], normal: [0.0, 1.0, 0.0], color },
                Vertex { position: [pos[0] + 1.0, pos[1] + 1.0, 0.0], normal: [0.0, 1.0, 0.0], color },

                Vertex { position: [pos[0] + 1.0, pos[1] + 1.0, 0.0], normal: [0.0, 1.0, 0.0], color },
                Vertex { position: [pos[0], pos[1] + 1.0, height], normal: [0.0, 1.0, 0.0], color },
                Vertex { position: [pos[0] + 1.0, pos[1] + 1.0, height], normal: [0.0, 1.0, 0.0], color },
            ]
        }
        Face::Left => {
            vec![
                Vertex { position: [pos[0], pos[1], 0.0], normal: [-1.0, 0.0, 0.0], color },
                Vertex { position: [pos[0], pos[1], height], normal: [-1.0, 0.0, 0.0], color },
                Vertex { position: [pos[0], pos[1] + 1.0, 0.0], normal: [-1.0, 0.0, 0.0], color },

                Vertex { position: [pos[0], pos[1] + 1.0, 0.0], normal: [-1.0, 0.0, 0.0], color },
                Vertex { position: [pos[0], pos[1], height], normal: [-1.0, 0.0, 0.0], color },
                Vertex { position: [pos[0], pos[1] + 1.0, height], normal: [-1.0, 0.0, 0.0], color },
            ]
        }
        Face::Right => {
            vec![
                Vertex { position: [pos[0] + 1.0, pos[1], 0.0], normal: [1.0, 0.0, 0.0], color },
                Vertex { position: [pos[0] + 1.0, pos[1] + 1.0, 0.0], normal: [1.0, 0.0, 0.0], color },
                Vertex { position: [pos[0] + 1.0, pos[1], height], normal: [1.0, 0.0, 0.0], color },

                Vertex { position: [pos[0] + 1.0, pos[1] + 1.0, 0.0], normal: [1.0, 0.0, 0.0], color },
                Vertex { position: [pos[0] + 1.0, pos[1] + 1.0, height], normal: [1.0, 0.0, 0.0], color },
                Vertex { position: [pos[0] + 1.0, pos[1], height], normal: [1.0, 0.0, 0.0], color },
            ]
        }
    }
}

/// Calculate bounding coordinates of a list of vertices, used for the clipping distance of the model
fn bounding_coords(points: &[Vertex]) -> ([f32; 3], [f32; 3]) {
    let mut min = [f32::MAX, f32::MAX, f32::MAX];
    let mut max = [f32::MIN, f32::MIN, f32::MIN];

    for point in points {
        let p = point.position;
        for i in 0..3 {
            min[i] = f32::min(min[i], p[i]);
            max[i] = f32::max(max[i], p[i]);
        }
    }

    (min, max)
}

#[inline]
fn align_to_multiple_of_four(number: &mut usize) {
    *number = (*number + 3) & !3;
}

#[inline]
fn calculate_buffer_length(vertices: &[Vertex]) -> usize {
    std::mem::size_of_val(vertices)
}