vee_models 0.1.0

Making Mii models. Part of `vfl`.
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
//! Building models. Only the mask needs this operation.
use super::positioning::{ImageOrigin, MaskFacePart, MaskFaceParts};

use glam::{vec2, Mat4, Quat, Vec2, Vec4};

pub const FACE_OUTPUT_SIZE: u16 = 512;
use crate::model::{Model2d, Vertex};
use crate::{TEX_SCALE_X, TEX_SCALE_Y};
pub use bytemuck::cast_slice;
use vee_parse::NxCharInfo;
use vee_resources::color::nx::{modulate, ColorModulated};
use vee_resources::packing::Float16;
use vee_resources::tex::{ResourceTexture, TextureElement};

const NON_REPLACEMENT: [f32; 4] = [f32::NAN, f32::NAN, f32::NAN, f32::NAN];

/// All the models required for rendering the mask texture.
pub struct MaskModels {
    pub left_eye: Model2d,
    pub right_eye: Model2d,
    pub left_brow: Option<Model2d>,
    pub right_brow: Option<Model2d>,
    pub left_mustache: Option<Model2d>,
    pub right_mustache: Option<Model2d>,
    pub mouth: Model2d,
    pub mole: Option<Model2d>,
}
impl MaskModels {
    /// Returns all the models in a `Vec` for easy consumption.
    pub fn all(self) -> Vec<Model2d> {
        [
            Some(self.left_eye),
            Some(self.right_eye),
            self.left_brow,
            self.right_brow,
            Some(self.mouth),
            self.left_mustache,
            self.right_mustache,
            self.mole,
        ]
        .into_iter()
        .flatten()
        .collect()
    }

    /// Returns all the eyebrows in a `Vec` for easy consumption.
    pub fn brows(self) -> Vec<Model2d> {
        [self.left_brow, self.right_brow]
            .into_iter()
            .flatten()
            .collect()
    }
}

/// Returns the models needed for the mask texture.
/// # Panics
/// - Panics if image loading fails.
pub fn mask_texture_meshes(
    char: &NxCharInfo,
    res_texture: &ResourceTexture,
    file_texture: &[u8],
) -> MaskModels {
    let mask = MaskFaceParts::init(char, 256.0);

    let make_shape = |part: MaskFacePart, modulated: ColorModulated, tex_data: TextureElement| {
        let (vertices, indices, mtx) = quad(
            part.x,
            part.y,
            part.width,
            part.height,
            part.angle_deg,
            part.origin,
            256.0,
        );

        if part.width <= 0.0 || part.height <= 0.0 {
            return None;
        };

        let tex = tex_data.get_image(file_texture).unwrap()?;

        Some(Model2d {
            vertices,
            indices,
            tex: image::DynamicImage::ImageRgba8(tex).flipv(),
            mvp_matrix: mtx,
            modulation: modulate(modulated, char),
            opaque: None,
            label: Some(format!("{modulated:?}")),
        })
    };

    let left_eye = make_shape(
        mask.eye[0],
        ColorModulated::Eye,
        res_texture.eye[char.eye_type as usize],
    );
    let right_eye = make_shape(
        mask.eye[1],
        ColorModulated::Eye,
        res_texture.eye[char.eye_type as usize],
    );

    let left_brow = make_shape(
        mask.eyebrow[0],
        ColorModulated::Eyebrow,
        res_texture.eyebrow[char.eyebrow_type as usize],
    );
    let right_brow = make_shape(
        mask.eyebrow[1],
        ColorModulated::Eyebrow,
        res_texture.eyebrow[char.eyebrow_type as usize],
    );

    let mouth = make_shape(
        mask.mouth,
        ColorModulated::Mouth,
        res_texture.mouth[char.mouth_type as usize],
    );

    let left_mustache = make_shape(
        mask.mustache[0],
        ColorModulated::Mustache,
        res_texture.mustache[char.mustache_type as usize],
    );
    let right_mustache = make_shape(
        mask.mustache[1],
        ColorModulated::Mustache,
        res_texture.mustache[char.mustache_type as usize],
    );

    let mole = make_shape(
        mask.mole,
        ColorModulated::Mole,
        res_texture.mole[if char.mole_type == 0 { 0 } else { 1 }],
    );

    MaskModels {
        left_eye: left_eye.unwrap(),
        right_eye: right_eye.unwrap(),
        left_brow,
        right_brow,
        left_mustache,
        right_mustache,
        mouth: mouth.unwrap(),
        mole,
    }
}

