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_depth_texture.html`, calling the three-rs
//! API in the same order the page's `init()` / `animate()` do.
//!
//! The page reads `window.innerWidth`, `window.innerHeight` and
//! `window.devicePixelRatio`. Under the e2e harness
//! (`test/e2e/puppeteer.js`) the viewport is `400 * 2` x `250 * 2` with no
//! `deviceScaleFactor`, so those are 800, 500 and 1.
use std::rc::Rc;
use three_rs::addons::controls::OrbitControls;
use three_rs::nodes::tsl;
use three_rs::testing::DeterministicRandom;
use three_rs::{
torus_knot_geometry, Color, DepthTexture, Mesh, MeshBasicNodeMaterial, PerspectiveCamera,
QuadMesh, RenderTarget, Renderer, RendererParameters, Scene, TextureType,
};
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: QuadMesh,
pub render_target: RenderTarget,
/// The page's `controls`.
pub controls: OrbitControls,
}
pub fn init() -> App {
let mut camera = PerspectiveCamera::new(70.0, INNER_WIDTH / INNER_HEIGHT, 1.0, 20.0);
camera.node.borrow_mut().position.z = 4.0;
let mut scene = Scene::new();
scene.set_background(Color::from_hex(0x222222));
scene.override_material = Some(MeshBasicNodeMaterial::new());
//
let geometry = Rc::new(torus_knot_geometry(1.0, 0.3, 128, 64, 2.0, 3.0));
let count = 50;
let scale = 5.0;
// `Math.random()` is the harness' deterministic sequence; the call order
// below (r, z, then three rotation values, per mesh) is what fixes the scene.
let mut random = DeterministicRandom::new();
for _ in 0..count {
let r = random.next() * 2.0 * std::f64::consts::PI;
let z = (random.next() * 2.0) - 1.0;
let z_scale = (1.0 - z * z).sqrt() * scale;
let mesh = Mesh::new(geometry.clone(), None);
{
let mut object = mesh.borrow_mut();
object
.position
.set(r.cos() * z_scale, r.sin() * z_scale, z * scale);
object.set_rotation(random.next(), random.next(), random.next());
}
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);
let depth_texture = DepthTexture::new();
depth_texture.set_type(TextureType::Float).unwrap();
let render_target = RenderTarget::new((INNER_WIDTH * DPR) as u32, (INNER_HEIGHT * DPR) as u32);
render_target.set_depth_texture(depth_texture.clone());
// FX
let mut material_fx = MeshBasicNodeMaterial::new();
// `materialFX.colorNode = texture( depthTexture )`
material_fx.color_node = Some(tsl::depth_texture(&depth_texture));
let quad = QuadMesh::new(material_fx);
//
// `controls = new OrbitControls( camera, renderer.domElement );` The
// constructor's own `update()` rebuilds the camera position from spherical
// coordinates — which gives back (0, 2.45e-16, 4), a y below f32
// precision — and then runs `object.lookAt( target )`.
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.enableDamping = true;` The page never calls `controls.update()`
// in `animate()`, so the damping never runs; that is the page's own
// inconsistency, transcribed as it stands.
controls.enable_damping = true;
App {
renderer,
scene,
camera,
quad,
render_target,
controls,
}
}
/// The page's `animate()`.
pub fn animate(app: &mut App) {
app.renderer
.set_render_target(Some(app.render_target.clone()));
app.renderer.render(&mut app.scene, &mut app.camera);
app.renderer.set_render_target(None);
app.renderer.render_quad(&app.quad);
}
/// 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 last line is the page's `renderTarget.setSize( window.innerWidth * dpr,
/// window.innerHeight * dpr )`: the target the scene pass draws into is sized
/// in device pixels, not CSS ones, as it was in `init()`.
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` 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.
///
/// They are two fields of the same `App`, so borrowing both is sound — but
/// only this module can say so; a host holding `&mut App` and calling
/// [`controls`] and then reaching for the camera cannot. Hence the pair.
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_depth_texture.png".to_string());
three_rs::testing::write_png(&path, width, height, &pixels);
println!("wrote {path} ({width}x{height})");
}