basic render distance

This commit is contained in:
2024-04-27 23:13:43 -04:00
parent 28047ebdb5
commit 279b7c0418
6 changed files with 65 additions and 42 deletions

View File

@@ -96,6 +96,8 @@ pub mod prelude {
} }
pub const ATTRIBUTE_PACKED_VERTEX_DATA: MeshVertexAttribute = pub const ATTRIBUTE_PACKED_VERTEX_DATA: MeshVertexAttribute =
MeshVertexAttribute::new("PackedVertexData", 988540817, VertexFormat::Uint32); MeshVertexAttribute::new("PackedVertexData", 988540817, VertexFormat::Uint32);
pub const ATTRIBUTE_VERTEX_HEIGHT: MeshVertexAttribute =
MeshVertexAttribute::new("VertexHeight", 988540717, VertexFormat::Float32);
pub const ATTRIBUTE_TEXTURE_INDEX: MeshVertexAttribute = pub const ATTRIBUTE_TEXTURE_INDEX: MeshVertexAttribute =
MeshVertexAttribute::new("TextureIndex", 988540917, VertexFormat::Uint32); MeshVertexAttribute::new("TextureIndex", 988540917, VertexFormat::Uint32);

View File

@@ -145,6 +145,7 @@ pub fn generate_packed_chunk_mesh(
RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD, RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD,
) )
.with_inserted_attribute(ATTRIBUTE_PACKED_VERTEX_DATA, packed_data) .with_inserted_attribute(ATTRIBUTE_PACKED_VERTEX_DATA, packed_data)
.with_inserted_attribute(ATTRIBUTE_VERTEX_HEIGHT, heights)
.with_inserted_indices(Indices::U32(indices)); .with_inserted_indices(Indices::U32(indices));
return mesh; return mesh;
} }
@@ -291,7 +292,7 @@ fn create_tile_wall(
verts.push(p3); verts.push(p3);
verts.push(p4); verts.push(p4);
let n = HEX_NORMALS[dir].normalize(); let n = HEX_NORMALS[dir];
normals.push(n); normals.push(n);
normals.push(n); normals.push(n);
normals.push(n); normals.push(n);
@@ -312,8 +313,8 @@ fn create_tile_wall(
} }
fn pack_vertex_data(offset: UVec2, vert: usize, tex: u32) -> u32 { fn pack_vertex_data(offset: UVec2, vert: usize, tex: u32) -> u32 {
//4 bits vert
//6 + 6 bits offset //6 + 6 bits offset
//4 bits vert
//12 bits texture //12 bits texture
let mut data = offset.x; let mut data = offset.x;
data += (offset.y) << 6; data += (offset.y) << 6;

View File

@@ -95,8 +95,8 @@ fn update_camera_mouse(
CursorGrabMode::None => (), CursorGrabMode::None => (),
_ => { _ => {
// Using smallest of height or width ensures equal vertical and horizontal sensitivity // Using smallest of height or width ensures equal vertical and horizontal sensitivity
pitch -= (ev.delta.y * time.delta_seconds() * 5.).to_radians(); pitch -= ev.delta.y.to_radians() * time.delta_seconds() * 5.;
yaw -= (ev.delta.x * time.delta_seconds() * 5.).to_radians(); yaw -= ev.delta.x.to_radians() * time.delta_seconds() * 5.;
} }
} }

View File

@@ -11,24 +11,25 @@ use phos::PhosGamePlugin;
fn main() { fn main() {
App::new() App::new()
.add_plugins(( .add_plugins((
DefaultPlugins.set(WindowPlugin { DefaultPlugins
primary_window: Some(Window { .set(WindowPlugin {
title: "Phos".into(), primary_window: Some(Window {
name: Some("phos".into()), title: "Phos".into(),
resolution: (1920.0, 1080.0).into(), name: Some("phos".into()),
resizable: true, present_mode: PresentMode::AutoNoVsync,
present_mode: PresentMode::AutoNoVsync, mode: bevy::window::WindowMode::BorderlessFullscreen,
..default()
}),
..default() ..default()
})
.set(ImagePlugin {
default_sampler: ImageSamplerDescriptor {
address_mode_u: ImageAddressMode::Repeat,
address_mode_v: ImageAddressMode::Repeat,
mag_filter: ImageFilterMode::Nearest,
..default()
},
}), }),
..default()
}).set(ImagePlugin {
default_sampler: ImageSamplerDescriptor {
address_mode_u: ImageAddressMode::Repeat,
address_mode_v: ImageAddressMode::Repeat,
mag_filter: ImageFilterMode::Nearest,
..default()
}
}),
WorldInspectorPlugin::new(), WorldInspectorPlugin::new(),
PhosGamePlugin, PhosGamePlugin,
)) ))

View File

@@ -1,11 +1,12 @@
use crate::prelude::*; use crate::prelude::*;
use crate::shader_extensions::chunk_material::ChunkMaterial; use crate::shader_extensions::chunk_material::ChunkMaterial;
use bevy::asset::LoadState; use bevy::asset::LoadState;
use bevy::core_pipeline::experimental::taa::TemporalAntiAliasPlugin;
use bevy::pbr::ExtendedMaterial; use bevy::pbr::ExtendedMaterial;
use bevy::render::view::visibility;
use bevy::{pbr::CascadeShadowConfig, prelude::*}; use bevy::{pbr::CascadeShadowConfig, prelude::*};
use bevy_rapier3d::plugin::{NoUserData, RapierPhysicsPlugin}; use bevy_rapier3d::plugin::{NoUserData, RapierPhysicsPlugin};
use bevy_rapier3d::render::RapierDebugRenderPlugin; use bevy_rapier3d::render::RapierDebugRenderPlugin;
use camera_system::prelude::PhosCamera;
use camera_system::PhosCameraPlugin; use camera_system::PhosCameraPlugin;
use iyes_perf_ui::prelude::*; use iyes_perf_ui::prelude::*;
use world_generation::biome_painter::{ use world_generation::biome_painter::{
@@ -25,12 +26,14 @@ impl Plugin for PhosGamePlugin {
app.add_plugins(PhosCameraPlugin) app.add_plugins(PhosCameraPlugin)
.add_plugins(MaterialPlugin::< .add_plugins(MaterialPlugin::<
ExtendedMaterial<StandardMaterial, ChunkMaterial>, ExtendedMaterial<StandardMaterial, ChunkMaterial>,
>::default()) >::default());
.add_plugins(TemporalAntiAliasPlugin);
//Systems - Startup //Systems - Startup
app.add_systems(Startup, init_game) app.add_systems(Startup, init_game)
.add_systems(Startup, (load_textures, load_tiles, create_map).chain()); .add_systems(Startup, (load_textures, load_tiles, create_map).chain());
//Systems - PreUpdate
app.add_systems(PreUpdate, render_distance_system);
//Systems - Update //Systems - Update
app.add_systems(Update, (finalize_texture, spawn_map)); app.add_systems(Update, (finalize_texture, spawn_map));
@@ -233,3 +236,18 @@ fn spawn_map(
)); ));
} }
} }
fn render_distance_system(
mut chunks: Query<(&Transform, &mut Visibility), With<PhosChunk>>,
camera: Query<&Transform, With<PhosCamera>>,
) {
let cam = camera.single();
for (transform, mut visibility) in chunks.iter_mut() {
let dist = (transform.translation - cam.translation).length_squared();
if dist > 1000000. {
*visibility = Visibility::Hidden;
} else {
*visibility = Visibility::Visible;
}
}
}

