mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-21 22:38:41 -06:00
i see... something?
This commit is contained in:
parent
3f768a8318
commit
f4e4886d33
|
@ -9,7 +9,7 @@ pub fn init() {
|
||||||
use env_logger::{fmt::Color, Builder, Env};
|
use env_logger::{fmt::Color, Builder, Env};
|
||||||
|
|
||||||
let env = Env::default()
|
let env = Env::default()
|
||||||
.filter_or("RUST_LOG", "trace,gilrs=warn,rusty_xinput=warn,wgpu=warn,wgpu_core=warn,wgpu_hal=warn,hui=info,hui-winit=info,hui-glium=info,hui-wgpu=info");
|
.filter_or("RUST_LOG", "trace,gilrs=warn,rusty_xinput=warn,wgpu=warn,wgpu_core=warn,wgpu_hal=warn,hui=info,hui-winit=info,hui-glium=info,hui-wgpu=info,naga=warn");
|
||||||
Builder::from_env(env)
|
Builder::from_env(env)
|
||||||
.format(|buf, record| {
|
.format(|buf, record| {
|
||||||
let mut level_style = buf.style();
|
let mut level_style = buf.style();
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
// struct Uniforms {
|
struct CameraUniform {
|
||||||
// transform: mat4x4<f32>;
|
view_proj: mat4x4<f32>,
|
||||||
// };
|
};
|
||||||
|
|
||||||
// @group(1) @binding(0)
|
@group(1) @binding(0)
|
||||||
// var<uniform> uniforms: Uniforms;
|
var<uniform> camera: CameraUniform;
|
||||||
|
|
||||||
struct VertexInput {
|
struct VertexInput {
|
||||||
@location(0) position: vec3<f32>,
|
@location(0) position: vec3<f32>,
|
||||||
|
@ -26,7 +26,7 @@ fn vs_main(
|
||||||
) -> VertexOutput {
|
) -> VertexOutput {
|
||||||
var out: VertexOutput;
|
var out: VertexOutput;
|
||||||
out.uv = in.uv;
|
out.uv = in.uv;
|
||||||
out.clip_position = vec4<f32>(in.position, 1.0);
|
out.clip_position = camera.view_proj * vec4<f32>(in.position, 1.0);
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -79,8 +79,11 @@ use player_controller::{debug_switch_ctl_type, update_player_controllers};
|
||||||
// clear_background, entities::render_entities, init_window_size, primitives::init_primitives, resize_renderer, selection_box::render_selection_box, sumberge::render_submerged_view, update_window_size, world::{draw_current_chunk_border, draw_world, draw_world_trans, init_trans_chunk_queue}, BackgroundColor, RenderTarget, Renderer
|
// clear_background, entities::render_entities, init_window_size, primitives::init_primitives, resize_renderer, selection_box::render_selection_box, sumberge::render_submerged_view, update_window_size, world::{draw_current_chunk_border, draw_world, draw_world_trans, init_trans_chunk_queue}, BackgroundColor, RenderTarget, Renderer
|
||||||
// };
|
// };
|
||||||
use rendering::{
|
use rendering::{
|
||||||
init_window_size, render_master, resize_renderer, update_window_size,
|
init_render_states,
|
||||||
world::{init_trans_chunk_queue, TransChunkQueue},
|
init_window_size,
|
||||||
|
render_master,
|
||||||
|
resize_renderer,
|
||||||
|
update_window_size,
|
||||||
BackgroundColor, Renderer,
|
BackgroundColor, Renderer,
|
||||||
};
|
};
|
||||||
use block_placement::update_block_placement;
|
use block_placement::update_block_placement;
|
||||||
|
@ -116,7 +119,7 @@ fn startup() -> Workload {
|
||||||
init_window_size,
|
init_window_size,
|
||||||
kubi_ui_init,
|
kubi_ui_init,
|
||||||
load_prefabs,
|
load_prefabs,
|
||||||
//init_primitives,
|
init_render_states,
|
||||||
insert_lock_state,
|
insert_lock_state,
|
||||||
init_state,
|
init_state,
|
||||||
initialize_from_args,
|
initialize_from_args,
|
||||||
|
@ -139,7 +142,6 @@ fn update() -> Workload {
|
||||||
process_inputs,
|
process_inputs,
|
||||||
kubi_ui_begin,
|
kubi_ui_begin,
|
||||||
(
|
(
|
||||||
init_trans_chunk_queue.run_if_missing_unique::<TransChunkQueue>(),
|
|
||||||
init_game_world.run_if_missing_unique::<ChunkTaskManager>(),
|
init_game_world.run_if_missing_unique::<ChunkTaskManager>(),
|
||||||
(
|
(
|
||||||
spawn_player.run_if_storage_empty::<MainPlayer>(),
|
spawn_player.run_if_storage_empty::<MainPlayer>(),
|
||||||
|
|
|
@ -38,6 +38,7 @@ impl AssetPaths for BlockTexture {
|
||||||
#[derive(Unique)]
|
#[derive(Unique)]
|
||||||
pub struct TexturePrefabs {
|
pub struct TexturePrefabs {
|
||||||
pub block_diffuse_texture: wgpu::Texture,
|
pub block_diffuse_texture: wgpu::Texture,
|
||||||
|
pub block_diffuse_bind_group_layout: wgpu::BindGroupLayout,
|
||||||
pub block_diffuse_bind_group: wgpu::BindGroup,
|
pub block_diffuse_bind_group: wgpu::BindGroup,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,6 +108,7 @@ pub fn load_prefabs(
|
||||||
});
|
});
|
||||||
storages.add_unique_non_send_sync(TexturePrefabs {
|
storages.add_unique_non_send_sync(TexturePrefabs {
|
||||||
block_diffuse_texture,
|
block_diffuse_texture,
|
||||||
|
block_diffuse_bind_group_layout,
|
||||||
block_diffuse_bind_group,
|
block_diffuse_bind_group,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,50 @@
|
||||||
use shipyard::{AllStoragesView, AllStoragesViewMut, IntoIter, Unique, UniqueView, UniqueViewMut, View};
|
use shipyard::{AllStoragesView, AllStoragesViewMut, IntoIter, IntoWorkload, Unique, UniqueView, UniqueViewMut, View, Workload};
|
||||||
use winit::dpi::PhysicalSize;
|
use winit::dpi::PhysicalSize;
|
||||||
use glam::{Vec3, UVec2};
|
use glam::{mat4, vec4, Mat4, UVec2, Vec3};
|
||||||
use crate::{events::WindowResizedEvent, state::is_ingame};
|
use crate::{events::WindowResizedEvent, state::is_ingame};
|
||||||
|
|
||||||
mod renderer;
|
mod renderer;
|
||||||
pub use renderer::Renderer;
|
pub use renderer::Renderer;
|
||||||
|
|
||||||
pub mod primitives;
|
use self::camera::update_camera_unform_buffer;
|
||||||
|
|
||||||
pub mod world;
|
pub mod world;
|
||||||
pub mod selection_box;
|
pub mod camera;
|
||||||
pub mod entities;
|
//pub mod primitives;
|
||||||
pub mod sumberge;
|
//pub mod selection_box;
|
||||||
|
//pub mod entities;
|
||||||
|
//pub mod sumberge;
|
||||||
|
|
||||||
|
pub const WGPU_COORDINATE_SYSTEM: Mat4 = mat4(
|
||||||
|
vec4(1.0, 0.0, 0.0, 0.0),
|
||||||
|
vec4(0.0, 1.0, 0.0, 0.0),
|
||||||
|
vec4(0.0, 0.0, 0.5, 0.5),
|
||||||
|
vec4(0.0, 0.0, 0.0, 1.0),
|
||||||
|
);
|
||||||
|
|
||||||
|
// #[repr(C)]
|
||||||
|
// #[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||||
|
// struct TrasnformUniformData {
|
||||||
|
// pub transform: [[f32; 4]; 4],
|
||||||
|
// }
|
||||||
|
|
||||||
|
// impl TrasnformUniformData {
|
||||||
|
// pub const LAYOUT: &wgpu::Layou
|
||||||
|
// }
|
||||||
|
|
||||||
|
// impl From<Mat4> for TrasnformUniformData {
|
||||||
|
// fn from(mat: Mat4) -> Self {
|
||||||
|
// Self {
|
||||||
|
// transform: mat.to_cols_array_2d(),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// impl From<Transform> for TrasnformUniformData {
|
||||||
|
// fn from(value: Transform) -> Self {
|
||||||
|
// value.0.into()
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
pub struct BufferPair {
|
pub struct BufferPair {
|
||||||
pub index: wgpu::Buffer,
|
pub index: wgpu::Buffer,
|
||||||
|
@ -28,6 +62,13 @@ pub struct RenderCtx<'a> {
|
||||||
pub surface_view: &'a wgpu::TextureView,
|
pub surface_view: &'a wgpu::TextureView,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn init_render_states() -> Workload {
|
||||||
|
(
|
||||||
|
camera::init_camera_uniform_buffer,
|
||||||
|
world::init_world_render_state,
|
||||||
|
).into_workload()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn render_master(storages: AllStoragesViewMut) {
|
pub fn render_master(storages: AllStoragesViewMut) {
|
||||||
let renderer = storages.borrow::<UniqueView<Renderer>>().unwrap();
|
let renderer = storages.borrow::<UniqueView<Renderer>>().unwrap();
|
||||||
|
|
||||||
|
@ -66,6 +107,10 @@ pub fn render_master(storages: AllStoragesViewMut) {
|
||||||
};
|
};
|
||||||
|
|
||||||
if storages.run(is_ingame) {
|
if storages.run(is_ingame) {
|
||||||
|
//XXX: probably should be in pre_update or sth
|
||||||
|
storages.run(update_camera_unform_buffer);
|
||||||
|
|
||||||
|
//TODO init world render state on demand
|
||||||
storages.run_with_data(world::draw_world, &mut data);
|
storages.run_with_data(world::draw_world, &mut data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
84
kubi/src/rendering/camera.rs
Normal file
84
kubi/src/rendering/camera.rs
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
use bytemuck::{Pod, Zeroable};
|
||||||
|
use kubi_shared::transform::Transform;
|
||||||
|
use shipyard::{AllStoragesView, IntoIter, Unique, UniqueView, View};
|
||||||
|
use wgpu::util::DeviceExt;
|
||||||
|
use crate::camera::{self, Camera};
|
||||||
|
|
||||||
|
use super::{Renderer, WGPU_COORDINATE_SYSTEM};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Default, Pod, Zeroable)]
|
||||||
|
#[repr(C, packed)]
|
||||||
|
pub struct CameraUniformData {
|
||||||
|
pub view_proj: [[f32; 4]; 4],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//TODO if multiple cameras, buffer per camera
|
||||||
|
#[derive(Unique)]
|
||||||
|
pub struct CameraUniformBuffer {
|
||||||
|
pub camera_uniform_buffer: wgpu::Buffer,
|
||||||
|
pub camera_bind_group_layout: wgpu::BindGroupLayout,
|
||||||
|
pub camera_bind_group: wgpu::BindGroup,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CameraUniformBuffer {
|
||||||
|
pub fn init(renderer: &Renderer, data: CameraUniformData) -> Self {
|
||||||
|
let camera_uniform_buffer = renderer.device().create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
|
label: Some("camera_uniform_buffer"),
|
||||||
|
contents: bytemuck::cast_slice(&[data]),
|
||||||
|
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||||
|
});
|
||||||
|
|
||||||
|
let camera_bind_group_layout = renderer.device().create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
|
label: Some("camera_bind_group_layout"),
|
||||||
|
entries: &[
|
||||||
|
wgpu::BindGroupLayoutEntry {
|
||||||
|
binding: 0,
|
||||||
|
visibility: wgpu::ShaderStages::VERTEX,
|
||||||
|
ty: wgpu::BindingType::Buffer {
|
||||||
|
ty: wgpu::BufferBindingType::Uniform,
|
||||||
|
has_dynamic_offset: false,
|
||||||
|
min_binding_size: None,
|
||||||
|
},
|
||||||
|
count: None,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
let camera_bind_group = renderer.device().create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
|
label: Some("camera_bind_group"),
|
||||||
|
layout: &camera_bind_group_layout,
|
||||||
|
entries: &[
|
||||||
|
wgpu::BindGroupEntry {
|
||||||
|
binding: 0,
|
||||||
|
resource: camera_uniform_buffer.as_entire_binding(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
Self { camera_uniform_buffer, camera_bind_group_layout, camera_bind_group }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init_default(renderer: &Renderer) -> Self {
|
||||||
|
Self::init(renderer, CameraUniformData::default())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&self, renderer: &Renderer, data: CameraUniformData) {
|
||||||
|
renderer.queue().write_buffer(&self.camera_uniform_buffer, 0, bytemuck::cast_slice(&[data]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init_camera_uniform_buffer(storages: AllStoragesView) {
|
||||||
|
let renderer = storages.borrow::<UniqueView<Renderer>>().unwrap();
|
||||||
|
storages.add_unique(CameraUniformBuffer::init_default(&renderer));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_camera_unform_buffer(
|
||||||
|
renderer: UniqueView<Renderer>,
|
||||||
|
camera_uniform_buffer: UniqueView<CameraUniformBuffer>,
|
||||||
|
camera: View<Camera>,
|
||||||
|
) {
|
||||||
|
let Some(camera) = camera.iter().next() else { return };
|
||||||
|
let proj = camera.view_matrix * camera.perspective_matrix * WGPU_COORDINATE_SYSTEM;
|
||||||
|
camera_uniform_buffer.update(&renderer, CameraUniformData { view_proj: proj.to_cols_array_2d() });
|
||||||
|
}
|
|
@ -1,57 +1,44 @@
|
||||||
use bytemuck::{Pod, Zeroable};
|
|
||||||
use glam::{IVec3, Vec3};
|
use glam::{IVec3, Vec3};
|
||||||
use shipyard::{AllStoragesView, IntoIter, NonSendSync, Unique, UniqueView, UniqueViewMut, View};
|
use shipyard::{AllStoragesView, IntoIter, NonSendSync, Unique, UniqueView, UniqueViewMut, View};
|
||||||
use kubi_shared::{chunk::CHUNK_SIZE, transform::Transform};
|
use kubi_shared::{chunk::CHUNK_SIZE, transform::Transform};
|
||||||
use wgpu::util::RenderEncoder;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
camera::Camera,
|
camera::Camera,
|
||||||
prefabs::TexturePrefabs,
|
prefabs::TexturePrefabs,
|
||||||
settings::GameSettings,
|
settings::GameSettings,
|
||||||
world::{ChunkMeshStorage, ChunkStorage},
|
world::{ChunkMeshStorage, ChunkStorage},
|
||||||
};
|
};
|
||||||
use super::{RenderCtx, Renderer};
|
use super::{camera::CameraUniformBuffer, RenderCtx, WGPU_COORDINATE_SYSTEM};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Pod, Zeroable)]
|
mod pipeline;
|
||||||
#[repr(C, packed)]
|
mod vertex;
|
||||||
pub struct ChunkVertex {
|
pub use vertex::ChunkVertex;
|
||||||
pub position: [f32; 3],
|
|
||||||
pub normal: [f32; 3],
|
|
||||||
pub uv: [f32; 2],
|
|
||||||
pub tex_index: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ChunkVertex {
|
|
||||||
pub const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout {
|
|
||||||
array_stride: std::mem::size_of::<ChunkVertex>() as wgpu::BufferAddress,
|
|
||||||
step_mode: wgpu::VertexStepMode::Vertex,
|
|
||||||
attributes: &wgpu::vertex_attr_array![
|
|
||||||
0 => Float32x3,
|
|
||||||
1 => Float32x3,
|
|
||||||
2 => Float32x2,
|
|
||||||
3 => Uint32,
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Unique)]
|
#[derive(Unique)]
|
||||||
pub struct TransChunkQueue(pub Vec<IVec3>);
|
pub struct WorldRenderState {
|
||||||
|
pub pipeline: wgpu::RenderPipeline,
|
||||||
|
pub trans_chunk_queue: Vec<IVec3>,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn init_trans_chunk_queue(storages: AllStoragesView) {
|
pub fn init_world_render_state(storages: AllStoragesView) {
|
||||||
storages.add_unique(TransChunkQueue(Vec::with_capacity(512)));
|
storages.add_unique(WorldRenderState {
|
||||||
|
pipeline: storages.run(pipeline::init_world_pipeline),
|
||||||
|
trans_chunk_queue: Vec::with_capacity(512),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_world(
|
pub fn draw_world(
|
||||||
ctx: &mut RenderCtx,
|
ctx: &mut RenderCtx,
|
||||||
|
mut state: UniqueViewMut<WorldRenderState>,
|
||||||
textures: UniqueView<TexturePrefabs>,
|
textures: UniqueView<TexturePrefabs>,
|
||||||
chunks: UniqueView<ChunkStorage>,
|
chunks: UniqueView<ChunkStorage>,
|
||||||
meshes: NonSendSync<UniqueView<ChunkMeshStorage>>,
|
meshes: NonSendSync<UniqueView<ChunkMeshStorage>>,
|
||||||
transform: View<Transform>,
|
//transform: View<Transform>,
|
||||||
camera: View<Camera>,
|
camera: View<Camera>,
|
||||||
|
camera_ubo: UniqueView<CameraUniformBuffer>,
|
||||||
settings: UniqueView<GameSettings>,
|
settings: UniqueView<GameSettings>,
|
||||||
mut trans_queue: UniqueViewMut<TransChunkQueue>,
|
|
||||||
) {
|
) {
|
||||||
let camera = camera.iter().next().expect("No cameras in the scene");
|
let camera = camera.iter().next().expect("No cameras in the scene");
|
||||||
let camera_matrix = camera.view_matrix * camera.perspective_matrix;
|
let matrix = WGPU_COORDINATE_SYSTEM * camera.view_matrix * camera.perspective_matrix;
|
||||||
|
|
||||||
let mut render_pass = ctx.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
let mut render_pass = ctx.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||||
label: Some("draw_world"),
|
label: Some("draw_world"),
|
||||||
|
@ -66,6 +53,10 @@ pub fn draw_world(
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
render_pass.set_pipeline(&state.pipeline);
|
||||||
|
render_pass.set_bind_group(0, &textures.block_diffuse_bind_group, &[]);
|
||||||
|
render_pass.set_bind_group(1, &camera_ubo.camera_bind_group, &[]);
|
||||||
|
|
||||||
for (&position, chunk) in &chunks.chunks {
|
for (&position, chunk) in &chunks.chunks {
|
||||||
if let Some(key) = chunk.mesh_index {
|
if let Some(key) = chunk.mesh_index {
|
||||||
let mesh = meshes.get(key).expect("Mesh index pointing to nothing");
|
let mesh = meshes.get(key).expect("Mesh index pointing to nothing");
|
||||||
|
@ -85,10 +76,8 @@ pub fn draw_world(
|
||||||
|
|
||||||
//Draw chunk mesh
|
//Draw chunk mesh
|
||||||
if mesh.main.index.size() > 0 {
|
if mesh.main.index.size() > 0 {
|
||||||
//TODO
|
|
||||||
render_pass.set_index_buffer(mesh.main.index.slice(..), wgpu::IndexFormat::Uint32);
|
render_pass.set_index_buffer(mesh.main.index.slice(..), wgpu::IndexFormat::Uint32);
|
||||||
render_pass.set_vertex_buffer(0, mesh.main.vertex.slice(..));
|
render_pass.set_vertex_buffer(0, mesh.main.vertex.slice(..));
|
||||||
render_pass.set_bind_group(0, &textures.block_diffuse_bind_group, &[]);
|
|
||||||
render_pass.draw_indexed(0..mesh.main.index_len, 0, 0..1);
|
render_pass.draw_indexed(0..mesh.main.index_len, 0, 0..1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
59
kubi/src/rendering/world/pipeline.rs
Normal file
59
kubi/src/rendering/world/pipeline.rs
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
use shipyard::{Unique, UniqueView};
|
||||||
|
use crate::{
|
||||||
|
prefabs::TexturePrefabs,
|
||||||
|
rendering::{camera::CameraUniformBuffer, world::ChunkVertex, Renderer}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn init_world_pipeline(
|
||||||
|
ren: UniqueView<Renderer>,
|
||||||
|
textures: UniqueView<TexturePrefabs>,
|
||||||
|
camera_ubo: UniqueView<CameraUniformBuffer>,
|
||||||
|
) -> wgpu::RenderPipeline {
|
||||||
|
let shader = ren.device().create_shader_module(
|
||||||
|
wgpu::include_wgsl!("../../../shaders/world.wgsl")
|
||||||
|
);
|
||||||
|
|
||||||
|
let world_pipeline_layout = ren.device().create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||||
|
label: Some("world_pipeline_layout"),
|
||||||
|
bind_group_layouts: &[
|
||||||
|
&textures.block_diffuse_bind_group_layout,
|
||||||
|
&camera_ubo.camera_bind_group_layout,
|
||||||
|
],
|
||||||
|
push_constant_ranges: &[],
|
||||||
|
});
|
||||||
|
|
||||||
|
ren.device().create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||||
|
label: Some("world_pipeline"),
|
||||||
|
layout: Some(&world_pipeline_layout),
|
||||||
|
fragment: Some(wgpu::FragmentState {
|
||||||
|
module: &shader,
|
||||||
|
entry_point: "fs_main",
|
||||||
|
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||||
|
targets: &[Some(wgpu::ColorTargetState {
|
||||||
|
format: ren.surface_config().format,
|
||||||
|
blend: Some(wgpu::BlendState::REPLACE),
|
||||||
|
write_mask: wgpu::ColorWrites::ALL,
|
||||||
|
})],
|
||||||
|
}),
|
||||||
|
vertex: wgpu::VertexState {
|
||||||
|
module: &shader,
|
||||||
|
entry_point: "vs_main",
|
||||||
|
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||||
|
buffers: &[
|
||||||
|
ChunkVertex::LAYOUT,
|
||||||
|
],
|
||||||
|
},
|
||||||
|
primitive: wgpu::PrimitiveState {
|
||||||
|
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||||
|
strip_index_format: None,
|
||||||
|
cull_mode: Some(wgpu::Face::Back),
|
||||||
|
front_face: wgpu::FrontFace::Cw,
|
||||||
|
unclipped_depth: false,
|
||||||
|
polygon_mode: wgpu::PolygonMode::Fill,
|
||||||
|
conservative: false,
|
||||||
|
},
|
||||||
|
depth_stencil: None,
|
||||||
|
multisample: wgpu::MultisampleState::default(),
|
||||||
|
multiview: None,
|
||||||
|
})
|
||||||
|
}
|
23
kubi/src/rendering/world/vertex.rs
Normal file
23
kubi/src/rendering/world/vertex.rs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
use bytemuck::{Pod, Zeroable};
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Pod, Zeroable)]
|
||||||
|
#[repr(C, packed)]
|
||||||
|
pub struct ChunkVertex {
|
||||||
|
pub position: [f32; 3],
|
||||||
|
pub normal: [f32; 3],
|
||||||
|
pub uv: [f32; 2],
|
||||||
|
pub tex_index: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ChunkVertex {
|
||||||
|
pub const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout {
|
||||||
|
array_stride: std::mem::size_of::<ChunkVertex>() as wgpu::BufferAddress,
|
||||||
|
step_mode: wgpu::VertexStepMode::Vertex,
|
||||||
|
attributes: &wgpu::vertex_attr_array![
|
||||||
|
0 => Float32x3,
|
||||||
|
1 => Float32x3,
|
||||||
|
2 => Float32x2,
|
||||||
|
3 => Uint32,
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
use glam::{IVec3, ivec3};
|
use glam::{ivec3, IVec3, Vec3};
|
||||||
use strum::IntoEnumIterator;
|
use strum::IntoEnumIterator;
|
||||||
use kubi_shared::block::{Block, RenderType, Transparency};
|
use kubi_shared::block::{Block, RenderType, Transparency};
|
||||||
use crate::world::chunk::CHUNK_SIZE;
|
use crate::world::chunk::CHUNK_SIZE;
|
||||||
|
@ -10,7 +10,7 @@ mod builder;
|
||||||
use data::MeshGenData;
|
use data::MeshGenData;
|
||||||
use builder::{MeshBuilder, CubeFace, DiagonalFace};
|
use builder::{MeshBuilder, CubeFace, DiagonalFace};
|
||||||
|
|
||||||
pub fn generate_mesh(data: MeshGenData) -> (
|
pub fn generate_mesh(position: IVec3, data: MeshGenData) -> (
|
||||||
(Vec<ChunkVertex>, Vec<u32>),
|
(Vec<ChunkVertex>, Vec<u32>),
|
||||||
(Vec<ChunkVertex>, Vec<u32>),
|
(Vec<ChunkVertex>, Vec<u32>),
|
||||||
) {
|
) {
|
||||||
|
@ -32,7 +32,7 @@ pub fn generate_mesh(data: MeshGenData) -> (
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut builder = MeshBuilder::new();
|
let mut builder = MeshBuilder::new_with_offset((position * CHUNK_SIZE as i32).as_vec3());
|
||||||
let mut trans_builder = MeshBuilder::new();
|
let mut trans_builder = MeshBuilder::new();
|
||||||
|
|
||||||
for x in 0..CHUNK_SIZE as i32 {
|
for x in 0..CHUNK_SIZE as i32 {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use strum::EnumIter;
|
use strum::EnumIter;
|
||||||
use glam::{Vec3, vec3, IVec3, ivec3};
|
use glam::{ivec3, vec3, IVec3, Vec3};
|
||||||
use std::f32::consts::FRAC_1_SQRT_2;
|
use std::f32::consts::FRAC_1_SQRT_2;
|
||||||
use crate::rendering::world::ChunkVertex;
|
use crate::rendering::world::ChunkVertex;
|
||||||
|
|
||||||
|
@ -87,6 +87,7 @@ const UV_COORDS: [[f32; 2]; 4] = [
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct MeshBuilder {
|
pub struct MeshBuilder {
|
||||||
|
offset: Vec3,
|
||||||
vertex_buffer: Vec<ChunkVertex>,
|
vertex_buffer: Vec<ChunkVertex>,
|
||||||
index_buffer: Vec<u32>,
|
index_buffer: Vec<u32>,
|
||||||
idx_counter: u32,
|
idx_counter: u32,
|
||||||
|
@ -96,6 +97,10 @@ impl MeshBuilder {
|
||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_with_offset(offset: Vec3) -> Self {
|
||||||
|
Self { offset, ..Self::new() }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn add_face(&mut self, face: CubeFace, coord: IVec3, texture: u8) {
|
pub fn add_face(&mut self, face: CubeFace, coord: IVec3, texture: u8) {
|
||||||
let coord = coord.as_vec3();
|
let coord = coord.as_vec3();
|
||||||
let face_index = face as usize;
|
let face_index = face as usize;
|
||||||
|
@ -106,10 +111,10 @@ impl MeshBuilder {
|
||||||
self.vertex_buffer.reserve(4);
|
self.vertex_buffer.reserve(4);
|
||||||
for i in 0..4 {
|
for i in 0..4 {
|
||||||
self.vertex_buffer.push(ChunkVertex {
|
self.vertex_buffer.push(ChunkVertex {
|
||||||
position: (coord + vert[i]).to_array(),
|
position: (coord + vert[i] + self.offset).to_array(),
|
||||||
normal: norm.to_array(),
|
normal: norm.to_array(),
|
||||||
uv: UV_COORDS[i],
|
uv: UV_COORDS[i],
|
||||||
tex_index: texture
|
tex_index: texture as u32
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,18 +134,18 @@ impl MeshBuilder {
|
||||||
self.vertex_buffer.reserve(8);
|
self.vertex_buffer.reserve(8);
|
||||||
for i in 0..4 { //push front vertices
|
for i in 0..4 { //push front vertices
|
||||||
self.vertex_buffer.push(ChunkVertex {
|
self.vertex_buffer.push(ChunkVertex {
|
||||||
position: (coord.as_vec3() + vertices[i]).to_array(),
|
position: (coord.as_vec3() + vertices[i] + self.offset).to_array(),
|
||||||
normal: normal_front,
|
normal: normal_front,
|
||||||
uv: UV_COORDS[i],
|
uv: UV_COORDS[i],
|
||||||
tex_index: front_texture
|
tex_index: front_texture as u32
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
for i in 0..4 { //push back vertices
|
for i in 0..4 { //push back vertices
|
||||||
self.vertex_buffer.push(ChunkVertex {
|
self.vertex_buffer.push(ChunkVertex {
|
||||||
position: (coord.as_vec3() + vertices[i]).to_array(),
|
position: (coord.as_vec3() + vertices[i] + self.offset).to_array(),
|
||||||
normal: normal_back,
|
normal: normal_back,
|
||||||
uv: UV_COORDS[i],
|
uv: UV_COORDS[i],
|
||||||
tex_index: back_texture
|
tex_index: back_texture as u32
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,24 +156,25 @@ impl MeshBuilder {
|
||||||
self.idx_counter += 8;
|
self.idx_counter += 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_model(&mut self, position: Vec3, vertices: &[ChunkVertex], indices: Option<&[u32]>) {
|
//XXX: needs offset supprt
|
||||||
//push vertices
|
// pub fn add_model(&mut self, position: Vec3, vertices: &[ChunkVertex], indices: Option<&[u32]>) {
|
||||||
self.vertex_buffer.extend(vertices.iter().map(|vertex| {
|
// //push vertices
|
||||||
let mut vertex = *vertex;
|
// self.vertex_buffer.extend(vertices.iter().map(|vertex| {
|
||||||
vertex.position[0] += position.x;
|
// let mut vertex = *vertex;
|
||||||
vertex.position[0] += position.y;
|
// vertex.position[0] += position.x;
|
||||||
vertex.position[0] += position.z;
|
// vertex.position[0] += position.y;
|
||||||
vertex
|
// vertex.position[0] += position.z;
|
||||||
}));
|
// vertex
|
||||||
//push indices
|
// }));
|
||||||
if let Some(indices) = indices {
|
// //push indices
|
||||||
self.index_buffer.extend(indices.iter().map(|x| x + self.idx_counter));
|
// if let Some(indices) = indices {
|
||||||
} else {
|
// self.index_buffer.extend(indices.iter().map(|x| x + self.idx_counter));
|
||||||
self.index_buffer.extend(0..(self.vertex_buffer.len() as u32));
|
// } else {
|
||||||
}
|
// self.index_buffer.extend(0..(self.vertex_buffer.len() as u32));
|
||||||
//increment idx counter
|
// }
|
||||||
self.idx_counter += vertices.len() as u32;
|
// //increment idx counter
|
||||||
}
|
// self.idx_counter += vertices.len() as u32;
|
||||||
|
// }
|
||||||
|
|
||||||
pub fn finish(self) -> (Vec<ChunkVertex>, Vec<u32>) {
|
pub fn finish(self) -> (Vec<ChunkVertex>, Vec<u32>) {
|
||||||
(self.vertex_buffer, self.index_buffer)
|
(self.vertex_buffer, self.index_buffer)
|
||||||
|
|
|
@ -63,7 +63,7 @@ impl ChunkTaskManager {
|
||||||
let (
|
let (
|
||||||
(vertices, indices),
|
(vertices, indices),
|
||||||
(trans_vertices, trans_indices),
|
(trans_vertices, trans_indices),
|
||||||
) = generate_mesh(data);
|
) = generate_mesh(position, data);
|
||||||
ChunkTaskResponse::GeneratedMesh {
|
ChunkTaskResponse::GeneratedMesh {
|
||||||
position,
|
position,
|
||||||
vertices, indices,
|
vertices, indices,
|
||||||
|
|
Loading…
Reference in a new issue