use std::cell::RefCell;
use std::rc::Rc;
use crate::cameras::{PerspectiveCamera, RenderCamera};
use crate::lights::LightKind;
use crate::materials::MeshBasicNodeMaterial;
use crate::math::{Color, Vector3};
use crate::nodes::frame::register_texture_update;
use crate::nodes::node::{LiveValue, SettableValue, Type, UniformGroup, UniformSource};
use crate::nodes::tsl::{
block, boolean, cube_depth_texture_compare, distance, dot, exp, float, frag_coord,
get_view_position, if_else, if_then, int, interleaved_gradient_noise, loop_n, max, mix,
pass_depth_texture_uv, round, texture_uv, to_const, to_var, uniform, uniform_array_live,
uniform_settable, uv, vec2_join, vec4, vec4_join, view_z_to_perspective_depth, UniformArray,
};
use crate::nodes::{NodeRef, NodeUpdate, NodeUpdateType};
use crate::objects::QuadMesh;
use crate::renderer::{RenderTarget, RenderTargetOptions, Renderer};
use crate::textures::{CubeDepthTexture, DepthTexture, Texture, TextureFilter, TextureType};
const DIRECTIONS: [[f64; 3]; 6] = [
[1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, -1.0],
];
pub fn godrays(
depth: &DepthTexture,
camera: Rc<RefCell<PerspectiveCamera>>,
light: &crate::core::ObjectRef,
) -> GodraysNode {
GodraysNode::new(depth, camera, light)
}
pub struct GodraysNode(Rc<GodraysState>);
pub(crate) struct GodraysState {
depth: DepthTexture,
camera: Rc<RefCell<PerspectiveCamera>>,
light: crate::core::ObjectRef,
target: RenderTarget,
quad: QuadMesh,
resolution_scale: f64,
camera_matrix_world: SettableValue,
camera_projection_matrix_inverse: SettableValue,
camera_position: SettableValue,
f_normals: Rc<RefCell<Vec<f64>>>,
f_constants: Rc<RefCell<Vec<f64>>>,
node: NodeRef,
raymarch_steps: SettableValue,
density: SettableValue,
max_density: SettableValue,
distance_attenuation: SettableValue,
}
struct Inputs<'a> {
depth: &'a DepthTexture,
shadow_map: &'a CubeDepthTexture,
camera_matrix_world: NodeRef,
camera_projection_matrix_inverse: NodeRef,
camera_position: NodeRef,
shadow_camera_near: NodeRef,
shadow_camera_far: NodeRef,
light_position: NodeRef,
f_normals: UniformArray,
f_constants: UniformArray,
raymarch_steps: NodeRef,
density: NodeRef,
max_density: NodeRef,
distance_attenuation: NodeRef,
}
fn shadow_camera_reference(light: &crate::core::ObjectRef, far: bool) -> NodeRef {
let light = light.downgrade();
uniform(
UniformSource::Live(LiveValue::new(move || {
let light = light
.upgrade()
.expect("three-rs: a GodraysNode outlived its light");
let object = light.borrow();
let shadow = object
.light()
.and_then(|light| light.shadow.as_ref())
.expect("three-rs: godrays() wants a shadow-casting light");
vec![if far {
shadow.camera.far()
} else {
shadow.camera.near()
}]
})),
Type::F32,
UniformGroup::Object,
None,
)
}
fn light_position(light: &crate::core::ObjectRef) -> NodeRef {
let light = light.downgrade();
uniform(
UniformSource::Live(LiveValue::new(move || {
let light = light
.upgrade()
.expect("three-rs: a GodraysNode outlived its light");
let e = light.borrow().matrix_world.elements;
vec![e[12], e[13], e[14]]
})),
Type::Vec3,
UniformGroup::Render,
None,
)
}
impl GodraysNode {
pub fn new(
depth: &DepthTexture,
camera: Rc<RefCell<PerspectiveCamera>>,
light: &crate::core::ObjectRef,
) -> Self {
let shadow_map = {
let mut object = light.borrow_mut();
let light = object
.light_mut()
.expect("three-rs: godrays() wants a light");
assert!(
light.kind == LightKind::Point,
"three-rs: GodraysNode supports point lights only (the directional branch is not ported)"
);
light
.shadow
.as_mut()
.expect("three-rs: godrays() wants a shadow-casting light")
.point_depth_texture()
};
let target = RenderTarget::new_with_options(
1,
1,
RenderTargetOptions {
texture_type: TextureType::UnsignedByte,
samples: 0,
depth_buffer: false,
min_filter: TextureFilter::Linear,
mag_filter: TextureFilter::Linear,
},
)
.expect("three-rs: the godrays target is a colour type");
let identity = crate::math::Matrix4::identity().elements.to_vec();
let (camera_matrix_world, camera_matrix_world_value) =
uniform_settable(Type::Mat4, identity.clone());
let (camera_projection_matrix_inverse, camera_projection_matrix_inverse_value) =
uniform_settable(Type::Mat4, identity);
let (camera_position, camera_position_value) =
uniform_settable(Type::Vec3, vec![0.0, 0.0, 0.0]);
let (raymarch_steps, raymarch_steps_value) = uniform_settable(Type::F32, vec![60.0]);
let (density, density_value) = uniform_settable(Type::F32, vec![0.7]);
let (max_density, max_density_value) = uniform_settable(Type::F32, vec![0.5]);
let (distance_attenuation, distance_attenuation_value) =
uniform_settable(Type::F32, vec![2.0]);
let f_normals_values = Rc::new(RefCell::new(vec![0.0; 24]));
let f_constants_values = Rc::new(RefCell::new(vec![0.0; 24]));
let f_normals = {
let values = f_normals_values.clone();
uniform_array_live(6, move || values.borrow().clone())
};
let f_constants = {
let values = f_constants_values.clone();
uniform_array_live(6, move || values.borrow().clone())
};
let fragment = godrays_fragment(&Inputs {
depth,
shadow_map: &shadow_map,
camera_matrix_world,
camera_projection_matrix_inverse,
camera_position,
shadow_camera_near: shadow_camera_reference(light, false),
shadow_camera_far: shadow_camera_reference(light, true),
light_position: light_position(light),
f_normals,
f_constants,
raymarch_steps,
density,
max_density,
distance_attenuation,
});
let mut material = MeshBasicNodeMaterial::new();
material.name = "Godrays";
material.fragment_node = Some(fragment);
let node = to_var(None, texture_uv(&target.texture(), uv()));
let state = Rc::new(GodraysState {
depth: depth.clone(),
camera,
light: light.clone(),
target,
quad: QuadMesh::new(material),
resolution_scale: 0.5,
camera_matrix_world: camera_matrix_world_value,
camera_projection_matrix_inverse: camera_projection_matrix_inverse_value,
camera_position: camera_position_value,
f_normals: f_normals_values,
f_constants: f_constants_values,
node,
raymarch_steps: raymarch_steps_value,
density: density_value,
max_density: max_density_value,
distance_attenuation: distance_attenuation_value,
});
register_texture_update(state.target.texture().id(), &state);
Self(state)
}
pub fn node(&self) -> NodeRef {
self.0.node.clone()
}
pub fn texture(&self) -> Texture {
self.0.target.texture()
}
pub fn raymarch_steps(&self) -> &SettableValue {
&self.0.raymarch_steps
}
pub fn density(&self) -> &SettableValue {
&self.0.density
}
pub fn max_density(&self) -> &SettableValue {
&self.0.max_density
}
pub fn distance_attenuation(&self) -> &SettableValue {
&self.0.distance_attenuation
}
#[doc(hidden)]
pub fn quad_material(&self) -> &MeshBasicNodeMaterial {
&self.0.quad.material
}
pub fn set_size(&self, width: u32, height: u32) {
self.0.set_size(width, height);
}
}
impl GodraysState {
fn set_size(&self, width: u32, height: u32) {
let scale = |size: u32| ((self.resolution_scale * size as f64).round() as u32).max(1);
self.target.set_size(scale(width), scale(height));
}
fn update_light_params(&self) {
let object = self.light.borrow();
let position = object.position;
let far = object
.light()
.and_then(|light| light.shadow.as_ref())
.expect("three-rs: godrays() wants a shadow-casting light")
.camera
.far();
let mut normals = self.f_normals.borrow_mut();
let mut constants = self.f_constants.borrow_mut();
for (i, d) in DIRECTIONS.iter().enumerate() {
let normal = Vector3::new(d[0], d[1], d[2]);
let mut point = position;
point.add_scaled_vector(&normal, far);
let constant = -point.dot(&normal);
normals[i * 4..i * 4 + 4].copy_from_slice(&[d[0], d[1], d[2], 0.0]);
constants[i * 4..i * 4 + 4].copy_from_slice(&[constant, 0.0, 0.0, 0.0]);
}
}
}
impl NodeUpdate for GodraysState {
fn update_before_type(&self) -> NodeUpdateType {
NodeUpdateType::Frame
}
fn update_before(&self, renderer: &mut Renderer) -> bool {
if let Some(pass) = crate::nodes::frame::texture_update(self.depth.id()) {
renderer.update_before_node(&pass);
}
let mut renderer = renderer.reset_state();
let (width, height) = renderer.drawing_buffer_size();
self.set_size(width, height);
self.update_light_params();
{
let camera = self.camera.borrow();
let world = RenderCamera::matrix_world(&*camera);
self.camera_matrix_world.set(world.elements.to_vec());
self.camera_projection_matrix_inverse
.set(camera.projection_matrix_inverse.elements.to_vec());
let e = world.elements;
self.camera_position.set(vec![e[12], e[13], e[14]]);
}
renderer.set_clear_color(Color::new(1.0, 1.0, 1.0), 1.0);
renderer.set_render_target(Some(self.target.clone()));
renderer.render_quad(&self.quad);
drop(renderer);
true
}
}
fn godrays_fragment(inputs: &Inputs<'_>) -> NodeRef {
let uv_node = uv();
let camera_position = inputs.camera_position.clone();
let f_normals = &inputs.f_normals;
let f_constants = &inputs.f_constants;
let sd_plane = |p: NodeRef, i: &NodeRef| {
dot(p, f_normals.element_xyz(i.clone())).add(f_constants.element_x(i.clone()))
};
let intersect_ray_plane = |origin: NodeRef, direction: NodeRef, i: &NodeRef| {
let denom = dot(f_normals.element_xyz(i.clone()), direction);
sd_plane(origin, i).div(denom).negate()
};
let output = to_var(None, vec4(0.0, 0.0, 0.0, 1.0));
let is_early_out = to_var(None, boolean(false));
let depth = to_const(None, pass_depth_texture_uv(inputs.depth, uv_node.clone()));
let view_position = to_const(
None,
get_view_position(
uv_node,
depth.clone(),
inputs.camera_projection_matrix_inverse.clone(),
),
);
let world_position = to_var(
None,
inputs
.camera_matrix_world
.clone()
.mul(view_position.clone()),
);
let in_box_dist = to_var(None, float(-10000.0));
let in_box = loop_n("i", int(6), |i| {
vec![in_box_dist.assign(max(
in_box_dist.clone(),
sd_plane(camera_position.clone(), i),
))]
});
let start_position = to_var(None, camera_position.clone());
let clip_end = loop_n("i", int(6), |i| {
let direction = to_const(None, world_position.sub(camera_position.clone()));
let t = intersect_ray_plane(camera_position.clone(), direction.clone(), i);
vec![if_then(
sd_plane(world_position.clone(), i).greater_than(float(0.0)),
vec![
direction.clone(),
world_position.assign(camera_position.clone().add(t.mul(direction))),
],
)]
});
let direction = to_const(None, world_position.sub(camera_position.clone()));
let min_t = to_var(None, float(10000.0));
let entry = loop_n("i", int(6), |i| {
let t = intersect_ray_plane(camera_position.clone(), direction.clone(), i);
vec![if_then(
t.less_than(min_t.clone()).and(t.greater_than(float(0.0))),
vec![min_t.assign(t)],
)]
});
let end_in_box_dist = to_var(None, float(-10000.0));
let end_in_box = loop_n("i", int(6), |i| {
vec![end_in_box_dist.assign(max(
end_in_box_dist.clone(),
sd_plane(world_position.clone(), i),
))]
});
let min_t2 = to_var(None, float(10000.0));
let exit = loop_n("i", int(6), |i| {
let t = intersect_ray_plane(start_position.clone(), direction.clone(), i);
vec![if_then(
sd_plane(world_position.clone(), i).greater_than(float(0.0)),
vec![if_then(
t.less_than(min_t2.clone()).and(t.greater_than(float(0.0))),
vec![min_t2.assign(t)],
)],
)]
});
let clip_exit = if_then(
end_in_box_dist.greater_than_equal(float(0.0)),
vec![
min_t2.clone(),
exit,
if_then(
min_t2.less_than(distance(world_position.clone(), start_position.clone())),
vec![world_position.assign(
start_position
.clone()
.add(min_t2.clone().mul(direction.clone())),
)],
),
],
);
let found_entry = if_else(
min_t.equal(float(10000.0)),
vec![is_early_out.assign(boolean(true))],
vec![
start_position.assign(
camera_position
.clone()
.add(min_t.clone().add(float(0.001)).mul(direction.clone())),
),
end_in_box_dist.clone(),
end_in_box,
clip_exit,
],
);
let clip = if_else(
in_box_dist.less_than(float(0.0)),
vec![clip_end],
vec![direction.clone(), min_t.clone(), entry, found_entry],
);
let illum = to_var(None, float(0.0));
let noise = to_const(None, interleaved_gradient_noise(frag_coord().xy()));
let steps = inputs.raymarch_steps.clone();
let samples_float = to_const(
None,
round(
steps
.clone()
.add(steps.div(float(8.0)).add(float(2.0)).mul(noise.clone())),
),
);
let samples = to_const(None, samples_float.to(Type::U32));
let march = loop_n("i", samples.clone(), |i| {
let sample_pos = to_const(
None,
mix(
start_position.clone(),
world_position.clone(),
i.to(Type::F32).div(samples_float.clone()),
),
);
let light_to_pos = to_const(None, sample_pos.sub(inputs.light_position.clone()));
let shadow_position_abs = to_const(None, light_to_pos.abs());
let view_z = shadow_position_abs
.x()
.max(shadow_position_abs.y())
.max(shadow_position_abs.z())
.negate();
let shadow_depth = view_z_to_perspective_depth(
view_z.clone(),
inputs.shadow_camera_near.clone(),
inputs.shadow_camera_far.clone(),
);
let result = cube_depth_texture_compare(inputs.shadow_map, light_to_pos, shadow_depth);
let shadow_info = vec2_join(vec![result.one_minus().add(float(0.005)), view_z.negate()]);
let shadow_amount = to_const(None, shadow_info.x().one_minus());
vec![illum.add_assign(
shadow_amount
.mul(
distance(start_position.clone(), world_position.clone())
.mul(inputs.density.clone().div(float(100.0))),
)
.mul(
shadow_info
.y()
.div(inputs.shadow_camera_far.clone())
.one_minus()
.pow(inputs.distance_attenuation.clone()),
),
)]
});
let shade = if_then(
is_early_out.equal(boolean(false)),
vec![
illum.clone(),
noise,
samples_float.clone(),
samples,
march,
illum.div_assign(samples_float),
output.assign(vec4_join(vec![
exp(illum.negate())
.one_minus()
.clamp(float(0.0), inputs.max_density.clone())
.to(Type::Vec3),
depth.clone(),
])),
],
);
block(
vec![
output.clone(),
is_early_out,
depth,
view_position,
world_position,
in_box_dist,
in_box,
start_position,
clip,
shade,
],
output,
)
}