View File

@@ -4,7 +4,7 @@ use bevy::reflect::TypePath;
use bevy::render::mesh::Mesh; use bevy::render::mesh::Mesh;
use bevy::render::render_resource::{AsBindGroup, ShaderRef}; use bevy::render::render_resource::{AsBindGroup, ShaderRef};
use bevy::render::texture::Image; use bevy::render::texture::Image;
use world_generation::prelude::ATTRIBUTE_PACKED_VERTEX_DATA; use world_generation::prelude::{ATTRIBUTE_PACKED_VERTEX_DATA, ATTRIBUTE_VERTEX_HEIGHT};
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] #[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
pub struct ChunkMaterial { pub struct ChunkMaterial {
@@ -18,23 +18,24 @@ impl MaterialExtension for ChunkMaterial {
"shaders/world/chunk.wgsl".into() "shaders/world/chunk.wgsl".into()
} }
fn vertex_shader() -> ShaderRef { // fn vertex_shader() -> ShaderRef {
"shaders/world/chunk_packed.wgsl".into() // "shaders/world/chunk_packed.wgsl".into()
} // }
fn specialize( // fn specialize(
_pipeline: &bevy::pbr::MaterialExtensionPipeline, // _pipeline: &bevy::pbr::MaterialExtensionPipeline,
descriptor: &mut bevy::render::render_resource::RenderPipelineDescriptor, // descriptor: &mut bevy::render::render_resource::RenderPipelineDescriptor,
layout: &bevy::render::mesh::MeshVertexBufferLayout, // layout: &bevy::render::mesh::MeshVertexBufferLayout,
_key: bevy::pbr::MaterialExtensionKey<Self>, // _key: bevy::pbr::MaterialExtensionKey<Self>,
) -> Result<(), bevy::render::render_resource::SpecializedMeshPipelineError> { // ) -> Result<(), bevy::render::render_resource::SpecializedMeshPipelineError> {
let vertex_layout = layout.get_layout(&[ // let vertex_layout = layout.get_layout(&[
Mesh::ATTRIBUTE_POSITION.at_shader_location(0), // // Mesh::ATTRIBUTE_POSITION.at_shader_location(0),
Mesh::ATTRIBUTE_UV_0.at_shader_location(1), // // Mesh::ATTRIBUTE_UV_0.at_shader_location(1),
Mesh::ATTRIBUTE_NORMAL.at_shader_location(2), // // Mesh::ATTRIBUTE_NORMAL.at_shader_location(2),
ATTRIBUTE_PACKED_VERTEX_DATA.at_shader_location(7), // ATTRIBUTE_PACKED_VERTEX_DATA.at_shader_location(7),
])?; // ATTRIBUTE_VERTEX_HEIGHT.at_shader_location(8),
descriptor.vertex.buffers = vec![vertex_layout]; // ])?;
Ok(()) // descriptor.vertex.buffers = vec![vertex_layout];
} // Ok(())
// }
} }