three-rs 0.3.0

A port of three.js core and its WebGPURenderer to Rust on wgpu, graded against three.js's own end-to-end pixel comparison.
Documentation
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Port of `three.js/examples/jsm/tsl/display/SSAONode.js` — screen space
//! ambient occlusion with a Vogel disk and a depth-aware blur.
//!
//! Every frame the node draws one full-screen quad into its AO target: for
//! each pixel it unprojects the scene depth to a view position, takes
//! `samples` points of a Vogel disk of `radius` in the view-aligned plane
//! through it (rotated per pixel by interleaved gradient noise), projects
//! each to the screen and reads one depth there. A sample counts as an
//! occluder by how far above the tangent plane it sits (normalised
//! obscurance, minus `bias`), faded with distance. Then, with the blur on,
//! [`depth_aware_blur`] runs horizontally (AO into the blur target) and
//! vertically (back into the AO target), so the texture downstream always
//! reads the AO target.
//!
//! Three's node is a `Node` with `updateBeforeType = FRAME`; so is this one
//! (its state implements [`NodeUpdate`] and is registered as the updater of
//! the AO texture, the way `passTexture( this, … )` makes it one in three).
//! The `samples` uniform is the loop's end, as in three: changing it does
//! not rebuild anything.
//!
//! Three swaps one blur material's `_blurInput.value` between the two blur
//! draws; the port has two blur materials, one per input texture, each with
//! its own direction uniform, and both build the same WGSL. Not ported: a
//! logarithmic depth buffer (the port has none), and the `RedFormat`
//! targets, which are `rgba8unorm` read through `.x` as in
//! [`GtaoNode`](super::GtaoNode).

use std::cell::{Cell, RefCell};
use std::rc::{Rc, Weak};

use crate::cameras::PerspectiveCamera;
use crate::materials::MeshBasicNodeMaterial;
use crate::math::Color;
use crate::nodes::frame::register_texture_update;
use crate::nodes::node::{LiveValue, SettableValue, Type, UniformGroup, UniformSource};
use crate::nodes::tsl::{
    block, depth_aware_blur, discard, dot, float, get_screen_position_from_clip, get_view_position,
    if_then, int, interleaved_gradient_noise, loop_options, max, pass_depth_texture_uv, pi2,
    screen_coordinate, texture_uv, texture_with_uv, to_var, uniform, uniform_settable, uv,
    vec4_join, vogel_disk_sample,
};
use crate::nodes::{NodeRef, NodeUpdate, NodeUpdateType};
use crate::objects::QuadMesh;
use crate::renderer::{RenderTarget, RenderTargetOptions, Renderer};
use crate::textures::{DepthTexture, Texture, TextureFilter, TextureType};

use super::SampleFn;

/// `ssao( depthNode, normalNode, camera )`.
///
/// `depth` is the (pre-)pass's depth attachment, `normal` the sampler of the
/// view normal — three's `normalNode.sample` (the AO page's is `( uv ) =>
/// unpackRGBToNormal( prePass.getTextureNode().sample( uv ) )`) — and
/// `camera` the pass's.
pub fn ssao(
    depth: &DepthTexture,
    normal: SampleFn,
    camera: &Rc<RefCell<PerspectiveCamera>>,
) -> SsaoNode {
    SsaoNode::new(depth, normal, camera)
}

/// `SSAONode` — see the module docs. Keep it alive for as long as the graph
/// reads [`node`](Self::node): the renderer reaches it through its texture,
/// weakly.
pub struct SsaoNode {
    /// `this.radius` — the view-space radius of the sample disk, and the
    /// distance scale of the falloff and of the blur's depth rejection.
    /// `0.5` by default.
    pub radius: SettableValue,
    /// `this.intensity` — the occlusion's multiplier, `1` by default.
    pub intensity: SettableValue,
    /// `this.bias` — the cosine below which a sample does not occlude,
    /// `0.025` by default.
    pub bias: SettableValue,
    /// `this.samples` — the number of disk samples, the loop's end. `16` by
    /// default.
    pub samples: SettableValue,
    /// `this.blurSharpness` — how strongly a depth difference rejects a
    /// neighbour in the blur, `2` by default.
    pub blur_sharpness: SettableValue,
    state: Rc<SsaoState>,
}

