GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — three-rs/examples/webgpu_postprocessing_radial_blur.rs at main · oneilltomhq/three-rs Expand file tree/ Copy pathwebgpu_postprocessing_radial_blur.rsMore file actions 234 lines (195 loc) · 8.57 KB / Copy pathwebgpu_postprocessing_radial_blur.rsFile metadata and controls234 lines (195 loc) · 8.57 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 //! Port of `three.js/examples/webgpu_postprocessing_radial_blur.html`, calling //! the three-rs API in the same order the page's `init()` / `animate()` do. //! //! Under the e2e harness the viewport is `400 * 2` x `250 * 2` with no //! `deviceScaleFactor`, so `window.innerWidth` / `innerHeight` / //! `devicePixelRatio` are 800, 500 and 1, and `performance.now()` is pinned to //! 0 — so `timer.getDelta()` is 0 and `group.rotation.y` stays at 0 for the //! graded frame. //! //! `Math.random` is the harness's seeded sequence //! ([`three_rs::testing::DeterministicRandom`]). The page //! draws from it seven times per instance, in this order: `position.x`, //! `position.y`, `position.z`, `scale`, `rotation.x`, `rotation.y`, //! `rotation.z` — 700 draws, and every one of them has to land in the same //! place or the tetrahedra move. Nothing in the page touches `Math.random` //! before the loop; `new Inspector()` runs after it and only registers the //! renderer with the inspector panel, so it changes nothing in the frame. //! //! `renderer.toneMapping = NeutralToneMapping` is set *before* the //! `RenderPipeline` is constructed, and `RenderPipeline._update()` captures it, //! so the Khronos PBR Neutral tone mapper ends up inside the post-processing //! quad's own shader rather than in a second output pass. See //! `docs/postprocessing.md`. use std::cell::RefCell; use std::rc::Rc; use three_rs::addons::controls::OrbitControls; use three_rs::geometries::tetrahedron_geometry; use three_rs::math::ColorSpace; use three_rs::nodes::display::{radial_blur, RadialBlurOptions}; use three_rs::nodes::tsl::uniform_value; use three_rs::nodes::Type; use three_rs::testing::DeterministicRandom; use three_rs::Timer; use three_rs::{ pass, Color, Group, HemisphereLight, InstancedMesh, MeshStandardNodeMaterial, Object3D, PassNode, PerspectiveCamera, PointLight, RenderPipeline, Renderer, RendererParameters, Scene, ToneMapping, Vector2, }; pub const INNER_WIDTH: f64 = 800.0; pub const INNER_HEIGHT: f64 = 500.0; /// `renderer.setPixelRatio( window.devicePixelRatio )`. pub const DPR: f64 = 1.0; /// `new THREE.InstancedMesh( geometry, material, 100 )`. const COUNT: usize = 100; pub struct App { pub renderer: Renderer, /// Shared with `scene_pass`, which renders it from `updateBefore()`. pub scene: Rc<RefCell<Scene>>, /// Shared with `scene_pass`. pub camera: Rc<RefCell<PerspectiveCamera>>, pub group: three_rs::ObjectRef, /// The page's module-level `timer`. pub timer: Timer, pub scene_pass: PassNode, pub render_pipeline: RenderPipeline, } pub fn init() -> App { let camera = PerspectiveCamera::new(45.0, INNER_WIDTH / INNER_HEIGHT, 0.1, 200.0); camera.node.borrow_mut().position.z = 50.0; let mut scene = Scene::new(); scene.set_background(Color::from_hex(0x000000)); // let hemi_light = HemisphereLight::new(Color::from_hex(0xffffff), Color::from_hex(0x8d8d8d), 1.0); hemi_light.borrow_mut().position.set(0.0, 1000.0, 0.0); scene.add(&hemi_light); let point_light = PointLight::new(Color::from_hex(0xffffff), 1000.0, 0.0); point_light.borrow_mut().position.set(0.0, 0.0, 0.0); scene.add(&point_light); // let group = Group::new(); let geometry = tetrahedron_geometry(1.0, 0); let mut material = MeshStandardNodeMaterial::standard(Color::from_hex(0xffffff), 1.0, 0.0); material.flat_shading = true; let mesh = InstancedMesh::new(Rc::new(geometry), material, COUNT); let mut dummy = Object3D::default(); let mut col = Color::new(1.0, 1.0, 1.0); let mut center = Vector2::default(); let mut random = DeterministicRandom::new(); for i in 0..COUNT { dummy.position.x = random.next() * 50.0 - 25.0; dummy.position.y = random.next() * 50.0 - 25.0; dummy.position.z = random.next() * 50.0 - 25.0; center.set(dummy.position.x, dummy.position.y); // make sure tetrahedrons are not positioned at the origin if center.length() < 6.0 { center.normalize().multiply_scalar(6.0); dummy.position.x = center.x; dummy.position.y = center.y; } dummy.scale.set_scalar(random.next() * 2.0 + 1.0); dummy.set_rotation( random.next() * std::f64::consts::PI, random.next() * std::f64::consts::PI, random.next() * std::f64::consts::PI, ); dummy.update_matrix(); mesh.borrow_mut().set_matrix_at(i, &dummy.matrix); col.set_hsl( 0.55 + (i as f64 / COUNT as f64) * 0.15, 1.0, 0.2, ColorSpace::LinearSrgb, ); mesh.borrow_mut().set_color_at(i, &col); } group.add(&mesh); scene.add(&group); let mut renderer = Renderer::new(RendererParameters::default()).unwrap(); renderer.set_pixel_ratio(DPR); renderer.set_size(INNER_WIDTH, INNER_HEIGHT); renderer.tone_mapping = ToneMapping::Neutral; // post processing // `pass( scene, camera )` — the pass holds both, and the renderer renders // it the first time a draw samples its texture (`docs/nodes.md` §57). let scene = Rc::new(RefCell::new(scene)); let camera = Rc::new(RefCell::new(camera)); let scene_pass = pass(scene.clone(), camera.clone()); // `uniform( float( 0.9 ) )` and friends. `uniform( int( 32 ) )` is an // `int` *node* wrapped in a uniform, and `UniformNode` takes its type from // the JavaScript value it is given, which is a node — so three.js falls // back to `float` and the dump carries four `f32` object uniforms. The // loop bound is `i32( count )` in the shader, not an `i32` uniform. let mut options = RadialBlurOptions::default(); options.weight = uniform_value(Type::F32, vec![0.9]); options.decay = uniform_value(Type::F32, vec![0.95]); options.exposure = uniform_value(Type::F32, vec![5.0]); options.count = uniform_value(Type::F32, vec![32.0]); let blur_pass = radial_blur(&scene_pass.texture(), &options); let mut render_pipeline = RenderPipeline::new(); render_pipeline.output_node = Some(blur_pass); App { // `const timer = new THREE.Timer();` — constructed in `init()`, as the // page does, so its `_startTime` is the moment the scene was built. timer: Timer::new(), renderer, scene, camera, group, scene_pass, render_pipeline, } } /// The page's `animate()`, run once by the harness's single RAF. pub fn animate(app: &mut App) { // `timer.update(); const delta = timer.getDelta();`, then the rotation // under `params.animated`, which defaults to true. app.timer.update(); let delta = app.timer.get_delta(); { let mut group = app.group.borrow_mut(); let rotation = group.rotation; group.set_rotation(rotation.x, rotation.y + delta * 0.1, rotation.z); } // `renderPipeline.render()`: the output quad's draw runs the scene pass's // `updateBefore()` first (`docs/nodes.md` §57). app.render_pipeline.render(&mut app.renderer); } /// The page's `onWindowResize()`. /// /// The rung harness never calls this — the graded frame is always /// 800 x 500 — but the viewer and the browser shell do, so the example /// owns its own reaction to a resized canvas instead of the host /// guessing at one. 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); } /// The example's controls, for a host that has a pointer. `None` here: /// the page creates none. pub fn controls(_app: &mut App) -> Option<&mut OrbitControls> { None } /// The controls and the camera at once, for a host delivering pointer events. /// `None` here: the page creates no controls. pub fn controls_and_camera(_app: &mut App) -> Option<(&mut OrbitControls, &mut PerspectiveCamera)> { None } fn main() { // Pin both clocks to zero, as three.js' `test/e2e/deterministic-injection.js` // does to the page, so that the frame this writes is the frame the rung // grades no matter how long `init()` took. 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_radial_blur.png".to_string()); three_rs::testing::write_png(&path, width, height, &pixels); println!("wrote {path} ({width}x{height})"); } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |