mirror of
https://github.com/lisk77/comet.git
synced 2025-10-24 13:58:49 +00:00
initial commit
This commit is contained in:
commit
6154c72b0e
55 changed files with 9481 additions and 0 deletions
146
crates/comet_resources/src/lib.rs
Normal file
146
crates/comet_resources/src/lib.rs
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
pub use resources::*;
|
||||
pub use texture::*;
|
||||
pub use vertex::*;
|
||||
|
||||
pub mod resources;
|
||||
pub mod texture;
|
||||
pub mod vertex;
|
||||
pub mod texture_atlas;
|
||||
/*use std::io::{BufReader, Cursor};
|
||||
use wgpu::util::DeviceExt;
|
||||
|
||||
use crate::{model, texture};
|
||||
|
||||
pub async fn load_string(file_name: &str) -> anyhow::Result<String> {
|
||||
let path = std::path::Path::new(env!("OUT_DIR"))
|
||||
.join("res")
|
||||
.join(file_name);
|
||||
let txt = std::fs::read_to_string(path)?;
|
||||
|
||||
Ok(txt)
|
||||
}
|
||||
|
||||
pub async fn load_binary(file_name: &str) -> anyhow::Result<Vec<u8>> {
|
||||
let path = std::path::Path::new(env!("OUT_DIR"))
|
||||
.join("res")
|
||||
.join(file_name);
|
||||
let data = std::fs::read(path)?;
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
pub async fn load_texture(
|
||||
file_name: &str,
|
||||
device: &wgpu::Device,
|
||||
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> {
|
||||
let obj_text = load_string(file_name).await?;
|
||||
let obj_cursor = Cursor::new(obj_text);
|
||||
let mut obj_reader = BufReader::new(obj_cursor);
|
||||
|
||||
let (models, obj_materials) = tobj::load_obj_buf_async(
|
||||
&mut obj_reader,
|
||||
&tobj::LoadOptions {
|
||||
triangulate: true,
|
||||
single_index: true,
|
||||
..Default::default()
|
||||
},
|
||||
|p| async move {
|
||||
let mat_text = load_string(&p).await.unwrap();
|
||||
tobj::load_mtl_buf(&mut BufReader::new(Cursor::new(mat_text)))
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
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: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::TextureView(&diffuse_texture.view),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: wgpu::BindingResource::Sampler(&diffuse_texture.sampler),
|
||||
},
|
||||
],
|
||||
label: None,
|
||||
});
|
||||
|
||||
materials.push(model::Material {
|
||||
name: m.name,
|
||||
diffuse_texture,
|
||||
bind_group,
|
||||
})
|
||||
}
|
||||
|
||||
let meshes = models
|
||||
.into_iter()
|
||||
.map(|m| {
|
||||
let vertices = (0..m.mesh.positions.len() / 3)
|
||||
.map(|i| {
|
||||
if m.mesh.normals.is_empty(){
|
||||
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], 1.0 - m.mesh.texcoords[i * 2 + 1]],
|
||||
normal: [0.0, 0.0, 0.0],
|
||||
}
|
||||
}else{
|
||||
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], 1.0 - 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,
|
||||
});
|
||||
|
||||
log::info!("Mesh: {}", m.name);
|
||||
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<_>>();
|
||||
|
||||
Ok(model::Model { meshes, materials })
|
||||
}*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue