Skip to content

Commit

Permalink
Implement leather armor rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
NickAcPT committed Nov 3, 2024
1 parent e621000 commit e621000
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
20 changes: 18 additions & 2 deletions nmsr-aas/src/model/armor/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ use std::path::PathBuf;
use ears_rs::utils::upgrade_skin_if_needed;
use hyper::Method;
use image::{GenericImageView, RgbaImage};
use nmsr_rendering::high_level::{
use nmsr_rendering::{high_level::{
model::{PlayerArmorSlot, PlayerArmorSlots},
parts::provider::minecraft::compute_base_part,
};
}, low_level::Vec3};
use strum::IntoEnumIterator;
use tokio::fs;
use tracing::Span;

use crate::{
error::{ArmorManagerError, ArmorManagerResult, ExplainableExt, Result},
model::armor::LeatherArmorColor,
utils::http_client::NmsrHttpClient,
};

Expand Down Expand Up @@ -44,6 +45,21 @@ impl<'a> VanillaArmorApplicable<'a> {
}

fn apply_modifications_if_needed(&self, image: &mut RgbaImage) {
if let Self::Armor(VanillaMinecraftArmorMaterial::Leather(LeatherArmorColor(color)))
| Self::Trim(VanillaMinecraftArmorMaterial::Leather(LeatherArmorColor(color)), _) = self
{
for pixel in image.pixels_mut() {
if pixel[3] == 0 {
continue;
}

let vec_color = Vec3::from([color[0] as f32, color[1] as f32, color[2] as f32]);
let skin_pixel = (Vec3::from([pixel[0] as f32, pixel[1] as f32, pixel[2] as f32]) / 255.0) * vec_color;

pixel.0 = [skin_pixel.x as u8, skin_pixel.y as u8, skin_pixel.z as u8, pixel[3]];
}
}

if let Self::Trim(armor_material, VanillaMinecraftArmorTrimData { material, .. }) = self {
let palette = material
.get_palette_for_trim_armor_material(*armor_material)
Expand Down
28 changes: 26 additions & 2 deletions nmsr-aas/src/model/armor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub mod manager;

use std::collections::VecDeque;

use derive_more::derive::{Deref, From};
use image::Rgb;
use nmsr_rendering::high_level::{
model::{ArmorMaterial, PlayerArmorSlot},
types::PlayerPartTextureType,
Expand All @@ -18,11 +20,21 @@ pub enum VanillaMinecraftArmorMaterial {
Diamond,
Gold,
Iron,
Leather(u64),
Leather(LeatherArmorColor),
Netherite,
Turtle,
}

#[derive(Debug, Deref, From, Copy, Clone, PartialEq, Eq)]
pub struct LeatherArmorColor(Rgb<u8>);

impl Default for LeatherArmorColor {
fn default() -> Self {
Self(Rgb([0xA0, 0x65, 0x40]))
}
}


impl VanillaMinecraftArmorMaterial {
const fn layer_count() -> usize {
2
Expand Down Expand Up @@ -193,11 +205,15 @@ impl TryFrom<String> for VanillaMinecraftArmorMaterialData {

fn try_from(value: String) -> Result<Self, Self::Error> {
let mut split_values: VecDeque<_> = value.split('_').collect();
let material: VanillaMinecraftArmorMaterial = if split_values.is_empty() {
let mut material: VanillaMinecraftArmorMaterial = if split_values.is_empty() {
return Err(ArmorManagerError::EmptyArmorSlotError);
} else {
partial_match(split_values.pop_front().unwrap_or_default())?
};

let dye_color = if split_values.back().is_some_and(|x| x.contains(',')) {
split_values.pop_back()
} else { None };

let trims = if split_values.is_empty() {
Vec::new()
Expand All @@ -217,6 +233,14 @@ impl TryFrom<String> for VanillaMinecraftArmorMaterialData {
.filter_map(std::result::Result::ok)
.collect::<Vec<_>>()
};

if let (Some(dye_color), VanillaMinecraftArmorMaterial::Leather(_)) = (dye_color, material) {
let rgb = dye_color.split(',').map(|x| x.parse::<u8>()).collect::<Result<Vec<_>, _>>();

if let Ok([r, g, b]) = rgb.as_deref() {
material = VanillaMinecraftArmorMaterial::Leather(LeatherArmorColor(Rgb([*r, *g, *b])));
}
}

Ok(Self { material, trims })
}
Expand Down

0 comments on commit e621000

Please sign in to comment.