Skip to content

Commit

Permalink
embed assets into the binary
Browse files Browse the repository at this point in the history
  • Loading branch information
erenoku committed Feb 11, 2023
1 parent e755f65 commit e6e76d2
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 11 deletions.
59 changes: 59 additions & 0 deletions crates/fj-viewer/src/assets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
pub struct Assets<'a> {
pub cube_obj: &'a [u8],
pub cube_mtl: &'a [u8],
pub front_texture: &'a [u8],
pub right_texture: &'a [u8],
pub rear_texture: &'a [u8],
pub left_texture: &'a [u8],
pub top_texture: &'a [u8],
pub bottom_texture: &'a [u8],
}

impl<'a> Assets<'a> {
pub fn get_instance() -> Self {
let cube_obj: &[u8] =
include_bytes!("../../../assets/navigation_cube/cube.obj");
let cube_mtl: &[u8] =
include_bytes!("../../../assets/navigation_cube/cube.mtl");
let front_texture: &[u8] =
include_bytes!("../../../assets/navigation_cube/front.png");
let right_texture: &[u8] =
include_bytes!("../../../assets/navigation_cube/right.png");
let rear_texture: &[u8] =
include_bytes!("../../../assets/navigation_cube/rear.png");
let left_texture: &[u8] =
include_bytes!("../../../assets/navigation_cube/left.png");
let top_texture: &[u8] =
include_bytes!("../../../assets/navigation_cube/top.png");
let bottom_texture: &[u8] =
include_bytes!("../../../assets/navigation_cube/bottom.png");

Self {
cube_obj,
cube_mtl,
front_texture,
right_texture,
rear_texture,
left_texture,
top_texture,
bottom_texture,
}
}

pub fn get_asset(&self, file_name: &str) -> &[u8] {
match file_name {
"cube.obj" => self.cube_obj,
"cube.mtl" => self.cube_mtl,
"front.png" => self.front_texture,
"right.png" => self.right_texture,
"rear.png" => self.rear_texture,
"left.png" => self.left_texture,
"top.png" => self.top_texture,
"bottom.png" => self.bottom_texture,
_ => unreachable!(
"An unknown asset: {} is trying to be loaded",
file_name
),
}
}
}
30 changes: 19 additions & 11 deletions crates/fj-viewer/src/graphics/model.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::{io, ops::Range};
use std::ops::Range;

use tobj::LoadError;
use wgpu::util::DeviceExt;

use super::texture::{self, LoadTextureError};
use crate::assets::Assets;

#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
Expand Down Expand Up @@ -63,9 +64,6 @@ pub enum LoadModelError {

#[error("Load texture error")]
Texture(#[from] LoadTextureError),

#[error("Load reading object file error")]
ReadFile(#[from] io::Error),
}

pub fn load_model(
Expand All @@ -74,25 +72,35 @@ pub fn load_model(
queue: &wgpu::Queue,
layout: &wgpu::BindGroupLayout,
) -> Result<Model, LoadModelError> {
let (models, obj_materials) = tobj::load_obj(
std::path::Path::new("assets/navigation_cube").join(file_name),
let assets = Assets::get_instance();

let (models, obj_materials) = tobj::load_obj_buf(
&mut assets.get_asset("cube.obj"),
&tobj::LoadOptions {
triangulate: true,
single_index: true,
..Default::default()
},
|p| {
tobj::load_mtl_buf(
&mut assets.get_asset(
p.file_name()
.unwrap()
.to_str()
.expect("OsStr could not be converted to a str"),
),
)
},
)?;

let mut materials = Vec::new();
for m in obj_materials? {
let texture_data = std::fs::read(
std::path::Path::new("assets/navigation_cube")
.join(m.diffuse_texture),
)?;
let texture_data: &[u8] = assets.get_asset(m.diffuse_texture.as_str());

let diffuse_texture = texture::Texture::from_bytes(
device,
queue,
&texture_data,
texture_data,
file_name,
)?;

Expand Down
1 change: 1 addition & 0 deletions crates/fj-viewer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#![warn(missing_docs)]

mod assets;
mod camera;
mod graphics;
mod gui;
Expand Down

0 comments on commit e6e76d2

Please sign in to comment.