Skip to content

Commit

Permalink
Material nodes are working again
Browse files Browse the repository at this point in the history
  • Loading branch information
markusmoenig committed Dec 26, 2024
1 parent 6b432f0 commit e83a3cf
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 433 deletions.
55 changes: 51 additions & 4 deletions shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub mod level;
pub mod maprender;
pub mod materialfxnode;
pub mod materialfxobject;
// pub mod patterns;
pub mod patterns;
// pub mod prerendered;
// pub mod prerenderthread;
pub mod project;
Expand Down Expand Up @@ -60,7 +60,7 @@ pub mod prelude {
pub use crate::maprender::*;
pub use crate::materialfxnode::*;
pub use crate::materialfxobject::*;
// pub use crate::patterns::*;
pub use crate::patterns::*;
pub use rusterix::map::*;
// pub use crate::prerendered::*;
// pub use crate::prerenderthread::*;
Expand Down Expand Up @@ -128,6 +128,53 @@ pub enum HitFace {
ZFace,
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub enum MaterialType {
Off,
PBR,
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Material {
pub base_color: Vec3<f32>,

pub roughness: f32,
pub metallic: f32,
pub ior: f32,

pub mat_type: MaterialType,
}

impl Default for Material {
fn default() -> Self {
Self::new()
}
}

impl Material {
pub fn new() -> Self {
Self {
base_color: Vec3::new(0.5, 0.5, 0.5),
roughness: 0.5,
metallic: 0.0,
ior: 1.45,

mat_type: MaterialType::Off,
}
}

/// Mixes two materials.
pub fn mix(&mut self, mat1: &Material, mat2: &Material, t: f32) {
self.base_color = mat1
.base_color
.map2(mat2.base_color, |a, b| a + t * (b - a));

self.metallic = f32::lerp(mat1.metallic, mat2.metallic, t);
self.roughness = f32::lerp(mat1.roughness, mat2.roughness, t);
self.ior = f32::lerp(mat1.ior, mat2.ior, t);
}
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Hit {
pub is_valid: bool,
Expand Down Expand Up @@ -156,7 +203,7 @@ pub struct Hit {

pub color: Vec4<f32>,

//pub mat: BSDFMaterial,
pub mat: Material,
pub noise: Option<f32>,
pub noise_scale: f32,

Expand Down Expand Up @@ -200,7 +247,7 @@ impl Hit {

color: Vec4::zero(),

// mat: BSDFMaterial::default(),
mat: Material::default(),
noise: None,
noise_scale: 1.0,

Expand Down
71 changes: 31 additions & 40 deletions shared/src/materialfxnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ impl MaterialFXNode {
resolved: Vec<Hit>,
params: &[f32],
) -> Option<u8> {
/*
match self.role {
Material => {
let mut used_texture = false;
Expand Down Expand Up @@ -333,14 +332,6 @@ impl MaterialFXNode {

hit.mat.roughness = params[1];
hit.mat.metallic = params[2];
hit.mat.anisotropic = params[3];
hit.mat.subsurface = params[4];
hit.mat.specular_tint = params[5];
hit.mat.sheen = params[6];
hit.mat.sheen_tint = params[7];
hit.mat.clearcoat = params[8];
hit.mat.clearcoat_roughness = params[9];
hit.mat.spec_trans = params[10];
hit.mat.ior = params[11];

Some(0)
Expand Down Expand Up @@ -430,6 +421,7 @@ impl MaterialFXNode {
Some(3)
}
}
/*
Distance => {
let collection = self.collection();
let from = collection.get_f32_default("From", 0.0);
Expand All @@ -439,11 +431,11 @@ impl MaterialFXNode {
return None;
}
fn smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
let t = ((x - edge0) / (edge1 - edge0)).clamped(0.0, 1.0);
t * t * (3.0 - 2.0 * t)
}
let value = smoothstep(from, to, -hit.interior_distance);
// fn smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
// let t = ((x - edge0) / (edge1 - edge0)).clamped(0.0, 1.0);
// t * t * (3.0 - 2.0 * t)
// }
// let value = smoothstep(from, to, -hit.interior_distance);
// if resolved.len() == 1 {
// hit.mat.base_color =
Expand All @@ -453,14 +445,13 @@ impl MaterialFXNode {
// }
Some(0)
}
}*/
Bump => {
hit.bump = hit.value * params[0];
None
}
_ => None,
}*/
None
}
}

/// Creates a new node from a name.
Expand Down Expand Up @@ -532,29 +523,29 @@ impl MaterialFXNode {
let xx = (i % width) as f32;
let yy = (i / width) as f32;

let color = Vec4::zero();

// match &self.role {
// Noise2D => {
// let uv = Vec2::new(xx / width as f32, yy / height as f32);

// let scale = Vec2::new(
// collection.get_f32_default("UV Scale X", 1.0),
// collection.get_f32_default("UV Scale Y", 1.0),
// );
// let octaves = collection.get_i32_default("Octaves", 5);

// let value = 0.0; //noise2d(&uv, scale, octaves);
// color = Vec4::new(value, value, value, 1.0);
// }
// Noise3D => {
// let hit_point = Vec3::new(xx / width as f32, 0.0, yy / height as f32);

// let value = 0.0; //noise3d(&collection, &hit_point);
// color = Vec4::new(value, value, value, 1.0);
// }
// _ => {}
// }
let mut color = Vec4::zero();

match &self.role {
Noise2D => {
let uv = Vec2::new(xx / width as f32, yy / height as f32);

let scale = Vec2::new(
collection.get_f32_default("UV Scale X", 1.0),
collection.get_f32_default("UV Scale Y", 1.0),
);
let octaves = collection.get_i32_default("Octaves", 5);

let value = noise2d(&uv, scale, octaves);
color = Vec4::new(value, value, value, 1.0);
}
Noise3D => {
let hit_point = Vec3::new(xx / width as f32, 0.0, yy / height as f32);

let value = noise3d(&collection, &hit_point);
color = Vec4::new(value, value, value, 1.0);
}
_ => {}
}

pixel.copy_from_slice(&TheColor::from_vec4f(color).to_u8_array());
}
Expand Down
Loading

0 comments on commit e83a3cf

Please sign in to comment.