Skip to content

Commit 8d05b26

Browse files
committed
feat: fog
1 parent 518a515 commit 8d05b26

File tree

5 files changed

+87
-14
lines changed

5 files changed

+87
-14
lines changed

src/pipeline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ impl Pipeline {
173173
&bind_group_0_layout,
174174
&bind_group_1_layout,
175175
&state.world.chunk_data_layout,
176+
&state.player.read().unwrap().camera.position_bind_group_layout
176177
],
177178
push_constant_ranges: &[],
178179
});
@@ -244,7 +245,6 @@ impl PipelineTrait for Pipeline {
244245
fn bind_group_0(&self) -> &BindGroup {
245246
&self.bind_group_0
246247
}
247-
248248
fn bind_group_1(&self) -> &BindGroup {
249249
&self.bind_group_1
250250
}

src/player.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::sync::{Arc, RwLock};
55
use std::time::{Duration, Instant};
66

77
use glam::{vec2, vec3, Mat2, Vec2, Vec3};
8+
use wgpu::util::{BufferInitDescriptor, DeviceExt};
9+
use wgpu::BindGroupLayout;
810

911
use crate::blocks::block::{Block, FaceDirections};
1012
use crate::collision::{CollisionPoint, RayResult};
@@ -46,6 +48,13 @@ pub struct Player {
4648
pub facing_face: Option<FaceDirections>,
4749
}
4850
impl Player {
51+
pub fn update(&mut self) {
52+
self.camera.queue.write_buffer(
53+
&self.camera.position_buffer,
54+
0,
55+
bytemuck::cast_slice(&[self.camera.eye]),
56+
)
57+
}
4958
// Position relative to the chunk
5059
pub fn to_relative_position(&self) -> glam::Vec3 {
5160
todo!();
@@ -220,21 +229,64 @@ pub struct Camera {
220229
pub znear: f32,
221230
pub zfar: f32,
222231
pub needs_update: bool,
232+
pub device: Arc<wgpu::Device>,
233+
pub queue: Arc<wgpu::Queue>,
234+
pub position_buffer: wgpu::Buffer,
235+
pub position_bind_group: wgpu::BindGroup,
236+
pub position_bind_group_layout: wgpu::BindGroupLayout,
223237
}
224238

225239
impl Camera {
226-
pub fn new(surface_width: f32, surface_height: f32) -> Camera {
240+
pub fn new(
241+
surface_width: f32,
242+
surface_height: f32,
243+
device: Arc<wgpu::Device>,
244+
queue: Arc<wgpu::Queue>,
245+
) -> Camera {
227246
let (eye, yaw, pitch) = if let Ok((eye, yaw, pitch)) = Camera::load(Box::new(())) {
228247
(eye, yaw, pitch)
229248
} else {
230249
(glam::vec3(-4.0, 50.0, 4.0), consts::FRAC_PI_2, 0.0)
231250
};
251+
252+
let position_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
253+
contents: bytemuck::cast_slice(&[eye]),
254+
label: Some("camera-position"),
255+
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
256+
});
257+
let position_bind_group_layout =
258+
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
259+
label: Some("camera-position-layout"),
260+
entries: &[wgpu::BindGroupLayoutEntry {
261+
binding: 0,
262+
visibility: wgpu::ShaderStages::VERTEX,
263+
ty: wgpu::BindingType::Buffer {
264+
ty: wgpu::BufferBindingType::Uniform,
265+
has_dynamic_offset: false,
266+
min_binding_size: None,
267+
},
268+
count: None,
269+
}],
270+
});
271+
let position_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
272+
layout: &position_bind_group_layout,
273+
label: Some("camera-position-layout"),
274+
entries: &[wgpu::BindGroupEntry {
275+
binding: 0,
276+
resource: position_buffer.as_entire_binding(),
277+
}],
278+
});
279+
232280
Self {
281+
position_bind_group_layout,
282+
position_bind_group,
283+
position_buffer,
233284
aspect_ratio: surface_width / surface_height,
285+
device,
286+
queue,
234287
eye,
235288
yaw,
236289
pitch,
237-
238290
fovy: consts::FRAC_PI_4,
239291
znear: 0.1,
240292
zfar: 1000.,

src/shaders/shader.wgsl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ struct VertexOutput {
2020
@location(1) normals: vec3<f32>,
2121
@location(2) chunk_position: vec2<i32>,
2222
@location(3) block_type: u32,
23-
@location(4) ao: f32
23+
@location(4) ao: f32,
24+
@location(5) fog: f32
2425
}
2526

2627

@@ -30,16 +31,22 @@ var<uniform> projection: mat4x4<f32>;
3031
var<uniform> view: mat4x4<f32>;
3132
@group(2) @binding(0)
3233
var <uniform> current_chunk: vec2<i32>;
34+
@group(3) @binding(0)
35+
var <uniform> player_position: vec3<f32>;
3336

3437

3538
@vertex
3639
fn vs_main(in: VertexInput, instance_data: InstanceInput) -> VertexOutput {
3740
var out: VertexOutput;
3841

3942

40-
let chunk_offset = vec4<f32>(f32(current_chunk.x) * 16.0, 0.0, f32(current_chunk.y) * 16.0, 0.0);
43+
let chunk_offset = vec3<f32>(f32(current_chunk.x) * 16.0, 0.0, f32(current_chunk.y) * 16.0);
44+
let block_position = in.position + chunk_offset;
4145

42-
out.clip_position = projection * view * (vec4<f32>(in.position.xyz, 1.0) + chunk_offset);
46+
let player_dist = distance(player_position, block_position);
47+
48+
out.fog = min(pow(player_dist / 80.0, 6.0), 1.0);
49+
out.clip_position = projection * view * (vec4<f32>(block_position, 1.0));
4350
out.normals = in.normal;
4451
out.tex_coords = in.tex_coords;
4552
out.ao = in.ao;
@@ -58,7 +65,8 @@ struct FragmentInput {
5865
@location(1) normals: vec3<f32>,
5966
@location(2) current_chunk: vec2<i32>,
6067
@location(3) block_type: u32,
61-
@location(4) ao: f32
68+
@location(4) ao: f32,
69+
@location(5) fog: f32
6270
}
6371

6472
const light_direction = vec3<f32>(0.25, 1.0, -0.5);
@@ -72,6 +80,7 @@ fn fs_main(in: FragmentInput) -> @location(0) vec4<f32> {
7280
color *= max(dot(in.normals, normalize(light_direction)), 0.2);
7381
color += vec4<f32>(vec3<f32>(ambient_light), 0.0);
7482
color *= 1.0 - (in.ao * 0.9);
83+
color = mix(color, vec4<f32>(0.1, 0.2, 0.3, 1.0), in.fog);
7584

7685
return color;
7786
}

src/state.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,16 @@ impl State {
6666
view_formats: vec![],
6767
};
6868

69-
let camera = Camera::new(surface_config.width as f32, surface_config.height as f32);
69+
let camera = Camera::new(
70+
surface_config.width as f32,
71+
surface_config.height as f32,
72+
device.clone(),
73+
queue.clone(),
74+
);
75+
let current_chunk = camera.eye.get_chunk_from_position_absolute();
7076
let player = Arc::new(RwLock::new(Player {
7177
camera,
72-
current_chunk: (0, 0),
78+
current_chunk,
7379
is_jumping: false,
7480
on_ground: false,
7581
facing_block: None,
@@ -84,7 +90,7 @@ impl State {
8490
};
8591

8692
let mut world = World::init_world(device.clone(), queue.clone());
87-
world.init_chunks();
93+
world.init_chunks(Arc::clone(&player));
8894
let ui = UI::new(device.clone(), queue.clone());
8995

9096
let mut state = Self {
@@ -254,6 +260,7 @@ impl State {
254260
delta_time,
255261
&collisions,
256262
);
263+
player.update();
257264
if let Some((block, face_dir)) = player.get_facing_block(&collisions) {
258265
let block = self.world.get_blocks_absolute(&block.to_block_position());
259266

@@ -309,6 +316,8 @@ impl State {
309316
.map(|f| f.read().unwrap())
310317
.collect::<Vec<_>>();
311318

319+
let player = self.player.read().unwrap();
320+
312321
{
313322
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
314323
label: None,
@@ -343,6 +352,8 @@ impl State {
343352
rpass.set_bind_group(0, pipeline.bind_group_0(), &[]);
344353
rpass.set_bind_group(1, pipeline.bind_group_1(), &[]);
345354

355+
rpass.set_bind_group(3, &player.camera.position_bind_group, &[]);
356+
346357
for chunk in chunks.iter() {
347358
if chunk.visible {
348359
rpass.set_bind_group(2, &chunk.chunk_bind_group, &[]);

src/world.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ impl World {
317317
sender.send(()).unwrap();
318318
});
319319
}
320-
for _ in self.chunks.iter(){
320+
for _ in self.chunks.iter() {
321321
receiver.recv().unwrap();
322322
}
323323
}
@@ -332,12 +332,13 @@ impl World {
332332
chunkbrw.save().expect("failed to save");
333333
}
334334
}
335-
pub fn init_chunks(&mut self) {
335+
pub fn init_chunks(&mut self, player: Arc<RwLock<Player>>) {
336336
let (sender, receiver) = mpsc::channel();
337+
let player = player.read().unwrap();
337338

338339
let mut chunks = vec![];
339-
for chunk_x in LB..=UB {
340-
for chunk_y in LB..=UB {
340+
for chunk_x in LB + player.current_chunk.0..=UB + player.current_chunk.0 {
341+
for chunk_y in LB + player.current_chunk.1..=UB + player.current_chunk.1 {
341342
let sender = sender.clone();
342343
let noise_data = Arc::clone(&self.noise_data);
343344
let chunk_data_layout = Arc::clone(&self.chunk_data_layout);

0 commit comments

Comments
 (0)