use std::cell::RefCell;
use std::rc::Rc;
use three_rs::addons::controls::OrbitControls;
use three_rs::geometries::plane_geometry;
use three_rs::loaders::GltfLoader;
use three_rs::materials::Side;
use three_rs::nodes::display::{
barrel_uv, bayer_dither, circle, color_bleeding, retro_pass, scanlines, vignette,
RetroPassNode, RetroPassOptions,
};
use three_rs::nodes::tsl::{
block, call, float, floor, fract, inline_fn, length, mix, mod_, normal_world, position_local,
posterize, replace_default_uv, rotate_uv_about, screen_size, smoothstep, step, texture_uv,
time, uniform_value, uv, vec2, vec2_join, vec3, vec4_join,
};
use three_rs::nodes::{NodeRef, Type};
use three_rs::objects::Background;
use three_rs::textures::Wrapping;
use three_rs::{
AmbientLight, Color, DirectionalLight, Mesh, MeshBasicNodeMaterial, PerspectiveCamera,
PointLight, RenderPipeline, Renderer, RendererParameters, Scene, TextureLoader,
};
pub const INNER_WIDTH: f64 = 800.0;
pub const INNER_HEIGHT: f64 = 500.0;
pub const DPR: f64 = 1.0;
fn examples_dir() -> std::path::PathBuf {
three_rs::testing::three_js_dir().join("examples")
}
pub struct App {
pub renderer: Renderer,
pub scene: Rc<RefCell<Scene>>,
pub camera: Rc<RefCell<PerspectiveCamera>>,
pub retro: RetroPassNode,
pub render_pipeline: RenderPipeline,
pub controls: OrbitControls,
}
fn ps1_background() -> NodeRef {
let flipped_y = normal_world().y().negate();
let sky_uv = flipped_y.mul(0.5).add(0.5);
let top_color: NodeRef = Color::from_hex(0x000033).into(); let mid_color: NodeRef = Color::from_hex(0x330066).into(); let horizon_color: NodeRef = Color::from_hex(0x663322).into();
let sky_gradient = mix(
horizon_color,
mix(mid_color, top_color, smoothstep(0.4, 0.9, sky_uv.clone())),
smoothstep(0.0, 0.4, sky_uv),
);
let longitude = normal_world().x().atan2(normal_world().z());
let latitude = flipped_y.asin();
let star_scale = float(50.0);
let star_uv = vec2_join(vec![
longitude.mul(star_scale.clone()),
latitude.mul(star_scale),
]);
let star_cell = floor(star_uv.clone());
let cell_hash = fract(
three_rs::nodes::tsl::dot(star_cell, vec2(12.9898, 78.233))
.sin()
.mul(43758.5453),
);
let cell_uv = fract(star_uv);
let to_center = cell_uv.sub(0.5);
let dist_to_center = length(to_center.clone());
let core = smoothstep(float(0.08), float(0.0), dist_to_center.clone());
let glow = smoothstep(float(0.25), float(0.0), dist_to_center).mul(0.4);
let cross_x = smoothstep(float(0.15), float(0.0), to_center.x().abs()).mul(smoothstep(
float(0.4),
float(0.0),
to_center.y().abs(),
));
let cross_y = smoothstep(float(0.15), float(0.0), to_center.y().abs()).mul(smoothstep(
float(0.4),
float(0.0),
to_center.x().abs(),
));
let cross = cross_x.add(cross_y).mul(0.3);
let star_shape = core.add(glow).add(cross);
let is_star = step(0.85, cell_hash.clone());
let above_horizon = smoothstep(float(-0.2), float(0.1), flipped_y);
let star_intensity = is_star
.mul(above_horizon)
.mul(star_shape)
.mul(cell_hash.clone().mul(0.6).add(0.4));
let star_color = mix(vec3(1.0, 1.0, 0.95), vec3(0.8, 0.9, 1.0), cell_hash);
mix(sky_gradient, star_color, star_intensity.clamp(0.0, 1.0))
}
pub fn init() -> App {
let camera = PerspectiveCamera::new(25.0, INNER_WIDTH / INNER_HEIGHT, 0.1, 100.0);
camera.node.borrow_mut().position.set(8.0, 5.0, 20.0);
let mut scene = Scene::new();
scene.background = Some(Background::Node(call(
&inline_fn(0, Type::Vec3, |_| ps1_background()),
Vec::new(),
)));
let gltf =
GltfLoader::load(examples_dir().join("models/gltf/coffeeMug.glb")).expect("coffeeMug.glb");
gltf.scene.borrow_mut().position.set(0.0, 0.0, 0.0);
scene.add(&gltf.scene);
let ambient_light = AmbientLight::new(Color::from_hex(0x404040), 2.0);
scene.add(&ambient_light);
let directional_light = DirectionalLight::new(Color::from_hex(0xffffff), 3.0);
directional_light.borrow_mut().position.set(5.0, 10.0, 5.0);
scene.add(&directional_light);
let point_light = PointLight::new(Color::from_hex(0xff6600), 5.0, 20.0);
point_light.borrow_mut().position.set(-3.0, 3.0, 2.0);
scene.add(&point_light);
let mut smoke_geometry = plane_geometry(1.0, 1.0, 16, 64);
smoke_geometry.translate(0.0, 0.5, 0.0);
smoke_geometry.scale(1.5, 6.0, 1.5);
let noise_texture = TextureLoader::new()
.load(examples_dir().join("textures/noises/perlin/128x128.png"))
.expect("perlin/128x128.png");
noise_texture.set_wrapping(Wrapping::Repeat, Wrapping::Repeat);
let mut smoke_material = MeshBasicNodeMaterial::new();
smoke_material.transparent = true;
smoke_material.side = Side::Double;
smoke_material.depth_write = false;
smoke_material.position_node = Some({
let twist_noise_uv = vec2_join(vec![
float(0.5),
mod_(uv().y().mul(0.2).sub(time().mul(0.005)), float(1.0)),
]);
let twist = texture_uv(&noise_texture, twist_noise_uv).x().mul(10.0);
let assign_twist = position_local().xz().assign(rotate_uv_about(
position_local().xz(),
twist,
vec2(0.0, 0.0),
));
let wind_offset = vec2_join(vec![
texture_uv(
&noise_texture,
mod_(vec2_join(vec![float(0.25), time().mul(0.01)]), float(1.0)),
)
.x()
.sub(0.5),
texture_uv(
&noise_texture,
mod_(vec2_join(vec![float(0.75), time().mul(0.01)]), float(1.0)),
)
.x()
.sub(0.5),
])
.mul(uv().y().pow(2.0).mul(10.0));
let add_wind = position_local().add_assign(wind_offset);
block(vec![assign_twist, add_wind], position_local())
});
smoke_material.color_node = Some({
let alpha_noise_uv = uv()
.mul(vec2(0.5, 0.3))
.add(vec2_join(vec![float(0.0), time().mul(0.03).negate()]));
let alpha = smoothstep(0.4, 1.0, texture_uv(&noise_texture, alpha_noise_uv).x())
.mul(smoothstep(0.0, 0.1, uv().x()))
.mul(smoothstep(0.0, 0.1, uv().x().one_minus()))
.mul(smoothstep(0.0, 0.1, uv().y()))
.mul(smoothstep(0.0, 0.1, uv().y().one_minus()));
let final_color = mix(vec3(0.6, 0.3, 0.2), vec3(1.0, 1.0, 1.0), alpha.pow(3.0));
vec4_join(vec![final_color, alpha])
});
let smoke = Mesh::new(Rc::new(smoke_geometry), smoke_material);
smoke.borrow_mut().position.y = 1.83;
scene.add(&smoke);
let mut parameters = RendererParameters::default();
parameters.antialias = true;
let mut renderer = Renderer::new(parameters).unwrap();
renderer.set_pixel_ratio(DPR);
renderer.set_size(INNER_WIDTH, INNER_HEIGHT);
let color_depth_steps = uniform_value(Type::F32, vec![32.0]);
let scanline_intensity = uniform_value(Type::F32, vec![0.3]); let scanline_density = uniform_value(Type::F32, vec![1.0]); let scanline_speed = uniform_value(Type::F32, vec![0.0]); let vignette_intensity = uniform_value(Type::F32, vec![0.3]); let bleeding = uniform_value(Type::F32, vec![0.001]); let curvature = uniform_value(Type::F32, vec![0.02]); let affine_distortion = uniform_value(Type::F32, vec![0.0]);
let mut render_pipeline = RenderPipeline::new();
let scene = Rc::new(RefCell::new(scene));
let camera = Rc::new(RefCell::new(camera));
let distorted_uv = barrel_uv(curvature.clone(), uv());
let distorted_delta = circle(curvature.add(0.1).mul(10.0), float(1.0), uv())
.mul(curvature.clone())
.mul(0.05);
let retro = retro_pass(
scene.clone(),
camera.clone(),
RetroPassOptions::default().affine_distortion(affine_distortion),
);
let mut retro_pipeline = retro.node();
retro_pipeline = replace_default_uv(distorted_uv, retro_pipeline);
retro_pipeline = color_bleeding(retro_pipeline, bleeding.add(distorted_delta));
retro_pipeline = bayer_dither(retro_pipeline, color_depth_steps.clone());
retro_pipeline = posterize(retro_pipeline, color_depth_steps);
retro_pipeline = vignette(retro_pipeline, vignette_intensity, float(0.6), uv());
retro_pipeline = scanlines(
retro_pipeline,
scanline_intensity,
screen_size().y().mul(scanline_density),
scanline_speed,
uv(),
);
render_pipeline.output_node = Some(retro_pipeline);
let mut controls = OrbitControls::new(&mut camera.borrow_mut());
controls.set_element_size(INNER_WIDTH, INNER_HEIGHT);
controls.enable_damping = true;
controls.min_distance = 0.1;
controls.max_distance = 50.0;
controls.target.y = 1.0;
App {
renderer,
scene,
camera,
retro,
render_pipeline,
controls,
}
}
pub fn animate(app: &mut App) {
app.controls.update(&mut app.camera.borrow_mut(), None);
app.render_pipeline.render(&mut app.renderer);
}
pub fn resize(app: &mut App, width: f64, height: f64) {
{
let mut camera = app.camera.borrow_mut();
camera.aspect = width / height;
camera.update_projection_matrix();
}
app.renderer.set_size(width, height);
}
pub fn controls(app: &mut App) -> Option<&mut OrbitControls> {
Some(&mut app.controls)
}
pub fn controls_and_camera(
app: &mut App,
) -> Option<(&mut OrbitControls, std::cell::RefMut<'_, PerspectiveCamera>)> {
Some((&mut app.controls, app.camera.borrow_mut()))
}
fn main() {
three_rs::testing::pin_time(Some(0.0));
let mut app = init();
println!("adapter: {:?}", app.renderer.adapter_info());
animate(&mut app);
let (width, height, pixels) = app.renderer.read_canvas_pixels().unwrap();
let path = std::env::args()
.nth(1)
.unwrap_or_else(|| "target/webgpu_postprocessing_retro.png".to_string());
three_rs::testing::write_png(&path, width, height, &pixels);
println!("wrote {path} ({width}x{height})");
}