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_sobel.rs at main · oneilltomhq/three-rs Expand file tree/ Copy pathwebgpu_postprocessing_sobel.rsMore file actions 178 lines (155 loc) · 6.72 KB / Copy pathwebgpu_postprocessing_sobel.rsFile metadata and controls178 lines (155 loc) · 6.72 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 //! Port of `three.js/examples/webgpu_postprocessing_sobel.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. Nothing in the page moves with time: //! `controls.update()` with damping and no pointer input only aims the camera //! at `( 0, 0.5, 0 )`. //! //! The page's output is `sobel( renderOutput( scenePass ) )`: //! `SobelOperatorNode` calls `convertToTexture()` on its input, so the tone //! mapped, sRGB-encoded scene is drawn into an `RTTNode` first and the //! operator reads nine taps of that. `outputColorTransform = false` keeps the //! pipeline's own quad from encoding it again. use std::cell::RefCell; use std::rc::Rc; use three_rs::addons::controls::OrbitControls; use three_rs::loaders::GltfLoader; use three_rs::materials::{render_output, ToneMapping}; use three_rs::nodes::display::{convert_to_texture, sobel, RttNode, SobelOperatorNode}; use three_rs::nodes::pmrem_node::PmremEnvironment; use three_rs::{ pass, Color, MeshStandardNodeMaterial, PassNode, PerspectiveCamera, RenderPipeline, Renderer, RendererParameters, RoomEnvironment, Scene, }; pub const INNER_WIDTH: f64 = 800.0; pub const INNER_HEIGHT: f64 = 500.0; /// `renderer.setPixelRatio( window.devicePixelRatio )`. 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, /// Shared with `scene_pass`, which renders it from `updateBefore()`. pub scene: Rc<RefCell<Scene>>, /// Shared with `scene_pass`. pub camera: Rc<RefCell<PerspectiveCamera>>, /// The page's `controls`. pub controls: OrbitControls, pub scene_pass: PassNode, /// The `RTTNode` `SobelOperatorNode.setup()` makes of `renderOutput( /// scenePass )`. pub sobel_input: RttNode, pub sobel: SobelOperatorNode, pub render_pipeline: RenderPipeline, } pub fn init() -> App { let mut scene = Scene::new(); scene.set_background(Color::from_hex(0x000000)); let mut camera = PerspectiveCamera::new(70.0, INNER_WIDTH / INNER_HEIGHT, 0.1, 100.0); camera.node.borrow_mut().position.set(0.0, 1.0, 3.0); // `loader.loadAsync( 'models/gltf/DragonAttenuation.glb' )`, then // `gltf.scene.children[ 1 ]` — the dragon, the only node the page keeps — // with a default `MeshStandardNodeMaterial` in place of its transmissive // one. let gltf = GltfLoader::load(examples_dir().join("models/gltf/DragonAttenuation.glb")) .expect("DragonAttenuation.glb"); let model = gltf.scene.children()[1].clone(); model .borrow_mut() .mesh_mut() .expect("the dragon is a mesh") .material = Some(MeshStandardNodeMaterial::standard( Color::from_hex(0xffffff), 1.0, 0.0, )); scene.add(&model); 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); renderer.tone_mapping = ToneMapping::Linear; // `scene.environment = pmremGenerator.fromScene( environment, 0.04 ).texture`. let mut room = RoomEnvironment::new(); let environment = PmremEnvironment::from_scene(&mut renderer, &mut room, 0.04).unwrap(); scene.environment = Some(environment.handle()); let mut controls = OrbitControls::new(&mut camera); // The canvas the example renders at, standing in for the element's // `clientWidth` / `clientHeight`. controls.set_element_size(INNER_WIDTH, INNER_HEIGHT); controls.enable_damping = true; controls.enable_zoom = false; controls.target.set(0.0, 0.5, 0.0); controls.update(&mut camera, None); // postprocessing let mut render_pipeline = RenderPipeline::new(); render_pipeline.output_color_transform = false; // `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()); let sobel_input = convert_to_texture(render_output(scene_pass.node(), renderer.tone_mapping)); let sobel = sobel(&sobel_input.texture()); render_pipeline.output_node = Some(sobel.node()); App { renderer, scene, camera, controls, scene_pass, sobel_input, sobel, render_pipeline, } } /// The page's `animate()` with `params.enabled` true: `controls.update()`, /// then `renderPipeline.render()`, whose output quad's draw runs the scene /// pass's and the RTT's `updateBefore()` first (`docs/nodes.md` §57). /// /// `SobelOperatorNode` is not itself a `NodeUpdate` node — `sobel.update()` /// stays a hand call — so its `invSize` uniform, unlike the RTT's own resize, /// is not covered by that automatic pass. The RTT's target follows the /// drawing buffer regardless of when it is actually drawn, so this sizes it /// explicitly first rather than reading a texture that has not resized yet. pub fn animate(app: &mut App) { app.controls.update(&mut app.camera.borrow_mut(), None); let (width, height) = app.renderer.drawing_buffer_size(); app.sobel_input.set_size(width, height); // `SobelOperatorNode.updateBefore()`, after the RTT has its size. app.sobel.update(); app.render_pipeline.render(&mut app.renderer); } /// The page's `onWindowResize()`. 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. pub fn controls(app: &mut App) -> Option<&mut OrbitControls> { Some(&mut app.controls) } /// The controls and the camera at once; see `webgpu_postprocessing_ca`. 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_sobel.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. |