Sprite distance fading, grass

This commit is contained in:
Joshua Barretto 2019-08-19 22:54:16 +01:00
parent b31cca4bb3
commit 7960e6ea6d
14 changed files with 112 additions and 35 deletions

View File

@ -12,6 +12,9 @@ out vec4 tgt_color;
#include <sky.glsl>
#include <light.glsl>
const float RENDER_DIST = 128.0;
const float FADE_DIST = 32.0;
void main() {
vec3 light = get_sun_diffuse(f_norm, time_of_day.x) * f_light + light_at(f_pos, f_norm);
vec3 surf_color = f_col * light;
@ -20,5 +23,5 @@ void main() {
vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x);
vec3 color = mix(surf_color, fog_color, fog_level);
tgt_color = vec4(color, 1.0);
tgt_color = vec4(color, 1.0 - clamp((distance(focus_pos.xy, f_pos.xy) - (RENDER_DIST - FADE_DIST)) / FADE_DIST, 0, 1));
}

BIN
assets/voxygen/voxel/sprite/grass-0.vox (Stored with Git LFS) Normal file

Binary file not shown.

BIN
assets/voxygen/voxel/sprite/grass-1.vox (Stored with Git LFS) Normal file

Binary file not shown.

BIN
assets/voxygen/voxel/sprite/grass-2.vox (Stored with Git LFS) Normal file

Binary file not shown.

BIN
assets/voxygen/voxel/sprite/grass-3.vox (Stored with Git LFS) Normal file

Binary file not shown.

BIN
assets/voxygen/voxel/sprite/grass-4.vox (Stored with Git LFS) Normal file

Binary file not shown.

