Skip to content

Commit

Permalink
Merge pull request #42 from neocturne/updates
Browse files Browse the repository at this point in the history
Dependency updates & some region processor refactoring
  • Loading branch information
neocturne authored Dec 30, 2023
2 parents b32bdb4 + 1812e5c commit c67f3b8
Show file tree
Hide file tree
Showing 11 changed files with 397 additions and 276 deletions.
235 changes: 140 additions & 95 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ clap = { version = "4.1.4", features = ["derive"] }
fastnbt = "2.3.2"
futures-util = "0.3.28"
git-version = "0.3.5"
glam = "0.24.0"
image = { version = "0.24.5", default-features = false, features = ["png"] }
indexmap = { version = "2.0.0", features = ["serde"] }
lru = "0.12.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/resource/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ repository.workspace = true

[dependencies]
enumflags2 = { version = "0.7.7", features = ["serde"] }
glam = "0.24.1"
glam = "0.25.0"
serde = { version = "1.0.183", features = ["derive"] }
52 changes: 25 additions & 27 deletions crates/resource/src/block_color.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
//! Functions for computations of block colors
use super::{Biome, BlockType, Color};

use glam::Vec3;
use super::{Biome, BlockType, Color, Colorf};

/// Converts an u8 RGB color to a float vector
fn color_vec_unscaled(color: Color) -> Vec3 {
Vec3::from_array(color.0.map(f32::from))
fn color_vec_unscaled(color: Color) -> Colorf {
Colorf::from_array(color.0.map(f32::from))
}

/// Converts an u8 RGB color to a float vector, scaling the components to 0.0..1.0
fn color_vec(color: Color) -> Vec3 {
fn color_vec(color: Color) -> Colorf {
color_vec_unscaled(color) / 255.0
}

/// Helper for grass and foliage colors
///
/// Biome temperature and downfall are modified based on the depth value
/// before using them to compute the final color
fn color_from_params(colors: &[Vec3; 3], biome: &Biome, depth: f32) -> Vec3 {
fn color_from_params(colors: &[Colorf; 3], biome: &Biome, depth: f32) -> Colorf {
let temp = (biome.temp() - f32::max((depth - 64.0) / 600.0, 0.0)).clamp(0.0, 1.0);
let downfall = biome.downfall().clamp(0.0, 1.0) * temp;

Expand All @@ -28,27 +26,27 @@ fn color_from_params(colors: &[Vec3; 3], biome: &Biome, depth: f32) -> Vec3 {
/// Extension trait with helpers for computing biome-specific block colors
trait BiomeExt {
/// Returns the grass color of the biome at a given depth
fn grass_color(&self, depth: f32) -> Vec3;
fn grass_color(&self, depth: f32) -> Colorf;
/// Returns the foliage color of the biome at a given depth
fn foliage_color(&self, depth: f32) -> Vec3;
fn foliage_color(&self, depth: f32) -> Colorf;
/// Returns the water color of the biome
fn water_color(&self) -> Vec3;
fn water_color(&self) -> Colorf;
}

impl BiomeExt for Biome {
fn grass_color(&self, depth: f32) -> Vec3 {
fn grass_color(&self, depth: f32) -> Colorf {
use super::BiomeGrassColorModifier::*;

/// Color matrix extracted from grass color texture
const GRASS_COLORS: [Vec3; 3] = [
Vec3::new(0.502, 0.706, 0.592), // lower right
Vec3::new(0.247, 0.012, -0.259), // lower left - lower right
Vec3::new(-0.471, 0.086, -0.133), // upper left - lower left
const GRASS_COLORS: [Colorf; 3] = [
Colorf::new(0.502, 0.706, 0.592), // lower right
Colorf::new(0.247, 0.012, -0.259), // lower left - lower right
Colorf::new(-0.471, 0.086, -0.133), // upper left - lower left
];
/// Used for dark forst grass color modifier
const DARK_FOREST_GRASS_COLOR: Vec3 = Vec3::new(0.157, 0.204, 0.039); // == color_vec(Color([40, 52, 10]))
const DARK_FOREST_GRASS_COLOR: Colorf = Colorf::new(0.157, 0.204, 0.039); // == color_vec(Color([40, 52, 10]))
/// Grass color in swamp biomes
const SWAMP_GRASS_COLOR: Vec3 = Vec3::new(0.416, 0.439, 0.224); // == color_vec(Color([106, 112, 57]))
const SWAMP_GRASS_COLOR: Colorf = Colorf::new(0.416, 0.439, 0.224); // == color_vec(Color([106, 112, 57]))

let regular_color = || {
self.grass_color
Expand All @@ -63,24 +61,24 @@ impl BiomeExt for Biome {
}
}

fn foliage_color(&self, depth: f32) -> Vec3 {
fn foliage_color(&self, depth: f32) -> Colorf {
/// Color matrix extracted from foliage color texture
const FOLIAGE_COLORS: [Vec3; 3] = [
Vec3::new(0.376, 0.631, 0.482), // lower right
Vec3::new(0.306, 0.012, -0.317), // lower left - lower right
Vec3::new(-0.580, 0.106, -0.165), // upper left - lower left
const FOLIAGE_COLORS: [Colorf; 3] = [
Colorf::new(0.376, 0.631, 0.482), // lower right
Colorf::new(0.306, 0.012, -0.317), // lower left - lower right
Colorf::new(-0.580, 0.106, -0.165), // upper left - lower left
];

self.foliage_color
.map(color_vec)
.unwrap_or_else(|| color_from_params(&FOLIAGE_COLORS, self, depth))
}

fn water_color(&self) -> Vec3 {
fn water_color(&self) -> Colorf {
/// Default biome water color
///
/// Used for biomes that don't explicitly set a water color
const DEFAULT_WATER_COLOR: Vec3 = Vec3::new(0.247, 0.463, 0.894); // == color_vec(Color([63, 118, 228]))
const DEFAULT_WATER_COLOR: Colorf = Colorf::new(0.247, 0.463, 0.894); // == color_vec(Color([63, 118, 228]))

self.water_color
.map(color_vec)
Expand All @@ -89,9 +87,9 @@ impl BiomeExt for Biome {
}

/// Color multiplier for birch leaves
const BIRCH_COLOR: Vec3 = Vec3::new(0.502, 0.655, 0.333); // == color_vec(Color([128, 167, 85]))
const BIRCH_COLOR: Colorf = Colorf::new(0.502, 0.655, 0.333); // == color_vec(Color([128, 167, 85]))
/// Color multiplier for spruce leaves
const EVERGREEN_COLOR: Vec3 = Vec3::new(0.380, 0.600, 0.380); // == color_vec(Color([97, 153, 97]))
const EVERGREEN_COLOR: Colorf = Colorf::new(0.380, 0.600, 0.380); // == color_vec(Color([97, 153, 97]))

/// Determined if calling [block_color] for a given [BlockType] needs biome information
pub fn needs_biome(block: BlockType) -> bool {
Expand All @@ -104,7 +102,7 @@ pub fn needs_biome(block: BlockType) -> bool {
///
/// [needs_biome] must be used to determine whether passing a [Biome] is necessary.
/// Will panic if a [Biome] is necessary, but none is passed.
pub fn block_color(block: BlockType, biome: Option<&Biome>, depth: f32) -> Vec3 {
pub fn block_color(block: BlockType, biome: Option<&Biome>, depth: f32) -> Colorf {
use super::BlockFlag::*;

let get_biome = || biome.expect("needs biome to determine block color");
Expand Down
5 changes: 4 additions & 1 deletion crates/resource/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ pub enum BlockFlag {
Water,
}

/// An RGB color
/// An RGB color with u8 components
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Color(pub [u8; 3]);

/// An RGB color with f32 components
pub type Colorf = glam::Vec3;

/// A block type specification
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct BlockType {
Expand Down
16 changes: 11 additions & 5 deletions src/core/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ use serde::{Deserialize, Serialize};

use crate::{io::fs::FileMetaVersion, resource::Biome, types::*, world::layer};

/// MinedMap data version number
///
/// Increase to force regeneration of all output files
pub const FILE_META_VERSION: FileMetaVersion = FileMetaVersion(0);
/// MinedMap processed region data version number
pub const REGION_FILE_META_VERSION: FileMetaVersion = FileMetaVersion(0);

/// MinedMap map tile data version number
pub const MAP_FILE_META_VERSION: FileMetaVersion = FileMetaVersion(0);

/// MinedMap lightmap data version number
pub const LIGHTMAP_FILE_META_VERSION: FileMetaVersion = FileMetaVersion(0);

/// Coordinate pair of a generated tile
///
Expand Down Expand Up @@ -53,7 +59,7 @@ impl TileCoordMap {
}

/// Data structure for storing chunk data between processing and rendering steps
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct ProcessedChunk {
/// Block type data
pub blocks: Box<layer::BlockArray>,
Expand All @@ -64,7 +70,7 @@ pub struct ProcessedChunk {
}

/// Data structure for storing region data between processing and rendering steps
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct ProcessedRegion {
/// List of biomes used in the region
///
Expand Down
Loading

0 comments on commit c67f3b8

Please sign in to comment.