mirror of https://github.com/sotrh/learn-wgpu
testing some things
parent
b8b8444030
commit
c269f46a0e
@ -1,32 +0,0 @@
|
||||
matrix:
|
||||
include:
|
||||
- language: rust
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- libasound2-dev
|
||||
rust:
|
||||
- stable
|
||||
# - beta
|
||||
# - nightly
|
||||
jobs:
|
||||
# allow_failures:
|
||||
# - rust: nightly
|
||||
fast_finish: true
|
||||
scripts:
|
||||
- cargo build --verbose
|
||||
- language: node_js
|
||||
node_js:
|
||||
- lts/*
|
||||
install:
|
||||
- npm ci
|
||||
script:
|
||||
- npm run build
|
||||
deploy:
|
||||
provider: pages
|
||||
skip_cleanup: true
|
||||
local_dir: docs/.vuepress/dist
|
||||
github_token: $GITHUB_TOKEN # A token generated on GitHub allowing Travis to push code on you repository. Set in the Travis settings page of your repository, as a secure variable
|
||||
keep_history: true
|
||||
on:
|
||||
branch: master
|
@ -1,8 +0,0 @@
|
||||
#version 450
|
||||
|
||||
layout(location=0) in vec3 v_color;
|
||||
layout(location=0) out vec4 f_color;
|
||||
|
||||
void main() {
|
||||
f_color = vec4(v_color, 1.0);
|
||||
}
|
Binary file not shown.
@ -1,27 +0,0 @@
|
||||
#version 450
|
||||
|
||||
layout(location=0) in vec3 a_position;
|
||||
|
||||
layout(location=0) out vec3 v_color;
|
||||
|
||||
layout(set=0, binding=0)
|
||||
uniform Camera {
|
||||
vec3 u_view_position; // unused
|
||||
mat4 u_view_proj;
|
||||
};
|
||||
|
||||
layout(set=1, binding=0)
|
||||
uniform Light {
|
||||
vec3 u_position;
|
||||
vec3 u_color;
|
||||
};
|
||||
|
||||
// Let's keep our light smaller than our other objects
|
||||
float scale = 0.25;
|
||||
|
||||
void main() {
|
||||
vec3 v_position = a_position * scale + u_position;
|
||||
gl_Position = u_view_proj * vec4(v_position, 1);
|
||||
|
||||
v_color = u_color;
|
||||
}
|
Binary file not shown.
@ -1,47 +0,0 @@
|
||||
#version 450
|
||||
|
||||
layout(location=0) in vec2 v_tex_coords;
|
||||
layout(location=1) in vec3 v_normal;
|
||||
layout(location=2) in vec3 v_position;
|
||||
|
||||
layout(location=0) out vec4 f_color;
|
||||
|
||||
layout(set = 0, binding = 0) uniform texture2D t_diffuse;
|
||||
layout(set = 0, binding = 1) uniform sampler s_diffuse;
|
||||
|
||||
layout(set=1, binding=0)
|
||||
uniform Camera {
|
||||
vec3 u_view_position;
|
||||
mat4 u_view_proj; // unused
|
||||
};
|
||||
|
||||
layout(set=2, binding=0) uniform Light {
|
||||
vec3 light_position;
|
||||
vec3 light_color;
|
||||
};
|
||||
|
||||
void main() {
|
||||
vec4 object_color = texture(sampler2D(t_diffuse, s_diffuse), v_tex_coords);
|
||||
|
||||
// We don't need (or want) much ambient light, so 0.1 is fine
|
||||
float ambient_strength = 0.1;
|
||||
vec3 ambient_color = light_color * ambient_strength;
|
||||
|
||||
vec3 normal = normalize(v_normal);
|
||||
vec3 light_dir = normalize(light_position - v_position);
|
||||
|
||||
float diffuse_strength = max(dot(normal, light_dir), 0.0);
|
||||
vec3 diffuse_color = light_color * diffuse_strength;
|
||||
|
||||
vec3 view_dir = normalize(u_view_position - v_position);
|
||||
vec3 half_dir = normalize(view_dir + light_dir);
|
||||
|
||||
float specular_strength = pow(max(dot(normal, half_dir), 0.0), 32);
|
||||
vec3 specular_color = specular_strength * light_color;
|
||||
|
||||
vec3 result = (ambient_color + diffuse_color + specular_color) * object_color.xyz;
|
||||
|
||||
// Since lights don't typically (afaik) cast transparency, so we use
|
||||
// the alpha here at the end.
|
||||
f_color = vec4(result, object_color.a);
|
||||
}
|
Binary file not shown.
@ -1,37 +0,0 @@
|
||||
#version 450
|
||||
|
||||
layout(location=0) in vec3 a_position;
|
||||
layout(location=1) in vec2 a_tex_coords;
|
||||
layout(location=2) in vec3 a_normal;
|
||||
|
||||
layout(location=0) out vec2 v_tex_coords;
|
||||
layout(location=1) out vec3 v_normal;
|
||||
layout(location=2) out vec3 v_position;
|
||||
|
||||
layout(set=1, binding=0)
|
||||
uniform Camera {
|
||||
vec3 u_view_position; // unused
|
||||
mat4 u_view_proj;
|
||||
};
|
||||
|
||||
layout(location=5) in vec4 model_matrix_0;
|
||||
layout(location=6) in vec4 model_matrix_1;
|
||||
layout(location=7) in vec4 model_matrix_2;
|
||||
layout(location=8) in vec4 model_matrix_3;
|
||||
|
||||
void main() {
|
||||
mat4 model_matrix = mat4(
|
||||
model_matrix_0,
|
||||
model_matrix_1,
|
||||
model_matrix_2,
|
||||
model_matrix_3
|
||||
);
|
||||
v_tex_coords = a_tex_coords;
|
||||
mat3 normal_matrix = mat3(transpose(inverse(model_matrix)));
|
||||
v_normal = normal_matrix * a_normal;
|
||||
|
||||
vec4 model_space = model_matrix * vec4(a_position, 1.0);
|
||||
v_position = model_space.xyz;
|
||||
|
||||
gl_Position = u_view_proj * model_space;
|
||||
}
|
Binary file not shown.
@ -0,0 +1,205 @@
|
||||
use std::io::{BufReader, Cursor};
|
||||
|
||||
use cfg_if::cfg_if;
|
||||
use wgpu::util::DeviceExt;
|
||||
|
||||
use crate::{model, texture};
|
||||
|
||||
pub async fn load_string(file_name: &str) -> anyhow::Result<String> {
|
||||
cfg_if! {
|
||||
if #[cfg(target_arch = "wasm32")] {
|
||||
let url = format!("http://127.0.0.1:8080/learn-wgpu/{}", file_name);
|
||||
let txt = reqwest::get(&url)
|
||||
.await?
|
||||
.text()
|
||||
.await?;
|
||||
} else {
|
||||
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>> {
|
||||
cfg_if! {
|
||||
if #[cfg(target_arch = "wasm32")] {
|
||||
let url = format!("http://127.0.0.1:8080/learn-wgpu/{}", file_name);
|
||||
let data = reqwest::get(url)
|
||||
.await?
|
||||
.bytes()
|
||||
.await?
|
||||
.to_vec();
|
||||
} else {
|
||||
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,
|
||||
is_normal_map: bool,
|
||||
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, is_normal_map)
|
||||
}
|
||||
|
||||
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, false, device, queue).await?;
|
||||
let normal_texture = load_texture(&m.normal_texture, true, device, queue).await?;
|
||||
|
||||
materials.push(model::Material::new(
|
||||
device,
|
||||
&m.name,
|
||||
diffuse_texture,
|
||||
normal_texture,
|
||||
layout,
|
||||
));
|
||||
}
|
||||
|
||||
let meshes = models
|
||||
.into_iter()
|
||||
.map(|m| {
|
||||
let mut 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],
|
||||
],
|
||||
// We'll calculate these later
|
||||
tangent: [0.0; 3],
|
||||
bitangent: [0.0; 3],
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let indices = &m.mesh.indices;
|
||||
let mut triangles_included = vec![0; vertices.len()];
|
||||
|
||||
// Calculate tangents and bitangets. We're going to
|
||||
// use the triangles, so we need to loop through the
|
||||
// indices in chunks of 3
|
||||
for c in indices.chunks(3) {
|
||||
let v0 = vertices[c[0] as usize];
|
||||
let v1 = vertices[c[1] as usize];
|
||||
let v2 = vertices[c[2] as usize];
|
||||
|
||||
let pos0: cgmath::Vector3<_> = v0.position.into();
|
||||
let pos1: cgmath::Vector3<_> = v1.position.into();
|
||||
let pos2: cgmath::Vector3<_> = v2.position.into();
|
||||
|
||||
let uv0: cgmath::Vector2<_> = v0.tex_coords.into();
|
||||
let uv1: cgmath::Vector2<_> = v1.tex_coords.into();
|
||||
let uv2: cgmath::Vector2<_> = v2.tex_coords.into();
|
||||
|
||||
// Calculate the edges of the triangle
|
||||
let delta_pos1 = pos1 - pos0;
|
||||
let delta_pos2 = pos2 - pos0;
|
||||
|
||||
// This will give us a direction to calculate the
|
||||
// tangent and bitangent
|
||||
let delta_uv1 = uv1 - uv0;
|
||||
let delta_uv2 = uv2 - uv0;
|
||||
|
||||
// Solving the following system of equations will
|
||||
// give us the tangent and bitangent.
|
||||
// delta_pos1 = delta_uv1.x * T + delta_u.y * B
|
||||
// delta_pos2 = delta_uv2.x * T + delta_uv2.y * B
|
||||
// Luckily, the place I found this equation provided
|
||||
// the solution!
|
||||
let r = 1.0 / (delta_uv1.x * delta_uv2.y - delta_uv1.y * delta_uv2.x);
|
||||
let tangent = (delta_pos1 * delta_uv2.y - delta_pos2 * delta_uv1.y) * r;
|
||||
let bitangent = (delta_pos2 * delta_uv1.x - delta_pos1 * delta_uv2.x) * r;
|
||||
|
||||
// We'll use the same tangent/bitangent for each vertex in the triangle
|
||||
vertices[c[0] as usize].tangent =
|
||||
(tangent + cgmath::Vector3::from(vertices[c[0] as usize].tangent)).into();
|
||||
vertices[c[1] as usize].tangent =
|
||||
(tangent + cgmath::Vector3::from(vertices[c[1] as usize].tangent)).into();
|
||||
vertices[c[2] as usize].tangent =
|
||||
(tangent + cgmath::Vector3::from(vertices[c[2] as usize].tangent)).into();
|
||||
vertices[c[0] as usize].bitangent =
|
||||
(bitangent + cgmath::Vector3::from(vertices[c[0] as usize].bitangent)).into();
|
||||
vertices[c[1] as usize].bitangent =
|
||||
(bitangent + cgmath::Vector3::from(vertices[c[1] as usize].bitangent)).into();
|
||||
vertices[c[2] as usize].bitangent =
|
||||
(bitangent + cgmath::Vector3::from(vertices[c[2] as usize].bitangent)).into();
|
||||
|
||||
// Used to average the tangents/bitangents
|
||||
triangles_included[c[0] as usize] += 1;
|
||||
triangles_included[c[1] as usize] += 1;
|
||||
triangles_included[c[2] as usize] += 1;
|
||||
}
|
||||
|
||||
// Average the tangents/bitangents
|
||||
for (i, n) in triangles_included.into_iter().enumerate() {
|
||||
let denom = 1.0 / n as f32;
|
||||
let mut v = &mut vertices[i];
|
||||
v.tangent = (cgmath::Vector3::from(v.tangent) * denom).into();
|
||||
v.bitangent = (cgmath::Vector3::from(v.bitangent) * denom).into();
|
||||
}
|
||||
|
||||
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<_>>();
|
||||
|
||||
Ok(model::Model { meshes, materials })
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
# abort on errors
|
||||
set -e
|
||||
|
||||
# build
|
||||
npm run build
|
||||
|
||||
# navigate into the build output directory
|
||||
cd docs/.vuepress/dist
|
||||
|
||||
# if you are deploying to a custom domain
|
||||
# echo 'www.example.com' > CNAME
|
||||
|
||||
git init
|
||||
git add -A
|
||||
git commit -m 'deploy'
|
||||
|
||||
# if you are deploying to https://<USERNAME>.github.io
|
||||
# git push -f git@github.com:<USERNAME>/<USERNAME>.github.io.git master
|
||||
|
||||
# if you are deploying to https://<USERNAME>.github.io/<REPO>
|
||||
git push -f git@github.com:sotrh/learn-wgpu.git master:gh-pages
|
||||
|
||||
# cleanup
|
||||
rm -rf ./.git
|
||||
|
||||
cd -
|
Loading…
Reference in New Issue