/// Constructs an [MV Matrix](https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/WebGL_model_view_projection)
pub fn model_view_matrix(translation: Vec2, scale: Vec2, rot_z: f32) -> Mat4 {
    Mat4::from_scale_rotation_translation(
        (scale * vec2(TEX_SCALE_X, TEX_SCALE_Y)).extend(1.0),
        Quat::from_rotation_z(-rot_z.to_radians()),
        translation.extend(0.0),
    )
}

fn v2(x: f32, y: f32) -> [f32; 3] {
    [x, y, 0.0]
}

const OPENGL_TO_WEBGPU_Y_FLIP: Mat4 = Mat4::from_cols(Vec4::X, Vec4::NEG_Y, Vec4::Z, Vec4::W);

// RFL_MakeTex.c :817
/// Constructs a `Quad` mesh from given arguments.
/// # Panics
/// Shouldn't panic!
pub fn quad(
    x: f32,
    y: f32,
    width: f32,
    height: f32,
    rot_z: f32,
    origin: ImageOrigin,
    resolution: f32,
) -> (Vec<Vertex>, Vec<u32>, Mat4) {
    let base_x: f32;
    let s0: f32;
    let s1: f32;

    let mv_mtx = model_view_matrix(vec2(x, resolution - y), vec2(width, height), rot_z);
    // let mv_mtx = mv_mtx.transpose();

    let p_mtx = Mat4::orthographic_rh(0.0, resolution, 0.0, resolution, 200.0, -200.0);
    // let p_mtx = p_mtx.transpose();
    // let p_mtx = Matrix4::new_orthographic(0.0, resolution, 0.0, resolution, 200.0, -200.0);
    let mvp_mtx = p_mtx * mv_mtx;

    //mvp_mtx.y_axis[1] *= -1.0;

    // let mvp_mtx = Mat4 {
    //     x_axis: vec4(0.33806923, 0.146022707, 0.0, 0.0),
    //     y_axis: vec4(-0.169284195, 0.426169604, 0.00249999994, 0.0),
    //     z_axis: vec4(0.0, 0.0, 0.5, 0.0),
    //     w_axis: vec4(-0.166802764, -0.0792831778, 0.5, 1.0),
    // };
    match origin {
        ImageOrigin::Center => {
            base_x = -0.5;
            s0 = 1.0;
            s1 = 0.0;
        }
        ImageOrigin::Right => {
            base_x = -1.0;
            s0 = 1.0;
            s1 = 0.0;
        }
        ImageOrigin::Left | ImageOrigin::Ignore => {
            base_x = 0.0;
            s0 = 0.0;
            s1 = 1.0;
        }
    }

    (
        vec![
            Vertex {
                position: v2(1.0 + base_x, -0.5).map(Float16::from_f32),
                _pad: 0,
                tex_coords: [s0, 0.0].map(Float16::from_f32),
                normal: [0.0, 0.0, 0.0],
            },
            Vertex {
                position: v2(1.0 + base_x, 0.5).map(Float16::from_f32),
                _pad: 0,
                tex_coords: [s0, 1.0].map(Float16::from_f32),
                normal: [0.0, 0.0, 0.0],
            },
            Vertex {
                position: v2(base_x, 0.5).map(Float16::from_f32),
                _pad: 0,
                tex_coords: [s1, 1.0].map(Float16::from_f32),
                normal: [0.0, 0.0, 0.0],
            },
            Vertex {
                position: v2(base_x, -0.5).map(Float16::from_f32),
                _pad: 0,
                tex_coords: [s1, 0.0].map(Float16::from_f32),
                normal: [0.0, 0.0, 0.0],
            },
        ],
        vec![0, 1, 2, 0, 2, 3],
        mvp_mtx,
    )
}

