Skip to content

Commit

Permalink
add voxel math mod;fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Feb 24, 2024
1 parent d1453c1 commit c6c3191
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 68 deletions.
5 changes: 0 additions & 5 deletions crates/core/block/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@ maintenance = { status = "passively-maintained" }

[dependencies]
rimecraft-registry = { path = "../registry" }
glam = "0.25"
rimecraft-state = { path = "../state" }
rimecraft-edcode = { path = "../../util/edcode", optional = true }
serde = { version = "1.0", optional = true }

[features]
serde = ["dep:serde"]
edcode = ["dep:rimecraft-edcode"]

[lints]
workspace = true
4 changes: 0 additions & 4 deletions crates/core/block/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ use rimecraft_state::{States, StatesMut};

use std::marker::PhantomData;

mod pos;

pub use pos::BlockPos;

pub use rimecraft_state as state;

/// Block containing settings and the state manager.
Expand Down
4 changes: 0 additions & 4 deletions crates/flare3d/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ pub struct State<'s> {
instances: Vec<Instance>,
instance_buffer: wgpu::Buffer,
depth_texture: Texture,

}

impl<'s> State<'s> {
Expand Down Expand Up @@ -674,8 +673,5 @@ impl<'s> State<'s> {

pub fn input(&mut self, event: &WindowEvent) -> bool {
self.camera_controller.process_events(event)

}


}
20 changes: 4 additions & 16 deletions crates/util/serde-update/src/erased.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ macro_rules! __internal_update_from_erased {
fn update<D>(
&mut self,
deserializer: D,
) -> Result<(), <D as serde::Deserializer<'de>>::Error>
) -> Result<(), <D as ::serde::Deserializer<'de>>::Error>
where
D: serde::Deserializer<'de>,
D: ::serde::Deserializer<'de>,
{
use serde::de::Error;
use ::serde::de::Error;
self.erased_update(&mut <dyn erased_serde::Deserializer<'de>>::erase(
deserializer,
))
Expand Down Expand Up @@ -49,7 +49,7 @@ macro_rules! update_trait_object {
}

/// [`Update`] but type erased.
pub trait ErasedUpdate<'de>: erased_serde::Serialize {
pub trait ErasedUpdate<'de> {
/// Update this type from an erased deserializer.
///
/// # Errors
Expand All @@ -62,8 +62,6 @@ pub trait ErasedUpdate<'de>: erased_serde::Serialize {
) -> Result<(), erased_serde::Error>;
}

erased_serde::serialize_trait_object!(<'de> ErasedUpdate<'de>);

crate::update_trait_object!(ErasedUpdate<'de>);

impl<'de, T> ErasedUpdate<'de> for T
Expand All @@ -81,16 +79,6 @@ where

struct ErasedWrapper<'a, 'de>(pub &'a mut dyn ErasedUpdate<'de>);

impl serde::Serialize for ErasedWrapper<'_, '_> {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.serialize(serializer)
}
}

