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_materials_basic.rs at main · oneilltomhq/three-rs Expand file tree/ Copy pathwebgpu_materials_basic.rsMore file actions 168 lines (141 loc) · 5.86 KB / Copy pathwebgpu_materials_basic.rsFile metadata and controls168 lines (141 loc) · 5.86 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 //! Port of `three.js/examples/webgpu_materials_basic.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 `Date.now()` is 0. //! //! `CubeTextureLoader.load()` is asynchronous on the page, but the harness only //! fires its single RAF once the network is idle, so the six faces are always //! present for the graded frame; here they are decoded synchronously. //! //! The example's GUI (`renderer.inspector.createParameters()`) only registers //! `onChange` callbacks, so it changes nothing about the graded frame. use std::rc::Rc; use three_rs::addons::controls::OrbitControls; use three_rs::testing::DeterministicRandom; use three_rs::utils::date_now_ms; use three_rs::{ sphere_geometry, Color, CubeTextureLoader, Mesh, MeshBasicNodeMaterial, PerspectiveCamera, Renderer, RendererParameters, Scene, Vector3, }; pub const INNER_WIDTH: f64 = 800.0; pub const INNER_HEIGHT: f64 = 500.0; /// `renderer.setPixelRatio( window.devicePixelRatio )` pub const DPR: f64 = 1.0; pub struct App { pub renderer: Renderer, pub scene: Scene, pub camera: PerspectiveCamera, } fn examples_dir() -> std::path::PathBuf { let three = three_rs::testing::three_js_dir(); three.join("examples") } pub fn init() -> App { let camera = PerspectiveCamera::new(60.0, INNER_WIDTH / INNER_HEIGHT, 0.01, 100.0); camera.node.borrow_mut().position.z = 3.0; let path = examples_dir().join("textures/cube/pisa"); let format = "png"; let urls = [ path.join(format!("px.{format}")), path.join(format!("nx.{format}")), path.join(format!("py.{format}")), path.join(format!("ny.{format}")), path.join(format!("pz.{format}")), path.join(format!("nz.{format}")), ]; let texture_cube = CubeTextureLoader::new().load(urls).unwrap(); let mut scene = Scene::new(); scene.set_background(texture_cube.clone()); let geometry = Rc::new(sphere_geometry(0.1, 32, 16)); let mut material = MeshBasicNodeMaterial::new(); material.color = Color::from_hex(0xffffff); material.env_map = Some(texture_cube); // `Math.random()` is the harness' deterministic sequence: four draws per // mesh, in the order x, y, z, scale. let mut random = DeterministicRandom::new(); for _ in 0..500 { let mesh = Mesh::new(geometry.clone(), material.clone()); { let mut object = mesh.borrow_mut(); object.position.x = random.next() * 10.0 - 5.0; object.position.y = random.next() * 10.0 - 5.0; object.position.z = random.next() * 10.0 - 5.0; let scale = random.next() * 3.0 + 1.0; object.scale.set(scale, scale, scale); } scene.add(&mesh); } // let mut renderer = Renderer::new(RendererParameters::default()).unwrap(); renderer.set_pixel_ratio(DPR); renderer.set_size(INNER_WIDTH, INNER_HEIGHT); App { renderer, scene, camera, } } /// The page's `animate()`. pub fn animate(app: &mut App) { // `const timer = 0.0001 * Date.now();` let timer = 0.0001 * date_now_ms(); // `mouseX` / `mouseY` are 0 with no pointer events, and the camera starts at // x = y = 0, so both of these are no-ops. { let mut camera_object = app.camera.node.borrow_mut(); let position = &mut camera_object.position; position.x += (0.0 - position.x) * 0.05; position.y += (0.0 - position.y) * 0.05; } app.camera.look_at(&Vector3::ZERO); // `for ( let i = 0 … spheres.length )` — the scene holds nothing but the // spheres, so `scene.children` *is* the page's `spheres` array. for (i, sphere) in app.scene.children().iter().enumerate() { let mut sphere = sphere.borrow_mut(); sphere.position.x = 5.0 * (timer + i as f64).cos(); sphere.position.y = 5.0 * (timer + i as f64 * 1.1).sin(); } app.renderer.render(&app.scene, &mut app.camera); } /// 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) { // The page also sets its module-level `windowHalfX` / `windowHalfY` here. // They exist only to recentre the `pointermove` handler's `mouseX` / // `mouseY`, which this port has no equivalent of, so there is nothing // here to update. app.camera.aspect = width / height; app.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_materials_basic.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. |