/// Constructs a `Quad` mesh. Simplified case of `quad` function.
pub fn trivial_quad() -> (Vec<Vertex>, Vec<u32>) {
    (
        vec![
            Vertex {
                position: [0.5, -0.5, 0.0].map(Float16::from_f32),
                _pad: 0,
                tex_coords: [0.0, 0.0].map(Float16::from_f32),
                normal: [0.0, 0.0, 0.0],
            },
            Vertex {
                position: [0.5, 0.5, 0.0].map(Float16::from_f32),
                _pad: 0,
                tex_coords: [0.0, 1.0].map(Float16::from_f32),
                normal: [0.0, 0.0, 0.0],
            },
            Vertex {
                position: [-0.5, 0.5, 0.0].map(Float16::from_f32),
                _pad: 0,
                tex_coords: [1.0, 1.0].map(Float16::from_f32),
                normal: [0.0, 0.0, 0.0],
            },
            Vertex {
                position: [-0.5, -0.5, 0.0].map(Float16::from_f32),
                _pad: 0,
                tex_coords: [1.0, 0.0].map(Float16::from_f32),
                normal: [0.0, 0.0, 0.0],
            },
        ],
        vec![0, 1, 2, 0, 2, 3],
    )
}

/// Converts a color from BGR to RGB (and the other way around, because this operation is symmetric.)
/// Currently needed because of some BGR/RGB conversion issues.
pub fn bgr_to_rgb([b, g, r, a]: [f32; 4]) -> [f32; 4] {
    [r, g, b, a]
}

#[cfg(test)]
mod tests {
    // use crate::charinfo;
    // use crate::charinfo::nx::NxCharInfo;
    // use crate::color::nx::modulate;
    // use crate::draw::faceline::{bgr_to_rgb, trivial_quad};
    // use crate::draw::render_2d::Model2d;
    // use crate::draw::render_3d::ProgramState;
    // use crate::draw::wgpu_render::{HeadlessRenderer, Vertex, model_view_matrix, quad, texture};
    // use crate::res::shape::nx::{ResourceShape, SHAPE_MID_DAT};
    // use crate::res::tex::nx::{ResourceTexture, ResourceTextureFormat, TEXTURE_MID_SRGB_DAT};
    // use binrw::BinRead;
    // use glam::{uvec2, vec3};
    // use nalgebra::Matrix4;
    // use std::error::Error;
    // use std::{fs::File, io::BufReader};
    // use wgpu::CommandEncoder;

    // type R = Result<(), Box<dyn Error>>;

    // #[test]
    // fn faceline_makeup() -> R {
    //     let mut headless_renderer = HeadlessRenderer::new();
    //     let mut encoder: CommandEncoder = headless_renderer
    //         .device()
    //         .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });

    //     let char = NxCharInfo::read(&mut File::open("../charline.charinfo").unwrap()).unwrap();
    //     let mut bin = BufReader::new(File::open(TEXTURE_MID_SRGB_DAT)?);

    //     let res_texture = ResourceTexture::read(&mut bin)?;

    //     let tex = res_texture.makeup[char.faceline_make as usize]
    //         .get_image(&mut BufReader::new(File::open(TEXTURE_MID_SRGB_DAT)?))?;
    //     let tex = image::DynamicImage::ImageRgba8(tex.unwrap());

    //     let target_texture =
    //         texture::Texture::create_texture(&headless_renderer.device(), &uvec2(256, 512), "");

    //     Rendered2dShape::render_texture_trivial(
    //         tex,
    //         modulate(crate::color::nx::ColorModulated::FacelineMakeup, &char),
    //         Some(bgr_to_rgb(
    //             crate::color::nx::srgb::FACELINE_COLOR[usize::from(char.faceline_color)],
    //         )),
    //         &mut headless_renderer,
    //         &target_texture.view,
    //         &mut encoder,
    //     );

    //     let image = headless_renderer.output_texture(&target_texture, encoder);

    //     println!("Done!");
    //     image.save(concat!(
    //         env!("CARGO_MANIFEST_DIR"),
    //         "/test_output/faceline_makeup.png"
    //     ))?;

