Skip to content

Commit 79f886d

Browse files
committed
fog based on render dist
1 parent 7d13f0a commit 79f886d

File tree

6 files changed

+78
-20
lines changed

6 files changed

+78
-20
lines changed

src/pipeline.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ impl Pipeline {
6868
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
6969
});
7070

71+
// Constant bindgroup for chunks per row
72+
let world_chunk_per_row_buffer =
73+
state
74+
.device
75+
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
76+
contents: bytemuck::cast_slice(&[crate::world::CHUNKS_PER_ROW]),
77+
label: Some("world_chunk_per_row"),
78+
usage: wgpu::BufferUsages::UNIFORM,
79+
});
80+
7181
// Bind groups
7282
let bind_group_0_layout =
7383
state
@@ -95,6 +105,16 @@ impl Pipeline {
95105
},
96106
count: None,
97107
},
108+
wgpu::BindGroupLayoutEntry {
109+
binding: 2,
110+
visibility: wgpu::ShaderStages::VERTEX,
111+
ty: wgpu::BindingType::Buffer {
112+
ty: wgpu::BufferBindingType::Uniform,
113+
has_dynamic_offset: false,
114+
min_binding_size: None,
115+
},
116+
count: None,
117+
},
98118
],
99119
});
100120
let bind_group_0 = state.device.create_bind_group(&wgpu::BindGroupDescriptor {
@@ -109,6 +129,10 @@ impl Pipeline {
109129
binding: 1,
110130
resource: view_buffer.as_entire_binding(),
111131
},
132+
wgpu::BindGroupEntry {
133+
binding: 2,
134+
resource: world_chunk_per_row_buffer.as_entire_binding(),
135+
},
112136
],
113137
});
114138