impl<'de> Update<'de> for ErasedWrapper<'_, 'de> {
#[inline]
fn update<D>(&mut self, deserializer: D) -> Result<(), <D as serde::Deserializer<'de>>::Error>
Expand Down
25 changes: 25 additions & 0 deletions crates/util/voxel-math/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "rimecraft-voxel-math"
version = "0.1.0"
edition = "2021"
authors = ["JieningYu <[email protected]>"]
description = "Voxel math library for rimecraft"
repository = "https://github.com/rimecraft-rs/rimecraft/"
license = "AGPL-3.0-or-later"
categories = ["game-development"]

[badges]
maintenance = { status = "passively-maintained" }

[dependencies]
rimecraft-edcode = { path = "../../util/edcode", optional = true }
serde = { version = "1.0", optional = true }
glam = "0.25"

[features]
default = ["serde", "edcode"]
serde = ["dep:serde"]
edcode = ["dep:rimecraft-edcode"]

[lints]
workspace = true
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Position types.

use glam::IVec3;

/// A position of a block in a three-dimensional volume.
Expand All @@ -12,25 +14,25 @@ impl BlockPos {

/// Creates a new block position.
#[inline]
pub fn new(x: i32, y: i32, z: i32) -> Self {
pub const fn new(x: i32, y: i32, z: i32) -> Self {
Self(IVec3::new(x, y, z))
}

/// Returns the x-coordinate of the position.
#[inline]
pub fn x(&self) -> i32 {
pub const fn x(&self) -> i32 {
self.0.x
}

/// Returns the y-coordinate of the position.
#[inline]
pub fn y(&self) -> i32 {
pub const fn y(&self) -> i32 {
self.0.y
}

/// Returns the z-coordinate of the position.
#[inline]
pub fn z(&self) -> i32 {
pub const fn z(&self) -> i32 {
self.0.z
}
}
Expand Down Expand Up @@ -95,38 +97,6 @@ impl std::ops::SubAssign<IVec3> for BlockPos {
}
}

impl std::ops::Add<BlockPos> for BlockPos {
type Output = BlockPos;

#[inline]
fn add(self, rhs: BlockPos) -> BlockPos {
BlockPos(self.0 + rhs.0)
}
}

impl std::ops::AddAssign<BlockPos> for BlockPos {
#[inline]
fn add_assign(&mut self, rhs: BlockPos) {
self.0 += rhs.0;
}
}

impl std::ops::Sub<BlockPos> for BlockPos {
type Output = BlockPos;

#[inline]
fn sub(self, rhs: BlockPos) -> BlockPos {
BlockPos(self.0 - rhs.0)
}
}

impl std::ops::SubAssign<BlockPos> for BlockPos {
#[inline]
fn sub_assign(&mut self, rhs: BlockPos) {
self.0 -= rhs.0;
}
}

const LEN_BITS_X: i32 = 1 + (1i32 << (32 - (30000000i32 - 1).leading_zeros())).ilog2() as i32;
const LEN_BITS_Y: i32 = 64 - LEN_BITS_X - LEN_BITS_Z;
const LEN_BITS_Z: i32 = LEN_BITS_X;
Expand Down Expand Up @@ -160,7 +130,7 @@ impl From<i64> for BlockPos {
}

#[cfg(feature = "serde")]
mod serde {
mod _serde {
use ::serde::{Deserialize, Serialize};

use super::*;
Expand Down Expand Up @@ -243,8 +213,6 @@ mod serde {

#[cfg(feature = "edcode")]
mod edcode {
use std::convert::Infallible;

use rimecraft_edcode::{Decode, Encode};

use super::*;
Expand Down
101 changes: 101 additions & 0 deletions crates/util/voxel-math/src/chunk_pos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/// A pair of two integers representing the X and Z coordinates of a chunk.
///
/// Chunk positions are usually serialized as an [`u64`].
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub struct ChunkPos {
/// The X coordinate of the chunk.
pub x: i32,
/// The Z coordinate of the chunk.
pub z: i32,
}

impl ChunkPos {
/// Creates a new `ChunkPos` with the given X and Z coordinates.
#[inline]
pub const fn new(x: i32, z: i32) -> Self {
Self { x, z }
}

/// Creates a new `ChunkPos` from the given region coordinates.
#[inline]
pub const fn from_region(x: i32, z: i32) -> Self {
Self::new(x << 5, z << 5)
}

/// Creates a new `ChunkPos` from the given region center coordinates.
#[inline]
pub const fn from_region_center(x: i32, z: i32) -> Self {
Self::new((x << 5) + 31, (z << 5) + 31)
}

/// Returns the x-coordinate of the position.
#[inline]
pub const fn x(&self) -> i32 {
self.x
}

/// Returns the z-coordinate of the position.
#[inline]
pub const fn z(&self) -> i32 {
self.z
}
}

impl From<(i32, i32)> for ChunkPos {
#[inline]
fn from((x, z): (i32, i32)) -> Self {
Self::new(x, z)
}
}

impl From<ChunkPos> for u64 {
#[inline]
fn from(ChunkPos { x, z }: ChunkPos) -> Self {
x as u64 & 0xFFFF_FFFF_u64 | (z as u64 & 0xFFFF_FFFF_u64) << 32
}
}

impl From<u64> for ChunkPos {
#[inline]
fn from(value: u64) -> Self {
Self {
x: (value & 0xFFFF_FFFF_u64) as i32,
z: (value >> 32u64 & 0xFFFF_FFFF_u64) as i32,
}
}
}

impl From<ChunkPos> for (i32, i32) {
#[inline]
fn from(ChunkPos { x, z }: ChunkPos) -> Self {
(x, z)
}
}

#[cfg(feature = "edcode")]
mod _edcode {
use rimecraft_edcode::{Decode, Encode};

use crate::ChunkPos;

impl Encode for ChunkPos {
#[inline]
fn encode<B>(&self, mut buf: B) -> Result<(), std::io::Error>
where
B: rimecraft_edcode::bytes::BufMut,
{
buf.put_u64((*self).into());
Ok(())
}
}

impl Decode for ChunkPos {
#[inline]
fn decode<B>(mut buf: B) -> Result<Self, std::io::Error>
where
B: rimecraft_edcode::bytes::Buf,
{
Ok(buf.get_u64().into())
}
}
}
Loading

0 comments on commit c6c3191

Please sign in to comment.