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_clearcoat.rs at main · oneilltomhq/three-rs Expand file tree/ Copy pathwebgpu_clearcoat.rsMore file actions 308 lines (267 loc) · 11.1 KB / Copy pathwebgpu_clearcoat.rsFile metadata and controls308 lines (267 loc) · 11.1 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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 //! Port of `three.js/examples/webgpu_clearcoat.html`, calling the three-rs API //! in the same order the page's `init()`, its loader callback and `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. //! //! # What this page is //! //! Four `MeshPhysicalMaterial` spheres with `clearcoat = 1` in front of the //! Pisa HDR cube, which is both `scene.background` (the raw cube) and //! `scene.environment` (PMREM-filtered), and one `PointLight` riding on a //! small white sphere. The coat is the second specular lobe of //! `PhysicalLightingModel`: its indirect half reflects the PMREM, and its //! direct half — `clearcoatSpecularDirect` — is the sharp highlight a light //! makes on it, over a base lobe that is rougher or bumpier. Two of the //! spheres give the coat its own normal map (`clearcoatNormalMap`), so the //! coat and the base are lit about different normals. //! //! Under the pinned clock `timer` is 0 and the light sits at `( 0, 4, 3 )`, //! above the top edge of the frame: the particle is culled, but the light still //! reaches all four spheres. //! //! # The random sequence //! //! `FlakesTexture` draws 20000 values from `Math.random()` — five per flake — //! and is the only thing on the page that does apart from `new Inspector()`, //! whose five draws come first: the inspector is built in `init()`, and the //! flakes only in the HDR loader's callback, after `init()` has returned. //! [`FlakesTexture`] takes the harness' seeded sequence past those five. use three_rs::addons::controls::OrbitControls; use three_rs::addons::textures::FlakesTexture; use three_rs::geometries::sphere_geometry; use three_rs::lights::PointLight; use three_rs::loaders::HdrCubeTextureLoader; use three_rs::materials::ToneMapping; use three_rs::math::Vector2; use three_rs::nodes::pmrem_node::PmremEnvironment; use three_rs::objects::Background; use three_rs::testing::DeterministicRandom; use three_rs::textures::{Texture, Wrapping}; use three_rs::{ Color, ColorSpace, Group, Mesh, MeshBasicNodeMaterial, MeshPhysicalNodeMaterial, PerspectiveCamera, Renderer, RendererParameters, Scene, TextureLoader, }; use std::rc::Rc; pub const INNER_WIDTH: f64 = 800.0; pub const INNER_HEIGHT: f64 = 500.0; /// `renderer.setPixelRatio( window.devicePixelRatio )`. pub const DPR: f64 = 1.0; /// `Math.random()` draws `new Inspector()` makes — the same five /// `webgpu_mesh_batch` documents, from the `List` constructors of the /// inspector's tabs. pub const INSPECTOR_RANDOM_DRAWS: usize = 5; fn examples_dir() -> std::path::PathBuf { three_rs::testing::three_js_dir().join("examples") } pub struct App { pub renderer: Renderer, pub scene: Scene, pub camera: PerspectiveCamera, /// The page's `controls`. pub controls: OrbitControls, pub environment: PmremEnvironment, /// The page's `particleLight`. pub particle_light: three_rs::core::ObjectRef, /// The page's `group`. pub group: three_rs::core::ObjectRef, } pub fn init() -> App { let mut camera = PerspectiveCamera::new(27.0, INNER_WIDTH / INNER_HEIGHT, 0.25, 50.0); camera.node.borrow_mut().position.z = 10.0; let mut scene = Scene::new(); let group = Group::new(); scene.add(&group); // `renderer = new THREE.WebGPURenderer( { antialias: true } )`. The page // builds it after the loader call; here it is first, because the PMREM // below takes it. 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::AcesFilmic; renderer.tone_mapping_exposure = 1.25; // `renderer.inspector = new Inspector()`. let mut random = DeterministicRandom::new(); random.skip(INSPECTOR_RANDOM_DRAWS); // `new HDRCubeTextureLoader().setPath( 'textures/cube/pisaHDR/' ).load( // … )`, synchronous here, so the callback's body follows inline. let texture = HdrCubeTextureLoader::new() .set_path(examples_dir().join("textures/cube/pisaHDR/")) .load(["px.hdr", "nx.hdr", "py.hdr", "ny.hdr", "pz.hdr", "nz.hdr"]) .unwrap(); let geometry = Rc::new(sphere_geometry(0.8, 64, 32)); let texture_loader = TextureLoader::new(); let load = |path: &str| texture_loader.load(examples_dir().join(path)).unwrap(); let diffuse = load("textures/carbon/Carbon.png"); diffuse.set_color_space(ColorSpace::Srgb); diffuse.set_wrapping(Wrapping::Repeat, Wrapping::Repeat); diffuse.set_repeat(10.0, 10.0); let normal_map = load("textures/carbon/Carbon_Normal.png"); normal_map.set_wrapping(Wrapping::Repeat, Wrapping::Repeat); normal_map.set_repeat(10.0, 10.0); let normal_map2 = load("textures/water/Water_1_M_Normal.jpg"); // `new THREE.CanvasTexture( new FlakesTexture() )` — 512², drawing from // the page's `Math.random()`. let normal_map3 = Texture::new( 512, 512, Some(FlakesTexture::new(512, 512, || random.next())), ); normal_map3.set_wrapping(Wrapping::Repeat, Wrapping::Repeat); normal_map3.set_repeat(10.0, 6.0); normal_map3.set_anisotropy(16); let normal_map4 = load("textures/golfball.jpg"); let clearcoat_normal_map = load("textures/pbr/Scratched_gold/Scratched_gold_01_1K_Normal.png"); // `MeshPhysicalMaterial`'s defaults: white, roughness 1, metalness 0. let physical = || MeshPhysicalNodeMaterial::physical(Color::new(1.0, 1.0, 1.0), 1.0, 0.0); // car paint let mut material = MeshPhysicalNodeMaterial { clearcoat: 1.0, clearcoat_roughness: 0.1, metalness: 0.9, roughness: 0.5, color: Color::from_hex(0x0000ff), normal_map: Some(normal_map3), normal_scale: Vector2::new(0.15, 0.15), ..physical() }; let mesh = Mesh::new(geometry.clone(), material); mesh.borrow_mut().position.x = -1.0; mesh.borrow_mut().position.y = 1.0; group.add(&mesh); // fibers material = MeshPhysicalNodeMaterial { roughness: 0.5, clearcoat: 1.0, clearcoat_roughness: 0.1, map: Some(diffuse), normal_map: Some(normal_map), ..physical() }; let mesh = Mesh::new(geometry.clone(), material); mesh.borrow_mut().position.x = 1.0; mesh.borrow_mut().position.y = 1.0; group.add(&mesh); // golf material = MeshPhysicalNodeMaterial { metalness: 0.0, roughness: 0.1, clearcoat: 1.0, normal_map: Some(normal_map4), clearcoat_normal_map: Some(clearcoat_normal_map.clone()), // "y scale is negated to compensate for normal map handedness." clearcoat_normal_scale: Vector2::new(2.0, -2.0), ..physical() }; let mesh = Mesh::new(geometry.clone(), material); mesh.borrow_mut().position.x = -1.0; mesh.borrow_mut().position.y = -1.0; group.add(&mesh); // clearcoat + normalmap material = MeshPhysicalNodeMaterial { clearcoat: 1.0, metalness: 1.0, color: Color::from_hex(0xff0000), normal_map: Some(normal_map2), normal_scale: Vector2::new(0.15, 0.15), clearcoat_normal_map: Some(clearcoat_normal_map), clearcoat_normal_scale: Vector2::new(2.0, -2.0), ..physical() }; let mesh = Mesh::new(geometry, material); mesh.borrow_mut().position.x = 1.0; mesh.borrow_mut().position.y = -1.0; group.add(&mesh); // `scene.background = texture`: the raw HDR cube as the skybox. scene.background = Some(Background::CubeTexture(texture.clone())); // `scene.environment = texture`: the same cube, PMREM-filtered. let mut environment = PmremEnvironment::new(&texture); environment.update(&mut renderer).unwrap(); scene.environment = Some(environment.handle()); // LIGHTS let particle_light = Mesh::new( Rc::new(sphere_geometry(0.05, 8, 8)), MeshBasicNodeMaterial { color: Color::from_hex(0xffffff), ..MeshBasicNodeMaterial::default() }, ); scene.add(&particle_light); particle_light.add(&PointLight::new(Color::from_hex(0xffffff), 30.0, 0.0)); // EVENTS let mut controls = OrbitControls::new(&mut camera); // The renderer's canvas stands in for the element's `clientWidth` / // `clientHeight`. controls.set_element_size(INNER_WIDTH, INNER_HEIGHT); controls.min_distance = 3.0; controls.max_distance = 30.0; App { renderer, scene, camera, controls, environment, particle_light, group, } } /// The page's `animate()`, which is its `render()`. pub fn animate(app: &mut App) { // `Date.now() * 0.00025`. let timer = three_rs::utils::date_now_ms() * 0.00025; { let mut light = app.particle_light.borrow_mut(); light.position.x = (timer * 7.0).sin() * 3.0; light.position.y = (timer * 5.0).cos() * 4.0; light.position.z = (timer * 3.0).cos() * 3.0; } for child in app.group.children() { let mut child = child.borrow_mut(); let rotation = child.rotation; child.set_rotation(rotation.x, rotation.y + 0.005, rotation.z); } app.environment.update(&mut app.renderer).unwrap(); 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` when the /// page creates none — the signature is the same for every example so the /// viewer and the browser shell can drive any of them through one call. pub fn controls(app: &mut App) -> Option<&mut OrbitControls> { Some(&mut app.controls) } /// The controls and the camera at once, which every one of the controls' /// event handlers needs: the JS holds the camera as `this.object` and Rust /// cannot, so `pointer_move` and the rest take it as an argument. pub fn controls_and_camera(app: &mut App) -> Option<(&mut OrbitControls, &mut PerspectiveCamera)> { Some((&mut app.controls, &mut app.camera)) } 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_clearcoat.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. |