This commit is contained in:
Piotrek
2021-05-04 20:26:15 +02:00
parent 78d3a43deb
commit 89934ba97c
13 changed files with 68 additions and 78 deletions
+2 -2
View File
@@ -43,8 +43,8 @@ impl CameraController {
Self {
enabled: false,
movement: CameraMovement { speed: 2.0, .. CameraMovement::default()},
rotation: CameraRotation { yaw: 45.0, sensitivity: 0.2, .. CameraRotation::default() },
cam_position: (0.0, 0.0, 0.0).into(),
rotation: CameraRotation { yaw: 135.0, sensitivity: 0.2, .. CameraRotation::default() },
cam_position: (-1.0, 2.0, -1.0).into(),
}
}
+1 -3
View File
@@ -1,6 +1,5 @@
use winit::event::{Event, WindowEvent, KeyboardInput, ElementState, VirtualKeyCode, MouseButton};
use winit::event::Event;
use noise::Perlin;
use std::sync::Arc;
use crate::renderer::{camera::CameraTransform, Level};
@@ -44,7 +43,6 @@ impl App {
GameState::Running => {
self.camera_controller.set_enabled(true);
}
_ => ()
};
// Currently we are not preventing any state changes, just return requested state
+3 -2
View File
@@ -1,9 +1,9 @@
use cgmath::{Point3, InnerSpace};
use cgmath::Point3;
use noise::{Perlin, NoiseFn};
use rand::{Rng, prelude::ThreadRng};
use crate::renderer::Level;
use crate::renderer::buffers::{BLOCK_SIZE, NODE_TEX_SIZE, Block};
use crate::renderer::buffers::BLOCK_SIZE;
const SCALE: f64 = 150.0;
@@ -33,6 +33,7 @@ impl WorldGen {
let value = (noise.get([(x + lx) as f64 / SCALE, (z + lz) as f64 /SCALE]) + 1.0) / 2.0;
let mut h = (value * 30.0) as usize;
h += (rand.gen::<f32>() * 2.0) as usize;
if h == 0 { h = 1; }
if h > max { max = h; }
map[lx][lz] = h;
}
-1
View File
@@ -1,4 +1,3 @@
use std::sync::Arc;
use winit::{
event::{Event, WindowEvent, KeyboardInput, ElementState, VirtualKeyCode, MouseButton},
event_loop::{ControlFlow, EventLoop},
+6 -23
View File
@@ -36,8 +36,8 @@ impl Blocks {
pub fn new(device: &wgpu::Device) -> Self {
// Create texture
let BLOCK_TEX_DIM = BLOCK_TEX_SIZE * BLOCK_SIZE;
let size = wgpu::Extent3d { width: BLOCK_SIZE as u32, height: BLOCK_TEX_DIM as u32, depth: BLOCK_TEX_DIM as u32 };
let block_tex_dim = BLOCK_TEX_SIZE * BLOCK_SIZE;
let size = wgpu::Extent3d { width: BLOCK_SIZE as u32, height: block_tex_dim as u32, depth: block_tex_dim as u32 };
let texture = device.create_texture(
&wgpu::TextureDescriptor {
size: size,
@@ -98,7 +98,10 @@ impl Blocks {
Self { blocks, texture, bind_layout, bind_group, free_block: 0 }
}
pub fn write_id(&mut self, queue: &wgpu::Queue, block_id: usize) {
/*
* Writes block to gpu
*/
pub fn write(&mut self, queue: &wgpu::Queue, block_id: usize) {
// Get block from id
let block = &self.blocks[block_id];
@@ -123,26 +126,6 @@ impl Blocks {
&mut self.blocks[block_id]
}
// /*
// Write block data to gpu, should be called to apply changes
// */
// pub fn write(&self, queue: &wgpu::Queue, block: &Block) {
// // Calculate block position in texture
// let y = (block.id%BLOCK_TEX_SIZE * BLOCK_SIZE) as u32;
// let z = (block.id/BLOCK_TEX_SIZE * BLOCK_SIZE) as u32;
// let block_origin = wgpu::Origin3d{ x:0, y, z };
// let block_size = wgpu::Extent3d{ width: BLOCK_SIZE as u32, height: BLOCK_SIZE as u32, depth: BLOCK_SIZE as u32 };
// let block_bytes : [u8;BLOCK_LEN] = block.data.try_into().unwrap();
// // Write to texture
// queue.write_texture(
// wgpu::TextureCopyView { texture: &self.texture, mip_level: 0, origin: block_origin },
// &block_bytes,
// wgpu::TextureDataLayout { offset: 0, bytes_per_row: block_size.width, rows_per_image: block_size.height },
// block_size
// );
// }
/*
* Allocates new block
*/
+1 -2
View File
@@ -1,5 +1,4 @@
use byteorder::{ByteOrder, LittleEndian};
use rand::prelude::*;
use crate::renderer::buffers::{NODE_TEX_SIZE, NODE_TEX_LEN};
@@ -70,7 +69,7 @@ impl Nodes {
// Data
let nodes = vec![0_u32; NODE_TEX_LEN].into_boxed_slice();
println!("Node buffer size: {} MB", nodes.len() as f32 * 4.0 / 1024.0 / 1024.0);
println!("Nodes size: {} MB", (nodes.len() as f32 * 4.0 / 1024.0 / 1024.0 * 100.0).round() / 100.0);
// Done
Self { nodes, texture, size, bind_layout, bind_group }
+3 -3
View File
@@ -24,9 +24,9 @@ impl Camera {
}
pub fn set_aspect(&mut self, aspect: f32) { self.proj_matrix = cgmath::perspective(Deg(60.0), aspect, 0.1, 100.0); }
pub fn get_projection(&self) -> Matrix4<f32> { return self.proj_matrix; }
pub fn get_view(&self) -> Matrix4<f32> { return self.view_matrix; }
pub fn get_position(&self) -> Point3<f32> { return self.position; }
pub fn get_projection(&self) -> Matrix4<f32> { self.proj_matrix }
pub fn get_view(&self) -> Matrix4<f32> { self.view_matrix }
pub fn get_position(&self) -> Point3<f32> { self.position }
}
pub trait CameraTransform {
+15 -4
View File
@@ -9,7 +9,13 @@ pub trait Level {
impl Level for Renderer {
fn block_get(&mut self, block_pos: Point3<usize>) -> &mut buffers::Block{
/*
* Grabs existing or allocates new block for given position
*
* block_pos: Position of the block in world, in block space
* block_id: Block identifier, equal to Block.id
*/
fn block_get(&mut self, block_pos: Point3<usize>) -> &mut buffers::Block {
// Get node
let node_idx = block_pos.x + buffers::NODE_TEX_SIZE * (block_pos.y + buffers::NODE_TEX_SIZE * block_pos.z);
let node_value = self.buffers.node_buffer.nodes[node_idx];
@@ -25,9 +31,14 @@ impl Level for Renderer {
self.buffers.brick_buffer.get((node_value-1) as usize)
}
fn block_dirty(&mut self, id: usize) {
if !self.changed_bricks.contains(&id) {
self.changed_bricks.push(id);
/*
* Marks block as dirty, will be sent to gpu at the next frame
*
* block_id: Block identifier, equal to Block.id
*/
fn block_dirty(&mut self, block_id: usize) {
if !self.changed_bricks.contains(&block_id) {
self.changed_bricks.push(block_id);
}
}
}
+1 -2
View File
@@ -1,5 +1,4 @@
use winit::{window::Window, dpi::PhysicalSize};
use cgmath::{Point3};
pub mod buffers;
use buffers::Buffers;
@@ -121,7 +120,7 @@ impl Renderer {
if self.changed_bricks.len() > 0 {
self.buffers.node_buffer.write(&self.queue);
for block_id in &self.changed_bricks {
self.buffers.brick_buffer.write_id(&self.queue, *block_id);
self.buffers.brick_buffer.write(&self.queue, *block_id);
}
// let used = self.buffers.brick_buffer.next_block(false);
Binary file not shown.
+10 -36
View File
@@ -1,26 +1,22 @@
#version 450
struct Material {
vec3 albedo;
float specular;
};
#include "structs.cginc"
// Variables
// Input output
layout(location=0) in vec2 _InUV;
layout(location=0) out vec4 _OutColor;
layout(set=0,binding=0) uniform Uniforms {
// Uniform
layout(set=0, binding=0) uniform Uniform {
mat4 _ViewMatrix;
mat4 _ProjMatrixInv;
vec3 _CamPos;
vec3 _SunDir;
float _Time;
};
layout(set=1,binding=0) uniform usampler3D _NodesTexture;
layout(set=2,binding=0) uniform usampler3D _BricksTexture;
layout(set=3, binding=0) uniform Materials {
Material _Materials[256];
};
layout(set=1, binding=0) uniform usampler3D _NodesTexture;
layout(set=2, binding=0) uniform usampler3D _BricksTexture;
layout(set=3, binding=0) uniform Materials { Material _Materials[256]; };
// Defines
#define EPSILON 0.00000001
@@ -29,34 +25,12 @@ layout(set=3, binding=0) uniform Materials {
#define BLOCK_TEX_SIZE 64
#define BLOCK_SIZE 32
// Include
#include "structs.cginc"
// Includes
#include "util.cginc"
#include "shading.cginc"
#include "raycasting.cginc"
float seed;
float random(float s) {
return fract(sin(seed++ + s)*43758.5453123);
}
vec3 randomHemisphere(vec3 dir)
{
vec3 uu = normalize(cross(dir, vec3(0.0,1.0,1.0)));
vec3 vv = cross(uu, dir);
vec2 rv2 = vec2(random(1), random(2));
float ra = sqrt(rv2.y);
float rx = ra*cos(6.2831*rv2.x);
float ry = ra*sin(6.2831*rv2.x);
float rz = sqrt(1.0-rv2.y);
vec3 rr = vec3(rx*uu + ry*vv + rz*dir );
return normalize(rr);
}
vec3 solve()
{
// Calculate ray direction
@@ -90,6 +64,6 @@ vec3 solve()
void main()
{
seed = 420 * _InUV.x + 1337 * _InUV.y + _Time * 1234;
random_seed = 420 * _InUV.x + 1337 * _InUV.y + _Time * 1234;
_OutColor = vec4(solve(), 1);
}
+5
View File
@@ -1,4 +1,9 @@
struct Material {
vec3 albedo;
float specular;
};
struct Ray {
vec3 origin;
vec3 dir;
+21
View File
@@ -0,0 +1,21 @@
float random_seed;
float random(float s) {
return fract(sin(random_seed++ + s)*43758.5453123);
}
vec3 randomHemisphere(vec3 dir)
{
vec3 uu = normalize(cross(dir, vec3(0.0,1.0,1.0)));
vec3 vv = cross(uu, dir);
vec2 rv2 = vec2(random(1), random(2));
float ra = sqrt(rv2.y);
float rx = ra*cos(6.2831*rv2.x);
float ry = ra*sin(6.2831*rv2.x);
float rz = sqrt(1.0-rv2.y);
vec3 rr = vec3(rx*uu + ry*vv + rz*dir );
return normalize(rr);
}