three-rs 0.2.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
414
415
416
417
418
419
420
421
422
423
424
425
//! What a scene-graph node *is*, beyond its transform.
//!
//! three.js gets this from subclassing: `Mesh extends Object3D` adds `geometry`
//! and `material`, and `Renderer._projectObject()` branches on `object.isMesh`.
//! Rust has no inheritance, so the subclass' extra state is a variant of
//! [`Payload`] held by [`crate::core::Object3D`], and `isMesh` is a `match`.
//!
//! This replaces the flat `Scene.children: Vec<Child>` list the renderer used to
//! iterate: a payload lives on the node itself, so a `Mesh` can sit anywhere in
//! the tree (under a `Group`, under a light) and the renderer's traversal finds
//! it. See `docs/scene-graph.md`.

use std::fmt;

use std::rc::Rc;

use crate::core::BufferGeometry;
use crate::lights::LightObject;
use crate::materials::MeshBasicNodeMaterial;
use crate::math::{Matrix4, Sphere};
use crate::objects::{
    BatchedMesh, InstancedBufferAttribute, InstancedMesh, Line, Mesh, Points, SkinnedMesh, Sprite,
};

/// The subclass state of one [`crate::core::Object3D`].
///
/// `None` is a plain `Object3D`, a `Group`, a `Bone` — anything the renderer
/// walks through without drawing.
#[derive(Clone, Default)]
#[non_exhaustive]
pub enum Payload {
    /// A plain `Object3D`, a `Group` or a `Bone`: nothing to draw.
    #[default]
    None,
    /// `Mesh.geometry` / `Mesh.material`.
    Mesh(Mesh),
    /// `InstancedMesh extends Mesh`.
    InstancedMesh(InstancedMesh),
    /// `SkinnedMesh extends Mesh` — the skeleton and the two bind matrices on
    /// top of the `Mesh`. `is_mesh()` is true for it, as `isMesh` is in
    /// three.js. Boxed because the two bind matrices and the bounding volumes
    /// make it half again the size of the next-largest variant, and every
    /// `Object3D` in the tree carries a `Payload`.
    SkinnedMesh(Box<SkinnedMesh>),
    /// `BatchedMesh extends Mesh` — boxed because it is much the largest
    /// variant (three data textures and two draw-list arrays).
    BatchedMesh(Box<BatchedMesh>),
    /// `Line extends Object3D` — and `LineSegments extends Line`, which is the
    /// `is_line_segments` flag inside. Not a `Mesh`: the renderer reads the
    /// object to pick `line-strip` / `line-list` over `triangle-list`.
    Line(Line),
    /// `Points extends Object3D` — like `Line`, not a `Mesh`: the renderer
    /// reads the object to pick `point-list` over `triangle-list`.
    Points(Points),
    /// `Sprite extends Object3D` — not a `Mesh` either: `_projectObject()`
    /// gives it an arm of its own, culled by `Frustum.intersectsSprite()` and
    /// sorted by its origin rather than by a geometry bounding sphere.
    Sprite(Sprite),
    /// `Light extends Object3D`, one variant per subclass. The renderer reaches
    /// it through `RenderList.lights`, which `_projectObject()` fills from
    /// `object.is_light` — set alongside this variant.
    Light(LightObject),
}

impl fmt::Debug for Payload {
    /// `Object3D` derives `Debug`; geometries, materials and node graphs do not,
    /// so a payload prints as its variant.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match self {
            Payload::None => "None",
            Payload::Mesh(_) => "Mesh",
            Payload::InstancedMesh(_) => "InstancedMesh",
            Payload::SkinnedMesh(_) => "SkinnedMesh",
            Payload::BatchedMesh(_) => "BatchedMesh",
            Payload::Line(line) => {
                if line.is_line_segments {
                    "LineSegments"
                } else {
                    "Line"
                }
            }
            Payload::Points(_) => "Points",
            Payload::Sprite(_) => "Sprite",
            Payload::Light(_) => "Light",
        };
        f.write_str(name)
    }
}

impl Payload {
    /// `object.isMesh` — true for `InstancedMesh` too, exactly as in three.js
    /// where `InstancedMesh extends Mesh`.
    pub fn is_mesh(&self) -> bool {
        matches!(
            self,
            Payload::Mesh(_)
                | Payload::InstancedMesh(_)
                | Payload::SkinnedMesh(_)
                | Payload::BatchedMesh(_)
        )
    }

