GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — three-rs/examples/webgpu_rtt.rs at main · oneilltomhq/three-rs

Expand file tree

/

webgpu_rtt.rs

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
166 lines (135 loc) · 5.58 KB
/

webgpu_rtt.rs

Copy path

File metadata and controls

166 lines (135 loc) · 5.58 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//! Port of `three.js/examples/webgpu_rtt.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.
//!
//! `TextureLoader.load()` is asynchronous on the page, but the harness only
//! fires its single RAF once the network is idle, so the image is always
//! present for the graded frame; here it is decoded synchronously.
//!
//! `renderer.inspector = new Inspector()` and `.toInspector( 'Scene Pass' )`
//! only register the node for the inspector panel — `toInspector()` returns the
//! node unchanged and changes nothing about the graded frame. Neither the
//! `mousemove` nor the `resize` listener fires, so `mouse` stays (0, 0).
use std::rc::Rc;
use three_rs::addons::controls::OrbitControls;
use three_rs::nodes::tsl::{hue, saturation, texture, uniform_value};
use three_rs::nodes::Type;
use three_rs::{
box_geometry, Color, Mesh, MeshBasicNodeMaterial, PerspectiveCamera, QuadMesh, RenderTarget,
Renderer, RendererParameters, Scene, Vector2,
};
pub const INNER_WIDTH: f64 = 800.0;
pub const INNER_HEIGHT: f64 = 500.0;
/// `const dpr = window.devicePixelRatio;`
pub const DPR: f64 = 1.0;
pub struct App {
pub renderer: Renderer,
pub scene: Scene,
pub camera: PerspectiveCamera,
pub quad_mesh: QuadMesh,
pub render_target: RenderTarget,
}
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(70.0, INNER_WIDTH / INNER_HEIGHT, 0.1, 10.0);
camera.node.borrow_mut().position.z = 3.0;
let mut scene = Scene::new();
scene.set_background(Color::from_hex(0x0066FF));
// textured mesh
let uv_texture = three_rs::TextureLoader::new()
.load(examples_dir().join("textures/uv_grid_opengl.jpg"))
.unwrap();
let geometry_box = Rc::new(box_geometry(1.0, 1.0, 1.0, 1, 1, 1));
let mut material_box = MeshBasicNodeMaterial::new();
material_box.color_node = Some(texture(&uv_texture));
//
let boxed = Mesh::new(geometry_box, material_box);
scene.add(&boxed);
//
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);
let render_target = RenderTarget::new((INNER_WIDTH * DPR) as u32, (INNER_HEIGHT * DPR) as u32);
// FX
// modulate the final color based on the mouse position
let mouse = Vector2::new(0.0, 0.0);
let screen_fx_node = uniform_value(Type::Vec2, vec![mouse.x, mouse.y]);
let mut material_fx = MeshBasicNodeMaterial::new();
let scene_pass_texture = texture(&render_target.texture());
material_fx.color_node = Some(hue(
saturation(scene_pass_texture.rgb(), screen_fx_node.x().one_minus()),
screen_fx_node.y(),
));
let quad_mesh = QuadMesh::new(material_fx);
App {
renderer,
scene,
camera,
quad_mesh,
render_target,
}
}
/// The page's `animate()`. `setAnimationLoop` runs it once under the harness,
/// so the box has been rotated exactly one step by the time the frame is read.
pub fn animate(app: &mut App) {
let boxed = app.scene.children()[0].clone();
let rotation = boxed.borrow().rotation;
boxed
.borrow_mut()
.set_rotation(rotation.x + 0.01, rotation.y + 0.02, rotation.z);
app.renderer
.set_render_target(Some(app.render_target.clone()));
app.renderer.render(&app.scene, &mut app.camera);
app.renderer.set_render_target(None);
app.renderer.render_quad(&app.quad_mesh);
}
/// 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.
///
/// The page resizes the render target as well as the canvas, at the device
/// pixel ratio it captured in `init()`:
/// `renderTarget.setSize( window.innerWidth * dpr, window.innerHeight * dpr )`.
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);
app.render_target
.set_size((width * DPR) as u32, (height * DPR) as u32);
}
/// 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_rtt.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.