use super::{Light, LightShadow};
use crate::core::{Object3D, ObjectRef};
use crate::math::{Color, Matrix4, SphericalHarmonics3, Vector3};
use crate::objects::Payload;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum LightKind {
Ambient,
Point,
Spot,
Directional,
Hemisphere,
Probe,
}
pub struct LightObject {
pub light: Light,
pub kind: LightKind,
pub distance: f64,
pub decay: f64,
pub angle: f64,
pub penumbra: f64,
pub ground_color: Color,
pub sh: SphericalHarmonics3,
pub target: Option<ObjectRef>,
pub cast_shadow: bool,
pub shadow: Option<Box<LightShadow>>,
}
impl Clone for LightObject {
fn clone(&self) -> Self {
Self {
light: self.light.clone(),
kind: self.kind,
distance: self.distance,
decay: self.decay,
angle: self.angle,
penumbra: self.penumbra,
ground_color: self.ground_color,
sh: self.sh,
target: self.target.as_ref().map(|t| t.borrow().clone().into_node()),
cast_shadow: self.cast_shadow,
shadow: self.shadow.clone(),
}
}
}
impl LightObject {
fn base(kind: LightKind, color: Color, intensity: f64) -> Self {
Self {
light: Light::new(color, intensity),
kind,
distance: 0.0,
decay: 2.0,
angle: std::f64::consts::FRAC_PI_3,
penumbra: 0.0,
ground_color: Color::new(0.0, 0.0, 0.0),
sh: SphericalHarmonics3::default(),
target: None,
cast_shadow: false,
shadow: None,
}
}
pub(crate) fn color_intensity(&self) -> Color {
let c = self.light.color;
let i = self.light.intensity;
Color::new(c.r * i, c.g * i, c.b * i)
}
pub(crate) fn sh_intensity(&self) -> [[f32; 4]; 9] {
let i = self.light.intensity;
self.sh
.coefficients
.map(|c| [(c.x * i) as f32, (c.y * i) as f32, (c.z * i) as f32, 0.0])
}
pub(crate) fn cone_cos(&self) -> f64 {
self.angle.cos()
}
pub(crate) fn penumbra_cos(&self) -> f64 {
(self.angle * (1.0 - self.penumbra)).cos()
}
pub(crate) fn world_position(matrix_world: &Matrix4) -> Vector3 {
let mut v = Vector3::default();
v.set_from_matrix_position(matrix_world);
v
}
pub(crate) fn ground_color_intensity(&self) -> Color {
let c = self.ground_color;
let i = self.light.intensity;
Color::new(c.r * i, c.g * i, c.b * i)
}
pub fn set_power(&mut self, power: f64) {
self.light.intensity = power / (4.0 * std::f64::consts::PI);
}
pub fn power(&self) -> f64 {
self.light.intensity * 4.0 * std::f64::consts::PI
}
pub(crate) fn target_world_position(&self) -> Vector3 {
match &self.target {
Some(target) => {
let m = target.borrow().matrix_world;
Self::world_position(&m)
}
None => Vector3::new(0.0, 0.0, 0.0),
}
}
}
fn into_node(object_type: &'static str, light: LightObject) -> ObjectRef {
let object = Object3D {
object_type,
is_light: true,
payload: Payload::Light(light),
..Default::default()
};
object.into_node()
}
pub struct AmbientLight;
impl AmbientLight {
#[allow(clippy::new_ret_no_self)] pub fn new(color: Color, intensity: f64) -> ObjectRef {
into_node(
"AmbientLight",
LightObject::base(LightKind::Ambient, color, intensity),
)
}
}
pub struct PointLight;
impl PointLight {
#[allow(clippy::new_ret_no_self)] pub fn new(color: Color, intensity: f64, distance: f64) -> ObjectRef {
let mut light = LightObject::base(LightKind::Point, color, intensity);
light.distance = distance;
light.shadow = Some(Box::new(LightShadow::point()));
into_node("PointLight", light)
}
}
pub struct HemisphereLight;
impl HemisphereLight {
#[allow(clippy::new_ret_no_self)] pub fn new(sky_color: Color, ground_color: Color, intensity: f64) -> ObjectRef {
let mut light = LightObject::base(LightKind::Hemisphere, sky_color, intensity);
light.ground_color = ground_color;
let node = into_node("HemisphereLight", light);
node.borrow_mut().position.set(0.0, 1.0, 0.0);
node
}
}
pub struct SpotLight;
impl SpotLight {
#[allow(clippy::new_ret_no_self)] pub fn new(color: Color, intensity: f64) -> ObjectRef {
let mut light = LightObject::base(LightKind::Spot, color, intensity);
light.target = Some(Object3D::new_node());
light.shadow = Some(Box::new(LightShadow::spot()));
let node = into_node("SpotLight", light);
node.borrow_mut().position.set(0.0, 1.0, 0.0);
node
}
}
pub struct DirectionalLight;
impl DirectionalLight {
#[allow(clippy::new_ret_no_self)] pub fn new(color: Color, intensity: f64) -> ObjectRef {
let mut light = LightObject::base(LightKind::Directional, color, intensity);
light.target = Some(Object3D::new_node());
light.shadow = Some(Box::new(LightShadow::directional()));
let node = into_node("DirectionalLight", light);
node.borrow_mut().position.set(0.0, 1.0, 0.0);
node
}
}
pub struct LightProbe;
impl LightProbe {
#[allow(clippy::new_ret_no_self)] pub fn new(sh: SphericalHarmonics3, intensity: f64) -> ObjectRef {
let mut light = LightObject::base(LightKind::Probe, Color::default(), intensity);
light.sh = sh;
into_node("LightProbe", light)
}
pub fn copy(target: &ObjectRef, source: &ObjectRef) {
let (light, sh) = {
let source = source.borrow();
let source = source
.light()
.expect("three-rs: LightProbe::copy reads a light");
(source.light.clone(), source.sh)
};
let mut target = target.borrow_mut();
let target = target
.light_mut()
.expect("three-rs: LightProbe::copy writes a light");
target.light = light;
target.sh = sh;
}
}