Correct backface culling.

This commit is contained in:
Joshua Yanovski 2020-05-20 21:22:30 +02:00
parent 560501df05
commit 654f7e0492
4 changed files with 54 additions and 73 deletions

View File

@ -74,9 +74,20 @@ float ShadowCalculation(uint lightIndex, vec3 fragToLight, /*float currentDepth*
};
float shadow = 0.0;
float bias = -0.015;//-0.05;//-0.1;//0.0;//0.1
float bias = 0.0;//-0.003;//-0.003;//-0.005;//0.001;//-1.0;//-0.001;//0.001;//0.003;//-0.05;//-0.1;//0.0;//0.1
{
float currentDepth = VectorToDepth(fragToLight);// + bias;
float visibility = texture(t_shadow_maps, vec4(fragToLight, currentDepth));// / (screen_res.w/* - screen_res.z*/)/*1.0 -bias*//*-(currentDepth - bias) / screen_res.w*//*-screen_res.w*/);
if (visibility == 1.0 || visibility == 0.0) {
return visibility;
}
// return visibility == 1.0 ? 1.0 : 0.0;
}
int samples = 20;
// float lightDistance = length(fragToLight);
float lightDistance = length(fragToLight);
float viewDistance = length(cam_pos.xyz - fragPos);
// float diskRadius = 0.00001;
// float diskRadius = 1.0;
@ -114,8 +125,8 @@ float ShadowCalculation(uint lightIndex, vec3 fragToLight, /*float currentDepth*
// currentDepth /= screen_res.w;
// float currentDepth = VectorToDepth(fragToLight) + bias;
// float visibility = texture(t_shadow_maps, vec4(fragToLight, 0.0), currentDepth);// / (screen_res.w/* - screen_res.z*/)/*1.0 -bias*//*-(currentDepth - bias) / screen_res.w*//*-screen_res.w*/);
// return visibility;
// float visibility = texture(t_shadow_maps, vec4(fragToLight, currentDepth));// / (screen_res.w/* - screen_res.z*/)/*1.0 -bias*//*-(currentDepth - bias) / screen_res.w*//*-screen_res.w*/);
// return visibility == 1.0 ? 1.0 : 0.0;
return shadow;
}
#else
@ -273,10 +284,10 @@ float lights_at(vec3 wpos, vec3 wnorm, vec3 /*cam_to_frag*/view_dir, vec3 mu, ve
#endif
vec3 direct_light = PI * color * strength * square_factor * light_reflection_factor(/*direct_norm_dir*/wnorm, /*cam_to_frag*/view_dir, direct_light_dir, k_d, k_s, alpha, voxel_lighting);
float computed_shadow = ShadowCalculation(i, -difference, wpos/*, light_distance*/);
// directed_light += /*is_direct ? */max(computed_shadow, /*LIGHT_AMBIENCE*/0.0) * direct_light * square_factor/* : vec3(0.0)*/;
directed_light += is_direct ? mix(/*LIGHT_AMBIENCE*/0.0, 1.0, computed_shadow) * direct_light * square_factor : vec3(0.0);
directed_light += is_direct ? max(computed_shadow, /*LIGHT_AMBIENCE*/0.0) * direct_light * square_factor : vec3(0.0);
// directed_light += is_direct ? mix(LIGHT_AMBIENCE, 1.0, computed_shadow) * direct_light * square_factor : vec3(0.0);
// ambient_light += is_direct ? vec3(0.0) : vec3(0.0); // direct_light * square_factor * LIGHT_AMBIENCE;
// ambient_light += direct_light * (1.0 - square_factor * LIGHT_AMBIENCE);
// ambient_light += is_direct ? direct_light * (1.0 - square_factor * LIGHT_AMBIENCE) : vec3(0.0);
vec3 cam_light_diff = light_pos - focus_pos.xyz;
float cam_distance_2 = dot(cam_light_diff, cam_light_diff);// + 0.0001;

View File

@ -48,8 +48,16 @@ void main() {
// f_pos.z -= 250.0 * (1.0 - min(1.0001 - 0.02 / pow(tick.x - load_time, 10.0), 1.0));
// f_pos.z -= min(32.0, 25.0 * pow(distance(focus_pos.xy, f_pos.xy) / view_distance.x, 20.0));
f_col = vec3((uvec3(v_col_light) >> uvec3(8, 16, 24)) & uvec3(0xFFu)) / 255.0;
// vec3 light_col = vec3(
// hash(floor(vec4(f_pos.x, 0, 0, 0))),
// hash(floor(vec4(0, f_pos.y, 0, 1))),
// hash(floor(vec4(0, 0, f_pos.z, 2)))
// );
// f_col = light_col;// f_col = vec3((uvec3(v_col_light) >> uvec3(8, 16, 24)) & uvec3(0xFFu)) / 255.0;
// f_light = 1.0;//float(v_col_light & 0x3Fu) / 64.0;
// f_ao = 1.0;//float((v_col_light >> 6u) & 3u) / 4.0;
f_col = f_col = vec3((uvec3(v_col_light) >> uvec3(8, 16, 24)) & uvec3(0xFFu)) / 255.0;
f_light = float(v_col_light & 0x3Fu) / 64.0;
f_ao = float((v_col_light >> 6u) & 3u) / 4.0;

View File

@ -479,7 +479,7 @@ impl<'a, V: RectRasterableVol<Vox = Block> + ReadVol + Debug>
// x (u = y, v = z)
greedy_mesh_cross_section(
Vec3::new(y_size, z_size, x_size),
Vec3::new(y_size, z_size, x_size - 1),
|pos| {
should_draw_greedy(
Vec3::new(pos.z, pos.x, pos.y),
@ -502,12 +502,12 @@ impl<'a, V: RectRasterableVol<Vox = Block> + ReadVol + Debug>
// y (u = z, v = x)
greedy_mesh_cross_section(
Vec3::new(z_size, x_size, y_size),
Vec3::new(z_size, x_size, y_size - 1),
|pos| {
should_draw_greedy(
Vec3::new(pos.y, pos.z, pos.x),
draw_delta,
Vec3::unit_y(), /* , pos.z, 0, y_size */
Vec3::unit_y(),
|pos| flat_get(pos),
)
},
@ -527,12 +527,18 @@ impl<'a, V: RectRasterableVol<Vox = Block> + ReadVol + Debug>
greedy_mesh_cross_section(
Vec3::new(x_size, y_size, z_size),
|pos| {
if pos.z == 0 {
let pos = pos.map(|e| e as i32) + draw_delta; // - delta;
let to = flat_get(pos).is_opaque(); //map(|v| v.is_opaque()).unwrap_or(false);
if to { Some(false) } else { None }
} else {
should_draw_greedy(
Vec3::new(pos.x, pos.y, pos.z),
draw_delta,
Vec3::unit_z(), /* , pos.z, 0, z_size */
Vec3::unit_z(),
|pos| flat_get(pos),
)
}
},
|pos, dim, faces_forward| {
shadow_mesh.push_quad(create_quad_greedy(
@ -647,29 +653,13 @@ fn create_quad_greedy(
norm: Vec3<f32>,
faces_forward: bool,
) -> Quad<ShadowPipeline> {
let origin = origin.map(|e| e as i32) + mesh_delta;
// let origin = (uv.x * origin.x + uv.y * origin.y + norm * origin.z) +
// Vec3::new(0, 0, z_start + range.min.z - 1);//Vec3::new(-1, -1, z_start +
// range.min.z - 1);
let origin = origin.map(|e| e as f32); // + orientation.z;
// let origin = uv.x * origin.x + uv.y * origin.y + norm * origin.z +
// Vec3::new(0.0, 0.0, (z_start + range.min.z - 1) as f32);
/* if (origin.x < 0.0 || origin.y < 0.0) {
return;
} */
// let ori = if faces_forward { Vec3::new(u, v, norm) } else { Vec3::new(uv.y,
// uv.x, -norm) };
let origin = (origin.map(|e| e as i32) + mesh_delta).map(|e| e as f32);
let dim = uv.map2(dim.map(|e| e as f32), |e, f| e * f);
let (dim, norm) = if faces_forward {
(dim, norm)
} else {
(Vec2::new(dim.y, dim.x), -norm)
};
// let (uv, norm, origin) = if faces_forward { (uv, norm, origin) } else {
// (Vec2::new(uv.y, uv.x), -norm, origin) }; let (uv, norm, origin) = if
// faces_forward { (uv, norm, origin) } else { (Vec2::new(uv.y, uv.x), -norm,
// origin/* - norm*/) }; let origin = Vec3::new(origin.x as f32., origin.y
// as f32, (origin.z + z_start) as f32); let norm = norm.map(|e| e as f32);
Quad::new(
ShadowVertex::new(origin, norm),
ShadowVertex::new(origin + dim.x, norm),
@ -682,36 +672,10 @@ fn should_draw_greedy(
pos: Vec3<usize>,
draw_delta: Vec3<i32>,
delta: Vec3<i32>,
/* depth, min_depth, max_depth, */ flat_get: impl Fn(Vec3<i32>) -> Block,
flat_get: impl Fn(Vec3<i32>) -> Block,
) -> Option<bool> {
let pos = pos.map(|e| e as i32) + draw_delta; // - delta;
//
/* if (depth as isize) <= min_depth {
// let to = flat_get(pos).is_opaque();
debug_assert!(depth <= max_depth);
/* if depth >= max_depth - 1 {
let from = flat_get(pos - delta).is_opaque();
} else {
None
} */
if flat_get(pos + delta).is_opaque() {
Some(true)
} else {
None
}
} else */
{
let from = flat_get(pos - delta).is_opaque(); // map(|v| v.is_opaque()).unwrap_or(false);
//
/* if depth > max_depth {
if from {
// Backward-facing
Some(false)
} else {
None
}
} else */
{
let to = flat_get(pos).is_opaque(); //map(|v| v.is_opaque()).unwrap_or(false);
if from == to {
None
@ -720,8 +684,6 @@ fn should_draw_greedy(
// facing.
Some(from)
}
}
}
}
/*

View File

@ -1510,11 +1510,11 @@ fn create_shadow_pipeline<P: gfx::pso::PipelineInit>(
// Second-depth shadow mapping: should help reduce z-fighting provided all objects
// are "watertight" (every triangle edge is shared with at most one other
// triangle); this *should* be true for Veloren.
cull_face, /*gfx::state::CullFace::Nothing*//*match cull_face {
cull_face: /*gfx::state::CullFace::Nothing*/match cull_face {
gfx::state::CullFace::Front => gfx::state::CullFace::Back,
gfx::state::CullFace::Back => gfx::state::CullFace::Front,
gfx::state::CullFace::Nothing => gfx::state::CullFace::Nothing,
}*/
},
method: gfx::state::RasterMethod::Fill,
offset: None, //Some(gfx::state::Offset(2, 10)),
samples: None, // Some(gfx::state::MultiSample),