Added colour smoothing

This commit is contained in:
Joshua Barretto 2019-09-24 07:42:09 +01:00
parent 6d745fc472
commit f1d590464f
7 changed files with 156 additions and 33 deletions

View File

@ -31,7 +31,7 @@ void main() {
vec3 diffuse_light, ambient_light;
get_sun_diffuse(f_norm, time_of_day.x, diffuse_light, ambient_light);
diffuse_light += light_at(f_pos, f_norm);
vec3 surf_color = illuminate(srgb_to_linear(model_col.rgb * f_col), diffuse_light * 4.0, ambient_light);
vec3 surf_color = illuminate(srgb_to_linear(model_col.rgb * f_col), diffuse_light, ambient_light);
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x, true);

View File

@ -31,16 +31,16 @@ void main() {
mat4 combined_mat = model_mat * bones[v_bone_idx].bone_mat;
f_pos = (
combined_mat *
combined_mat *
vec4(v_pos, 1)).xyz;
f_col = v_col;
// Calculate normal here rather than for each pixel in the fragment shader
f_norm = (
combined_mat *
f_norm = normalize((
combined_mat *
vec4(v_norm, 0.0)
).xyz;
).xyz);
gl_Position = proj_mat * view_mat * vec4(f_pos, 1);
}

View File

