Skip to content

Commit

Permalink
merge with upstream building-blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
bonsairobo committed Jul 3, 2021
1 parent 25dd7ad commit 67c78c0
Show file tree
Hide file tree
Showing 24 changed files with 258 additions and 249 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Duncan <[email protected]>"]
edition = "2018"

[profile.release]
lto = true
lto = "thin"

[profile.dev]
opt-level = 2
Expand All @@ -32,8 +32,9 @@ thread_profiler = { version = "0.3", optional = true }
[dependencies.building-blocks]
# version = "0.2"
git = "https://github.com/bonsairobo/building-blocks"
branch = "main"
features = ["mesh", "mint", "ncollide", "partition", "search"]
rev = "bb9634c"
# branch = "main"
features = ["mesh", "mint", "ncollide", "search"]

[features]
profiler = ["thread_profiler", "thread_profiler/thread_profiler"]
1 change: 1 addition & 0 deletions assets/maps/example_map.ron
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
),
// TODO: procgen maps are currently broken, awaiting a port from ilattice3
// voxels_file_path: Some((ProcGenDungeon, "assets/maps/procgen_dungeon.ron")),
// TODO: serializable maps are currently broken, need to add support for ChunkDb from building-blocks
// voxels_file_path: Some((Bincode, "saved_voxels.bin")),
voxels_file_path: None,
)
1 change: 1 addition & 0 deletions run/linux_release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cargo run --release --features="amethyst/vulkan,amethyst/no-slow-safety-checks" "$@"
6 changes: 4 additions & 2 deletions src/bin/editor/control/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ where
&self.screen_dims,
);
let local_cache = LocalChunkCache3::new();
let map_reader = ChunkMapReader3::new(&self.voxel_map.voxels, &local_cache);
let voxel_infos = TransformMap::new(&map_reader, self.voxel_map.voxel_info_transform());
let map_reader = self.voxel_map.voxels.reader(&local_cache);
let lod0_reader = map_reader.lod_view(0);
let voxel_infos =
TransformMap::new(&lod0_reader, self.voxel_map.voxel_info_transform());
let CameraControllerComponent(ctrlr) = ctrlr;
let (new_cam_tfm, new_camera_state) =
ctrlr.update(&tpc_state, &proc_input, &voxel_infos, &self.voxel_bvt);
Expand Down
36 changes: 18 additions & 18 deletions src/bin/editor/control/camera/colliding_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use voxel_mapper::{

use amethyst::core::math::{Point3, Vector3};
use building_blocks::{
partition::collision::voxel_sphere_cast, prelude::*, search::greedy_path_with_l1_heuristic,
prelude::*, search::collision::cast_ball_at_voxels, search::greedy_path_with_l1_heuristic,
};
use ncollide3d::query::Ray;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -75,7 +75,7 @@ impl CollidingController {
voxel_bvt: &VoxelBVT,
) -> ThirdPersonCameraState
where
V: for<'r> Get<&'r Point3i, Data = T>,
V: Get<Point3i, Item = T>,
T: IsEmpty + IsFloor,
{
// Figure out the where the camera feet are.
Expand All @@ -86,7 +86,7 @@ impl CollidingController {

set_desired_camera_position(input, self.colliding, config, &mut cam_state);

let voxel_is_empty_fn = |p: &Point3i| voxels.get(p).is_empty();
let voxel_is_empty_fn = |p: &Point3i| voxels.get(*p).is_empty();
self.resolve_camera_collisions(
&config.collision,
&voxel_is_empty_fn,
Expand Down Expand Up @@ -197,8 +197,8 @@ impl CollidingController {
let mut best_point = None;
let mut best_point_closeness = std::usize::MAX;

let range_point_closeness = |point_in_range: &Point3i| {
if let Some(previous_camera) = self.previous_camera_voxel.as_ref() {
let range_point_closeness = |point_in_range: Point3i| {
if let Some(previous_camera) = self.previous_camera_voxel {
let (_reached_finish, path) = greedy_path_with_l1_heuristic(
point_in_range,
previous_camera,
Expand All @@ -217,7 +217,7 @@ impl CollidingController {
// If the camera is really close to the target, it might not show up in the
// unobstructed ranges, due to min_range_length, but we still want to consider it.
if let Some(target_point) = self.last_empty_feet_point {
let closeness = range_point_closeness(&target_point);
let closeness = range_point_closeness(target_point);
if closeness < best_point_closeness {
// Use the target point to start the sphere cast.
best_point_closeness = closeness;
Expand All @@ -233,7 +233,7 @@ impl CollidingController {
range,
config.range_point_selection_offset,
);
let closeness = range_point_closeness(&point_in_range);
let closeness = range_point_closeness(point_in_range);
if closeness < best_point_closeness {
best_point_closeness = closeness;
best_point = Some(project_point_onto_line(
Expand Down Expand Up @@ -328,11 +328,11 @@ fn find_unobstructed_ranges(
}
};

for (i, p) in path.iter().enumerate() {
let p_center = voxel_center(*p);
for (i, &p) in path.iter().enumerate() {
let p_center = voxel_center(p);
let p_proj = project_point_onto_line(&p_center, &eye_line);

if point_is_obstructed(p, &p_center, &p_proj, voxel_is_empty_fn, config) {
if point_is_obstructed(p, p_center, p_proj, voxel_is_empty_fn, config) {
try_add_range(i, p_proj, &mut current_range_start);
} else if let None = current_range_start {
// We're no longer obstructed, so start a new range.
Expand All @@ -341,10 +341,10 @@ fn find_unobstructed_ranges(
}

// Finish off the last range.
if let Some(p) = path.last() {
if let Some(&p) = path.last() {
try_add_range(
path.len(),
project_point_onto_line(&voxel_center(*p), &eye_line),
project_point_onto_line(&voxel_center(p), &eye_line),
&mut current_range_start,
);
}
Expand All @@ -356,21 +356,21 @@ fn find_unobstructed_ranges(
/// 1. The point strays too far from the eye line.
/// 2. The voxel projection of the point on the eye line is not connected to empty space.
fn point_is_obstructed(
p: &Point3i,
p_float: &Point3<f32>,
p_proj: &Point3<f32>,
p: Point3i,
p_float: Point3<f32>,
p_proj: Point3<f32>,
voxel_is_empty_fn: &impl Fn(&Point3i) -> bool,
config: &CameraCollisionConfig,
) -> bool {
let p_rej = p_float - p_proj;
if p_rej.norm_squared() < config.min_obstruction_width.powi(2) {
return false;
} else {
let voxel_p_proj = voxel_containing_point(*p_proj);
let voxel_p_proj = voxel_containing_point(p_proj);
if voxel_is_empty_fn(&voxel_p_proj) {
// Projection must still be path-connected to empty space.
let (connected, _) = greedy_path_with_l1_heuristic(
&voxel_p_proj,
voxel_p_proj,
p,
voxel_is_empty_fn,
config.projection_connection_max_iterations,
Expand All @@ -392,7 +392,7 @@ fn move_ball_until_collision(
let max_toi = 1.0;

if let Some(impact) =
voxel_sphere_cast(&voxel_bvt, ball_radius, upgrade_ray(ray), max_toi, |_| true)
cast_ball_at_voxels(&voxel_bvt, ball_radius, upgrade_ray(ray), max_toi, |_| true)
{
// Move ball up until an impact occurs. Make sure not to go in reverse (negative stop_time).
// Note: this calculation works because `extreme_ball_voxel_impact` ensures the max TOI is
Expand Down
2 changes: 1 addition & 1 deletion src/bin/editor/control/camera/final_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl FinalController {
voxel_bvt: &VoxelBVT,
) -> (Transform, ThirdPersonCameraState)
where
V: for<'r> Get<&'r Point3i, Data = T>,
V: Get<Point3i, Item = T>,
T: IsEmpty + IsFloor,
{
let new_camera_state = self.colliding_controller.apply_input(
Expand Down
10 changes: 5 additions & 5 deletions src/bin/editor/control/hover_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use amethyst::{
input::{BindingTypes, InputHandler},
};
use building_blocks::{
partition::collision::{voxel_ray_cast, VoxelRayImpact},
prelude::*,
search::collision::{cast_ray_at_voxels, VoxelRayImpact},
};
use ncollide3d::query::Ray;
use std::marker::PhantomData;
Expand All @@ -37,9 +37,9 @@ impl HoverVoxel {

/// Returns the normal vector of the face that the ray hit first.
pub fn hover_face(&self) -> Point3i {
let mint_v: mint::Vector3<f32> = self.impact.impact.normal.normalize().into();

Point3f::from(mint_v).round().as_3i()
Point3f::from(self.impact.impact.normal.normalize())
.round()
.into_int()
}

/// Returns the point of the adjacent voxel that shares a face with the voxel that was hit by
Expand Down Expand Up @@ -84,7 +84,7 @@ where

// Check for intersection with a voxel.
let max_toi = std::f32::MAX;
let voxel_impact = voxel_ray_cast(&*voxel_bvt, upgrade_ray(ray), max_toi, |_| true);
let voxel_impact = cast_ray_at_voxels(&*voxel_bvt, upgrade_ray(ray), max_toi, |_| true);
objects.voxel = voxel_impact.map(|impact| HoverVoxel { impact, ray });

// Check for intersection with the XZ plane.
Expand Down
8 changes: 3 additions & 5 deletions src/bin/editor/only_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ use crate::{
use voxel_mapper::{
collision::{insert_all_chunk_bvts, VoxelBVT},
voxel::{
asset_loader::VoxelAssetLoader,
map_file::{load_voxel_map, save_voxel_map},
meshing::manager::VoxelMeshManager,
VoxelMap, VoxelType,
asset_loader::VoxelAssetLoader, map_file::load_voxel_map,
meshing::manager::VoxelMeshManager, VoxelMap, VoxelType,
},
};

Expand Down Expand Up @@ -106,7 +104,7 @@ impl SimpleState for OnlyState {
|(mut manager, map): (VoxelMeshManager, ReadExpect<VoxelMap>)| {
manager.destroy();

save_voxel_map("saved_voxels.bin", &map).expect("Failed to save voxels");
// save_voxel_map("saved_voxels.bin", &map).expect("Failed to save voxels");
},
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/bin/editor/voxel_brush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::{

use voxel_mapper::voxel::{
centered_extent, chunk_cache_flusher::ChunkCacheFlusher, chunk_processor::MeshMode,
double_buffer::EditedChunksBackBuffer, voxel_containing_point, Voxel, VoxelMap, VoxelType,
EMPTY_VOXEL,
double_buffer::EditedChunksBackBuffer, voxel_containing_point, Voxel, VoxelChunkReader,
VoxelMap, VoxelType, EMPTY_VOXEL,
};

use amethyst::{
Expand Down Expand Up @@ -125,7 +125,7 @@ impl<'a> System<'a> for VoxelBrushSystem {
let brush_center = voxel_containing_point(center);

let local_cache = LocalChunkCache3::new();
let map_reader = ChunkMapReader3::new(&voxel_map.voxels, &local_cache);
let map_reader = voxel_map.voxels.reader(&local_cache);

let mut lock_brush_dist_from_camera = false;
if input_handler
Expand Down Expand Up @@ -180,7 +180,7 @@ fn edit_sphere(
center: Point3i,
radius: u32,
voxel_type: VoxelType,
map_reader: &ChunkMapReader3<Voxel>,
map_reader: &VoxelChunkReader,
voxel_backbuffer: &mut EditedChunksBackBuffer,
) {
let fradius = radius as f32;
Expand Down
24 changes: 11 additions & 13 deletions src/collision.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
use crate::voxel::{Voxel, VoxelMap};
use crate::voxel::{LocalVoxelCache, VoxelMap};

pub mod floor_translation;

use building_blocks::{
partition::{Octree, OctreeDBVT},
prelude::*,
};
use building_blocks::{prelude::*, search::OctreeDbvt, storage::OctreeSet};

pub type VoxelBVT = OctreeDBVT<Point3i>;
pub type VoxelBVT = OctreeDbvt<Point3i>;

pub fn insert_all_chunk_bvts(
bvt: &mut VoxelBVT,
voxel_map: &VoxelMap,
chunk_cache: &LocalChunkCache3<Voxel>,
chunk_cache: &LocalVoxelCache,
) {
for chunk_key in voxel_map.voxels.chunk_keys() {
let chunk = voxel_map.voxels.get_chunk(*chunk_key, chunk_cache).unwrap();
let chunk_infos = TransformMap::new(&chunk.array, voxel_map.voxel_info_transform());
let octree = Octree::from_array3(&chunk_infos, *chunk_infos.extent());
let reader = voxel_map.voxels.reader(chunk_cache);
for &chunk_key in voxel_map.voxels.storage().chunk_keys() {
let chunk = reader.get_chunk(chunk_key).unwrap();
let chunk_infos = TransformMap::new(chunk, voxel_map.voxel_info_transform());
let octree = OctreeSet::from_array3(&chunk_infos, *chunk_infos.extent());
if octree.is_empty() {
bvt.remove(chunk_key);
bvt.remove(&chunk_key.minimum);
} else {
bvt.insert(*chunk_key, octree);
bvt.insert(chunk_key.minimum, octree);
}
}
}
20 changes: 10 additions & 10 deletions src/collision/floor_translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ fn integer_points_on_line_segment_3d(
}

/// True if the voxel at `p` is not a floor voxel AND the voxel directly under `p` is a floor voxel.
fn voxel_is_on_top_of_floor<V, T>(p: &Point3i, voxels: &V) -> bool
fn voxel_is_on_top_of_floor<V, T>(p: Point3i, voxels: &V) -> bool
where
V: for<'r> Get<&'r Point3i, Data = T>,
V: Get<Point3i, Item = T>,
T: IsFloor,
{
if !voxels.get(p).is_floor() {
let under_p = *p - PointN([0, 1, 0]);
return voxels.get(&under_p).is_floor();
let under_p = p - PointN([0, 1, 0]);
return voxels.get(under_p).is_floor();
}

false
Expand All @@ -76,15 +76,15 @@ const MAX_PROBE_ITERS: i32 = 10;
// POTENTIAL BUG: can skip over non-floor voxels in a column
fn vertical_probe<V, T>(vertical_iters: i32, start: &Point3i, voxels: &V) -> Option<i32>
where
V: for<'r> Get<&'r Point3i, Data = T>,
V: Get<Point3i, Item = T>,
T: IsFloor,
{
let dir = vertical_iters.signum();
let probe_iters = vertical_iters.abs();
let mut p = *start;
let mut dh = 0;
for _ in 0..probe_iters {
if voxel_is_on_top_of_floor(&p, voxels) {
if voxel_is_on_top_of_floor(p, voxels) {
return Some(dh);
}
p = p + PointN([0, dir, 0]);
Expand All @@ -109,7 +109,7 @@ pub fn translate_over_floor<V, T>(
blocking_collisions: bool,
) -> Point3<f32>
where
V: for<'r> Get<&'r Point3i, Data = T>,
V: Get<Point3i, Item = T>,
T: IsFloor,
{
let up = Vector3::from(UP);
Expand All @@ -118,11 +118,11 @@ where
let start_voxel = voxel_containing_point(start);

// Sometimes geometry gets created on top of the camera feet, so just probe out of it.
if voxels.get(&start_voxel).is_floor() {
if voxels.get(start_voxel).is_floor() {
if let Some(dh) = vertical_probe(100, &start_voxel, voxels) {
start += dh as f32 * up;
}
} else if !voxel_is_on_top_of_floor(&start_voxel, voxels) {
} else if !voxel_is_on_top_of_floor(start_voxel, voxels) {
if let Some(dh) = vertical_probe(-100, &start_voxel, voxels) {
start += dh as f32 * up;
}
Expand Down Expand Up @@ -151,7 +151,7 @@ where

let voxel_p = midpoint_voxel + PointN([0, height_delta, 0]);

let voxel_is_floor = voxels.get(&voxel_p).is_floor();
let voxel_is_floor = voxels.get(voxel_p).is_floor();
let probe_vector = if voxel_is_floor {
MAX_PROBE_ITERS
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ pub fn screen_ray(
// TODO: amethyst is using an older version of nalgebra than building-blocks, so we need to upgrade
// the old ncollide types to new ones when using them with building-blocks
use amethyst::core::math as na_old;
use building_blocks::partition::ncollide3d as nc_new;
use building_blocks::search::ncollide3d as nc_new;
use ncollide3d as nc_old;

pub fn upgrade_ray(old_ray: nc_old::query::Ray<f32>) -> nc_new::query::Ray<f32> {
Expand Down
Loading

0 comments on commit 67c78c0

Please sign in to comment.