BIN
assets/voxygen/voxel/sprite/grass-5.vox (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -20,6 +20,10 @@ impl<T: Copy + gfx::traits::Pod> Instances<T> {
})
}
pub fn count(&self) -> usize {
self.ibuf.len()
}
pub fn update(
&mut self,
encoder: &mut gfx::Encoder<gfx_backend::Resources, gfx_backend::CommandBuffer>,

View File

@ -10,22 +10,20 @@ use std::ops::Range;
/// Represents a mesh that has been sent to the GPU.
pub struct Model<P: Pipeline> {
pub vbuf: gfx::handle::Buffer<gfx_backend::Resources, P::Vertex>,
pub slice: gfx::Slice<gfx_backend::Resources>,
pub vertex_range: Range<u32>,
}
impl<P: Pipeline> Model<P> {
pub fn new(factory: &mut gfx_backend::Factory, mesh: &Mesh<P>) -> Self {
Self {
vbuf: factory.create_vertex_buffer(mesh.vertices()),
slice: gfx::Slice {
start: 0,
end: mesh.vertices().len() as u32,
base_vertex: 0,
instances: None,
buffer: gfx::IndexBuffer::Auto,
},
vertex_range: 0..mesh.vertices().len() as u32,
}
}
pub fn vertex_range(&self) -> Range<u32> {
self.vertex_range.clone()
}
}
/// Represents a mesh on the GPU which can be updated dynamically.
@ -46,13 +44,7 @@ impl<P: Pipeline> DynamicModel<P> {
pub fn submodel(&self, range: Range<usize>) -> Model<P> {
Model {
vbuf: self.vbuf.clone(),
slice: gfx::Slice {
start: range.start as u32,
end: range.end as u32,
base_vertex: 0,
instances: None,
buffer: gfx::IndexBuffer::Auto,
},
vertex_range: range.start as u32..range.end as u32,
}
}

View File

@ -10,6 +10,7 @@ use gfx::{
gfx_pipeline,
gfx_pipeline_inner,
gfx_vertex_struct_meta,
state::ColorMask,
};
use vek::*;
@ -32,7 +33,7 @@ gfx_defines! {
globals: gfx::ConstantBuffer<Globals> = "u_globals",
lights: gfx::ConstantBuffer<Light> = "u_lights",
tgt_color: gfx::RenderTarget<TgtColorFmt> = "tgt_color",
tgt_color: gfx::BlendTarget<TgtColorFmt> = ("tgt_color", ColorMask::all(), gfx::preset::blend::ALPHA),
tgt_depth: gfx::DepthTarget<TgtDepthFmt> = gfx::preset::depth::LESS_EQUAL_WRITE,
}
}

View File

@ -366,7 +366,13 @@ impl Renderer {
locals: &Consts<skybox::Locals>,
) {
self.encoder.draw(
&model.slice,
&gfx::Slice {
start: model.vertex_range().start,
end: model.vertex_range().end,
base_vertex: 0,
instances: None,
buffer: gfx::IndexBuffer::Auto,
},
&self.skybox_pipeline.pso,
&skybox::pipe::Data {
vbuf: model.vbuf.clone(),
@ -388,7 +394,13 @@ impl Renderer {
lights: &Consts<Light>,
) {
self.encoder.draw(
&model.slice,
&gfx::Slice {
start: model.vertex_range().start,
end: model.vertex_range().end,
base_vertex: 0,
instances: None,
buffer: gfx::IndexBuffer::Auto,
},
&self.figure_pipeline.pso,
&figure::pipe::Data {
vbuf: model.vbuf.clone(),
@ -411,7 +423,13 @@ impl Renderer {
lights: &Consts<Light>,
) {
self.encoder.draw(
&model.slice,
&gfx::Slice {
start: model.vertex_range().start,
end: model.vertex_range().end,
base_vertex: 0,
instances: None,
buffer: gfx::IndexBuffer::Auto,
},
&self.terrain_pipeline.pso,
&terrain::pipe::Data {
vbuf: model.vbuf.clone(),
@ -433,7 +451,13 @@ impl Renderer {
lights: &Consts<Light>,
) {
self.encoder.draw(
&model.slice,
&gfx::Slice {
start: model.vertex_range().start,
end: model.vertex_range().end,
base_vertex: 0,
instances: None,
buffer: gfx::IndexBuffer::Auto,
},
&self.fluid_pipeline.pso,
&fluid::pipe::Data {
vbuf: model.vbuf.clone(),
@ -455,7 +479,13 @@ impl Renderer {
lights: &Consts<Light>,
) {
self.encoder.draw(
&model.slice,
&gfx::Slice {
start: model.vertex_range().start,
end: model.vertex_range().end,
base_vertex: 0,
instances: Some((instances.count() as u32, 0)),
buffer: gfx::IndexBuffer::Auto,
},
&self.sprite_pipeline.pso,
&sprite::pipe::Data {
vbuf: model.vbuf.clone(),
@ -479,7 +509,13 @@ impl Renderer {
) {
let Aabr { min, max } = scissor;
self.encoder.draw(
&model.slice,
&gfx::Slice {
start: model.vertex_range().start,
end: model.vertex_range().end,
base_vertex: 0,
instances: None,
buffer: gfx::IndexBuffer::Auto,
},
&self.ui_pipeline.pso,
&ui::pipe::Data {
vbuf: model.vbuf.clone(),
@ -505,7 +541,13 @@ impl Renderer {
locals: &Consts<postprocess::Locals>,
) {
self.encoder.draw(
&model.slice,
&gfx::Slice {
start: model.vertex_range().start,
end: model.vertex_range().end,
base_vertex: 0,
instances: None,
buffer: gfx::IndexBuffer::Auto,
},
&self.postprocess_pipeline.pso,
&postprocess::pipe::Data {
vbuf: model.vbuf.clone(),

View File

@ -229,7 +229,12 @@ impl Scene {
// Render terrain and figures.
self.figure_mgr
.render(renderer, client, &self.globals, &self.lights, &self.camera);
self.terrain.render(renderer, &self.globals, &self.lights);
self.terrain.render(
renderer,
&self.globals,
&self.lights,
self.camera.get_focus_pos(),
);
renderer.render_post_process(
&self.postprocess.model,

View File

@ -74,7 +74,7 @@ fn mesh_worker(
match volume.get(wpos).unwrap_or(&Block::empty()).kind() {
BlockKind::Wheat => instances.push(SpriteInstance::new(
wpos.map(|e| e as f32),
wpos.map(|e| e as f32) + Vec3::new(0.5, 0.5, 0.0),
Rgb::broadcast(1.0),
)),
_ => {}
@ -110,7 +110,7 @@ impl Terrain {
let wheat_mesh = Meshable::<SpritePipeline, SpritePipeline>::generate_mesh(
&Segment::from(
assets::load_expect::<DotVoxData>("voxygen.voxel.sprite.wheat").as_ref(),
assets::load_expect::<DotVoxData>("voxygen.voxel.sprite.grass-0").as_ref(),
),
Vec3::new(6.0, 6.0, 0.0),
)
@ -388,17 +388,26 @@ impl Terrain {
renderer: &mut Renderer,
globals: &Consts<Globals>,
lights: &Consts<Light>,
focus_pos: Vec3<f32>,
) {
// Opaque
for (_pos, chunk) in &self.chunks {
for (pos, chunk) in &self.chunks {
if chunk.visible {
renderer.render_terrain_chunk(&chunk.opaque_model, globals, &chunk.locals, lights);
renderer.render_sprites(
&self.wheat_model,
globals,
&chunk.sprite_instances,
lights,
);
const SPRITE_RENDER_DISTANCE: f32 = 128.0;
let chunk_center = pos.map2(Vec2::from(TerrainChunkSize::SIZE), |e, sz: u32| {
(e as f32 + 0.5) * sz as f32
});
if Vec2::from(focus_pos).distance(chunk_center) < SPRITE_RENDER_DISTANCE {
renderer.render_sprites(
&self.wheat_model,
globals,
&chunk.sprite_instances,
lights,
);
}
}
}

View File

@ -254,7 +254,10 @@ impl<'a> BlockGen<'a> {
BlockKind::Normal,
saturate_srgb(col, 0.45).map(|e| (e * 255.0) as u8),
))
} else if (wposf.z as f32) < height + 0.02 {
} else if (wposf.z as f32) < height + 1.0
&& (wposf.z as f32 > water_height + 3.0)
&& (chaos * 4096.0).fract() < 0.025
{
Some(Block::new(BlockKind::Wheat, Rgb::broadcast(0)))
} else {
None