|
|
|
@ -1,19 +1,20 @@
|
|
|
|
|
use std::io::{BufReader, Cursor};
|
|
|
|
|
|
|
|
|
|
use cfg_if::cfg_if;
|
|
|
|
|
use tobj::LoadError;
|
|
|
|
|
use wgpu::util::DeviceExt;
|
|
|
|
|
|
|
|
|
|
use crate::{texture, model};
|
|
|
|
|
use crate::{model, texture};
|
|
|
|
|
|
|
|
|
|
pub async fn load_string(file_name: &str) -> anyhow::Result<String> {
|
|
|
|
|
cfg_if! {
|
|
|
|
|
if #[cfg(target_arch = "wasm32")] {
|
|
|
|
|
log::warn!("Creating url for {}", file_name);
|
|
|
|
|
let url = format!("http://127.0.0.1:8080/learn-wgpu/{}", file_name);
|
|
|
|
|
let txt = reqwest::get(url)
|
|
|
|
|
.await?
|
|
|
|
|
.text()
|
|
|
|
|
.await?;
|
|
|
|
|
log::warn!("Making request for {}", url);
|
|
|
|
|
let res = reqwest::get(&url).await?;
|
|
|
|
|
log::warn!("Recieved response");
|
|
|
|
|
let txt = res.text().await?;
|
|
|
|
|
log::warn!("Finished request for {}", url);
|
|
|
|
|
} else {
|
|
|
|
|
let path = std::path::Path::new(env!("OUT_DIR"))
|
|
|
|
|
.join("res")
|
|
|
|
@ -41,51 +42,50 @@ pub async fn load_binary(file_name: &str) -> anyhow::Result<Vec<u8>> {
|
|
|
|
|
let data = std::fs::read(path)?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn load_texture(
|
|
|
|
|
file_name: &str,
|
|
|
|
|
device: &wgpu::Device,
|
|
|
|
|
queue: &wgpu::Queue
|
|
|
|
|
queue: &wgpu::Queue,
|
|
|
|
|
) -> anyhow::Result<texture::Texture> {
|
|
|
|
|
let data = load_binary(file_name).await?;
|
|
|
|
|
texture::Texture::from_bytes(device, queue, &data, file_name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn load_model(
|
|
|
|
|
file_name: &str,
|
|
|
|
|
device: &wgpu::Device,
|
|
|
|
|
queue: &wgpu::Queue,
|
|
|
|
|
layout: &wgpu::BindGroupLayout,
|
|
|
|
|
) -> anyhow::Result<model::Model> {
|
|
|
|
|
log::warn!("Loading obj text");
|
|
|
|
|
let obj_text = load_string(file_name).await?;
|
|
|
|
|
let obj_cursor = Cursor::new(obj_text);
|
|
|
|
|
let mut obj_reader = BufReader::new(obj_cursor);
|
|
|
|
|
|
|
|
|
|
log::warn!("Loading model: {}", file_name);
|
|
|
|
|
let (models, materials) = tobj::load_obj_buf(
|
|
|
|
|
log::warn!("Loading model");
|
|
|
|
|
let (models, obj_materials) = tobj::load_obj_buf_async(
|
|
|
|
|
&mut obj_reader,
|
|
|
|
|
&tobj::LoadOptions {
|
|
|
|
|
triangulate: true,
|
|
|
|
|
single_index: true,
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
|p| {
|
|
|
|
|
log::warn!("Loading material: {:?}", &p);
|
|
|
|
|
let mat_path = p.to_str().ok_or(tobj::LoadError::OpenFileFailed)?;
|
|
|
|
|
let mat_text = pollster::block_on(load_string(mat_path)).or_else(|_| Err(LoadError::OpenFileFailed))?;
|
|
|
|
|
let mat_cursor = Cursor::new(mat_text);
|
|
|
|
|
let mut mat_reader = BufReader::new(mat_cursor);
|
|
|
|
|
tobj::load_mtl_buf(&mut mat_reader)
|
|
|
|
|
}
|
|
|
|
|
)?;
|
|
|
|
|
|p| async move {
|
|
|
|
|
log::warn!("Loading material");
|
|
|
|
|
let mat_text = load_string(&p).await.unwrap();
|
|
|
|
|
tobj::load_mtl_buf(&mut BufReader::new(Cursor::new(mat_text)))
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
log::warn!("Creating materials");
|
|
|
|
|
let materials = materials?.into_iter().map(|m| {
|
|
|
|
|
let diffuse_texture = pollster::block_on(load_texture(&m.diffuse_texture, device, queue))?;
|
|
|
|
|
log::warn!("Processing materials");
|
|
|
|
|
let mut materials = Vec::new();
|
|
|
|
|
for m in obj_materials? {
|
|
|
|
|
let diffuse_texture = load_texture(&m.diffuse_texture, device, queue).await?;
|
|
|
|
|
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
|
|
|
layout,
|
|
|
|
|
entries: &[
|
|
|
|
@ -101,53 +101,53 @@ pub async fn load_model(
|
|
|
|
|
label: None,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Ok(model::Material {
|
|
|
|
|
materials.push(model::Material {
|
|
|
|
|
name: m.name,
|
|
|
|
|
diffuse_texture,
|
|
|
|
|
bind_group,
|
|
|
|
|
})
|
|
|
|
|
}).collect::<anyhow::Result<Vec<_>>>()?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log::warn!("Creating meshes");
|
|
|
|
|
let meshes = models.into_iter().map(|m| {
|
|
|
|
|
let vertices = (0..m.mesh.positions.len() / 3).map(|i| {
|
|
|
|
|
model::ModelVertex {
|
|
|
|
|
position: [
|
|
|
|
|
m.mesh.positions[i * 3],
|
|
|
|
|
m.mesh.positions[i * 3 + 1],
|
|
|
|
|
m.mesh.positions[i * 3 + 2],
|
|
|
|
|
],
|
|
|
|
|
tex_coords: [
|
|
|
|
|
m.mesh.texcoords[i * 2],
|
|
|
|
|
m.mesh.texcoords[i * 2 + 1],
|
|
|
|
|
],
|
|
|
|
|
normal: [
|
|
|
|
|
m.mesh.normals[i * 3],
|
|
|
|
|
m.mesh.normals[i * 3 + 1],
|
|
|
|
|
m.mesh.normals[i * 3 + 2],
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}).collect::<Vec<_>>();
|
|
|
|
|
let meshes = models
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|m| {
|
|
|
|
|
let vertices = (0..m.mesh.positions.len() / 3)
|
|
|
|
|
.map(|i| model::ModelVertex {
|
|
|
|
|
position: [
|
|
|
|
|
m.mesh.positions[i * 3],
|
|
|
|
|
m.mesh.positions[i * 3 + 1],
|
|
|
|
|
m.mesh.positions[i * 3 + 2],
|
|
|
|
|
],
|
|
|
|
|
tex_coords: [m.mesh.texcoords[i * 2], m.mesh.texcoords[i * 2 + 1]],
|
|
|
|
|
normal: [
|
|
|
|
|
m.mesh.normals[i * 3],
|
|
|
|
|
m.mesh.normals[i * 3 + 1],
|
|
|
|
|
m.mesh.normals[i * 3 + 2],
|
|
|
|
|
],
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
|
|
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some(&format!("{:?} Vertex Buffer", file_name)),
|
|
|
|
|
contents: bytemuck::cast_slice(&vertices),
|
|
|
|
|
usage: wgpu::BufferUsages::VERTEX,
|
|
|
|
|
});
|
|
|
|
|
let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some(&format!("{:?} Index Buffer", file_name)),
|
|
|
|
|
contents: bytemuck::cast_slice(&m.mesh.indices),
|
|
|
|
|
usage: wgpu::BufferUsages::INDEX,
|
|
|
|
|
});
|
|
|
|
|
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some(&format!("{:?} Vertex Buffer", file_name)),
|
|
|
|
|
contents: bytemuck::cast_slice(&vertices),
|
|
|
|
|
usage: wgpu::BufferUsages::VERTEX,
|
|
|
|
|
});
|
|
|
|
|
let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some(&format!("{:?} Index Buffer", file_name)),
|
|
|
|
|
contents: bytemuck::cast_slice(&m.mesh.indices),
|
|
|
|
|
usage: wgpu::BufferUsages::INDEX,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
model::Mesh {
|
|
|
|
|
name: file_name.to_string(),
|
|
|
|
|
vertex_buffer,
|
|
|
|
|
index_buffer,
|
|
|
|
|
num_elements: m.mesh.indices.len() as u32,
|
|
|
|
|
material: m.mesh.material_id.unwrap_or(0),
|
|
|
|
|
}
|
|
|
|
|
}).collect::<Vec<_>>();
|
|
|
|
|
model::Mesh {
|
|
|
|
|
name: file_name.to_string(),
|
|
|
|
|
vertex_buffer,
|
|
|
|
|
index_buffer,
|
|
|
|
|
num_elements: m.mesh.indices.len() as u32,
|
|
|
|
|
material: m.mesh.material_id.unwrap_or(0),
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
|
|
log::warn!("Done with model: {}", file_name);
|
|
|
|
|
Ok(model::Model { meshes, materials })
|
|
|
|
|