use crate::nodes::node::Type;
use crate::nodes::pmrem_utils::roughness_to_mip;
use crate::nodes::tsl::{
bent_normal_view, camera_world_matrix, clearcoat_normal_view, clearcoat_roughness,
cube_texture_level, float, material_env_intensity, material_env_rotation, mix, normal_view,
normal_world, position_view_direction, reflect, roughness, transform_direction, vec4_join,
};
use crate::nodes::NodeRef;
use crate::textures::CubeTexture;
#[derive(Clone, Debug)]
pub struct PmremHandle {
pub texture: CubeTexture,
pub max_lod: NodeRef,
}
impl std::hash::Hash for PmremHandle {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.texture.id().hash(state);
}
}
impl PmremHandle {
pub fn sample(&self, uv: NodeRef, level: NodeRef) -> NodeRef {
let uv = if uv.ty() == Type::Vec4 {
uv
} else {
vec4_join(vec![uv, float(1.0)])
};
let direction = material_env_rotation().mul(uv);
cube_texture_level(
&self.texture,
direction,
roughness_to_mip(level, self.max_lod.clone()),
)
.xyz()
}
}
#[derive(Clone)]
pub struct EnvironmentNode {
id: u64,
build: std::rc::Rc<dyn Fn(NodeRef, NodeRef) -> NodeRef>,
}
impl EnvironmentNode {
pub fn new(build: impl Fn(NodeRef, NodeRef) -> NodeRef + 'static) -> Self {
static NEXT_ID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
Self {
id: NEXT_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
build: std::rc::Rc::new(build),
}
}
pub fn sample(&self, uv: NodeRef, level: NodeRef) -> NodeRef {
(self.build)(uv, level)
}
pub fn with_texture_level(&self, level: NodeRef) -> Self {
let inner = self.clone();
Self::new(move |uv, _| inner.sample(uv, level.clone()))
}
}
impl std::fmt::Debug for EnvironmentNode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("EnvironmentNode")
.field("id", &self.id)
.finish_non_exhaustive()
}
}
impl std::hash::Hash for EnvironmentNode {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
#[derive(Clone, Debug, Hash)]
#[doc(hidden)]
pub enum Environment {
Pmrem(PmremHandle),
Node(EnvironmentNode),
}
impl Environment {
pub fn sample(&self, uv: NodeRef, level: NodeRef) -> NodeRef {
match self {
Self::Pmrem(pmrem) => pmrem.sample(uv, level),
Self::Node(node) => node.sample(uv, level),
}
}
}
fn reflect_vector_of(normal: NodeRef) -> NodeRef {
reflect_vector_of_roughness(normal, roughness())
}
fn reflect_vector_of_roughness(normal: NodeRef, roughness_value: NodeRef) -> NodeRef {
let reflect_vec = reflect(position_view_direction().negate(), normal.clone());
let reflect_vec = mix(reflect_vec, normal, pow4(roughness_value)).normalize();
transform_direction(camera_world_matrix(), reflect_vec)
}
fn pow4(x: NodeRef) -> NodeRef {
x.clone().mul(x.clone()).mul(x.clone()).mul(x)
}
pub(crate) fn setup(env: &Environment, anisotropy: bool, clearcoat: bool, out: &mut Vec<NodeRef>) {
let radiance_prop = crate::nodes::tsl::radiance();
let ibl_prop = crate::nodes::tsl::ibl_irradiance();
out.push(radiance_prop.assign(crate::nodes::tsl::vec3(0.0, 0.0, 0.0)));
let radiance_normal = if anisotropy {
bent_normal_view()
} else {
normal_view()
};
let radiance = env
.sample(reflect_vector_of(radiance_normal), roughness())
.mul(material_env_intensity());
out.push(radiance_prop.assign(radiance_prop.add(radiance)));
out.push(ibl_prop.assign(crate::nodes::tsl::vec3(0.0, 0.0, 0.0)));
let irradiance = env
.sample(normal_world(), float(1.0))
.mul(float(std::f64::consts::PI))
.mul(material_env_intensity());
out.push(ibl_prop.assign(ibl_prop.add(irradiance)));
if clearcoat {
let clearcoat_prop = crate::nodes::tsl::clearcoat_radiance();
let radiance = env
.sample(
reflect_vector_of_roughness(clearcoat_normal_view(), clearcoat_roughness()),
clearcoat_roughness(),
)
.mul(material_env_intensity());
out.push(clearcoat_prop.assign(clearcoat_prop.add(radiance)));
}
}