    /// `object.isSkinnedMesh`.
    pub fn is_skinned_mesh(&self) -> bool {
        matches!(self, Payload::SkinnedMesh(_))
    }

    /// The `SkinnedMesh` this node is, if it is one.
    pub fn skinned_mesh(&self) -> Option<&SkinnedMesh> {
        match self {
            Payload::SkinnedMesh(skin) => Some(skin),
            _ => None,
        }
    }

    /// The `SkinnedMesh` this node is, mutably, if it is one.
    pub fn skinned_mesh_mut(&mut self) -> Option<&mut SkinnedMesh> {
        match self {
            Payload::SkinnedMesh(skin) => Some(skin),
            _ => None,
        }
    }

    /// `object.isInstancedMesh`.
    pub fn is_instanced_mesh(&self) -> bool {
        matches!(self, Payload::InstancedMesh(_))
    }

    /// `object.isLine` — true for a `LineSegments` too, exactly as in three.js
    /// where `LineSegments extends Line`.
    pub fn is_line(&self) -> bool {
        matches!(self, Payload::Line(_))
    }

    /// `object.isLineSegments`.
    pub fn is_line_segments(&self) -> bool {
        matches!(self, Payload::Line(line) if line.is_line_segments)
    }

    /// `object.isPoints`.
    pub fn is_points(&self) -> bool {
        matches!(self, Payload::Points(_))
    }

    /// `object.isSprite`.
    pub fn is_sprite(&self) -> bool {
        matches!(self, Payload::Sprite(_))
    }

    /// The `Sprite` this node is, if it is one.
    pub fn sprite(&self) -> Option<&Sprite> {
        match self {
            Payload::Sprite(sprite) => Some(sprite),
            _ => None,
        }
    }

    /// The `Sprite` this node is, mutably, if it is one.
    pub fn sprite_mut(&mut self) -> Option<&mut Sprite> {
        match self {
            Payload::Sprite(sprite) => Some(sprite),
            _ => None,
        }
    }

    /// The `Points` this node is, if it is one.
    pub fn points(&self) -> Option<&Points> {
        match self {
            Payload::Points(points) => Some(points),
            _ => None,
        }
    }

    /// The `Points` this node is, mutably, if it is one.
    pub fn points_mut(&mut self) -> Option<&mut Points> {
        match self {
            Payload::Points(points) => Some(points),
            _ => None,
        }
    }

    /// The `Line` this node is, if it is one.
    pub fn line(&self) -> Option<&Line> {
        match self {
            Payload::Line(line) => Some(line),
            _ => None,
        }
    }

    /// The `Line` this node is, mutably, if it is one.
    pub fn line_mut(&mut self) -> Option<&mut Line> {
        match self {
            Payload::Line(line) => Some(line),
            _ => None,
        }
    }

    /// `object.geometry` for anything `_projectObject()`'s
    /// `isMesh || isLine || isPoints` arm draws, and for its `isSprite` arm.
    pub fn geometry(&self) -> Option<&Rc<BufferGeometry>> {
        match self {
            Payload::Mesh(mesh) => Some(&mesh.geometry),
            Payload::InstancedMesh(instanced) => Some(&instanced.mesh.geometry),
            Payload::SkinnedMesh(skin) => Some(&skin.mesh.geometry),
            Payload::BatchedMesh(batched) => Some(&batched.mesh.geometry),
            Payload::Line(line) => Some(&line.geometry),
            Payload::Points(points) => Some(&points.geometry),
            Payload::Sprite(sprite) => Some(&sprite.geometry),
            _ => None,
        }
    }

    /// `object.material` for the same set. `None` is three.js' "no material of
    /// its own", which `scene.overrideMaterial` (or `MeshBasicNodeMaterial`'s
    /// defaults) stands in for.
    pub fn material(&self) -> Option<&MeshBasicNodeMaterial> {
        match self {
            Payload::Mesh(mesh) => mesh.material.as_ref(),
            Payload::InstancedMesh(instanced) => instanced.mesh.material.as_ref(),
            Payload::SkinnedMesh(skin) => skin.mesh.material.as_ref(),
            Payload::BatchedMesh(batched) => batched.mesh.material.as_ref(),
            Payload::Line(line) => line.material.as_ref(),
            Payload::Points(points) => points.material.as_ref(),
            Payload::Sprite(sprite) => Some(&sprite.material),
            _ => None,
        }
    }

