pub const MATERIAL_PLUGIN_MAX_PARAMS: usize = 16;
pub const MATERIAL_PLUGIN_MAX_TEXTURES: usize = 8;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ParamKind {
F32,
Vec2,
Vec3,
Vec4,
}
impl ParamKind {
pub fn wgsl(self) -> &'static str {
match self {
ParamKind::F32 => "f32",
ParamKind::Vec2 => "vec2<f32>",
ParamKind::Vec3 => "vec3<f32>",
ParamKind::Vec4 => "vec4<f32>",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ParamValue {
F32(f32),
Vec2([f32; 2]),
Vec3([f32; 3]),
Vec4([f32; 4]),
Colour(crate::colour::Colour),
}
impl ParamValue {
pub fn fits(&self, kind: ParamKind) -> bool {
matches!(
(self, kind),
(ParamValue::F32(_), ParamKind::F32)
| (ParamValue::Vec2(_), ParamKind::Vec2)
| (ParamValue::Vec3(_), ParamKind::Vec3)
| (ParamValue::Vec4(_), ParamKind::Vec4)
| (ParamValue::Colour(_), ParamKind::Vec3 | ParamKind::Vec4)
)
}
pub fn to_vec4(&self) -> [f32; 4] {
match *self {
ParamValue::F32(x) => [x, 0.0, 0.0, 0.0],
ParamValue::Vec2([x, y]) => [x, y, 0.0, 0.0],
ParamValue::Vec3([x, y, z]) => [x, y, z, 0.0],
ParamValue::Vec4(v) => v,
ParamValue::Colour(c) => c.to_linear_rgba(),
}
}
}
impl From<f32> for ParamValue {
fn from(v: f32) -> Self {
ParamValue::F32(v)
}
}
impl From<[f32; 2]> for ParamValue {
fn from(v: [f32; 2]) -> Self {
ParamValue::Vec2(v)
}
}
impl From<[f32; 3]> for ParamValue {
fn from(v: [f32; 3]) -> Self {
ParamValue::Vec3(v)
}
}
impl From<[f32; 4]> for ParamValue {
fn from(v: [f32; 4]) -> Self {
ParamValue::Vec4(v)
}
}
impl From<crate::colour::Colour> for ParamValue {
fn from(c: crate::colour::Colour) -> Self {
ParamValue::Colour(c)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ParamDesc {
pub name: String,
pub kind: ParamKind,
pub default: ParamValue,
}
impl ParamDesc {
pub fn f32(name: impl Into<String>, default: f32) -> Self {
Self {
name: name.into(),
kind: ParamKind::F32,
default: ParamValue::F32(default),
}
}
pub fn vec2(name: impl Into<String>, default: [f32; 2]) -> Self {
Self {
name: name.into(),
kind: ParamKind::Vec2,
default: ParamValue::Vec2(default),
}
}
pub fn vec3(name: impl Into<String>, default: [f32; 3]) -> Self {
Self {
name: name.into(),
kind: ParamKind::Vec3,
default: ParamValue::Vec3(default),
}
}
pub fn vec4(name: impl Into<String>, default: [f32; 4]) -> Self {
Self {
name: name.into(),
kind: ParamKind::Vec4,
default: ParamValue::Vec4(default),
}
}
pub fn colour(name: impl Into<String>, default: crate::colour::Colour) -> Self {
Self {
name: name.into(),
kind: ParamKind::Vec3,
default: ParamValue::Colour(default),
}
}
}
pub trait MaterialPlugin {
fn name(&self) -> &str;
fn wgsl_body(&self) -> String;
fn needs_back_hemisphere(&self) -> bool {
false
}
fn texture_count(&self) -> u32 {
0
}
fn reads_vertex_attribute(&self) -> bool {
false
}
fn param_layout(&self) -> Vec<ParamDesc> {
Vec::new()
}
fn sampler(&self) -> crate::material::SamplerKey {
crate::material::SamplerKey::default()
}
}