Skip to content

Commit

Permalink
Merge pull request #16 from splashdust/feat/bevy-0.13
Browse files Browse the repository at this point in the history
Upgrade to Bevy 0.13
  • Loading branch information
splashdust authored Feb 28, 2024
2 parents c1d1baa + 33f505b commit db0f4c2
Show file tree
Hide file tree
Showing 15 changed files with 884 additions and 567 deletions.
1,281 changes: 797 additions & 484 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ categories = ["game-development", "graphics"]
opt-level = 3

[dependencies]
bevy = { version = "0.12", features = [
bevy = { version = "0.13", features = [
"bevy_render",
"bevy_asset",
"bevy_pbr",
Expand All @@ -30,4 +30,3 @@ weak-table = { version = "0.3.2", features = ["ahash"] }

[dev-dependencies]
noise = "0.8.2"
bevy_flycam = "0.12.0"
2 changes: 1 addition & 1 deletion assets/custom_material.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@fragment
fn fragment(
in: VertexOutput,
@location(6) tex_idx: vec3<u32>,
@location(8) tex_idx: vec3<u32>,
) -> FragmentOutput {
var out: FragmentOutput;

Expand Down
3 changes: 0 additions & 3 deletions examples/custom_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use bevy::{
},
},
};
use bevy_flycam::prelude::*;
use bevy_voxel_world::{
prelude::*,
rendering::{
Expand Down Expand Up @@ -37,7 +36,6 @@ fn main() {
// Don't forget to Register the material with Bevy too.
.add_plugins(MaterialPlugin::<CustomVoxelMaterial>::default())
//
.add_plugins(NoCameraPlayerPlugin)
.add_systems(Startup, (setup, create_voxel_scene))
.run();
}
Expand Down Expand Up @@ -66,7 +64,6 @@ fn setup(mut commands: Commands, mut materials: ResMut<Assets<CustomVoxelMateria
..default()
},
VoxelWorldCamera,
FlyCam,
));
}

Expand Down
26 changes: 9 additions & 17 deletions examples/ray_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn setup(
commands.spawn((
PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgba_u8(124, 144, 255, 128).into()),
material: materials.add(Color::rgba_u8(124, 144, 255, 128)),
transform: Transform::from_xyz(0.0, -10.0, 0.0),
..default()
},
Expand All @@ -58,28 +58,20 @@ fn setup(
// Camera
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
transform: Transform::from_xyz(10.0, 10.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
// This tells bevy_voxel_world tos use this cameras transform to calculate spawning area
VoxelWorldCamera,
));

// Ambient light
commands.insert_resource(AmbientLight {
color: Color::rgb(0.98, 0.95, 0.82),
brightness: 1.0,
});

// Point light
// light
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(0.0, 5.0, 0.0),
point_light: PointLight {
color: Color::rgb(0.98, 0.95, 0.82),
intensity: 100.0,
range: 40.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
}
Expand Down Expand Up @@ -116,9 +108,9 @@ fn update_cursor_cube(
for ev in cursor_evr.read() {
// Get a ray from the cursor position into the world
let (camera, cam_gtf) = camera_info.single();
let ray = camera
.viewport_to_world(cam_gtf, ev.position)
.unwrap_or_default();
let Some(ray) = camera.viewport_to_world(cam_gtf, ev.position) else {
return;
};

if let Some(result) = voxel_world_raycast.raycast(ray, &|(_pos, _vox)| true) {
let (mut transform, mut cursor_cube) = cursor_cube.single_mut();
Expand All @@ -131,7 +123,7 @@ fn update_cursor_cube(
}

fn mouse_button_input(
buttons: Res<Input<MouseButton>>,
buttons: Res<ButtonInput<MouseButton>>,
mut voxel_world: VoxelWorld,
cursor_cube: Query<&CursorCube>,
) {
Expand Down
23 changes: 6 additions & 17 deletions examples/textures.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use bevy::prelude::*;
use bevy_flycam::prelude::*;
use bevy_voxel_world::prelude::*;
use std::sync::Arc;

Expand All @@ -17,8 +16,7 @@ fn main() {
"example_voxel_texture.png",
4, // number of indexes in the texture
))
.add_plugins(NoCameraPlayerPlugin)
.add_systems(Startup, (setup, create_voxel_scene))
.add_systems(Startup, (setup, create_voxel_scene).chain())
.run();
}

Expand All @@ -37,29 +35,20 @@ fn setup(mut commands: Commands) {
// Camera
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
transform: Transform::from_xyz(10.0, 10.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
// This tells bevy_voxel_world tos use this cameras transform to calculate spawning area
// This tells bevy_voxel_world to use this cameras transform to calculate spawning area
VoxelWorldCamera,
FlyCam,
));

// Ambient light
commands.insert_resource(AmbientLight {
color: Color::rgb(0.98, 0.95, 0.82),
brightness: 1.0,
});

// Point light
// light
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(0.0, 5.0, 0.0),
point_light: PointLight {
color: Color::rgb(0.98, 0.95, 0.82),
intensity: 100.0,
range: 40.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
}
Expand Down
11 changes: 2 additions & 9 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,9 @@ pub struct ChunkAabbGizmo {
pub color: Option<Color>,
}

fn draw_aabbs(
query: Query<(&Chunk, &GlobalTransform, &ChunkAabbGizmo)>,
config: Res<GizmoConfig>,
mut gizmos: Gizmos,
) {
fn draw_aabbs(query: Query<(&Chunk, &GlobalTransform, &ChunkAabbGizmo)>, mut gizmos: Gizmos) {
for (chunk, &transform, gizmo) in &query {
let color = gizmo
.color
.or(config.aabb.default_color)
.unwrap_or(Color::WHITE);
let color = gizmo.color.unwrap_or(Color::WHITE);
gizmos.cuboid(aabb_transform(chunk.aabb(), transform), color);
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/meshing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use bevy::{
prelude::*,
render::{
mesh::{Indices, VertexAttributeValues},
render_asset::RenderAssetUsages,
render_resource::PrimitiveTopology,
},
};
Expand Down Expand Up @@ -93,7 +94,10 @@ fn mesh_from_quads(
}
}

let mut render_mesh = Mesh::new(PrimitiveTopology::TriangleList);
let mut render_mesh = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::default(),
);

render_mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
Expand Down Expand Up @@ -128,7 +132,7 @@ fn mesh_from_quads(
render_mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
}

render_mesh.set_indices(Some(Indices::U32(indices.clone())));
render_mesh.insert_indices(Indices::U32(indices.clone()));

render_mesh
}
Expand Down
23 changes: 13 additions & 10 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use bevy::{
asset::load_internal_asset,
pbr::ExtendedMaterial,
prelude::*,
render::texture::{CompressedImageFormats, ImageSampler, ImageType},
render::{
render_asset::RenderAssetUsages,
texture::{CompressedImageFormats, ImageSampler, ImageType},
},
};

use crate::{
Expand Down Expand Up @@ -88,16 +91,15 @@ impl Plugin for VoxelWorldPlugin {
.add_systems(PreStartup, setup_internals)
.add_systems(
PreUpdate,
((spawn_chunks, retire_chunks).chain(), remesh_dirty_chunks).chain(),
)
.add_systems(
PostUpdate,
(
flush_voxel_write_buffer,
despawn_retired_chunks,
(flush_chunk_map_buffers, flush_mesh_cache_buffers),
)
.chain(),
((spawn_chunks, retire_chunks).chain(), remesh_dirty_chunks).chain(),
(
flush_voxel_write_buffer,
despawn_retired_chunks,
(flush_chunk_map_buffers, flush_mesh_cache_buffers),
)
.chain(),
),
)
.add_event::<ChunkWillSpawn>()
.add_event::<ChunkWillDespawn>()
Expand Down Expand Up @@ -131,6 +133,7 @@ impl Plugin for VoxelWorldPlugin {
CompressedImageFormats::default(),
false,
ImageSampler::Default,
RenderAssetUsages::default(),
)
.unwrap();
image.reinterpret_stacked_2d_as_array(4);
Expand Down
40 changes: 31 additions & 9 deletions src/shaders/voxel_texture.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,40 @@
}
#endif

@group(1) @binding(100)
@group(2) @binding(100)
var mat_array_texture: texture_2d_array<f32>;

@group(1) @binding(101)
@group(2) @binding(101)
var mat_array_texture_sampler: sampler;

struct Vertex {
@builtin(instance_index) instance_index: u32,
#ifdef VERTEX_POSITIONS
@location(0) position: vec3<f32>,
#endif
#ifdef VERTEX_NORMALS
@location(1) normal: vec3<f32>,
#endif
#ifdef VERTEX_UVS
@location(2) uv: vec2<f32>,
#endif
#ifdef VERTEX_UVS_B
@location(3) uv_b: vec2<f32>,
#endif
#ifdef VERTEX_TANGENTS
@location(4) tangent: vec4<f32>,
#endif
#ifdef VERTEX_COLORS
@location(5) color: vec4<f32>,
#endif
// #ifdef SKINNED
// @location(6) joint_indices: vec4<u32>,
// @location(7) joint_weights: vec4<f32>,
// #endif
#ifdef MORPH_TARGETS
@builtin(vertex_index) index: u32,
#endif

@location(8) tex_idx: vec3<u32>
};

Expand All @@ -43,17 +62,20 @@ struct CustomVertexOutput {
#ifdef VERTEX_UVS
@location(2) uv: vec2<f32>,
#endif
#ifdef VERTEX_UVS_B
@location(3) uv_b: vec2<f32>,
#endif
#ifdef VERTEX_TANGENTS
@location(3) world_tangent: vec4<f32>,
@location(4) world_tangent: vec4<f32>,
#endif
#ifdef VERTEX_COLORS
@location(4) color: vec4<f32>,
@location(5) color: vec4<f32>,
#endif
#ifdef VERTEX_OUTPUT_INSTANCE_INDEX
@location(5) @interpolate(flat) instance_index: u32,
@location(6) @interpolate(flat) instance_index: u32,
#endif

@location(6) tex_idx: vec3<u32>,
@location(8) tex_idx: vec3<u32>,
}

@vertex
Expand All @@ -62,7 +84,7 @@ fn vertex(vertex: Vertex) -> CustomVertexOutput {
var model = mesh_functions::get_model_matrix(vertex.instance_index);

out.world_normal = mesh_functions::mesh_normal_local_to_world(
vertex.normal, get_instance_index(vertex.instance_index));
vertex.normal, vertex.instance_index);

out.world_position = mesh_functions::mesh_position_local_to_world(
model, vec4<f32>(vertex.position, 1.0));
Expand All @@ -77,14 +99,14 @@ fn vertex(vertex: Vertex) -> CustomVertexOutput {
out.world_tangent = mesh_functions::mesh_tangent_local_to_world(
model,
vertex.tangent,
get_instance_index(vertex.instance_index)
vertex.instance_index
);
#endif

out.color = vertex.color;

#ifdef VERTEX_OUTPUT_INSTANCE_INDEX
out.instance_index = get_instance_index(vertex.instance_index);
out.instance_index = vertex.instance_index;
#endif

out.tex_idx = vertex.tex_idx;
Expand Down
6 changes: 3 additions & 3 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn chunk_will_remesh_event_after_set_voxel() {
Update,
|mut ev_chunk_will_remesh: EventReader<ChunkWillRemesh>| {
let count = ev_chunk_will_remesh.read().count();
assert_eq!(count, 1)
assert!(count > 0)
},
);

Expand Down Expand Up @@ -216,9 +216,9 @@ fn raycast_finds_voxel() {
app.add_systems(Update, move |voxel_world_raycast: VoxelWorldRaycast| {
let test_voxel = crate::voxel::WorldVoxel::Solid(1);

let ray = Ray {
let ray = Ray3d {
origin: Vec3::new(0.5, 0.5, 70.0),
direction: -Vec3::Z,
direction: -Direction3d::Z,
};

let Some(result) = voxel_world_raycast.raycast(ray, &|(_pos, _vox)| true) else {
Expand Down
4 changes: 2 additions & 2 deletions src/voxel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ impl MergeVoxel for WorldVoxel {
}

pub(crate) trait VoxelAabb {
fn ray_intersection(&self, ray: Ray) -> Option<(Vec3, Vec3)>;
fn ray_intersection(&self, ray: Ray3d) -> Option<(Vec3, Vec3)>;
}

impl VoxelAabb for Aabb {
fn ray_intersection(&self, ray: Ray) -> Option<(Vec3, Vec3)> {
fn ray_intersection(&self, ray: Ray3d) -> Option<(Vec3, Vec3)> {
let min = self.min();
let max = self.max();

Expand Down
Loading

0 comments on commit db0f4c2

Please sign in to comment.