Implement biome colors

This commit is contained in:
Thinkofname 2016-03-24 19:39:14 +00:00
parent f8b8c5eea4
commit 99a500b4dd
3 changed files with 53 additions and 8 deletions

View File

@ -58,7 +58,7 @@ impl ChunkBuilder {
let rm = self.resources.read().unwrap(); let rm = self.resources.read().unwrap();
if rm.version() != self.resource_version { if rm.version() != self.resource_version {
self.resource_version = rm.version(); self.resource_version = rm.version();
self.models.write().unwrap().clear_cache(); self.models.write().unwrap().version_change();
} }
} }
@ -170,7 +170,7 @@ fn build_func(id: usize, models: Arc<RwLock<model::Factory>>, work_recv: mpsc::R
solid_count += model::Factory::get_state_model( solid_count += model::Factory::get_state_model(
&models, &model_name.0, &model_name.1, &variant, &mut rng, &models, &model_name.0, &model_name.1, &variant, &mut rng,
&snapshot, x, y, z, &mut solid_buffer &snapshot, x, y, z, &mut solid_buffer
); );
} }
} }
} }

View File

@ -13,12 +13,16 @@ use std::hash::BuildHasherDefault;
use types::hash::FNVHash; use types::hash::FNVHash;
use rand::Rng; use rand::Rng;
use image::{self, GenericImage};
pub struct Factory { pub struct Factory {
resources: Arc<RwLock<resources::Manager>>, resources: Arc<RwLock<resources::Manager>>,
textures: Arc<RwLock<render::TextureManager>>, textures: Arc<RwLock<render::TextureManager>>,
models: HashMap<Key, StateModel, BuildHasherDefault<FNVHash>>, models: HashMap<Key, StateModel, BuildHasherDefault<FNVHash>>,
grass_colors: image::DynamicImage,
foliage_colors: image::DynamicImage,
} }
#[derive(PartialEq, Eq, Hash, Clone)] #[derive(PartialEq, Eq, Hash, Clone)]
@ -48,6 +52,8 @@ macro_rules! try_log {
impl Factory { impl Factory {
pub fn new(resources: Arc<RwLock<resources::Manager>>, textures: Arc<RwLock<render::TextureManager>>) -> Factory { pub fn new(resources: Arc<RwLock<resources::Manager>>, textures: Arc<RwLock<render::TextureManager>>) -> Factory {
Factory { Factory {
grass_colors: Factory::load_biome_colors(resources.clone(), "grass"),
foliage_colors: Factory::load_biome_colors(resources.clone(), "foliage"),
resources: resources, resources: resources,
textures: textures, textures: textures,
@ -55,8 +61,17 @@ impl Factory {
} }
} }
pub fn clear_cache(&mut self) { fn load_biome_colors(res: Arc<RwLock<resources::Manager>>, name: &str) -> image::DynamicImage {
let mut val = res.read().unwrap().open("minecraft", &format!("textures/colormap/{}.png", name)).unwrap();
let mut data = Vec::new();
val.read_to_end(&mut data).unwrap();
image::load_from_memory(&data).unwrap()
}
pub fn version_change(&mut self) {
self.models.clear(); self.models.clear();
self.grass_colors = Factory::load_biome_colors(self.resources.clone(), "grass");
self.foliage_colors = Factory::load_biome_colors(self.resources.clone(), "foliage");
} }
pub fn get_state_model<R: Rng, W: Write>(models: &Arc<RwLock<Factory>>, plugin: &str, name: &str, variant: &str, rng: &mut R, pub fn get_state_model<R: Rng, W: Write>(models: &Arc<RwLock<Factory>>, plugin: &str, name: &str, variant: &str, rng: &mut R,
@ -68,7 +83,7 @@ impl Factory {
if let Some(model) = m.models.get(&key) { if let Some(model) = m.models.get(&key) {
if let Some(var) = model.get_variants(variant) { if let Some(var) = model.get_variants(variant) {
let model = var.choose_model(rng); let model = var.choose_model(rng);
return model.render(snapshot, x, y, z, buf); return model.render(&*m, snapshot, x, y, z, buf);
} }
missing = true; missing = true;
} }
@ -83,7 +98,7 @@ impl Factory {
if let Some(model) = m.models.get(&key) { if let Some(model) = m.models.get(&key) {
if let Some(var) = model.get_variants(variant) { if let Some(var) = model.get_variants(variant) {
let model = var.choose_model(rng); let model = var.choose_model(rng);
return model.render(snapshot, x, y, z, buf); return model.render(&*m, snapshot, x, y, z, buf);
} }
} }
} }
@ -720,7 +735,7 @@ struct Face {
} }
impl Model { impl Model {
fn render<W: Write>(&self, snapshot: &world::Snapshot, x: i32, y: i32, z: i32, buf: &mut W) -> usize { fn render<W: Write>(&self, factory: &Factory, snapshot: &world::Snapshot, x: i32, y: i32, z: i32, buf: &mut W) -> usize {
let this = snapshot.get_block(x, y, z); let this = snapshot.get_block(x, y, z);
let this_mat = this.get_material(); let this_mat = this.get_material();
let mut indices = 0; let mut indices = 0;
@ -747,8 +762,18 @@ impl Model {
cg = g; cg = g;
cb = b; cb = b;
}, },
TintType::Grass => {}, // TODO TintType::Grass => {
TintType::Foliage => {}, // TODO let (r, g, b) = calculate_biome(snapshot, x, z, &factory.grass_colors);
cr = r;
cg = g;
cb = b;
},
TintType::Foliage => {
let (r, g, b) = calculate_biome(snapshot, x, z, &factory.foliage_colors);
cr = r;
cg = g;
cb = b;
},
} }
}, },
_ => {}, _ => {},
@ -789,7 +814,26 @@ impl Model {
} }
} }
fn calculate_biome(snapshot: &world::Snapshot, x: i32, z: i32, img: &image::DynamicImage) -> (u8, u8, u8) {
let mut count = 0;
let mut r = 0;
let mut g = 0;
let mut b = 0;
for xx in -2 .. 3 {
for zz in -2 .. 3 {
let bi = snapshot.get_biome(x+xx, z+zz);
let ix = bi.color_index & 0xFF;
let iy = bi.color_index >> 8;
let col = img.get_pixel(ix as u32, iy as u32);
r += col.data[0] as u32;
g += col.data[1] as u32;
b += col.data[2] as u32;
count += 1;
}
}
((r/count) as u8, (g/count) as u8, (b/count) as u8)
}
fn calculate_light(snapshot: &world::Snapshot, orig_x: i32, orig_y: i32, orig_z: i32, fn calculate_light(snapshot: &world::Snapshot, orig_x: i32, orig_y: i32, orig_z: i32,
x: f64, y: f64, z: f64, face: Direction, smooth: bool, force: bool) -> (u16, u16) { x: f64, y: f64, z: f64, face: Direction, smooth: bool, force: bool) -> (u16, u16) {

View File

@ -439,6 +439,7 @@ define_blocks! {
force_shade: true, force_shade: true,
}, },
model { ("minecraft", format!("{}_leaves", variant.as_string()) ) }, model { ("minecraft", format!("{}_leaves", variant.as_string()) ) },
tint TintType::Foliage,
} }
Missing { Missing {
props {}, props {},