use crate::core::{BufferGeometry, ObjectRef};
use crate::materials::MeshBasicNodeMaterial;
use crate::math::{Color, Vector3};
use crate::nodes::node::SettableValue;
use crate::nodes::reflector_node::{reflector, ReflectorNode, ReflectorParameters};
use crate::nodes::tsl::{
block, camera_position, dot, float, length, max, mix, position_world, reflect, texture_with_uv,
time, to_const, to_var, uniform_settable, vec2, vec2_join,
};
use crate::nodes::{NodeRef, Type};
use crate::objects::Mesh;
use crate::textures::Texture;
use std::rc::Rc;
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct WaterMeshOptions {
pub resolution_scale: f64,
pub water_normals: Texture,
pub alpha: f64,
pub size: f64,
pub sun_color: Color,
pub sun_direction: Vector3,
pub water_color: Color,
pub distortion_scale: f64,
}
impl WaterMeshOptions {
pub fn new(water_normals: Texture) -> Self {
Self {
resolution_scale: 0.5,
water_normals,
alpha: 1.0,
size: 1.0,
sun_color: Color::from_hex(0xffffff),
sun_direction: Vector3::new(0.70707, 0.70707, 0.0),
water_color: Color::from_hex(0x7f7f7f),
distortion_scale: 20.0,
}
}
}
pub struct WaterMesh {
pub mesh: ObjectRef,
pub resolution_scale: f64,
pub water_normals: Texture,
pub alpha: SettableValue,
pub size: SettableValue,
pub sun_color: SettableValue,
pub sun_direction: SettableValue,
pub water_color: SettableValue,
pub distortion_scale: SettableValue,
pub mirror_sampler: ReflectorNode,
}
struct Uniforms {
alpha: NodeRef,
size: NodeRef,
sun_color: NodeRef,
sun_direction: NodeRef,
water_color: NodeRef,
distortion_scale: NodeRef,
}
impl WaterMesh {
pub fn new(geometry: Rc<BufferGeometry>, options: WaterMeshOptions) -> Self {
let color = |c: Color| vec![c.r, c.g, c.b];
let v = &options.sun_direction;
let (alpha, alpha_cell) = uniform_settable(Type::F32, vec![options.alpha]);
let (size, size_cell) = uniform_settable(Type::F32, vec![options.size]);
let (sun_color, sun_color_cell) = uniform_settable(Type::Vec3, color(options.sun_color));
let (sun_direction, sun_direction_cell) = uniform_settable(Type::Vec3, vec![v.x, v.y, v.z]);
let (water_color, water_color_cell) =
uniform_settable(Type::Vec3, color(options.water_color));
let (distortion_scale, distortion_scale_cell) =
uniform_settable(Type::F32, vec![options.distortion_scale]);
let uniforms = Uniforms {
alpha,
size,
sun_color,
sun_direction,
water_color,
distortion_scale,
};
let mut mirror_sampler = reflector(ReflectorParameters {
resolution_scale: options.resolution_scale,
..ReflectorParameters::default()
});
let material = Self::material(&uniforms, &options.water_normals, &mut mirror_sampler);
let mesh = Mesh::new(geometry, material);
mirror_sampler.add_target_on_setup(&mesh);
Self {
mesh,
resolution_scale: options.resolution_scale,
water_normals: options.water_normals,
alpha: alpha_cell,
size: size_cell,
sun_color: sun_color_cell,
sun_direction: sun_direction_cell,
water_color: water_color_cell,
distortion_scale: distortion_scale_cell,
mirror_sampler,
}
}
fn material(
u: &Uniforms,
water_normals: &Texture,
mirror_sampler: &mut ReflectorNode,
) -> MeshBasicNodeMaterial {
let noise = get_noise(
water_normals,
to_const(None, position_world().xz().mul(u.size.clone())),
);
let surface_normal = to_const(
None,
noise.swizzle("xzy").mul(1.5).mul(1.0).mul(1.5).normalize(),
);
let world_to_eye = to_const(None, camera_position().sub(position_world()));
let eye_direction = to_const(None, world_to_eye.normalize());
let reflection = reflect(u.sun_direction.negate(), surface_normal.clone()).normalize();
let direction = max(0.0, dot(eye_direction.clone(), reflection));
let specular_light = direction.pow(100.0).mul(u.sun_color.clone()).mul(2.0);
let diffuse_light = max(dot(u.sun_direction.clone(), surface_normal.clone()), 0.0)
.mul(u.sun_color.clone())
.mul(0.5);
let distance = length(world_to_eye);
let distortion = surface_normal
.xz()
.mul(float(0.001).add(float(1.0).div(distance)))
.mul(u.distortion_scale.clone());
let mut material = MeshBasicNodeMaterial::new();
material.transparent = true;
material.opacity_node = Some(u.alpha.clone());
material.received_shadow_position_node = Some(position_world().add(distortion.clone()));
mirror_sampler.uv_node = mirror_sampler.uv_node.add(distortion);
let theta = max(dot(eye_direction.clone(), surface_normal.clone()), 0.0);
let rf0 = float(0.02);
let reflectance = float(1.0)
.sub(theta)
.pow(5.0)
.mul(float(1.0).sub(rf0.clone()))
.add(rf0);
let scatter = max(0.0, dot(surface_normal, eye_direction)).mul(u.water_color.clone());
let albedo = mix(
u.sun_color.mul(diffuse_light).mul(0.3).add(scatter),
mirror_sampler.node().rgb().add(specular_light),
reflectance,
);
material.color_node = Some(albedo);
material
}
}
fn get_noise(water_normals: &Texture, uv: NodeRef) -> NodeRef {
let offset = time();
let uv0 = to_var(
None,
uv.div(103.0)
.add(vec2_join(vec![offset.div(17.0), offset.div(29.0)])),
);
let uv1 = to_var(
None,
uv.div(107.0)
.sub(vec2_join(vec![offset.div(-19.0), offset.div(31.0)])),
);
let uv2 = to_var(
None,
uv.div(vec2(8907.0, 9803.0))
.add(vec2_join(vec![offset.div(101.0), offset.div(97.0)])),
);
let uv3 = to_var(
None,
uv.div(vec2(1091.0, 1027.0))
.sub(vec2_join(vec![offset.div(109.0), offset.div(-113.0)])),
);
let sample0 = texture_with_uv(water_normals, uv0.clone());
let sample1 = texture_with_uv(water_normals, uv1.clone());
let sample2 = texture_with_uv(water_normals, uv2.clone());
let sample3 = texture_with_uv(water_normals, uv3.clone());
let noise = sample0.add(sample1).add(sample2).add(sample3);
block(vec![uv0, uv1, uv2, uv3], noise.mul(0.5).sub(1.0))
}