diff --git a/common/src/terrain/block.rs b/common/src/terrain/block.rs index 7da8ff3ab9..f346ee4cb8 100644 --- a/common/src/terrain/block.rs +++ b/common/src/terrain/block.rs @@ -25,6 +25,15 @@ impl Block { Some(self.color.into()) } } + + pub fn get_opacity(&self) -> f32 { + match self.kind { + 0 => 0.0, + 1 => 0.3, + 2 => 1.0, + _ => unimplemented!(), + } + } } impl Vox for Block { diff --git a/voxygen/shaders/include/sky.glsl b/voxygen/shaders/include/sky.glsl new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/voxygen/shaders/include/sky.glsl @@ -0,0 +1 @@ + diff --git a/voxygen/shaders/terrain.frag b/voxygen/shaders/terrain.frag index 5f8aefbb7a..5e96ccaca5 100644 --- a/voxygen/shaders/terrain.frag +++ b/voxygen/shaders/terrain.frag @@ -5,6 +5,7 @@ in vec3 f_pos; in vec3 f_norm; in vec3 f_col; +in float f_light; layout (std140) uniform u_locals { @@ -13,12 +14,23 @@ uniform u_locals { out vec4 tgt_color; -void main() { - float ambient = 0.5; - - vec3 sun_dir = normalize(vec3(1.3, 1.7, 1.1)); - - float sun_diffuse = dot(sun_dir, f_norm) * 0.5; - - tgt_color = vec4(f_col * (ambient + sun_diffuse), 1.0); +vec3 point_light() { + return vec3(1.0, 0.9, 0.4) * 3.0 / pow(length(f_pos - focus_pos.xyz), 2.0); +} + +void main() { + float glob_ambience = 0.001; + + float sun_ambience = 0.9; + + vec3 sun_dir = normalize(vec3(1.3, 1.7, 2.1)); + + float sun_diffuse = dot(sun_dir, f_norm); + float sun_light = sun_ambience + sun_diffuse; + + float static_light = glob_ambience + min(sun_light, f_light); + + vec3 light = point_light() + static_light; + + tgt_color = vec4(f_col * light, 1.0); } diff --git a/voxygen/shaders/terrain.vert b/voxygen/shaders/terrain.vert index 2a0fb39c90..0f4d4bd306 100644 --- a/voxygen/shaders/terrain.vert +++ b/voxygen/shaders/terrain.vert @@ -4,6 +4,7 @@ in uint v_pos; in uint v_col_norm; +in uint v_light; layout (std140) uniform u_locals { @@ -13,13 +14,14 @@ uniform u_locals { out vec3 f_pos; out vec3 f_norm; out vec3 f_col; +out float f_light; void main() { f_pos = vec3( float((v_pos >> 0) & 0x00FFu), float((v_pos >> 8) & 0x00FFu), float((v_pos >> 16) & 0xFFFFu) - ); + ) + model_offs; f_norm = vec3( float((v_col_norm >> 0) & 0x3u), @@ -33,8 +35,10 @@ void main() { float((v_col_norm >> 24) & 0xFFu) ) / 255.0; + f_light = float(v_light) / 4096.0; + gl_Position = proj_mat * view_mat * - vec4(f_pos + model_offs, 1); + vec4(f_pos, 1); } diff --git a/voxygen/src/mesh/terrain.rs b/voxygen/src/mesh/terrain.rs index 29ccc019ea..e85e216d7e 100644 --- a/voxygen/src/mesh/terrain.rs +++ b/voxygen/src/mesh/terrain.rs @@ -16,6 +16,7 @@ use crate::{ type TerrainVertex = ::Vertex; +/* impl Meshable for Dyna { type Pipeline = TerrainPipeline; type Supplement = (); @@ -43,6 +44,7 @@ impl Meshable for Dyna { mesh } } +*/ impl + ReadVol, S: VolSize + Clone> Meshable for VolMap2d { type Pipeline = TerrainPipeline; @@ -53,21 +55,22 @@ impl + ReadVol, S: VolSize + Clone> Meshable for VolMap2 for x in range.min.x + 1..range.max.x - 1 { for y in range.min.y + 1..range.max.y - 1 { - let mut neighbour_shade = [[1.0f32; 3]; 3]; + let mut neighbour_light = [[1.0f32; 3]; 3]; for z in (range.min.z..range.max.z).rev() { let pos = Vec3::new(x, y, z); // Create mesh polygons if let Some(col) = self.get(pos).ok().and_then(|vox| vox.get_color()) { - let avg_shade = neighbour_shade + let avg_light = neighbour_light .iter() .map(|col| col.iter()) .flatten() .fold(0.0, |a, x| a + x) / 9.0; + let light = avg_light; - let col = col.map(|e| e as f32 / 255.0) * (0.01 + avg_shade * 0.99); + let col = col.map(|e| e as f32 / 255.0); let offs = (pos - range.min * Vec3::new(1, 1, 0)).map(|e| e as f32) - Vec3::new(1.0, 1.0, 0.0); @@ -78,7 +81,7 @@ impl + ReadVol, S: VolSize + Clone> Meshable for VolMap2 pos, offs, col, - TerrainVertex::new, + |pos, norm, col| TerrainVertex::new(pos, norm, col, light), false, ); } @@ -86,14 +89,13 @@ impl + ReadVol, S: VolSize + Clone> Meshable for VolMap2 // Accumulate shade under opaque blocks for i in 0..3 { for j in 0..3 { - neighbour_shade[i][j] = if self + neighbour_light[i][j] = if let Ok(opacity) = self .get(pos + Vec3::new(i as i32 - 1, j as i32 - 1, 0)) - .map(|vox| !vox.is_empty()) - .unwrap_or(false) + .map(|vox| vox.get_opacity()) { - neighbour_shade[i][j] * 0.85 + neighbour_light[i][j] * (1.0 - opacity * 0.5) } else { - (neighbour_shade[i][j] * 1.01).min(1.0) + (neighbour_light[i][j] * 1.05).min(1.0) }; } } @@ -104,6 +106,7 @@ impl + ReadVol, S: VolSize + Clone> Meshable for VolMap2 } } +/* impl + ReadVol, S: VolSize + Clone> Meshable for VolMap3d { type Pipeline = TerrainPipeline; type Supplement = Aabb; @@ -167,3 +170,4 @@ impl + ReadVol, S: VolSize + Clone> Meshable for VolMap3 mesh } } +*/ diff --git a/voxygen/src/render/pipelines/terrain.rs b/voxygen/src/render/pipelines/terrain.rs index 06bec4cd98..6a4b720396 100644 --- a/voxygen/src/render/pipelines/terrain.rs +++ b/voxygen/src/render/pipelines/terrain.rs @@ -19,6 +19,7 @@ gfx_defines! { vertex Vertex { pos: u32 = "v_pos", col_norm: u32 = "v_col_norm", + light: u16 = "v_light", } constant Locals { @@ -37,7 +38,7 @@ gfx_defines! { } impl Vertex { - pub fn new(pos: Vec3, norm: Vec3, col: Rgb) -> Self { + pub fn new(pos: Vec3, norm: Vec3, col: Rgb, light: f32) -> Self { Self { pos: 0 | ((pos.x as u32) & 0x00FF) << 0 @@ -50,6 +51,7 @@ impl Vertex { | ((norm.x.add(1.0) as u32) & 0x3) << 0 | ((norm.y.add(1.0) as u32) & 0x3) << 2 | ((norm.z.add(1.0) as u32) & 0x3) << 4, + light: light.mul(4096.0) as u16, } } } diff --git a/voxygen/src/render/renderer.rs b/voxygen/src/render/renderer.rs index db4a64d3b0..fa7317ae89 100644 --- a/voxygen/src/render/renderer.rs +++ b/voxygen/src/render/renderer.rs @@ -78,9 +78,14 @@ impl Renderer { env!("CARGO_MANIFEST_DIR"), "/shaders/include/globals.glsl" )); + let sky = include_str!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/shaders/include/sky.glsl" + )); let mut include_ctx = IncludeContext::new(); include_ctx.include("globals.glsl", globals); + include_ctx.include("sky.glsl", globals); // Construct a pipeline for rendering skyboxes let skybox_pipeline = create_pipeline( diff --git a/world/src/sim.rs b/world/src/sim.rs index 221cacdf4f..572b66df4a 100644 --- a/world/src/sim.rs +++ b/world/src/sim.rs @@ -276,11 +276,10 @@ impl<'a> Sampler<'a> { // Sample blocks let air = Block::empty(); - let stone = Block::new(1, Rgb::new(200, 220, 255)); - let grass = Block::new(2, Rgb::new(75, 150, 0)); - let dirt = Block::new(3, Rgb::new(128, 90, 0)); - let sand = Block::new(4, Rgb::new(180, 150, 50)); - let water = Block::new(5, Rgb::new(100, 150, 255)); + let stone = Block::new(2, Rgb::new(200, 220, 255)); + let dirt = Block::new(1, Rgb::new(128, 90, 0)); + let sand = Block::new(1, Rgb::new(180, 150, 50)); + let water = Block::new(1, Rgb::new(100, 150, 255)); let ground_block = if (wposf.z as f32) < height - 4.0 { // Underground