    /// `object.material` for writing — what `ReflectorNode.updateBefore()`
    /// needs to set `material.visible = false` around its own render.
    pub fn material_mut(&mut self) -> Option<&mut MeshBasicNodeMaterial> {
        match self {
            Payload::Mesh(mesh) => mesh.material.as_mut(),
            Payload::InstancedMesh(instanced) => instanced.mesh.material.as_mut(),
            Payload::SkinnedMesh(skin) => skin.mesh.material.as_mut(),
            Payload::BatchedMesh(batched) => batched.mesh.material.as_mut(),
            Payload::Line(line) => line.material.as_mut(),
            Payload::Points(points) => points.material.as_mut(),
            Payload::Sprite(sprite) => Some(&mut sprite.material),
            _ => None,
        }
    }

    /// `object.material` when it is an array (`Array.isArray( material )`):
    /// the [`Mesh::materials`] of a plain `Mesh`, empty for everything else
    /// and for a mesh with a single material.
    pub(crate) fn material_array(&self) -> &[MeshBasicNodeMaterial] {
        match self {
            Payload::Mesh(mesh) => &mesh.materials,
            _ => &[],
        }
    }

    /// `Frustum.intersectsObject( object )`' geometry half, for a mesh or a
    /// line.
    ///
    /// three.js' `intersectsObject` reads `object.boundingSphere` when the
    /// object has one and falls back to `geometry.boundingSphere` otherwise;
    /// [`InstancedMesh::bounding_sphere`] is that field, and it is the only way
    /// an instanced draw whose instances are spread out is not culled as a
    /// point at its own origin.
    pub(crate) fn bounding_sphere_in(&self, matrix_world: &Matrix4) -> Option<Sphere> {
        match self {
            Payload::Line(line) => line.bounding_sphere_in(matrix_world),
            Payload::Points(points) => points.bounding_sphere_in(matrix_world),
            // `Sprite.intersectsFrustum( frustum )` is
            // `frustum.intersectsSprite( this )`, whose sphere is this.
            Payload::Sprite(sprite) => Some(sprite.bounding_sphere_in(matrix_world)),
            Payload::InstancedMesh(instanced) => match instanced.bounding_sphere {
                Some(bounding_sphere) => {
                    let mut sphere = bounding_sphere;
                    sphere.apply_matrix4(matrix_world);
                    Some(sphere)
                }
                None => instanced.mesh.bounding_sphere_in(matrix_world),
            },
            // `Frustum.intersectsObject` prefers the object's own
            // `boundingSphere` — `SkinnedMesh.computeBoundingSphere()` puts the
            // posed skin there — and falls back to the geometry's.
            Payload::SkinnedMesh(skin) => match skin.bounding_sphere {
                Some(bounding_sphere) => {
                    let mut sphere = bounding_sphere;
                    sphere.apply_matrix4(matrix_world);
                    Some(sphere)
                }
                None => skin.mesh.bounding_sphere_in(matrix_world),
            },
            _ => self.mesh()?.bounding_sphere_in(matrix_world),
        }
    }

    /// `Mesh.morphTargetInfluences`. A `Line` has the field in three.js too, but
    /// nothing on the ladder morphs one, so it is always empty here. A
    /// `SkinnedMesh` keeps its influences behind an `Rc<RefCell<…>>` — the
    /// animation binding writes into them from outside — so this returns a
    /// value rather than a borrow.
    pub fn morph_target_influences(&self) -> Vec<f64> {
        match self {
            Payload::SkinnedMesh(skin) => skin.morph_target_influences.borrow().clone(),
            _ => match self.mesh() {
                Some(mesh) => mesh.morph_target_influences.clone(),
                None => Vec::new(),
            },
        }
    }