@ -12,7 +12,7 @@ uniform u_lights {
vec3 illuminate(vec3 color, vec3 diffuse, vec3 ambience) {
float avg_col = (color.r + color.g + color.b) / 3.0;
return ((color - avg_col) * ambience * 2.0 + (diffuse + ambience) * avg_col) * (diffuse + ambience);
return ((color - avg_col) * ambience * 5.0 + (diffuse + ambience) * avg_col) * (diffuse + ambience);
}
float attenuation_strength(vec3 rpos) {

View File

@ -5,7 +5,7 @@ const float PI = 3.141592;
const vec3 SKY_DAY_TOP = vec3(0.1, 0.2, 0.9);
const vec3 SKY_DAY_MID = vec3(0.02, 0.08, 0.8);
const vec3 SKY_DAY_BOT = vec3(0.02, 0.01, 0.3);
const vec3 DAY_LIGHT = vec3(1.0, 1.0, 1.0);
const vec3 DAY_LIGHT = vec3(1.2, 1.0, 1.0);
const vec3 SKY_DUSK_TOP = vec3(0.06, 0.1, 0.20);
const vec3 SKY_DUSK_MID = vec3(0.35, 0.1, 0.15);
@ -27,13 +27,13 @@ vec3 get_sun_dir(float time_of_day) {
}
float get_sun_brightness(vec3 sun_dir) {
return max(-sun_dir.z + 0.6, 0.0) / 1.6;
return max(-sun_dir.z + 0.6, 0.0) * 1.0;
}
const float PERSISTENT_AMBIANCE = 0.008;
void get_sun_diffuse(vec3 norm, float time_of_day, out vec3 diffuse_light, out vec3 ambient_light) {
const float SUN_AMBIANCE = 0.8;
const float SUN_AMBIANCE = 0.2;
vec3 sun_dir = get_sun_dir(time_of_day);

View File

@ -32,7 +32,7 @@ impl Meshable<FigurePipeline, FigurePipeline> for Segment {
self,
pos,
offs + pos.map(|e| e as f32),
col,
&[[[Some(col); 3]; 3]; 3],
|origin, norm, col, ao, light| {
FigureVertex::new(
origin,
@ -91,7 +91,7 @@ impl Meshable<SpritePipeline, SpritePipeline> for Segment {
self,
pos,
offs + pos.map(|e| e as f32),
col,
&[[[Some(col); 3]; 3]; 3],
|origin, norm, col, ao, light| {
SpriteVertex::new(
origin,

View File

@ -5,7 +5,7 @@ use crate::{
use common::{
terrain::{Block, BlockKind},
vol::{ReadVol, RectRasterableVol, Vox},
volumes::{dyna::Dyna, vol_grid_2d::VolGrid2d},
volumes::vol_grid_2d::VolGrid2d,
};
use hashbrown::{HashMap, HashSet};
use std::fmt::Debug;
@ -137,7 +137,7 @@ impl<V: RectRasterableVol<Vox = Block> + ReadVol + Debug> Meshable<TerrainPipeli
let mut opaque_mesh = Mesh::new();
let mut fluid_mesh = Mesh::new();
let mut light = calc_light(range, self);
let light = calc_light(range, self);
for x in range.min.x + 1..range.max.x - 1 {
for y in range.min.y + 1..range.max.y - 1 {
@ -152,14 +152,30 @@ impl<V: RectRasterableVol<Vox = Block> + ReadVol + Debug> Meshable<TerrainPipeli
.filter(|vox| vox.is_opaque())
.and_then(|vox| vox.get_color())
{
let col = col.map(|e| e as f32 / 255.0);
vol::push_vox_verts(
&mut opaque_mesh,
self,
pos,
offs,
col,
&{
let mut cols = [[[None; 3]; 3]; 3];
for x in 0..3 {
for y in 0..3 {
for z in 0..3 {
cols[x][y][z] = self
.get(
pos + Vec3::new(x as i32, y as i32, z as i32)
- 1,
)
.ok()
.filter(|vox| vox.is_opaque())
.and_then(|vox| vox.get_color())
.map(|col| col.map(|e| e as f32 / 255.0));
}
}
}
cols
},
|pos, norm, col, ao, light| {
TerrainVertex::new(pos, norm, col, light * ao)
},
@ -191,7 +207,25 @@ impl<V: RectRasterableVol<Vox = Block> + ReadVol + Debug> Meshable<TerrainPipeli
self,
pos,
offs,
col,
&{
let mut cols = [[[None; 3]; 3]; 3];
for x in 0..3 {
for y in 0..3 {
for z in 0..3 {
cols[x][y][z] = self
.get(
pos + Vec3::new(x as i32, y as i32, z as i32)
- 1,
)
.ok()
.filter(|vox| vox.is_fluid())
.and_then(|vox| vox.get_color())
.map(|col| col.map(|e| e as f32 / 255.0));
}
}
}
cols
},
|pos, norm, col, ao, light| {
FluidVertex::new(pos, norm, col, light * ao, 0.3)
},

View File

@ -35,13 +35,48 @@ fn get_ao_quad<V: ReadVol>(
.collect::<Vec4<(f32, f32)>>()
}
fn get_col_quad<V: ReadVol>(
vol: &V,
pos: Vec3<i32>,
shift: Vec3<i32>,
dirs: &[Vec3<i32>],
cols: &[[[Option<Rgb<f32>>; 3]; 3]; 3],
is_opaque: impl Fn(&V::Vox) -> bool,
) -> Vec4<Rgb<f32>> {
dirs.windows(2)
.map(|offs| {
let mut color = Rgb::zero();
let mut total = 0.0;
for x in 0..2 {
for y in 0..2 {
for z in 0..2 {
let col_pos = shift * z + offs[0] * x + offs[1] * y + 1;
if let Some(col) =
cols[col_pos.x as usize][col_pos.y as usize][col_pos.z as usize]
{
color += col;
total += 1.0;
}
}
}
}
if total == 0.0 {
Rgb::zero()
} else {
color / total
}
})
.collect()
}
// Utility function
fn create_quad<P: Pipeline, F: Fn(Vec3<f32>, Vec3<f32>, Rgb<f32>, f32, f32) -> P::Vertex>(
origin: Vec3<f32>,
unit_x: Vec3<f32>,
unit_y: Vec3<f32>,
norm: Vec3<f32>,
col: Rgb<f32>,
cols: Vec4<Rgb<f32>>,
darkness_ao: Vec4<(f32, f32)>,
vcons: &F,
) -> Quad<P> {
@ -52,17 +87,29 @@ fn create_quad<P: Pipeline, F: Fn(Vec3<f32>, Vec3<f32>, Rgb<f32>, f32, f32) -> P
if ao[0].min(ao[2]) < ao[1].min(ao[3]) {
Quad::new(
vcons(origin + unit_y, norm, col, darkness[3], ao_map[3]),
vcons(origin, norm, col, darkness[0], ao_map[0]),
vcons(origin + unit_x, norm, col, darkness[1], ao_map[1]),
vcons(origin + unit_x + unit_y, norm, col, darkness[2], ao_map[2]),
vcons(origin + unit_y, norm, cols[3], darkness[3], ao_map[3]),
vcons(origin, norm, cols[0], darkness[0], ao_map[0]),
vcons(origin + unit_x, norm, cols[1], darkness[1], ao_map[1]),
vcons(
origin + unit_x + unit_y,
norm,
cols[2],
darkness[2],
ao_map[2],
),
)
} else {
Quad::new(
vcons(origin, norm, col, darkness[0], ao_map[0]),
vcons(origin + unit_x, norm, col, darkness[1], ao_map[1]),
vcons(origin + unit_x + unit_y, norm, col, darkness[2], ao_map[2]),
vcons(origin + unit_y, norm, col, darkness[3], ao_map[3]),
vcons(origin, norm, cols[0], darkness[0], ao_map[0]),
vcons(origin + unit_x, norm, cols[1], darkness[1], ao_map[1]),
vcons(
origin + unit_x + unit_y,
norm,
cols[2],
darkness[2],
ao_map[2],
),
vcons(origin + unit_y, norm, cols[3], darkness[3], ao_map[3]),
)
}
}
@ -72,7 +119,7 @@ pub fn push_vox_verts<V: ReadVol, P: Pipeline>(
vol: &V,
pos: Vec3<i32>,
offs: Vec3<f32>,
col: Rgb<f32>,
cols: &[[[Option<Rgb<f32>>; 3]; 3]; 3],
vcons: impl Fn(Vec3<f32>, Vec3<f32>, Rgb<f32>, f32, f32) -> P::Vertex,
error_makes_face: bool,
darknesses: &[[[f32; 3]; 3]; 3],
@ -92,7 +139,14 @@ pub fn push_vox_verts<V: ReadVol, P: Pipeline>(
Vec3::unit_z(),
Vec3::unit_y(),
-Vec3::unit_x(),
col,
get_col_quad(
vol,
pos,
-Vec3::unit_x(),
&[-z, -y, z, y, -z],
cols,
&is_opaque,
),
get_ao_quad(
vol,
pos,
@ -115,7 +169,14 @@ pub fn push_vox_verts<V: ReadVol, P: Pipeline>(
Vec3::unit_y(),
Vec3::unit_z(),
Vec3::unit_x(),
col,
get_col_quad(
vol,
pos,
Vec3::unit_x(),
&[-y, -z, y, z, -y],
cols,
&is_opaque,
),
get_ao_quad(
vol,
pos,
@ -138,7 +199,14 @@ pub fn push_vox_verts<V: ReadVol, P: Pipeline>(
Vec3::unit_x(),
Vec3::unit_z(),
-Vec3::unit_y(),
col,
get_col_quad(
vol,
pos,
-Vec3::unit_y(),
&[-x, -z, x, z, -x],
cols,
&is_opaque,
),
get_ao_quad(
vol,
pos,
@ -161,7 +229,14 @@ pub fn push_vox_verts<V: ReadVol, P: Pipeline>(
Vec3::unit_z(),
Vec3::unit_x(),
Vec3::unit_y(),
col,
get_col_quad(
vol,
pos,
Vec3::unit_y(),
&[-z, -x, z, x, -z],
cols,
&is_opaque,
),
get_ao_quad(
vol,
pos,
@ -184,7 +259,14 @@ pub fn push_vox_verts<V: ReadVol, P: Pipeline>(
Vec3::unit_y(),
Vec3::unit_x(),
-Vec3::unit_z(),
col,
get_col_quad(
vol,
pos,
-Vec3::unit_z(),
&[-y, -x, y, x, -y],
cols,
&is_opaque,
),
get_ao_quad(
vol,
pos,
@ -207,7 +289,14 @@ pub fn push_vox_verts<V: ReadVol, P: Pipeline>(
Vec3::unit_x(),
Vec3::unit_y(),
Vec3::unit_z(),
col,
get_col_quad(
vol,
pos,
Vec3::unit_z(),
&[-x, -y, x, y, -x],
cols,
&is_opaque,
),
get_ao_quad(
vol,
pos,