CHUNK GENERATION IS REAL
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
use crate::client::camera::Camera;
|
||||
use crate::{
|
||||
client::camera::Camera,
|
||||
common::component::{ChunkMesh, ChunkPos},
|
||||
};
|
||||
|
||||
use super::{voxel::VoxelColor, Renderer};
|
||||
use bevy_ecs::entity::Entity;
|
||||
@@ -10,6 +13,7 @@ use winit::window::Window;
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum RenderCommand {
|
||||
CreateVoxelGrid(CreateVoxelGrid),
|
||||
AddChunk(AddChunk),
|
||||
UpdateGridTransform(UpdateGridTransform),
|
||||
ViewUpdate(Camera),
|
||||
}
|
||||
@@ -23,6 +27,13 @@ pub struct CreateVoxelGrid {
|
||||
pub grid: Array3<VoxelColor>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AddChunk {
|
||||
pub id: Entity,
|
||||
pub pos: ChunkPos,
|
||||
pub mesh: ChunkMesh,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UpdateGridTransform {
|
||||
pub id: Entity,
|
||||
@@ -49,26 +60,28 @@ impl<'a> Renderer<'a> {
|
||||
let mut new_camera = false;
|
||||
for cmd in commands {
|
||||
match cmd {
|
||||
RenderCommand::CreateVoxelGrid(desc) => {
|
||||
self.voxel_pipeline.add_group(
|
||||
&self.device,
|
||||
&mut self.encoder,
|
||||
&mut self.staging_belt,
|
||||
desc,
|
||||
);
|
||||
}
|
||||
RenderCommand::CreateVoxelGrid(desc) => self.voxel_pipeline.add_group(
|
||||
&self.device,
|
||||
&mut self.encoder,
|
||||
&mut self.staging_belt,
|
||||
desc,
|
||||
),
|
||||
RenderCommand::ViewUpdate(camera) => {
|
||||
new_camera = true;
|
||||
self.camera = camera;
|
||||
}
|
||||
RenderCommand::UpdateGridTransform(update) => {
|
||||
self.voxel_pipeline.update_transform(
|
||||
&self.device,
|
||||
&mut self.encoder,
|
||||
&mut self.staging_belt,
|
||||
update,
|
||||
);
|
||||
}
|
||||
RenderCommand::UpdateGridTransform(update) => self.voxel_pipeline.update_transform(
|
||||
&self.device,
|
||||
&mut self.encoder,
|
||||
&mut self.staging_belt,
|
||||
update,
|
||||
),
|
||||
RenderCommand::AddChunk(desc) => self.voxel_pipeline.add_chunk(
|
||||
&self.device,
|
||||
&mut self.encoder,
|
||||
&mut self.staging_belt,
|
||||
desc,
|
||||
),
|
||||
}
|
||||
}
|
||||
if new_camera {
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
mod command;
|
||||
mod util;
|
||||
pub mod voxel;
|
||||
pub mod voxel_poly;
|
||||
|
||||
pub use command::*;
|
||||
use util::Texture;
|
||||
|
||||
use super::camera::Camera;
|
||||
use crate::client::rsc::CLEAR_COLOR;
|
||||
use nalgebra::Vector2;
|
||||
use smaa::{SmaaMode, SmaaTarget};
|
||||
use voxel_poly::VoxelPipeline;
|
||||
use util::Texture;
|
||||
use voxel::VoxelPipeline;
|
||||
use winit::dpi::PhysicalSize;
|
||||
|
||||
pub struct Renderer<'a> {
|
||||
|
||||
@@ -1,239 +1,3 @@
|
||||
mod color;
|
||||
mod grid;
|
||||
mod group;
|
||||
mod light;
|
||||
mod view;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use bevy_ecs::entity::Entity;
|
||||
pub use color::*;
|
||||
|
||||
use light::GlobalLight;
|
||||
use nalgebra::{Projective3, Transform3, Translation3, Vector2, Vector3};
|
||||
|
||||
use super::UpdateGridTransform;
|
||||
use crate::client::{
|
||||
camera::Camera,
|
||||
render::{
|
||||
util::{ArrBufUpdate, Storage, Uniform},
|
||||
CreateVoxelGrid,
|
||||
},
|
||||
};
|
||||
|
||||
use {group::VoxelGroup, view::View};
|
||||
|
||||
pub struct VoxelPipeline {
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
view: Uniform<View>,
|
||||
bind_group_layout: wgpu::BindGroupLayout,
|
||||
bind_group: wgpu::BindGroup,
|
||||
voxel_groups: Storage<VoxelGroup>,
|
||||
voxels: Storage<VoxelColor>,
|
||||
global_lights: Storage<GlobalLight>,
|
||||
id_map: HashMap<Entity, (usize, VoxelGroup)>,
|
||||
}
|
||||
|
||||
impl VoxelPipeline {
|
||||
pub fn new(device: &wgpu::Device, format: &wgpu::TextureFormat) -> Self {
|
||||
// shaders
|
||||
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some("Tile Shader"),
|
||||
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
|
||||
});
|
||||
|
||||
let view = Uniform::init(device, "view", 0);
|
||||
let voxels = Storage::init(device, "voxels", 1);
|
||||
let voxel_groups = Storage::init(device, "voxel groups", 2);
|
||||
let global_lights = Storage::init_with(
|
||||
device,
|
||||
"global lights",
|
||||
3,
|
||||
&[GlobalLight {
|
||||
direction: Vector3::new(-0.5, -4.0, 2.0).normalize(),
|
||||
}],
|
||||
);
|
||||
|
||||
// bind groups
|
||||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
entries: &[
|
||||
view.bind_group_layout_entry(),
|
||||
voxels.bind_group_layout_entry(),
|
||||
voxel_groups.bind_group_layout_entry(),
|
||||
global_lights.bind_group_layout_entry(),
|
||||
],
|
||||
label: Some("tile_bind_group_layout"),
|
||||
});
|
||||
|
||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &bind_group_layout,
|
||||
entries: &[
|
||||
view.bind_group_entry(),
|
||||
voxels.bind_group_entry(),
|
||||
voxel_groups.bind_group_entry(),
|
||||
global_lights.bind_group_entry(),
|
||||
],
|
||||
label: Some("tile_bind_group"),
|
||||
});
|
||||
|
||||
// pipeline
|
||||
let render_pipeline_layout =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("Tile Pipeline Layout"),
|
||||
bind_group_layouts: &[&bind_group_layout],
|
||||
push_constant_ranges: &[],
|
||||
});
|
||||
|
||||
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Voxel Pipeline"),
|
||||
layout: Some(&render_pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: "vs_main",
|
||||
buffers: &[],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point: "fs_main",
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format: *format,
|
||||
blend: Some(wgpu::BlendState::REPLACE),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleStrip,
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw,
|
||||
cull_mode: None,
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
unclipped_depth: false,
|
||||
conservative: false,
|
||||
},
|
||||
depth_stencil: None,
|
||||
multisample: wgpu::MultisampleState {
|
||||
count: 1,
|
||||
mask: !0,
|
||||
alpha_to_coverage_enabled: true,
|
||||
},
|
||||
multiview: None,
|
||||
});
|
||||
|
||||
Self {
|
||||
pipeline: render_pipeline,
|
||||
view,
|
||||
bind_group,
|
||||
bind_group_layout,
|
||||
voxels,
|
||||
voxel_groups,
|
||||
global_lights,
|
||||
id_map: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_group(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
belt: &mut wgpu::util::StagingBelt,
|
||||
CreateVoxelGrid {
|
||||
id,
|
||||
pos,
|
||||
orientation,
|
||||
dimensions,
|
||||
grid,
|
||||
}: CreateVoxelGrid,
|
||||
) {
|
||||
let offset = self.voxels.len();
|
||||
|
||||
let updates = [ArrBufUpdate {
|
||||
offset,
|
||||
data: &grid.as_slice().unwrap(),
|
||||
}];
|
||||
let size = offset + grid.len();
|
||||
self.voxels.update(device, encoder, belt, size, &updates);
|
||||
|
||||
let proj = Projective3::identity()
|
||||
* Translation3::from(pos)
|
||||
* orientation
|
||||
* Translation3::from(-dimensions.cast() / 2.0);
|
||||
let group = VoxelGroup {
|
||||
transform: proj,
|
||||
transform_inv: proj.inverse(),
|
||||
dimensions: dimensions.cast(),
|
||||
offset: offset as u32,
|
||||
};
|
||||
let updates = [ArrBufUpdate {
|
||||
offset: self.voxel_groups.len(),
|
||||
data: &[group],
|
||||
}];
|
||||
let i = self.voxel_groups.len();
|
||||
let size = i + 1;
|
||||
self.voxel_groups
|
||||
.update(device, encoder, belt, size, &updates);
|
||||
|
||||
self.id_map.insert(id, (i, group));
|
||||
|
||||
self.bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &self.bind_group_layout,
|
||||
entries: &[
|
||||
self.view.bind_group_entry(),
|
||||
self.voxels.bind_group_entry(),
|
||||
self.voxel_groups.bind_group_entry(),
|
||||
self.global_lights.bind_group_entry(),
|
||||
],
|
||||
label: Some("tile_bind_group"),
|
||||
});
|
||||
}
|
||||
|
||||
pub fn update_transform(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
belt: &mut wgpu::util::StagingBelt,
|
||||
update: UpdateGridTransform,
|
||||
) {
|
||||
if let Some((i, group)) = self.id_map.get_mut(&update.id) {
|
||||
let proj = Projective3::identity()
|
||||
* Translation3::from(update.pos)
|
||||
* update.orientation
|
||||
* Translation3::from(-group.dimensions.cast() / 2.0);
|
||||
group.transform = proj;
|
||||
group.transform_inv = proj.inverse();
|
||||
let updates = [ArrBufUpdate {
|
||||
offset: *i,
|
||||
data: &[*group],
|
||||
}];
|
||||
let size = self.voxel_groups.len();
|
||||
self.voxel_groups
|
||||
.update(device, encoder, belt, size, &updates);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_view(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
belt: &mut wgpu::util::StagingBelt,
|
||||
size: Vector2<u32>,
|
||||
camera: &Camera,
|
||||
) {
|
||||
let transform =
|
||||
Transform3::identity() * Translation3::from(camera.pos) * camera.orientation;
|
||||
let data = View {
|
||||
width: size.x,
|
||||
height: size.y,
|
||||
zoom: camera.scale,
|
||||
transform,
|
||||
};
|
||||
self.view.update(device, encoder, belt, data)
|
||||
}
|
||||
|
||||
pub fn draw<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
|
||||
render_pass.set_pipeline(&self.pipeline);
|
||||
render_pass.set_bind_group(0, &self.bind_group, &[]);
|
||||
render_pass.draw(0..4, 0..1);
|
||||
}
|
||||
}
|
||||
mod poly;
|
||||
// mod ray;
|
||||
pub use poly::*;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use block_mesh::Voxel;
|
||||
use rand::distributions::{Distribution, Standard};
|
||||
|
||||
#[repr(C)]
|
||||
@@ -46,3 +47,13 @@ impl Distribution<VoxelColor> for Standard {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl block_mesh::Voxel for VoxelColor {
|
||||
fn get_visibility(&self) -> block_mesh::VoxelVisibility {
|
||||
match self.a {
|
||||
0 => block_mesh::VoxelVisibility::Empty,
|
||||
255 => block_mesh::VoxelVisibility::Opaque,
|
||||
_ => block_mesh::VoxelVisibility::Translucent,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
use bytemuck::Zeroable;
|
||||
|
||||
use crate::client::render::voxel::VoxelColor;
|
||||
use super::VoxelColor;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug, Zeroable)]
|
||||
@@ -1,21 +1,26 @@
|
||||
mod color;
|
||||
mod face;
|
||||
mod group;
|
||||
mod instance;
|
||||
mod square;
|
||||
mod light;
|
||||
mod view;
|
||||
|
||||
use core::panic;
|
||||
use block_mesh::{ndshape::RuntimeShape, UnitQuadBuffer, RIGHT_HANDED_Y_UP_CONFIG};
|
||||
pub use color::*;
|
||||
pub use face::*;
|
||||
|
||||
use group::FaceGroup;
|
||||
use instance::VoxelFace;
|
||||
use light::GlobalLight;
|
||||
use nalgebra::{Perspective3, Transform3, Translation3, Vector2, Vector3};
|
||||
use view::View;
|
||||
use wgpu::{SurfaceConfiguration, VertexAttribute, VertexFormat};
|
||||
|
||||
use crate::client::camera::Camera;
|
||||
use crate::{
|
||||
client::{camera::Camera, render::AddChunk},
|
||||
common::component::{chunk, ChunkData},
|
||||
};
|
||||
|
||||
use super::{
|
||||
util::{Instances, Texture, Uniform},
|
||||
use super::super::{
|
||||
util::{Instances, Storage, Texture, Uniform},
|
||||
CreateVoxelGrid, UpdateGridTransform,
|
||||
};
|
||||
|
||||
@@ -25,6 +30,7 @@ pub struct VoxelPipeline {
|
||||
bind_group_layout: wgpu::BindGroupLayout,
|
||||
bind_groups: Vec<wgpu::BindGroup>,
|
||||
vertices: Vec<Instances<VoxelFace>>,
|
||||
global_lights: Storage<GlobalLight>,
|
||||
}
|
||||
|
||||
const INSTANCE_ATTRS: [wgpu::VertexAttribute; 2] = [
|
||||
@@ -52,12 +58,21 @@ impl VoxelPipeline {
|
||||
let example_faces =
|
||||
Instances::<VoxelFace>::init(device, "voxel groups", 0, &INSTANCE_ATTRS);
|
||||
let example_group = Uniform::<FaceGroup>::init(device, "voxel group", 1);
|
||||
let global_lights = Storage::init_with(
|
||||
device,
|
||||
"global lights",
|
||||
3,
|
||||
&[GlobalLight {
|
||||
direction: Vector3::new(-0.5, -4.0, 2.0).normalize(),
|
||||
}],
|
||||
);
|
||||
|
||||
// bind groups
|
||||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
entries: &[
|
||||
view.bind_group_layout_entry(),
|
||||
example_group.bind_group_layout_entry(),
|
||||
global_lights.bind_group_layout_entry(),
|
||||
],
|
||||
label: Some("tile_bind_group_layout"),
|
||||
});
|
||||
@@ -84,7 +99,7 @@ impl VoxelPipeline {
|
||||
entry_point: "fs_main",
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format: config.format,
|
||||
blend: Some(wgpu::BlendState::REPLACE),
|
||||
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
@@ -119,6 +134,7 @@ impl VoxelPipeline {
|
||||
bind_group_layout,
|
||||
bind_groups: Vec::new(),
|
||||
vertices: Vec::new(),
|
||||
global_lights,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,7 +154,7 @@ impl VoxelPipeline {
|
||||
size.x as f32 / size.y as f32,
|
||||
std::f32::consts::PI / 2.0,
|
||||
0.1,
|
||||
1000.0,
|
||||
10000.0,
|
||||
);
|
||||
transform = projection.as_matrix() * transform;
|
||||
let data = View {
|
||||
@@ -177,46 +193,31 @@ impl VoxelPipeline {
|
||||
* Translation3::from(pos)
|
||||
* orientation
|
||||
* Translation3::from(-dimensions.cast() / 2.0);
|
||||
for face in 0..6 {
|
||||
let group = FaceGroup {
|
||||
dimensions: dimensions.cast(),
|
||||
transform: proj,
|
||||
face,
|
||||
};
|
||||
let uniform = Uniform::init_with(device, "voxel group", 1, &[group]);
|
||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &self.bind_group_layout,
|
||||
entries: &[self.view.bind_group_entry(), uniform.bind_group_entry()],
|
||||
label: Some("voxel bind group"),
|
||||
});
|
||||
self.bind_groups.push(bind_group);
|
||||
|
||||
let mut data = Vec::new();
|
||||
let n_offset = match face % 3 {
|
||||
0 => 1,
|
||||
1 => dimensions.z * dimensions.y,
|
||||
2 => dimensions.z,
|
||||
_ => 0,
|
||||
} as i32
|
||||
* ((face as i32 / 3) * 2 - 1);
|
||||
let face_dir = (face as i32 / 3) * 2 - 1;
|
||||
for (i, ((x, y, z), color)) in grid.indexed_iter().enumerate() {
|
||||
let neighbor = match face {
|
||||
0 => if z > 0 {Some((x, y, z - 1))} else {None},
|
||||
2 => if y > 0 {Some((x, y - 1, z))} else {None},
|
||||
1 => if x > 0 {Some((x - 1, y, z))} else {None},
|
||||
3 => if z < dimensions.z - 1 {Some((x, y, z + 1))} else {None},
|
||||
5 => if y < dimensions.y - 1 {Some((x, y + 1, z))} else {None},
|
||||
4 => if x < dimensions.x - 1 {Some((x + 1, y, z))} else {None},
|
||||
_ => panic!("what"),
|
||||
}.map(|p| grid.get(p).unwrap());
|
||||
if color.a > 0 && !neighbor.is_some_and(|c| c.a == color.a) {
|
||||
data.push(VoxelFace {
|
||||
index: i as u32,
|
||||
color: *color,
|
||||
});
|
||||
}
|
||||
}
|
||||
let mut buffer = UnitQuadBuffer::new();
|
||||
let dim: Vector3<u32> = dimensions.cast();
|
||||
let dim = Vector3::new(dim.z, dim.y, dim.x);
|
||||
let shape = RuntimeShape::<u32, 3>::new(dim.into());
|
||||
let slice = grid.as_slice().unwrap();
|
||||
block_mesh::visible_block_faces(
|
||||
slice,
|
||||
&shape,
|
||||
[0; 3],
|
||||
(dim - Vector3::new(1, 1, 1)).into(),
|
||||
&RIGHT_HANDED_Y_UP_CONFIG.faces,
|
||||
&mut buffer,
|
||||
);
|
||||
for (face, group) in buffer.groups.iter().enumerate() {
|
||||
let data: Vec<VoxelFace> = group
|
||||
.iter()
|
||||
.map(|a| {
|
||||
let i = a.minimum[0] + a.minimum[1] * dim.y + a.minimum[2] * dim.y * dim.x;
|
||||
VoxelFace {
|
||||
index: i,
|
||||
color: slice[i as usize],
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
self.vertices.push(Instances::init_with(
|
||||
device,
|
||||
"vvvvv",
|
||||
@@ -224,6 +225,64 @@ impl VoxelPipeline {
|
||||
&INSTANCE_ATTRS,
|
||||
&data,
|
||||
));
|
||||
let group = FaceGroup {
|
||||
dimensions: dimensions.cast(),
|
||||
transform: proj,
|
||||
face: ((8 - face) % 6) as u32,
|
||||
};
|
||||
let uniform = Uniform::init_with(device, "voxel group", 1, &[group]);
|
||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &self.bind_group_layout,
|
||||
entries: &[
|
||||
self.view.bind_group_entry(),
|
||||
uniform.bind_group_entry(),
|
||||
self.global_lights.bind_group_entry(),
|
||||
],
|
||||
label: Some("voxel bind group"),
|
||||
});
|
||||
self.bind_groups.push(bind_group);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_chunk(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
belt: &mut wgpu::util::StagingBelt,
|
||||
AddChunk { id, pos, mesh }: AddChunk,
|
||||
) {
|
||||
if mesh.faces.iter().all(|f| f.is_empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
let proj = Transform3::identity()
|
||||
* Translation3::from(pos.cast() * crate::common::component::chunk::SIDE_LENGTH as f32)
|
||||
* Translation3::from(-chunk::DIMENSIONS.cast() / 2.0);
|
||||
|
||||
for (face, meshes) in mesh.faces.iter().enumerate() {
|
||||
self.vertices.push(Instances::init_with(
|
||||
device,
|
||||
"vvvvv",
|
||||
0,
|
||||
&INSTANCE_ATTRS,
|
||||
meshes,
|
||||
));
|
||||
let group = FaceGroup {
|
||||
dimensions: chunk::DIMENSIONS.cast(),
|
||||
transform: proj,
|
||||
face: face as u32,
|
||||
};
|
||||
let uniform = Uniform::init_with(device, "voxel group", 1, &[group]);
|
||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &self.bind_group_layout,
|
||||
entries: &[
|
||||
self.view.bind_group_entry(),
|
||||
uniform.bind_group_entry(),
|
||||
self.global_lights.bind_group_entry(),
|
||||
],
|
||||
label: Some("voxel bind group"),
|
||||
});
|
||||
self.bind_groups.push(bind_group);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ struct InstanceInput {
|
||||
struct VertexOutput {
|
||||
@builtin(position) clip_position: vec4<f32>,
|
||||
@location(0) color: vec4<f32>,
|
||||
@location(1) normal: vec3<f32>,
|
||||
};
|
||||
|
||||
struct VoxelFace {
|
||||
@@ -28,16 +29,16 @@ struct VoxelGroup {
|
||||
face: u32,
|
||||
};
|
||||
|
||||
struct GlobalLight {
|
||||
dir: vec3<f32>,
|
||||
};
|
||||
|
||||
@group(0) @binding(0)
|
||||
var<uniform> view: View;
|
||||
@group(0) @binding(1)
|
||||
var<uniform> group: VoxelGroup;
|
||||
|
||||
const DIRECTIONS = array(
|
||||
vec3<f32>(1.0, 1.0, 0.0),
|
||||
vec3<f32>(0.0, 1.0, 1.0),
|
||||
vec3<f32>(1.0, 0.0, 1.0),
|
||||
);
|
||||
@group(0) @binding(3)
|
||||
var<storage, read> global_lights: array<GlobalLight>;
|
||||
|
||||
@vertex
|
||||
fn vs_main(
|
||||
@@ -48,14 +49,23 @@ fn vs_main(
|
||||
|
||||
let invert = select(0.0, 1.0, group.face / 3 == 1);
|
||||
let invert_mult = 1.0 - invert * 2.0;
|
||||
let face_axis = group.face % 3;
|
||||
var square_pos = vec2<f32>(
|
||||
f32(vi % 2u),
|
||||
invert + invert_mult * f32(vi / 2u),
|
||||
);
|
||||
var cube_pos = vec3<f32>(invert);
|
||||
square_pos *= invert_mult;
|
||||
cube_pos[(group.face) % 3] += square_pos.x;
|
||||
cube_pos[(group.face + 1) % 3] += square_pos.y;
|
||||
cube_pos[(group.face + 1) % 3] += square_pos.x;
|
||||
cube_pos[(group.face + 2) % 3] += square_pos.y;
|
||||
|
||||
let cube_normal = invert_mult * vec3<f32>(
|
||||
f32(face_axis == 0),
|
||||
f32(face_axis % 2),
|
||||
f32(face_axis / 2),
|
||||
);
|
||||
out.normal = (group.transform * vec4<f32>(cube_normal, 0.0)).xyz;
|
||||
|
||||
var pos = vec4<f32>(
|
||||
cube_pos,
|
||||
1.0,
|
||||
@@ -78,5 +88,9 @@ fn vs_main(
|
||||
fn fs_main(
|
||||
in: VertexOutput,
|
||||
) -> @location(0) vec4<f32> {
|
||||
return in.color;
|
||||
let diffuse = max(dot(global_lights[0].dir, in.normal) + 0.1, 0.0);
|
||||
let ambient = 0.2;
|
||||
let lighting = max(diffuse, ambient);
|
||||
let new_rgb = min(in.color.xyz * lighting, vec3<f32>(1.0));
|
||||
return vec4<f32>(new_rgb, in.color.a);
|
||||
}
|
||||
9
src/client/render/voxel/ray/light.rs
Normal file
9
src/client/render/voxel/ray/light.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use nalgebra::Vector3;
|
||||
|
||||
#[repr(C, align(16))]
|
||||
#[derive(Clone, Copy, PartialEq, bytemuck::Zeroable)]
|
||||
pub struct GlobalLight {
|
||||
pub direction: Vector3<f32>,
|
||||
}
|
||||
|
||||
unsafe impl bytemuck::Pod for GlobalLight {}
|
||||
237
src/client/render/voxel/ray/mod.rs
Normal file
237
src/client/render/voxel/ray/mod.rs
Normal file
@@ -0,0 +1,237 @@
|
||||
mod color;
|
||||
mod grid;
|
||||
mod group;
|
||||
mod light;
|
||||
mod view;
|
||||
|
||||
pub use color::*;
|
||||
|
||||
use super::super::UpdateGridTransform;
|
||||
use crate::client::{
|
||||
camera::Camera,
|
||||
render::{
|
||||
util::{ArrBufUpdate, Storage, Uniform},
|
||||
CreateVoxelGrid,
|
||||
},
|
||||
};
|
||||
use bevy_ecs::entity::Entity;
|
||||
use light::GlobalLight;
|
||||
use nalgebra::{Projective3, Transform3, Translation3, Vector2, Vector3};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use {group::VoxelGroup, view::View};
|
||||
|
||||
pub struct VoxelPipeline {
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
view: Uniform<View>,
|
||||
bind_group_layout: wgpu::BindGroupLayout,
|
||||
bind_group: wgpu::BindGroup,
|
||||
voxel_groups: Storage<VoxelGroup>,
|
||||
voxels: Storage<VoxelColor>,
|
||||
global_lights: Storage<GlobalLight>,
|
||||
id_map: HashMap<Entity, (usize, VoxelGroup)>,
|
||||
}
|
||||
|
||||
impl VoxelPipeline {
|
||||
pub fn new(device: &wgpu::Device, format: &wgpu::TextureFormat) -> Self {
|
||||
// shaders
|
||||
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some("Tile Shader"),
|
||||
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
|
||||
});
|
||||
|
||||
let view = Uniform::init(device, "view", 0);
|
||||
let voxels = Storage::init(device, "voxels", 1);
|
||||
let voxel_groups = Storage::init(device, "voxel groups", 2);
|
||||
let global_lights = Storage::init_with(
|
||||
device,
|
||||
"global lights",
|
||||
3,
|
||||
&[GlobalLight {
|
||||
direction: Vector3::new(-0.5, -4.0, 2.0).normalize(),
|
||||
}],
|
||||
);
|
||||
|
||||
// bind groups
|
||||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
entries: &[
|
||||
view.bind_group_layout_entry(),
|
||||
voxels.bind_group_layout_entry(),
|
||||
voxel_groups.bind_group_layout_entry(),
|
||||
global_lights.bind_group_layout_entry(),
|
||||
],
|
||||
label: Some("tile_bind_group_layout"),
|
||||
});
|
||||
|
||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &bind_group_layout,
|
||||
entries: &[
|
||||
view.bind_group_entry(),
|
||||
voxels.bind_group_entry(),
|
||||
voxel_groups.bind_group_entry(),
|
||||
global_lights.bind_group_entry(),
|
||||
],
|
||||
label: Some("tile_bind_group"),
|
||||
});
|
||||
|
||||
// pipeline
|
||||
let render_pipeline_layout =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("Tile Pipeline Layout"),
|
||||
bind_group_layouts: &[&bind_group_layout],
|
||||
push_constant_ranges: &[],
|
||||
});
|
||||
|
||||
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Voxel Pipeline"),
|
||||
layout: Some(&render_pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: "vs_main",
|
||||
buffers: &[],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point: "fs_main",
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format: *format,
|
||||
blend: Some(wgpu::BlendState::REPLACE),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleStrip,
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw,
|
||||
cull_mode: None,
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
unclipped_depth: false,
|
||||
conservative: false,
|
||||
},
|
||||
depth_stencil: None,
|
||||
multisample: wgpu::MultisampleState {
|
||||
count: 1,
|
||||
mask: !0,
|
||||
alpha_to_coverage_enabled: true,
|
||||
},
|
||||
multiview: None,
|
||||
});
|
||||
|
||||
Self {
|
||||
pipeline: render_pipeline,
|
||||
view,
|
||||
bind_group,
|
||||
bind_group_layout,
|
||||
voxels,
|
||||
voxel_groups,
|
||||
global_lights,
|
||||
id_map: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_group(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
belt: &mut wgpu::util::StagingBelt,
|
||||
CreateVoxelGrid {
|
||||
id,
|
||||
pos,
|
||||
orientation,
|
||||
dimensions,
|
||||
grid,
|
||||
}: CreateVoxelGrid,
|
||||
) {
|
||||
let offset = self.voxels.len();
|
||||
|
||||
let updates = [ArrBufUpdate {
|
||||
offset,
|
||||
data: &grid.as_slice().unwrap(),
|
||||
}];
|
||||
let size = offset + grid.len();
|
||||
self.voxels.update(device, encoder, belt, size, &updates);
|
||||
|
||||
let proj = Projective3::identity()
|
||||
* Translation3::from(pos)
|
||||
* orientation
|
||||
* Translation3::from(-dimensions.cast() / 2.0);
|
||||
let group = VoxelGroup {
|
||||
transform: proj,
|
||||
transform_inv: proj.inverse(),
|
||||
dimensions: dimensions.cast(),
|
||||
offset: offset as u32,
|
||||
};
|
||||
let updates = [ArrBufUpdate {
|
||||
offset: self.voxel_groups.len(),
|
||||
data: &[group],
|
||||
}];
|
||||
let i = self.voxel_groups.len();
|
||||
let size = i + 1;
|
||||
self.voxel_groups
|
||||
.update(device, encoder, belt, size, &updates);
|
||||
|
||||
self.id_map.insert(id, (i, group));
|
||||
|
||||
self.bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &self.bind_group_layout,
|
||||
entries: &[
|
||||
self.view.bind_group_entry(),
|
||||
self.voxels.bind_group_entry(),
|
||||
self.voxel_groups.bind_group_entry(),
|
||||
self.global_lights.bind_group_entry(),
|
||||
],
|
||||
label: Some("tile_bind_group"),
|
||||
});
|
||||
}
|
||||
|
||||
pub fn update_transform(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
belt: &mut wgpu::util::StagingBelt,
|
||||
update: UpdateGridTransform,
|
||||
) {
|
||||
if let Some((i, group)) = self.id_map.get_mut(&update.id) {
|
||||
let proj = Projective3::identity()
|
||||
* Translation3::from(update.pos)
|
||||
* update.orientation
|
||||
* Translation3::from(-group.dimensions.cast() / 2.0);
|
||||
group.transform = proj;
|
||||
group.transform_inv = proj.inverse();
|
||||
let updates = [ArrBufUpdate {
|
||||
offset: *i,
|
||||
data: &[*group],
|
||||
}];
|
||||
let size = self.voxel_groups.len();
|
||||
self.voxel_groups
|
||||
.update(device, encoder, belt, size, &updates);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_view(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
belt: &mut wgpu::util::StagingBelt,
|
||||
size: Vector2<u32>,
|
||||
camera: &Camera,
|
||||
) {
|
||||
let transform =
|
||||
Transform3::identity() * Translation3::from(camera.pos) * camera.orientation;
|
||||
let data = View {
|
||||
width: size.x,
|
||||
height: size.y,
|
||||
zoom: camera.scale,
|
||||
transform,
|
||||
};
|
||||
self.view.update(device, encoder, belt, data)
|
||||
}
|
||||
|
||||
pub fn draw<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
|
||||
render_pass.set_pipeline(&self.pipeline);
|
||||
render_pass.set_bind_group(0, &self.bind_group, &[]);
|
||||
render_pass.draw(0..4, 0..1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user