src/shaders/shader.wgsl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ struct VertexOutput {
2929
var<uniform> projection: mat4x4<f32>;
3030
@group(0) @binding(1)
3131
var<uniform> view: mat4x4<f32>;
32+
@group(0) @binding(2)
33+
var <uniform> chunks_per_row: u32;
34+
3235
@group(2) @binding(0)
3336
var <uniform> current_chunk: vec2<i32>;
3437
@group(3) @binding(0)
@@ -45,7 +48,10 @@ fn vs_main(in: VertexInput, instance_data: InstanceInput) -> VertexOutput {
4548

4649
let player_dist = distance(player_position, block_position);
4750

48-
out.fog = min(pow(player_dist / 80.0, 6.0), 1.0);
51+
let r = f32(16 * (i32(chunks_per_row) / 2));
52+
out.fog = clamp((player_dist - r) / 8.0, 0.0, 1.0);
53+
54+
// out.fog = min(pow(player_dist / 80.0, 6.0), 1.0);
4955
out.clip_position = projection * view * (vec4<f32>(block_position, 1.0));
5056
out.normals = in.normal;
5157
out.tex_coords = in.tex_coords;

src/shaders/water_shader.wgsl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ struct VertexInput {
55
@location(0) position: vec3<f32>,
66
@location(1) normal: vec3<f32>,
77
@location(2) tex_coords: vec2<f32>,
8-
@location(3) ao: f32,
98

109
}
1110
struct InstanceInput {
@@ -20,15 +19,16 @@ struct VertexOutput {
2019
@location(1) normals: vec3<f32>,
2120
@location(2) chunk_position: vec2<i32>,
2221
@location(3) block_type: u32,
23-
@location(4) ao: f32,
24-
@location(5) fog: f32
22+
@location(4) fog: f32
2523
}
2624

2725

2826
@group(0) @binding(0)
2927
var<uniform> projection: mat4x4<f32>;
3028
@group(0) @binding(1)
3129
var<uniform> view: mat4x4<f32>;
30+
@group(0) @binding(2)
31+
var <uniform> chunks_per_row: u32;
3232
@group(2) @binding(0)
3333
var <uniform> current_chunk: vec2<i32>;
3434
@group(3) @binding(0)
@@ -45,11 +45,12 @@ fn vs_main(in: VertexInput, instance_data: InstanceInput) -> VertexOutput {
4545

4646
let player_dist = distance(player_position, block_position);
4747

48-
out.fog = min(pow(player_dist / 80.0, 6.0), 1.0);
48+
let r = f32(16 * (i32(chunks_per_row) / 2));
49+
out.fog = clamp((player_dist - r) / 8.0, 0.0, 1.0);
50+
4951
out.clip_position = projection * view * (vec4<f32>(block_position, 1.0));
5052
out.normals = in.normal;
5153
out.tex_coords = in.tex_coords;
52-
out.ao = in.ao;
5354

5455
return out;
5556
}
@@ -65,8 +66,7 @@ struct FragmentInput {
6566
@location(1) normals: vec3<f32>,
6667
@location(2) current_chunk: vec2<i32>,
6768
@location(3) block_type: u32,
68-
@location(4) ao: f32,
69-
@location(5) fog: f32
69+
@location(4) fog: f32
7070
}
7171

7272

@@ -75,6 +75,8 @@ fn fs_main(in: FragmentInput) -> @location(0) vec4<f32> {
7575
var color: vec4<f32>;
7676
color = textureSample(diffuse, t_sampler, in.tex_coords);
7777
color.a = 0.6;
78+
color = mix(color, vec4<f32>(0.03, 0.64, 0.97, 1.0), in.fog);
79+
7880

7981
return color;
8082
}

src/state.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl State {
322322
let player = self.player.read().unwrap();
323323

324324
{
325-
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
325+
let mut main_rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
326326
label: None,
327327
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
328328
view: &view,
@@ -350,33 +350,33 @@ impl State {
350350
});
351351
let pipeline = &self.pipelines[0];
352352

353-
rpass.set_pipeline(pipeline.pipeline());
353+
main_rpass.set_pipeline(pipeline.pipeline());
354354

355-
rpass.set_bind_group(0, pipeline.bind_group_0(), &[]);
356-
rpass.set_bind_group(1, pipeline.bind_group_1(), &[]);
355+
main_rpass.set_bind_group(0, pipeline.bind_group_0(), &[]);
356+
main_rpass.set_bind_group(1, pipeline.bind_group_1(), &[]);
357357

358-
rpass.set_bind_group(3, &player.camera.position_bind_group, &[]);
358+
main_rpass.set_bind_group(3, &player.camera.position_bind_group, &[]);
359359

360360
for chunk in chunks.iter() {
361361
if chunk.visible {
362-
rpass.set_bind_group(2, &chunk.chunk_bind_group, &[]);
363-
rpass.set_vertex_buffer(
362+
main_rpass.set_bind_group(2, &chunk.chunk_bind_group, &[]);
363+
main_rpass.set_vertex_buffer(
364364
0,
365365
chunk
366366
.chunk_vertex_buffer
367367
.as_ref()
368368
.expect("Vertex buffer not initiated")
369369
.slice(..),
370370
);
371-
rpass.set_index_buffer(
371+
main_rpass.set_index_buffer(
372372
chunk
373373
.chunk_index_buffer
374374
.as_ref()
375375
.expect("Index buffer not initiated")
376376
.slice(..),
377377
wgpu::IndexFormat::Uint32,
378378
);
379-
rpass.draw_indexed(0..chunk.indices, 0, 0..1);
379+
main_rpass.draw_indexed(0..chunk.indices, 0, 0..1);
380380
};
381381
}
382382
}

src/water.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct WaterPipeline {
2323
pub pipeline_type: crate::pipeline::PipelineType,
2424
}
2525
impl WaterPipeline {
26-
// TODO: This is very ugly and should be abstracted for all pipelines
26+
// TODO: This is very ugly and should be abstracted for all pipelines. Also doubles the resource for uniforms etc.
2727
pub fn new(state: &State) -> Self {
2828
let swapchain_capabilities = state.surface.get_capabilities(&state.adapter);
2929
let swapchain_format = swapchain_capabilities.formats[0];
@@ -57,6 +57,16 @@ impl WaterPipeline {
5757
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
5858
});
5959

60+
// Constant bindgroup for chunks per row
61+
let world_chunk_per_row_buffer =
62+
state
63+
.device
64+
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
65+
contents: bytemuck::cast_slice(&[crate::world::CHUNKS_PER_ROW]),
66+
label: Some("world_chunk_per_row"),
67+
usage: wgpu::BufferUsages::UNIFORM,
68+
});
69+
6070
// Bind groups
6171
let bind_group_0_layout =
6272
state
@@ -84,6 +94,16 @@ impl WaterPipeline {
8494
},
8595
count: None,
8696
},
97+
wgpu::BindGroupLayoutEntry {
98+
binding: 2,
99+
visibility: wgpu::ShaderStages::VERTEX,
100+
ty: wgpu::BindingType::Buffer {
101+
ty: wgpu::BufferBindingType::Uniform,
102+
has_dynamic_offset: false,
103+
min_binding_size: None,
104+
},
105+
count: None,
106+
},
87107
],
88108
});
89109
let bind_group_0 = state.device.create_bind_group(&wgpu::BindGroupDescriptor {
@@ -98,6 +118,10 @@ impl WaterPipeline {
98118
binding: 1,
99119
resource: view_buffer.as_entire_binding(),
100120
},
121+
wgpu::BindGroupEntry {
122+
binding: 2,
123+
resource: world_chunk_per_row_buffer.as_entire_binding(),
124+
},
101125
],
102126
});
103127

src/world.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use std::{
66
sync::{mpsc, Arc},
77
thread,
88
};
9+
use wgpu::util::DeviceExt;
10+
use wgpu::BindGroupEntry;
911

1012
use crate::blocks::block_type::BlockType;
1113
use crate::persistence::Saveable;
@@ -18,8 +20,8 @@ pub const NOISE_SIZE: u32 = 1024;
1820
pub const FREQUENCY: f32 = 1. / 128.;
1921
pub const NOISE_CHUNK_PER_ROW: u32 = NOISE_SIZE / CHUNK_SIZE;
2022
pub const MAX_TREES_PER_CHUNK: u32 = 3;
21-
// There will be a CHUNKS_PER_ROW * CHUNKS_PER_ROW region
22-
pub const CHUNKS_PER_ROW: u32 = 15;
23+
24+
pub const CHUNKS_PER_ROW: u32 = 25;
2325
pub const CHUNKS_REGION: u32 = CHUNKS_PER_ROW * CHUNKS_PER_ROW;
2426
pub const WATER_HEIGHT_LEVEL: u8 = 5;
2527

0 commit comments

Comments
 (0)