Add type alias for f32 colors to minedmap-resource

Allow removing the dependency on a specific glam version from the main
crate.
This commit is contained in:
Matthias Schiffer 2023-12-29 20:13:03 +01:00
parent 1432df7c93
commit 1812e5c6d6
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
5 changed files with 32 additions and 34 deletions

View file

@ -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");

View file

@ -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 {