    /// The `Mesh` half of whichever mesh-ish payload this is.
    pub fn mesh(&self) -> Option<&Mesh> {
        match self {
            Payload::Mesh(mesh) => Some(mesh),
            Payload::InstancedMesh(instanced) => Some(&instanced.mesh),
            Payload::SkinnedMesh(skin) => Some(&skin.mesh),
            Payload::BatchedMesh(batched) => Some(&batched.mesh),
            _ => None,
        }
    }

    /// The `Mesh` half, mutably, of whichever mesh-ish payload this is.
    pub fn mesh_mut(&mut self) -> Option<&mut Mesh> {
        match self {
            Payload::Mesh(mesh) => Some(mesh),
            Payload::InstancedMesh(instanced) => Some(&mut instanced.mesh),
            Payload::SkinnedMesh(skin) => Some(&mut skin.mesh),
            Payload::BatchedMesh(batched) => Some(&mut batched.mesh),
            _ => None,
        }
    }

    /// The light this node is, if it is one.
    pub fn light(&self) -> Option<&LightObject> {
        match self {
            Payload::Light(light) => Some(light),
            _ => None,
        }
    }

    /// The light this node is, mutably, if it is one.
    pub fn light_mut(&mut self) -> Option<&mut LightObject> {
        match self {
            Payload::Light(light) => Some(light),
            _ => None,
        }
    }

    /// `object.isBatchedMesh`.
    pub fn is_batched_mesh(&self) -> bool {
        matches!(self, Payload::BatchedMesh(_))
    }

    /// The `BatchedMesh` this node is, if it is one.
    pub fn batched_mesh(&self) -> Option<&BatchedMesh> {
        match self {
            Payload::BatchedMesh(batched) => Some(batched),
            _ => None,
        }
    }

    /// The `BatchedMesh` this node is, mutably, if it is one.
    pub fn batched_mesh_mut(&mut self) -> Option<&mut BatchedMesh> {
        match self {
            Payload::BatchedMesh(batched) => Some(batched),
            _ => None,
        }
    }

    /// The `InstancedMesh` this node is, if it is one.
    pub fn instanced_mesh(&self) -> Option<&InstancedMesh> {
        match self {
            Payload::InstancedMesh(instanced) => Some(instanced),
            _ => None,
        }
    }

    /// The `InstancedMesh` this node is, mutably, if it is one.
    pub fn instanced_mesh_mut(&mut self) -> Option<&mut InstancedMesh> {
        match self {
            Payload::InstancedMesh(instanced) => Some(instanced),
            _ => None,
        }
    }

    /// `RenderObject.getInstanceCount()` (`RenderObject.js:617-631`): the
    /// instanced geometry's count, else `object.count` when the object defines
    /// one, else 1. `Points.count` is the second arm — see [`Points::count`].
    pub(crate) fn count(&self) -> u32 {
        match self {
            Payload::InstancedMesh(instanced) => instanced.count as u32,
            Payload::Points(points) => points.count.unwrap_or(1) as u32,
            Payload::Sprite(sprite) => sprite.count as u32,
            // `RenderObject.getInstanceCount()`: an instanced geometry's
            // `instanceCount` wins over `object.count`. A `LineSegments2`'s
            // geometry is one, and its count is the number of segments.
            Payload::Mesh(mesh) => match &mesh.line_segments {
                Some(attributes) => attributes.instance_count() as u32,
                None => mesh.count.unwrap_or(1) as u32,
            },
            _ => 1,
        }
    }

    /// `InstancedMesh.instanceMatrix`, `None` for a plain mesh.
    pub fn instance_matrix(&self) -> Option<&InstancedBufferAttribute> {
        match self {
            Payload::InstancedMesh(instanced) => Some(&instanced.instance_matrix),
            _ => None,
        }
    }

    /// A `LineSegments2`'s instanced attributes, `None` for anything else.
    pub fn line_segments(&self) -> Option<&crate::nodes::lines::LineSegmentsAttributes> {
        match self {
            Payload::Mesh(mesh) => mesh.line_segments.as_ref(),
            _ => None,
        }
    }

    /// `InstancedMesh.instanceColor` — `None` until `setColorAt()` is called,
    /// and `None` for a plain mesh.
    pub fn instance_color(&self) -> Option<&InstancedBufferAttribute> {
        match self {
            Payload::InstancedMesh(instanced) => instanced.instance_color.as_ref(),
            _ => None,
        }
    }
}