pub(crate) mod blending;
mod dfg_lut;
pub mod environment;
pub(crate) mod fields;
pub mod lighting_model;
pub(crate) mod line2;
mod node_material;
pub mod phong;
pub mod physical;
pub(crate) mod toon;
pub mod transmission;
pub use node_material::{
background_color_node, background_environment_color_node, background_node_color_node,
background_pmrem_color_node, background_vertex_node, instanced_range, output_fragment_node,
quad_vertex_node, render_output, setup, shadow_material, shadow_material_for,
tone_mapping_node, AoContext, MrtContext, OutputContext, SetupContext,
};
pub use blending::{BlendEquation, BlendFactor, BlendMode, Blending};
use std::cell::Cell;
use crate::math::Color;
use crate::nodes::NodeRef;
use crate::textures::{CubeTexture, Texture};
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MaterialId(usize);
impl MaterialId {
fn next() -> Self {
thread_local! {
static MATERIAL_ID: Cell<usize> = const { Cell::new(0) };
}
MATERIAL_ID.with(|id| {
let next = id.get();
id.set(next + 1);
MaterialId(next)
})
}
pub fn get(&self) -> usize {
self.0
}
}
impl Clone for MaterialId {
fn clone(&self) -> Self {
Self::next()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Side {
Front,
Back,
Double,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum DepthFunc {
Never,
Always,
Less,
#[default]
LessEqual,
Equal,
GreaterEqual,
Greater,
NotEqual,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum StencilFunc {
Never,
Less,
Equal,
LessEqual,
Greater,
NotEqual,
GreaterEqual,
#[default]
Always,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum StencilOp {
Zero,
#[default]
Keep,
Replace,
Increment,
Decrement,
IncrementWrap,
DecrementWrap,
Invert,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ToneMapping {
#[default]
None,
Reinhard,
AcesFilmic,
Linear,
Neutral,
AgX,
Cineon,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MaterialKind {
#[default]
Basic,
Phong,
Lambert,
Sprite,
Standard,
Physical,
Points,
Normal,
Line2,
Toon,
}
#[derive(Clone, Debug)]
pub struct MeshBasicNodeMaterial {
pub id: MaterialId,
pub version: u32,
pub kind: MaterialKind,
pub color: Color,
pub opacity: f64,
pub reflectivity: f64,
pub refraction_ratio: f64,
pub env_map: Option<CubeTexture>,
pub pmrem_env: Option<environment::PmremHandle>,
pub color_node: Option<NodeRef>,
pub opacity_node: Option<NodeRef>,
pub alpha_test_node: Option<NodeRef>,
pub alpha_test: f64,
pub alpha_hash: bool,
pub alpha_map: Option<Texture>,
pub emissive_node: Option<NodeRef>,
pub specular: Color,
pub shininess: f64,
pub emissive: Color,
pub emissive_intensity: f64,
pub flat_shading: bool,
pub lights: bool,
pub lights_node: Option<Vec<usize>>,
pub lighting_model: Option<std::rc::Rc<dyn lighting_model::LightingModel>>,
pub environment_specular: bool,
pub mask_node: Option<NodeRef>,
pub cast_shadow_node: Option<NodeRef>,
pub received_shadow_position_node: Option<NodeRef>,
pub fog: bool,
pub metalness: f64,
pub roughness: f64,
pub map: Option<Texture>,
pub vertex_colors: bool,
pub roughness_map: Option<Texture>,
pub metalness_map: Option<Texture>,
pub emissive_map: Option<Texture>,
pub gradient_map: Option<Texture>,
pub ao_map: Option<Texture>,
pub ao_map_intensity: f64,
pub light_map: Option<Texture>,
pub light_map_intensity: f64,
pub specular_map: Option<Texture>,
pub bump_map: Option<Texture>,
pub bump_scale: f64,
pub clearcoat: f64,
pub clearcoat_roughness: f64,
pub clearcoat_map: Option<Texture>,
pub clearcoat_roughness_map: Option<Texture>,
pub sheen: f64,
pub sheen_color: Color,
pub sheen_roughness: f64,
pub diffuse_roughness: f64,
pub ior: f64,
pub specular_intensity: f64,
pub specular_color: Color,
pub specular_node: Option<NodeRef>,
pub normal_map: Option<Texture>,
pub normal_scale: crate::math::Vector2,
pub specular_color_map: Option<Texture>,
pub anisotropy: f64,
pub anisotropy_rotation: f64,
pub anisotropy_map: Option<Texture>,
pub iridescence: f64,
pub iridescence_ior: f64,
pub iridescence_thickness_range: [f64; 2],
pub iridescence_thickness_map: Option<Texture>,
pub clearcoat_normal_map: Option<Texture>,
pub clearcoat_normal_scale: crate::math::Vector2,
pub transmission: f64,
pub transmission_map: Option<Texture>,
pub thickness: f64,
pub thickness_map: Option<Texture>,
pub attenuation_distance: f64,
pub attenuation_color: Color,
pub normal_node: Option<NodeRef>,
pub position_node: Option<NodeRef>,
pub scale_node: Option<NodeRef>,
pub rotation_node: Option<NodeRef>,
pub size_node: Option<NodeRef>,
pub size: f64,
pub rotation: f64,
pub linewidth: f64,
pub size_attenuation: bool,
pub vertex_node: Option<NodeRef>,
pub fragment_node: Option<NodeRef>,
pub output_node: Option<NodeRef>,
pub backdrop_node: Option<NodeRef>,
pub backdrop_alpha_node: Option<NodeRef>,
pub mrt_node: Option<crate::nodes::MrtNode>,
pub metalness_node: Option<NodeRef>,
pub roughness_node: Option<NodeRef>,
pub depth_node: Option<NodeRef>,
pub context_overrides: Option<crate::nodes::tsl::OverrideNodes>,
pub context_node: Option<crate::nodes::ContextValue>,
pub side: Side,
pub visible: bool,
pub transparent: bool,
pub force_single_pass: bool,
pub wireframe: bool,
pub blending: Blending,
pub premultiplied_alpha: bool,
pub alpha_to_coverage: bool,
pub world_units: bool,
pub blend_src: BlendFactor,
pub blend_dst: BlendFactor,
pub blend_equation: BlendEquation,
pub blend_src_alpha: Option<BlendFactor>,
pub blend_dst_alpha: Option<BlendFactor>,
pub blend_equation_alpha: Option<BlendEquation>,
pub depth_test: bool,
pub depth_write: bool,
pub depth_func: DepthFunc,
pub color_write: bool,
pub stencil_write: bool,
pub stencil_write_mask: u32,
pub stencil_func: StencilFunc,
pub stencil_ref: u32,
pub stencil_func_mask: u32,
pub stencil_fail: StencilOp,
pub stencil_z_fail: StencilOp,
pub stencil_z_pass: StencilOp,
pub name: &'static str,
}
impl Default for MeshBasicNodeMaterial {
fn default() -> Self {
Self {
id: MaterialId::next(),
version: 0,
kind: MaterialKind::Basic,
color: Color::new(1.0, 1.0, 1.0),
opacity: 1.0,
specular: Color::from_hex(0x111111),
shininess: 30.0,
emissive: Color::new(0.0, 0.0, 0.0),
emissive_intensity: 1.0,
flat_shading: false,
lights: false,
lights_node: None,
lighting_model: None,
environment_specular: true,
mask_node: None,
cast_shadow_node: None,
received_shadow_position_node: None,
fog: true,
metalness: 0.0,
roughness: 1.0,
map: None,
vertex_colors: false,
roughness_map: None,
metalness_map: None,
emissive_map: None,
gradient_map: None,
ao_map: None,
ao_map_intensity: 1.0,
light_map: None,
light_map_intensity: 1.0,
specular_map: None,
bump_map: None,
bump_scale: 1.0,
clearcoat: 0.0,
clearcoat_roughness: 0.0,
clearcoat_map: None,
clearcoat_roughness_map: None,
sheen: 0.0,
sheen_color: Color::new(0.0, 0.0, 0.0),
sheen_roughness: 1.0,
diffuse_roughness: 0.0,
ior: 1.5,
specular_intensity: 1.0,
specular_color: Color::new(1.0, 1.0, 1.0),
specular_node: None,
normal_map: None,
normal_scale: crate::math::Vector2::new(1.0, 1.0),
specular_color_map: None,
anisotropy: 0.0,
anisotropy_rotation: 0.0,
anisotropy_map: None,
iridescence: 0.0,
iridescence_ior: 1.3,
iridescence_thickness_range: [100.0, 400.0],
iridescence_thickness_map: None,
clearcoat_normal_map: None,
clearcoat_normal_scale: crate::math::Vector2::new(1.0, 1.0),
transmission: 0.0,
thickness: 0.0,
transmission_map: None,
thickness_map: None,
attenuation_distance: f64::INFINITY,
attenuation_color: Color::new(1.0, 1.0, 1.0),
normal_node: None,
position_node: None,
reflectivity: 1.0,
refraction_ratio: 0.98,
env_map: None,
pmrem_env: None,
color_node: None,
opacity_node: None,
alpha_test_node: None,
alpha_test: 0.0,
alpha_hash: false,
alpha_map: None,
emissive_node: None,
scale_node: None,
size_node: None,
size: 1.0,
rotation_node: None,
rotation: 0.0,
linewidth: 1.0,
size_attenuation: true,
vertex_node: None,
metalness_node: None,
roughness_node: None,
depth_node: None,
context_overrides: None,
context_node: None,
fragment_node: None,
output_node: None,
backdrop_node: None,
backdrop_alpha_node: None,
mrt_node: None,
side: Side::Front,
visible: true,
transparent: false,
force_single_pass: false,
wireframe: false,
blending: Blending::Normal,
premultiplied_alpha: false,
alpha_to_coverage: false,
world_units: false,
blend_src: BlendFactor::SrcAlpha,
blend_dst: BlendFactor::OneMinusSrcAlpha,
blend_equation: BlendEquation::Add,
blend_src_alpha: None,
blend_dst_alpha: None,
blend_equation_alpha: None,
depth_test: true,
depth_write: true,
depth_func: DepthFunc::LessEqual,
color_write: true,
stencil_write: false,
stencil_write_mask: 0xff,
stencil_func: StencilFunc::Always,
stencil_ref: 0,
stencil_func_mask: 0xff,
stencil_fail: StencilOp::Keep,
stencil_z_fail: StencilOp::Keep,
stencil_z_pass: StencilOp::Keep,
name: "",
}
}
}
impl MeshBasicNodeMaterial {
pub fn dispose(self) {}
pub fn new() -> Self {
Self::default()
}
pub fn unsupported_fields(&self) -> Vec<&'static str> {
self.kind
.table()
.loud
.iter()
.filter(|field| field.is_set(self))
.map(|field| field.name())
.collect()
}
pub(crate) fn accessor_only_fields(&self) -> Vec<&'static str> {
fields::ACCESSOR_ONLY
.iter()
.filter(|field| field.is_set(self))
.map(|field| field.name())
.collect()
}
pub fn check_supported(&self) -> Result<(), crate::Error> {
match self.unsupported_fields().first() {
Some(&field) => Err(crate::Error::Unsupported {
field,
kind: self.kind,
}),
None => Ok(()),
}
}
pub fn set_needs_update(&mut self) {
self.version += 1;
}
pub(crate) fn texture_sampler_key(&self) -> TextureSamplerKey<'_> {
TextureSamplerKey(self)
}
pub(crate) fn blend_mode(&self) -> BlendMode {
BlendMode {
blending: self.blending,
premultiplied_alpha: self.premultiplied_alpha,
blend_src: self.blend_src,
blend_dst: self.blend_dst,
blend_equation: self.blend_equation,
blend_src_alpha: self.blend_src_alpha,
blend_dst_alpha: self.blend_dst_alpha,
blend_equation_alpha: self.blend_equation_alpha,
}
}
pub(crate) fn blend_state(&self) -> Option<wgpu::BlendState> {
let mode = self.blend_mode();
if blending::needs_blend_state(&mode, self.transparent) {
blending::blending(&mode)
} else {
None
}
}
pub(crate) fn stencil_face(&self) -> Option<wgpu::StencilFaceState> {
if !self.stencil_write {
return None;
}
let op = |op: StencilOp| match op {
StencilOp::Zero => wgpu::StencilOperation::Zero,
StencilOp::Keep => wgpu::StencilOperation::Keep,
StencilOp::Replace => wgpu::StencilOperation::Replace,
StencilOp::Increment => wgpu::StencilOperation::IncrementClamp,
StencilOp::Decrement => wgpu::StencilOperation::DecrementClamp,
StencilOp::IncrementWrap => wgpu::StencilOperation::IncrementWrap,
StencilOp::DecrementWrap => wgpu::StencilOperation::DecrementWrap,
StencilOp::Invert => wgpu::StencilOperation::Invert,
};
Some(wgpu::StencilFaceState {
compare: match self.stencil_func {
StencilFunc::Never => wgpu::CompareFunction::Never,
StencilFunc::Less => wgpu::CompareFunction::Less,
StencilFunc::Equal => wgpu::CompareFunction::Equal,
StencilFunc::LessEqual => wgpu::CompareFunction::LessEqual,
StencilFunc::Greater => wgpu::CompareFunction::Greater,
StencilFunc::NotEqual => wgpu::CompareFunction::NotEqual,
StencilFunc::GreaterEqual => wgpu::CompareFunction::GreaterEqual,
StencilFunc::Always => wgpu::CompareFunction::Always,
},
fail_op: op(self.stencil_fail),
depth_fail_op: op(self.stencil_z_fail),
pass_op: op(self.stencil_z_pass),
})
}
pub(crate) fn in_transparent_list(&self) -> bool {
self.transparent || self.transmission > 0.0 || self.backdrop_node.is_some()
}
pub(crate) fn is_opaque(&self) -> bool {
!self.transparent && self.blending == Blending::Normal && !self.alpha_to_coverage
}
pub fn sprite() -> Self {
Self {
kind: MaterialKind::Sprite,
transparent: true,
refraction_ratio: 0.0,
..Self::default()
}
}
pub fn points() -> Self {
Self {
kind: MaterialKind::Points,
transparent: true,
refraction_ratio: 0.0,
..Self::default()
}
}
pub fn line(color: Color) -> Self {
Self {
color,
refraction_ratio: 0.0,
..Self::default()
}
}
pub fn line2(color: Color) -> Self {
Self {
kind: MaterialKind::Line2,
color,
blending: Blending::No,
alpha_to_coverage: true,
refraction_ratio: 0.0,
..Self::default()
}
}
pub fn normal() -> Self {
Self {
kind: MaterialKind::Normal,
refraction_ratio: 0.0,
..Self::default()
}
}
pub fn phong(color: Color) -> Self {
Self {
kind: MaterialKind::Phong,
color,
lights: true,
..Self::default()
}
}
pub fn lambert(color: Color) -> Self {
Self {
kind: MaterialKind::Lambert,
color,
lights: true,
..Self::default()
}
}
}
impl MeshBasicNodeMaterial {
pub fn toon(color: Color, gradient_map: Option<Texture>) -> Self {
Self {
kind: MaterialKind::Toon,
color,
gradient_map,
lights: true,
refraction_ratio: 0.0,
..Self::default()
}
}
pub fn standard(color: Color, roughness: f64, metalness: f64) -> Self {
Self {
kind: MaterialKind::Standard,
color,
roughness,
metalness,
lights: true,
refraction_ratio: 0.0,
..Self::default()
}
}
pub fn physical(color: Color, roughness: f64, metalness: f64) -> Self {
Self {
kind: MaterialKind::Physical,
..Self::standard(color, roughness, metalness)
}
}
}
pub type MeshPhongNodeMaterial = MeshBasicNodeMaterial;
pub type MeshLambertNodeMaterial = MeshBasicNodeMaterial;
pub type MeshToonNodeMaterial = MeshBasicNodeMaterial;
pub type SpriteNodeMaterial = MeshBasicNodeMaterial;
pub type PointsNodeMaterial = MeshBasicNodeMaterial;
pub type MeshStandardNodeMaterial = MeshBasicNodeMaterial;
pub type MeshPhysicalNodeMaterial = MeshBasicNodeMaterial;
pub type MeshNormalNodeMaterial = MeshBasicNodeMaterial;
pub type Line2NodeMaterial = MeshBasicNodeMaterial;
pub type LineBasicNodeMaterial = MeshBasicNodeMaterial;
pub(crate) struct TextureSamplerKey<'a>(&'a MeshBasicNodeMaterial);
impl std::hash::Hash for TextureSamplerKey<'_> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let m = self.0;
m.env_map
.as_ref()
.map(|t| {
let t = t.borrow();
(t.mapping, t.mag_filter, t.min_filter)
})
.hash(state);
for map in [
&m.alpha_map,
&m.map,
&m.roughness_map,
&m.metalness_map,
&m.emissive_map,
&m.gradient_map,
&m.ao_map,
&m.light_map,
&m.specular_map,
&m.bump_map,
&m.clearcoat_map,
&m.clearcoat_roughness_map,
&m.normal_map,
&m.specular_color_map,
&m.anisotropy_map,
&m.iridescence_thickness_map,
&m.clearcoat_normal_map,
&m.transmission_map,
&m.thickness_map,
] {
map.as_ref()
.map(|t| {
let t = t.borrow();
(t.mag_filter, t.min_filter, t.wrap_s, t.wrap_t)
})
.hash(state);
}
}
}