/// The node's state, shared with the renderer's update-before registry.
struct SsaoState {
    /// `this.depthNode.value`.
    depth: DepthTexture,
    /// `this._aoRenderTarget`.
    ao_target: RenderTarget,
    /// `this._blurRenderTarget`.
    blur_target: RenderTarget,
    /// `_quadMesh` with `this._aoMaterial`.
    ao_quad: QuadMesh,
    /// `_quadMesh` with `this._blurMaterial` reading the AO target.
    blur_horizontal_quad: QuadMesh,
    /// `_quadMesh` with `this._blurMaterial` reading the blur target.
    blur_vertical_quad: QuadMesh,
    /// `this._blurDirection` for the horizontal draw.
    blur_direction_horizontal: SettableValue,
    /// `this._blurDirection` for the vertical draw.
    blur_direction_vertical: SettableValue,
    /// `this._textureNode` — `passTexture( this, aoRenderTarget.texture )`.
    node: NodeRef,
    /// `this.resolutionScale`.
    resolution_scale: Cell<f64>,
    /// `this.blurEnabled`.
    blur_enabled: Cell<bool>,
}

/// The node halves of the uniforms the AO `Fn()` reads.
struct Uniforms {
    radius: NodeRef,
    intensity: NodeRef,
    bias: NodeRef,
    samples: NodeRef,
    projection: NodeRef,
    projection_inverse: NodeRef,
}

/// `new RenderTarget( 1, 1, { depthBuffer: false, format: RedFormat } )`.
/// The port has no `RedFormat`; the quads write their float into an
/// `rgba8unorm` and the texture node reads `.x`.
fn red_target() -> RenderTarget {
    RenderTarget::new_with_options(
        1,
        1,
        RenderTargetOptions {
            texture_type: TextureType::UnsignedByte,
            samples: 0,
            depth_buffer: false,
            min_filter: TextureFilter::Linear,
            mag_filter: TextureFilter::Linear,
        },
    )
    .expect("three-rs: the SSAO targets are a colour type")
}

/// `uniform( camera.<matrix> )` — bound by reference, read at draw time.
fn camera_matrix(
    camera: &Rc<RefCell<PerspectiveCamera>>,
    read: fn(&PerspectiveCamera) -> Vec<f64>,
) -> NodeRef {
    let camera: Weak<RefCell<PerspectiveCamera>> = Rc::downgrade(camera);
    uniform(
        UniformSource::Live(LiveValue::new(move || {
            let camera = camera
                .upgrade()
                .expect("three-rs: an SSAONode outlived its camera");
            let value = read(&camera.borrow());
            value
        })),
        Type::Mat4,
        UniformGroup::Object,
        None,
    )
}

impl SsaoNode {
    /// `new SSAONode( depthNode, normalNode, camera )`, with the materials
    /// `setup()` gives it. See [`ssao`].
    pub fn new(
        depth: &DepthTexture,
        normal: SampleFn,
        camera: &Rc<RefCell<PerspectiveCamera>>,
    ) -> Self {
        let ao_target = red_target();
        let blur_target = red_target();

        let (radius, radius_value) = uniform_settable(Type::F32, vec![0.5]);
        let (intensity, intensity_value) = uniform_settable(Type::F32, vec![1.0]);
        let (bias, bias_value) = uniform_settable(Type::F32, vec![0.025]);
        let (samples, samples_value) = uniform_settable(Type::F32, vec![16.0]);
        let (blur_sharpness, blur_sharpness_value) = uniform_settable(Type::F32, vec![2.0]);
        let (direction_h, direction_h_value) = uniform_settable(Type::Vec2, vec![0.0, 0.0]);
        let (direction_v, direction_v_value) = uniform_settable(Type::Vec2, vec![0.0, 0.0]);
        let uniforms = Uniforms {
            radius: radius.clone(),
            intensity,
            bias,
            samples,
            projection: camera_matrix(camera, |c| c.projection_matrix.elements.to_vec()),
            projection_inverse: camera_matrix(camera, |c| {
                c.projection_matrix_inverse.elements.to_vec()
            }),
        };

        let mut ao_material = MeshBasicNodeMaterial::new();
        ao_material.name = "SSAO.AO";
        ao_material.fragment_node = Some(ao_node(depth, &normal, &uniforms));

        let blur_material = |input: &Texture, direction: NodeRef| {
            let mut material = MeshBasicNodeMaterial::new();
            material.name = "SSAO.Blur";
            let input = input.clone();
            material.fragment_node = Some(depth_aware_blur(
                move |coord| texture_with_uv(&input, coord),
                depth,
                direction,
                camera,
                blur_sharpness.clone(),
                radius.clone(),
            ));
            material
        };
        let blur_horizontal = blur_material(&ao_target.texture(), direction_h);
        let blur_vertical = blur_material(&blur_target.texture(), direction_v);

        let node = to_var(None, texture_uv(&ao_target.texture(), uv()));
        let state = Rc::new(SsaoState {
            depth: depth.clone(),
            ao_target,
            blur_target,
            ao_quad: QuadMesh::new(ao_material),
            blur_horizontal_quad: QuadMesh::new(blur_horizontal),
            blur_vertical_quad: QuadMesh::new(blur_vertical),
            blur_direction_horizontal: direction_h_value,
            blur_direction_vertical: direction_v_value,
            node,
            resolution_scale: Cell::new(0.5),
            blur_enabled: Cell::new(true),
        });
        register_texture_update(state.ao_target.texture().id(), &state);
        Self {
            radius: radius_value,
            intensity: intensity_value,
            bias: bias_value,
            samples: samples_value,
            blur_sharpness: blur_sharpness_value,
            state,
        }
    }

