Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support front face configuration for meshes #65

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions blade-render/code/fill-gbuf.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ var sampler_nearest: sampler;
struct HitEntry {
index_buf: u32,
vertex_buf: u32,
winding: f32,
// packed quaternion
geometry_to_world_rotation: u32,
pad: u32,
geometry_to_object: mat4x3<f32>,
prev_object_to_world: mat4x3<f32>,
base_color_texture: u32,
Expand Down Expand Up @@ -105,7 +105,7 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
let positions = intersection.object_to_world * mat3x4(
vec4<f32>(positions_object[0], 1.0), vec4<f32>(positions_object[1], 1.0), vec4<f32>(positions_object[2], 1.0)
);
flat_normal = normalize(cross(positions[1].xyz - positions[0].xyz, positions[2].xyz - positions[0].xyz));
flat_normal = entry.winding * normalize(cross(positions[1].xyz - positions[0].xyz, positions[2].xyz - positions[0].xyz));

let barycentrics = vec3<f32>(1.0 - intersection.barycentrics.x - intersection.barycentrics.y, intersection.barycentrics);
let position_object = vec4<f32>(positions_object * barycentrics, 1.0);
Expand Down
6 changes: 2 additions & 4 deletions blade-render/code/ray-trace.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,12 @@ fn estimate_target_score_with_occlusion(

fn evaluate_sample(ls: LightSample, surface: Surface, start_pos: vec3<f32>, debug_len: f32) -> f32 {
let dir = map_equirect_uv_to_dir(ls.uv);
if (dot(dir, surface.flat_normal) <= 0.0)
{
if (dot(dir, surface.flat_normal) <= 0.0) {
return 0.0;
}

let brdf = evaluate_brdf(surface, dir);
if (brdf <= 0.0)
{
if (brdf <= 0.0) {
return 0.0;
}

Expand Down
23 changes: 21 additions & 2 deletions blade-render/src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
borrow::Cow,
collections::hash_map::{Entry, HashMap},

Check warning on line 3 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused imports: `Entry`, `HashMap`

Check warning on line 3 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unused imports: `Entry`, `HashMap`

Check warning on line 3 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unused imports: `Entry`, `HashMap`
fmt, hash, mem,
ops::Range,
ptr, str,
Expand Down Expand Up @@ -50,6 +50,7 @@

pub struct Model {
pub name: String,
pub winding: f32,
pub geometries: Vec<Geometry>,
pub materials: Vec<Material>,
pub vertex_buffer: blade_graphics::Buffer,
Expand Down Expand Up @@ -163,6 +164,7 @@
#[derive(blade_macros::Flat)]
pub struct CookedModel<'a> {
name: &'a [u8],
winding: f32,
materials: Vec<CookedMaterial<'a>>,
geometries: Vec<CookedGeometry<'a>>,
}
Expand Down Expand Up @@ -272,14 +274,26 @@
}
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FrontFace {
Clockwise,
CounterClockwise,
}
impl Default for FrontFace {
fn default() -> Self {
Self::CounterClockwise
}
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct Meta {
pub generate_tangents: bool,
pub front_face: FrontFace,
}

impl fmt::Display for Meta {
fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result {
Ok(())
Ok(()) //TODO
}
}

Expand Down Expand Up @@ -456,11 +470,11 @@

fn cook(
&self,
source: &[u8],

Check warning on line 473 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `source`

Check warning on line 473 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unused variable: `source`

Check warning on line 473 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unused variable: `source`
extension: &str,
meta: Meta,

Check warning on line 475 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `meta`

Check warning on line 475 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unused variable: `meta`

Check warning on line 475 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unused variable: `meta`
cooker: Arc<blade_asset::Cooker<Self>>,

Check warning on line 476 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `cooker`

Check warning on line 476 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unused variable: `cooker`

Check warning on line 476 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unused variable: `cooker`
exe_context: &choir::ExecutionContext,

Check warning on line 477 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `exe_context`

Check warning on line 477 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unused variable: `exe_context`

Check warning on line 477 in blade-render/src/model/mod.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unused variable: `exe_context`
) {
match extension {
#[cfg(feature = "asset")]
Expand Down Expand Up @@ -496,6 +510,10 @@
let mut sources = slab::Slab::new();
let mut model = CookedModel {
name: &[],
winding: match meta.front_face {
FrontFace::Clockwise => -1.0,
FrontFace::CounterClockwise => 1.0,
},
materials: Vec::new(),
geometries: Vec::new(),
};
Expand Down Expand Up @@ -756,6 +774,7 @@

Model {
name: String::from_utf8_lossy(model.name).into_owned(),
winding: model.winding,
geometries,
materials,
vertex_buffer,
Expand Down
4 changes: 2 additions & 2 deletions blade-render/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,8 @@ struct PostProcData {
struct HitEntry {
index_buf: u32,
vertex_buf: u32,
winding: f32,
geometry_to_world_rotation: [i8; 4],
unused: u32,
//Note: it's technically `mat4x3` on WGSL side,
// but it's aligned and sized the same way as `mat4`.
geometry_to_object: mint::ColumnMatrix4<f32>,
Expand Down Expand Up @@ -913,8 +913,8 @@ impl Renderer {
vertex_buf: self
.vertex_buffers
.alloc(model.vertex_buffer.at(vertex_offset)),
winding: model.winding,
geometry_to_world_rotation,
unused: 0,
geometry_to_object: mint::ColumnMatrix4::from(mint::RowMatrix4 {
x: geometry.transform.x,
y: geometry.transform.y,
Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changelog for Blade
## blade-0.2 (TBD)
- high-level engine
- support object motion
- support clockwise mesh winding

## blade-graphics-0.3, blade-render-0.2 (17 Nov 2023)
- tangent space generation
Expand Down
9 changes: 3 additions & 6 deletions examples/init/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(irrefutable_let_patterns)]

use std::{env, path::Path, ptr, sync::Arc};

Check warning on line 3 in examples/init/main.rs

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unused import: `ptr`

use blade_graphics as gpu;

Expand Down Expand Up @@ -179,12 +179,9 @@
environment_map = Some(texture);
} else if arg.ends_with(".gltf") {
println!("\tmodels += {}", arg);
let (model, model_task) = asset_hub.models.load(
arg,
blade_render::model::Meta {
generate_tangents: false,
},
);
let (model, model_task) = asset_hub
.models
.load(arg, blade_render::model::Meta::default());
load_finish.depend_on(model_task);
_object = Some(blade_render::Object::from(model));
} else {
Expand Down
2 changes: 2 additions & 0 deletions examples/scene/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ impl Example {
parent.join(&config_object.path),
blade_render::model::Meta {
generate_tangents: true,
..Default::default()
},
);
load_finish.depend_on(model_task);
Expand Down Expand Up @@ -983,6 +984,7 @@ impl Example {
file_path,
blade_render::model::Meta {
generate_tangents: true,
..Default::default()
},
);
self.scene_load_task = Some(model_task.clone());
Expand Down
14 changes: 14 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#[derive(serde::Deserialize)]
pub enum FrontFace {
Cw,
Ccw,
}
impl Default for FrontFace {
fn default() -> Self {
Self::Ccw
}
}

fn default_vec() -> mint::Vector3<f32> {
[0.0; 3].into()
}
Expand All @@ -8,6 +19,8 @@ fn default_scale() -> f32 {
#[derive(serde::Deserialize)]
pub struct Visual {
pub model: String,
#[serde(default)]
pub front_face: FrontFace,
#[serde(default = "default_vec")]
pub pos: mint::Vector3<f32>,
#[serde(default = "default_vec")]
Expand All @@ -19,6 +32,7 @@ impl Default for Visual {
fn default() -> Self {
Self {
model: String::new(),
front_face: FrontFace::default(),
pos: default_vec(),
rot: default_vec(),
scale: default_scale(),
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ impl Engine {
format!("{}/{}", self.data_path, visual.model),
blade_render::model::Meta {
generate_tangents: true,
front_face: match visual.front_face {
config::FrontFace::Cw => blade_render::model::FrontFace::Clockwise,
config::FrontFace::Ccw => blade_render::model::FrontFace::CounterClockwise,
},
},
);
visuals.push(Visual {
Expand Down
Loading