use rust-embed for assets
This commit is contained in:
parent
a99b018ec1
commit
8947398ad0
11 changed files with 142 additions and 32 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,2 +1,2 @@
|
|||
/target
|
||||
/res
|
||||
/res/models
|
||||
|
|
120
Cargo.lock
generated
120
Cargo.lock
generated
|
@ -130,6 +130,15 @@ version = "0.1.6"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a"
|
||||
|
||||
[[package]]
|
||||
name = "block-buffer"
|
||||
version = "0.10.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "69cce20737498f97b993470a6e536b8523f0af7892a4f928cceb1ac5e52ebe7e"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.12.0"
|
||||
|
@ -312,6 +321,15 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cpufeatures"
|
||||
version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.3.2"
|
||||
|
@ -393,6 +411,16 @@ version = "0.2.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
|
||||
|
||||
[[package]]
|
||||
name = "crypto-common"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
"typenum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cty"
|
||||
version = "0.2.2"
|
||||
|
@ -455,6 +483,16 @@ dependencies = [
|
|||
"byteorder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "digest"
|
||||
version = "0.10.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f"
|
||||
dependencies = [
|
||||
"block-buffer",
|
||||
"crypto-common",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dispatch"
|
||||
version = "0.2.0"
|
||||
|
@ -675,6 +713,16 @@ dependencies = [
|
|||
"byteorder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.14.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9"
|
||||
dependencies = [
|
||||
"typenum",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.8"
|
||||
|
@ -1565,12 +1613,47 @@ dependencies = [
|
|||
"log",
|
||||
"pollster",
|
||||
"regex",
|
||||
"rust-embed",
|
||||
"tobj",
|
||||
"wgpu",
|
||||
"wgpu-types",
|
||||
"winit",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust-embed"
|
||||
version = "6.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "283ffe2f866869428c92e0d61c2f35dfb4355293cdfdc48f49e895c15f1333d1"
|
||||
dependencies = [
|
||||
"rust-embed-impl",
|
||||
"rust-embed-utils",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust-embed-impl"
|
||||
version = "6.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "31ab23d42d71fb9be1b643fe6765d292c5e14d46912d13f3ae2815ca048ea04d"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"rust-embed-utils",
|
||||
"syn",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust-embed-utils"
|
||||
version = "7.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c1669d81dfabd1b5f8e2856b8bbe146c6192b0ba22162edc738ac0a5de18f054"
|
||||
dependencies = [
|
||||
"sha2",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hash"
|
||||
version = "1.1.0"
|
||||
|
@ -1606,6 +1689,15 @@ dependencies = [
|
|||
"bytemuck",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "same-file"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scoped-tls"
|
||||
version = "1.0.1"
|
||||
|
@ -1685,6 +1777,17 @@ dependencies = [
|
|||
"pkg-config",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sha2"
|
||||
version = "0.10.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cpufeatures",
|
||||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "slotmap"
|
||||
version = "1.0.6"
|
||||
|
@ -1861,6 +1964,12 @@ dependencies = [
|
|||
"toml_datetime",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typenum"
|
||||
version = "1.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.6"
|
||||
|
@ -1891,6 +2000,17 @@ version = "0.9.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
|
||||
dependencies = [
|
||||
"same-file",
|
||||
"winapi",
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
|
|
|
@ -19,6 +19,7 @@ tobj = { version = "3.2.1", features = ["async"] }
|
|||
gltf = "1.0.0"
|
||||
wgpu-types = "0.14.1"
|
||||
regex = "1.7.1"
|
||||
rust-embed = "6.4.2"
|
||||
|
||||
[build-dependencies]
|
||||
anyhow = "1.0.68"
|
||||
|
|
18
build.rs
18
build.rs
|
@ -1,18 +0,0 @@
|
|||
#![allow(clippy::needless_return)]
|
||||
|
||||
use anyhow::*;
|
||||
use fs_extra::copy_items;
|
||||
use fs_extra::dir::CopyOptions;
|
||||
use std::env;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
println!("cargo:rerun-if-changed=res/**/*");
|
||||
|
||||
let out_dir = env::var("OUT_DIR")?;
|
||||
let mut copy_options = CopyOptions::new();
|
||||
copy_options.overwrite = true;
|
||||
let paths_to_copy = vec!["res/"];
|
||||
copy_items(&paths_to_copy, out_dir, ©_options)?;
|
||||
|
||||
return Ok(());
|
||||
}
|
|
@ -1,13 +1,20 @@
|
|||
use std::path::PathBuf;
|
||||
use wgpu::util::DeviceExt;
|
||||
use rust_embed::RustEmbed;
|
||||
|
||||
use crate::core::model::{Material, Mesh, Model, ModelVertex};
|
||||
use crate::core::texture::Texture;
|
||||
|
||||
pub fn get_resource_path(file_name: &str) -> PathBuf {
|
||||
return std::path::Path::new(env!("OUT_DIR"))
|
||||
.join("res")
|
||||
.join(file_name);
|
||||
#[derive(RustEmbed)]
|
||||
#[folder = "res"]
|
||||
struct Asset;
|
||||
|
||||
pub fn load_binary(file_name: &str) -> Vec<u8> {
|
||||
Asset::get(file_name).unwrap().data.into_owned()
|
||||
}
|
||||
|
||||
pub fn load_string(file_name: &str) -> String {
|
||||
let binary = Asset::get(file_name).unwrap();
|
||||
std::str::from_utf8(binary.data.as_ref()).unwrap().to_owned()
|
||||
}
|
||||
|
||||
pub async fn load_model_gltf(
|
||||
|
@ -20,7 +27,8 @@ pub async fn load_model_gltf(
|
|||
let mut meshes = Vec::new();
|
||||
|
||||
println!("gltf: Loading file {}", file_name);
|
||||
let (document, buffers, mut images) = gltf::import(get_resource_path(file_name))?;
|
||||
let binary = Asset::get(file_name).unwrap();
|
||||
let (document, buffers, mut images) = gltf::import_slice(binary.data.as_ref())?;
|
||||
|
||||
println!("gltf: Loading meshes");
|
||||
for mesh in document.meshes() {
|
||||
|
|
|
@ -210,7 +210,7 @@ impl State {
|
|||
});
|
||||
|
||||
let obj_model = resources::load_model_gltf(
|
||||
"Sponza.glb",
|
||||
"models/Sponza.glb",
|
||||
&device,
|
||||
&queue,
|
||||
&texture_bind_group_layout,
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
use wgpu;
|
||||
use regex::Regex;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
use crate::core::resources::load_string;
|
||||
|
||||
pub fn preprocess_wgsl(filename: &str) -> wgpu::ShaderSource {
|
||||
let source_path = env!("CARGO_MANIFEST_DIR").to_owned() + "/src/shaders/wgsl/" + filename;
|
||||
let source_path = "shaders/".to_owned() + filename;
|
||||
println!("preprocess_wgsl: loading source {}", source_path);
|
||||
let mut source =
|
||||
read_to_string(&source_path)
|
||||
.unwrap();
|
||||
let mut source = load_string(&source_path);
|
||||
|
||||
let re = Regex::new(r"#include (.*?)\n").unwrap();
|
||||
for cap in re.captures_iter(&source.clone()) {
|
||||
|
@ -16,7 +15,7 @@ pub fn preprocess_wgsl(filename: &str) -> wgpu::ShaderSource {
|
|||
full_path = full_path.replace(filename, &cap[1]);
|
||||
|
||||
println!("preprocess_wgsl: replacing {} with file {}", whole_match, full_path);
|
||||
let nested_source = read_to_string(full_path).unwrap();
|
||||
let nested_source = load_string(&full_path);
|
||||
source = source.replace(whole_match, &nested_source);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue