Skip to content

Commit

Permalink
Support front face configuration for meshes
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Dec 12, 2023
1 parent 8577eb9 commit 4dbf262
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 16 deletions.
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
Expand Up @@ -50,6 +50,7 @@ pub struct Material {

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 @@ impl FlattenedGeometry {
#[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 @@ impl CookedModel<'_> {
}
}

#[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 @@ -496,6 +510,10 @@ impl blade_asset::Baker for Baker {
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 @@ impl blade_asset::Baker for Baker {

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
Expand Up @@ -179,12 +179,9 @@ fn main() {
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

0 comments on commit 4dbf262

Please sign in to comment.