    //     Ok(())
    // }

    // #[test]
    // fn faceline_beard() -> R {
    //     let mut headless_renderer = HeadlessRenderer::new();
    //     let mut encoder: CommandEncoder = headless_renderer
    //         .device()
    //         .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });

    //     let char = NxCharInfo::read(&mut File::open("../testguy.charinfo").unwrap()).unwrap();
    //     let mut bin = BufReader::new(File::open(TEXTURE_MID_SRGB_DAT)?);

    //     let res_texture = ResourceTexture::read(&mut bin)?;

    //     let tex = res_texture.beard[0]
    //         .get_image(&mut BufReader::new(File::open(TEXTURE_MID_SRGB_DAT)?))?;
    //     let tex = image::DynamicImage::ImageRgba8(tex.unwrap());

    //     let target_texture =
    //         texture::Texture::create_texture(&headless_renderer.device(), &uvec2(256, 512), "");

    //     Rendered2dShape::render_texture_trivial(
    //         tex,
    //         modulate(crate::color::nx::ColorModulated::FacelineMakeup, &char),
    //         None,
    //         &mut headless_renderer,
    //         &target_texture.view,
    //         &mut encoder,
    //     );

    //     let image = headless_renderer.output_texture(&target_texture, encoder);

    //     println!("Done!");
    //     image.save(concat!(
    //         env!("CARGO_MANIFEST_DIR"),
    //         "/test_output/faceline_beard.png"
    //     ))?;

    //     Ok(())
    // }

    // use crate::draw::mask::MaskFaceParts;
    // use crate::res::shape::nx::{ResourceShape, SHAPE_MID_DAT};
    // use crate::res::tex::nx::{ResourceTexture, TEXTURE_MID_SRGB_DAT};
    // use binrw::BinRead;
    // use glam::uvec2;
    // use image_compare::Algorithm;

    // use super::*;
    // use std::{error::Error, fs::File, io::BufReader};

    // type R = Result<(), Box<dyn Error>>;

    // #[test]
    // #[allow(clippy::too_many_lines)]
    // fn test_render() -> R {
    //     let mut tex_file = BufReader::new(File::open(TEXTURE_MID_SRGB_DAT)?);
    //     let mut tex_shape = BufReader::new(File::open(SHAPE_MID_DAT)?);

    //     let mut char =
    //         File::open(concat!(env!("CARGO_MANIFEST_DIR"), "/../Jasmine.charinfo")).unwrap();
    //     let char = NxCharInfo::read(&mut char).unwrap();

    //     let image = pollster::block_on(render_context_wgpu(RenderContext::new(
    //         // &FaceParts::init(&char, 256.0),
    //         &char,
    //         (&mut tex_shape, &mut tex_file),
    //     )?));
    //     let image = image.flipv();

    //     image.save(concat!(
    //         env!("CARGO_MANIFEST_DIR"),
    //         "/test_output/mask-rendered.png"
    //     ))?;

    //     let reference_image = image::open(concat!(
    //         env!("CARGO_MANIFEST_DIR"),
    //         "/test_data/jasmine-mask.png"
    //     ))
    //     .unwrap();

    //     let similarity = image_compare::rgb_hybrid_compare(
    //         &image.clone().into_rgb8(),
    //         &reference_image.clone().into_rgb8(),
    //     )
    //     .expect("wrong size!");

    //     similarity
    //         .image
    //         .to_color_map()
    //         .save(concat!(
    //             env!("CARGO_MANIFEST_DIR"),
    //             "/test_output/mask-similarity.png"
    //         ))
    //         .unwrap();

    //     let similarity = image_compare::gray_similarity_structure(
    //         &Algorithm::MSSIMSimple,
    //         &image.into_luma8(),
    //         &reference_image.into_luma8(),
    //     )
    //     .expect("wrong size!");

    //     similarity
    //         .image
    //         .to_color_map()
    //         .save(concat!(
    //             env!("CARGO_MANIFEST_DIR"),
    //             "/test_output/mask-similarity-grey.png"
    //         ))
    //         .unwrap();

    //     Ok(())
    // }
}