resource: do not require Biome to get color for all block types

Only grass, foliage and water have biome-dependent colors.
This commit is contained in:
Matthias Schiffer 2023-08-03 18:28:57 +02:00
parent 45171aa26a
commit 0a485343a0
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
3 changed files with 21 additions and 7 deletions

View file

@ -8,7 +8,7 @@ use num_integer::div_mod_floor;
use minedmap::{ use minedmap::{
io::{fs, storage}, io::{fs, storage},
resource::{block_color, Biome}, resource::{block_color, needs_biome, Biome},
types::*, types::*,
}; };
@ -85,8 +85,14 @@ impl<'a> TileRenderer<'a> {
) -> Option<[u8; 4]> { ) -> Option<[u8; 4]> {
let block = chunk.blocks[block_coords]?; let block = chunk.blocks[block_coords]?;
let depth = chunk.depths[block_coords]?; let depth = chunk.depths[block_coords]?;
if !needs_biome(block) {
return Some(block_color(block, None, depth.0 as f32));
}
let biome = chunk.biomes[block_coords].as_ref()?; let biome = chunk.biomes[block_coords].as_ref()?;
Some(block_color(block, biome, depth.0 as f32))
Some(block_color(block, Some(biome), depth.0 as f32))
} }
fn render_chunk( fn render_chunk(

View file

@ -72,16 +72,24 @@ impl BiomeExt for Biome {
const BIRCH_COLOR: Vec3 = Vec3::new(0.502, 0.655, 0.333); // == color_vec(Color([128, 167, 85])) const BIRCH_COLOR: Vec3 = Vec3::new(0.502, 0.655, 0.333); // == color_vec(Color([128, 167, 85]))
const EVERGREEN_COLOR: Vec3 = Vec3::new(0.380, 0.600, 0.380); // == color_vec(Color([97, 153, 97])) const EVERGREEN_COLOR: Vec3 = Vec3::new(0.380, 0.600, 0.380); // == color_vec(Color([97, 153, 97]))
pub fn block_color(block: BlockType, biome: &Biome, depth: f32) -> [u8; 4] { pub fn needs_biome(block: BlockType) -> bool {
use super::BlockFlag::*; use super::BlockFlag::*;
block.is(Grass) || block.is(Foliage) || block.is(Water)
}
pub fn block_color(block: BlockType, biome: Option<&Biome>, depth: f32) -> [u8; 4] {
use super::BlockFlag::*;
let get_biome = || biome.expect("needs biome to determine block color");
let mut color = color_vec_unscaled(block.color); let mut color = color_vec_unscaled(block.color);
if block.is(Grass) { if block.is(Grass) {
color *= biome.grass_color(depth); color *= get_biome().grass_color(depth);
} }
if block.is(Foliage) { if block.is(Foliage) {
color *= biome.foliage_color(depth); color *= get_biome().foliage_color(depth);
} }
if block.is(Birch) { if block.is(Birch) {
color *= BIRCH_COLOR; color *= BIRCH_COLOR;
@ -90,7 +98,7 @@ pub fn block_color(block: BlockType, biome: &Biome, depth: f32) -> [u8; 4] {
color *= EVERGREEN_COLOR; color *= EVERGREEN_COLOR;
} }
if block.is(Water) { if block.is(Water) {
color *= biome.water_color(); color *= get_biome().water_color();
} }
color *= 0.5 + 0.005 * depth; color *= 0.5 + 0.005 * depth;

View file

@ -91,7 +91,7 @@ impl BlockTypes {
} }
pub use biomes::{Biome, BiomeGrassColorModifier}; pub use biomes::{Biome, BiomeGrassColorModifier};
pub use block_color::block_color; pub use block_color::{block_color, needs_biome};
#[derive(Debug)] #[derive(Debug)]
pub struct BiomeTypes { pub struct BiomeTypes {