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_instance_mesh.rs at main · oneilltomhq/three-rs Expand file tree/ Copy pathwebgpu_instance_mesh.rsMore file actions 189 lines (154 loc) · 6.18 KB / Copy pathwebgpu_instance_mesh.rsFile metadata and controls189 lines (154 loc) · 6.18 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 //! Port of `three.js/examples/webgpu_instance_mesh.html`, calling the three-rs //! API in the same order the page's `init()` / `render()` 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; `Date.now()` is 0. //! //! The page's `loader.load().unwrap()` is asynchronous, but the harness only fires its //! single RAF once the network is idle, so the geometry is always in place for //! the graded frame; here it is loaded synchronously during `init()`. use std::rc::Rc; use three_rs::addons::controls::OrbitControls; use three_rs::materials::instanced_range; use three_rs::nodes::tsl::{float, mix, normal_world, osc_sine, time}; use three_rs::utils::date_now_ms; use three_rs::{ BufferGeometryLoader, Color, InstancedMesh, MeshBasicNodeMaterial, Object3D, 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; /// `const amount = parseInt( window.location.search.slice( 1 ) ) || 10;` — the /// harness loads the example with no query string. const AMOUNT: usize = 10; pub struct App { pub renderer: Renderer, pub scene: Scene, pub camera: PerspectiveCamera, } fn models_dir() -> std::path::PathBuf { let three = three_rs::testing::three_js_dir(); three.join("examples") } pub fn init() -> App { let count = AMOUNT.pow(3); let mut camera = PerspectiveCamera::new(60.0, INNER_WIDTH / INNER_HEIGHT, 0.1, 100.0); camera.node.borrow_mut().position.set( AMOUNT as f64 * 0.9, AMOUNT as f64 * 0.9, AMOUNT as f64 * 0.9, ); camera.look_at(&Vector3::ZERO); let scene = Scene::new(); let mut material = MeshBasicNodeMaterial::new(); // random colors between instances from 0x000000 to 0xFFFFFF // // const randomColors = range( new THREE.Color( 0x000000 ), // new THREE.Color( 0xFFFFFF ) ); // material.colorNode = mix( normalWorld, randomColors, // oscSine( time.mul( .1 ) ) ); let random_colors = instanced_range(Color::from_hex(0x000000), Color::from_hex(0xFFFFFF), count); material.color_node = Some(mix( normal_world(), random_colors.xyz(), osc_sine(time().mul(float(0.1))), )); let loader = BufferGeometryLoader::new(); let mut geometry = loader .load(models_dir().join("models/json/suzanne_buffergeometry.json")) .unwrap(); geometry.compute_vertex_normals(); geometry.scale(0.5, 0.5, 0.5); let mesh = InstancedMesh::new(Rc::new(geometry), material, count); scene.add(&mesh); // 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); App { renderer, scene, camera, } } /// The page's `animate()` / `render()`. pub fn animate(app: &mut App) { animate_cpu_only(app); 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) { 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 } /// The CPU half of `render()`, split out so tests can inspect it. pub fn animate_cpu_only(app: &mut App) { // `const time = Date.now() * 0.001;` let time = date_now_ms() * 0.001; for child in app.scene.children() { if !child.borrow().is_instanced_mesh() { continue; } let mut mesh = child.borrow_mut(); let rotation_z = mesh.rotation.z; mesh.set_rotation((time / 4.0).sin(), (time / 2.0).sin(), rotation_z); let mut dummy = Object3D::default(); let mut i = 0usize; let offset = (AMOUNT as f64 - 1.0) / 2.0; for x in 0..AMOUNT { for y in 0..AMOUNT { for z in 0..AMOUNT { dummy .position .set(offset - x as f64, offset - y as f64, offset - z as f64); let rotation_y = (x as f64 / 4.0 + time).sin() + (y as f64 / 4.0 + time).sin() + (z as f64 / 4.0 + time).sin(); // `dummy.rotation.y = …; dummy.rotation.z = dummy.rotation.y * 2;` // `rotation.x` is never assigned and stays 0. dummy.set_rotation(dummy.rotation.x, rotation_y, rotation_y * 2.0); dummy.update_matrix(); mesh.set_matrix_at(i, &dummy.matrix); i += 1; } } } } } 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_instance_mesh.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. |