    /// `ssaoNode.getTextureNode()` — the occlusion, in `.x`, at `uv()`. It
    /// is the AO target with or without the blur.
    pub fn node(&self) -> NodeRef {
        self.state.node.clone()
    }

    /// `ssaoNode.getTextureNode().sample( uvNode )` — the occlusion at an
    /// explicit uv (a material the scene pass draws reads it at `screenUV`).
    pub fn sample(&self, coord: NodeRef) -> NodeRef {
        texture_uv(&self.state.ao_target.texture(), coord)
    }

    /// The AO target's texture, which [`node`](Self::node) reads.
    pub fn texture(&self) -> Texture {
        self.state.ao_target.texture()
    }

    /// `ssaoNode.resolutionScale = value` — the targets' size as a fraction
    /// of the drawing buffer's, `0.5` by default.
    pub fn set_resolution_scale(&self, scale: f64) {
        self.state.resolution_scale.set(scale);
    }

    /// `ssaoNode.blurEnabled = value` — run the two blur passes after the
    /// AO pass. On by default.
    pub fn set_blur_enabled(&self, on: bool) {
        self.state.blur_enabled.set(on);
    }

    /// The `SSAO.AO` quad material, for the dump gate.
    #[doc(hidden)]
    pub fn quad_material(&self) -> MeshBasicNodeMaterial {
        self.state.ao_quad.material.clone()
    }

    /// The horizontal `SSAO.Blur` quad material, for the dump gate.
    #[doc(hidden)]
    pub fn blur_quad_material(&self) -> MeshBasicNodeMaterial {
        self.state.blur_horizontal_quad.material.clone()
    }
}

