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
use crate::core::{Object3D, ObjectArena, ObjectId};
use crate::lights::Light;
use crate::math::{Color, Matrix4};
use crate::textures::CubeTexture;
use std::sync::Arc;
/// Fog parameters mirroring three.js [`Fog`](https://threejs.org/docs/#api/en/scene/Fog)
/// (linear) and `FogExp2`.
///
/// `mode`: `0` = disabled, `1` = linear, `2` = exponential squared.
#[derive(Debug, Clone, Copy)]
pub struct FogParams {
pub color: Color,
pub near: f32,
pub far: f32,
pub density: f32,
pub mode: u32,
}
impl Default for FogParams {
fn default() -> Self {
Self {
color: Color::WHITE,
near: 1.0,
far: 1000.0,
density: 0.0,
mode: 0,
}
}
}
/// Root scene container: object arena, background, environment, fog.
///
/// Mirrors three.js [`Scene`](https://threejs.org/docs/#api/en/scenes/Scene) —
/// the root is an `Object3D` group named `"Scene"`.
pub struct Scene {
pub arena: ObjectArena,
pub root: ObjectId,
pub background: Color,
/// Clear alpha for the background (0.0 = fully transparent framebuffer,
/// 1.0 = opaque). Lets offscreen renders produce transparent PNGs.
pub background_alpha: f32,
/// Optional environment map used for image-based lighting (PBR ambient).
/// The current renderer uploads it as a cube texture but does not yet
/// prefilter for roughness — sampling falls back to the default white
/// 1x1 fallback if `None`.
pub environment: Option<Arc<CubeTexture>>,
/// Alternate environment input: a CubeRenderTarget's cube view. When set,
/// the renderer samples from the live RT contents (typically produced by
/// a CubeCamera) instead of the CPU-backed `environment` cube texture.
pub environment_cube_rt: Option<u32>,
pub fog: FogParams,
}
impl Scene {
/// Empty scene with dark gray background and no fog.
pub fn new() -> Self {
let mut arena = ObjectArena::new();
let mut root_obj = Object3D::group();
root_obj.name = "Scene".into();
let root = arena.insert(root_obj);
Self {
arena,
root,
background: Color::from_hex(0x111111),
background_alpha: 1.0,
environment: None,
environment_cube_rt: None,
fog: FogParams::default(),
}
}
/// `scene.add(obj)` — insert and parent under the scene root.
pub fn add(&mut self, obj: Object3D) -> ObjectId {
let id = self.arena.insert(obj);
self.arena.add_child(self.root, id);
id
}
/// Convenience: build a light Object3D and add it to the scene.
pub fn add_light(&mut self, light: impl Into<Light>) -> ObjectId {
self.add(Object3D::light(light))
}
/// Add under a specific parent (not the scene root).
pub fn add_to(&mut self, parent: ObjectId, obj: Object3D) -> ObjectId {
let id = self.arena.insert(obj);
self.arena.add_child(parent, id);
id
}
pub fn get(&self, id: ObjectId) -> Option<&Object3D> {
self.arena.get(id)
}
pub fn get_mut(&mut self, id: ObjectId) -> Option<&mut Object3D> {
self.arena.get_mut(id)
}
/// Refresh all `matrix_world`s. The renderer calls this each frame.
pub fn update_world(&mut self) {
self.arena
.update_world_matrices(self.root, Matrix4::identity());
}
}
impl Default for Scene {
fn default() -> Self {
Self::new()
}
}