From 1812e5c6d636474a5891ce3ac632443e70cb654c Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 29 Dec 2023 20:13:03 +0100 Subject: [PATCH] Add type alias for f32 colors to minedmap-resource Allow removing the dependency on a specific glam version from the main crate. --- Cargo.lock | 1 - Cargo.toml | 1 - crates/resource/src/block_color.rs | 52 ++++++++++++++---------------- crates/resource/src/lib.rs | 5 ++- src/core/tile_renderer.rs | 7 ++-- 5 files changed, 32 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b76fb71..f681a43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -528,7 +528,6 @@ dependencies = [ "fastnbt", "futures-util", "git-version", - "glam", "image", "indexmap", "lru", diff --git a/Cargo.toml b/Cargo.toml index a1d5daf..47c13c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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.25.0" image = { version = "0.24.5", default-features = false, features = ["png"] } indexmap = { version = "2.0.0", features = ["serde"] } lru = "0.12.0" diff --git a/crates/resource/src/block_color.rs b/crates/resource/src/block_color.rs index 3df9a54..cffebeb 100644 --- a/crates/resource/src/block_color.rs +++ b/crates/resource/src/block_color.rs @@ -1,16 +1,14 @@ //! 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 } @@ -18,7 +16,7 @@ fn color_vec(color: Color) -> Vec3 { /// /// 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; @@ -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 @@ -63,12 +61,12 @@ 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 @@ -76,11 +74,11 @@ impl BiomeExt for Biome { .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) @@ -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 { @@ -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"); diff --git a/crates/resource/src/lib.rs b/crates/resource/src/lib.rs index 5423bd7..a832a58 100644 --- a/crates/resource/src/lib.rs +++ b/crates/resource/src/lib.rs @@ -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 { diff --git a/src/core/tile_renderer.rs b/src/core/tile_renderer.rs index 34f0310..b341076 100644 --- a/src/core/tile_renderer.rs +++ b/src/core/tile_renderer.rs @@ -8,7 +8,6 @@ use std::{ }; use anyhow::{Context, Result}; -use glam::Vec3; use lru::LruCache; use rayon::prelude::*; use tokio::sync::OnceCell; @@ -17,7 +16,7 @@ use tracing::{debug, info}; use super::{common::*, region_group::RegionGroup}; use crate::{ io::{fs, storage}, - resource::{block_color, needs_biome}, + resource::{block_color, needs_biome, Colorf}, types::*, util::coord_offset, }; @@ -128,7 +127,7 @@ impl<'a> TileRenderer<'a> { chunk: &ProcessedChunk, chunk_coords: ChunkCoords, block_coords: LayerBlockCoords, - ) -> Option { + ) -> Option { /// Helper for keys in the weight table /// /// Hashing the value as a single u32 is more efficient than hashing @@ -182,7 +181,7 @@ impl<'a> TileRenderer<'a> { return None; } - let mut color = Vec3::ZERO; + let mut color = Colorf::ZERO; let mut total = 0.0; for ((region_x, region_z, index), w) in weights.into_values() {