/// `SSAONode.setup()`'s `ao` `Fn()`, which has no layout and so inlines.
#[inline(never)]
fn ao_node(depth: &DepthTexture, normal: &SampleFn, u: &Uniforms) -> NodeRef {
    let uv_node = uv();
    // `sampleDepth( uv )` — `this.depthNode.sample( uv ).r`; no logarithmic
    // depth buffer.
    let sample_depth = |coord: NodeRef| pass_depth_texture_uv(depth, coord);

    let depth_value = to_var(None, sample_depth(uv_node.clone()));
    let discard_far = if_then(depth_value.greater_than_equal(1.0), vec![discard()]);
    let view_position = to_var(
        None,
        get_view_position(
            uv_node.clone(),
            depth_value.clone(),
            u.projection_inverse.clone(),
        ),
    );
    let view_normal = to_var(None, normal(uv_node).rgb().normalize());
    let phi = to_var(
        None,
        interleaved_gradient_noise(screen_coordinate()).mul(pi2()),
    );
    let samples = u.samples.clone();
    let clip_position = to_var(
        None,
        u.projection
            .mul(vec4_join(vec![view_position.clone(), float(1.0)])),
    );
    let occlusion = to_var(None, float(0.0));

    let taps = loop_options("i", Type::I32, int(0), samples.clone(), "<", |i| {
        let offset =
            vogel_disk_sample(i.clone(), samples.clone(), phi.clone()).mul(u.radius.clone());
        let clip_offset = u
            .projection
            .mul(vec4_join(vec![offset, float(0.0), float(0.0)]));
        let sample_uv = get_screen_position_from_clip(clip_position.add(clip_offset));
        let sample_view_position = get_view_position(
            sample_uv.clone(),
            sample_depth(sample_uv),
            u.projection_inverse.clone(),
        );
        let v = to_var(None, sample_view_position.sub(view_position.clone()));
        let dist = to_var(None, v.length());
        let cos_angle = dot(v.clone(), view_normal.clone()).div(max(dist.clone(), float(0.0001)));
        let falloff = u.radius.div(u.radius.add(dist.clone()));
        vec![
            v,
            dist,
            occlusion.add_assign(max(cos_angle.sub(u.bias.clone()), 0.0).mul(falloff)),
        ]
    });

    block(
        vec![
            depth_value,
            discard_far,
            view_position,
            view_normal,
            phi,
            clip_position,
            occlusion.clone(),
            taps,
        ],
        occlusion
            .div(samples)
            .mul(u.intensity.clone())
            .one_minus()
            .clamp(0.0, 1.0),
    )
}

impl NodeUpdate for SsaoState {
    /// `this.updateBeforeType = NodeUpdateType.FRAME`.
    fn update_before_type(&self) -> NodeUpdateType {
        NodeUpdateType::Frame
    }

    /// `SSAONode.updateBefore( frame )`.
    fn update_before(&self, renderer: &mut Renderer) -> bool {
        // The pre-pass first, with the AO context lifted — see
        // `GtaoState::update_before`. The normal is a sampler here, so the
        // pass is found through the depth attachment it owns.
        if let Some(pass) = crate::nodes::frame::texture_update(self.depth.id()) {
            let outer_ao = renderer.context_ao.take();
            renderer.update_before_node(&pass);
            renderer.context_ao = outer_ao;
        }

        // `_rendererState = RendererUtils.resetRendererState( renderer, … )`.
        let previous_target = renderer.render_target();
        let previous_mrt = renderer.mrt();
        let previous_auto_clear = renderer.auto_clear;
        let previous_clear_color = renderer.clear_color();
        let previous_clear_alpha = renderer.clear_alpha();
        renderer.set_mrt(None);
        renderer.auto_clear = true;

        // `setSize( renderer.getDrawingBufferSize() )`.
        let (width, height) = renderer.drawing_buffer_size();
        let scale = self.resolution_scale.get();
        let width = ((scale * f64::from(width)).round() as u32).max(1);
        let height = ((scale * f64::from(height)).round() as u32).max(1);
        for target in [&self.ao_target, &self.blur_target] {
            if target.size() != (width, height) {
                target.set_size(width, height);
            }
        }

        renderer.set_clear_color(Color::new(1.0, 1.0, 1.0), 1.0);

        // Ambient occlusion.
        renderer.set_render_target(Some(self.ao_target.clone()));
        renderer.render_quad(&self.ao_quad);

        // Separable, depth-aware blur: horizontal (AO -> blur) then vertical
        // (blur -> AO).
        if self.blur_enabled.get() {
            self.blur_direction_horizontal
                .set(vec![1.0 / f64::from(width), 0.0]);
            renderer.set_render_target(Some(self.blur_target.clone()));
            renderer.render_quad(&self.blur_horizontal_quad);

            self.blur_direction_vertical
                .set(vec![0.0, 1.0 / f64::from(height)]);
            renderer.set_render_target(Some(self.ao_target.clone()));
            renderer.render_quad(&self.blur_vertical_quad);
        }

        // `RendererUtils.restoreRendererState( renderer, _rendererState )`.
        renderer.set_render_target(previous_target);
        renderer.set_mrt(previous_mrt);
        renderer.set_clear_color(previous_clear_color, previous_clear_alpha);
        renderer.auto_clear = previous_auto_clear;
        true
    }
}