Added fog
This commit is contained in:
+2
-2
@@ -40,8 +40,8 @@ impl Game {
|
|||||||
// Generate new world
|
// Generate new world
|
||||||
else {
|
else {
|
||||||
let timer = Instant::now();
|
let timer = Instant::now();
|
||||||
for x in 0..128 {
|
for x in 0..32 {
|
||||||
for z in 0..128 {
|
for z in 0..32 {
|
||||||
let pos = &Point3{x,y:0,z};
|
let pos = &Point3{x,y:0,z};
|
||||||
let block = app.world.gen_block(pos);
|
let block = app.world.gen_block(pos);
|
||||||
content.write(pos, block);
|
content.write(pos, block);
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
use std::path::Path;
|
|
||||||
use image::GenericImageView;
|
|
||||||
|
|
||||||
pub struct Texture {
|
|
||||||
pub texture: wgpu::Texture,
|
|
||||||
pub view: wgpu::TextureView,
|
|
||||||
pub sampler: wgpu::Sampler,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Texture {
|
|
||||||
|
|
||||||
pub fn from_file(device: &wgpu::Device, queue: &wgpu::Queue, str_path: &str) -> Self {
|
|
||||||
let img_path = Path::new(str_path);
|
|
||||||
let img = image::open(img_path).unwrap();
|
|
||||||
Self::from_image(&device, &queue, &img)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_image(device: &wgpu::Device, queue: &wgpu::Queue, img: &image::DynamicImage) -> Self {
|
|
||||||
// Data from image
|
|
||||||
let img_dim = img.dimensions();
|
|
||||||
let img_data = img.as_rgba8().unwrap();
|
|
||||||
|
|
||||||
// Create texture on device
|
|
||||||
let texture_size = wgpu::Extent3d { width: img_dim.0, height: img_dim.1, depth: 1, };
|
|
||||||
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
|
||||||
size: texture_size,
|
|
||||||
mip_level_count: 1,
|
|
||||||
sample_count: 1,
|
|
||||||
dimension: wgpu::TextureDimension::D2,
|
|
||||||
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
|
||||||
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
|
|
||||||
label: None,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Write texture to memory
|
|
||||||
queue.write_texture(
|
|
||||||
wgpu::TextureCopyView { texture: &texture, mip_level: 0, origin: wgpu::Origin3d::ZERO },
|
|
||||||
img_data,
|
|
||||||
wgpu::TextureDataLayout { offset: 0, bytes_per_row: 4 * img_dim.0, rows_per_image: img_dim.1 },
|
|
||||||
texture_size,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Create view and sampler
|
|
||||||
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
|
|
||||||
let sampler = device.create_sampler(
|
|
||||||
&wgpu::SamplerDescriptor {
|
|
||||||
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
|
||||||
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
|
||||||
address_mode_w: wgpu::AddressMode::ClampToEdge,
|
|
||||||
//mag_filter: wgpu::FilterMode::Linear,
|
|
||||||
mag_filter: wgpu::FilterMode::Nearest,
|
|
||||||
min_filter: wgpu::FilterMode::Nearest,
|
|
||||||
mipmap_filter: wgpu::FilterMode::Nearest,
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
Self { texture, view, sampler }
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Binary file not shown.
@@ -55,9 +55,15 @@ vec3 solve()
|
|||||||
// HitResult second = castNodes(secondRay, 25);
|
// HitResult second = castNodes(secondRay, 25);
|
||||||
// if(second.data > 0) color += getAlbedo(second.data) * 0.8;
|
// if(second.data > 0) color += getAlbedo(second.data) * 0.8;
|
||||||
|
|
||||||
|
// Fog
|
||||||
|
float dist = distance(primary.pos, primaryRay.origin);
|
||||||
|
float fog = exp2(-pow(0.02*dist, 2));
|
||||||
|
color = mix(vec3(0.8, 0.8, 0.75), color, clamp(fog, 0.0, 1.0));
|
||||||
|
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sky
|
||||||
float up = clamp(dot(primaryRay.dir, vec3(0, 1, 0)) + 0.1, 0, 1);
|
float up = clamp(dot(primaryRay.dir, vec3(0, 1, 0)) + 0.1, 0, 1);
|
||||||
return vec3(0.176, 0.592, 0.901) * up;
|
return vec3(0.176, 0.592, 0.901) * up;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user