scena 1.7.1

A Rust-native scene-graph renderer with typed scene state, glTF assets, and explicit prepare/render lifecycles.
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use wasm_bindgen::prelude::*;

use super::inputs::vec3_array_from_slice;
use super::wasm_readback::browser_canvas_rgba8;
use super::{SceneHostCore, SceneHostError};
use crate::{Assets, PlatformSurface, RenderOutcome, Renderer, SurfaceViewport};

#[wasm_bindgen]
pub struct SceneHost {
    pub(super) core: SceneHostCore,
    browser_canvas: Option<web_sys::HtmlCanvasElement>,
}

#[wasm_bindgen]
impl SceneHost {
    #[wasm_bindgen(js_name = newWebgl2)]
    pub async fn new_webgl2(
        canvas: web_sys::HtmlCanvasElement,
        logical_width: f32,
        logical_height: f32,
        device_pixel_ratio: f32,
    ) -> Result<SceneHost, JsValue> {
        build_from_canvas(
            BrowserBackend::WebGl2,
            canvas,
            logical_width,
            logical_height,
            device_pixel_ratio,
        )
        .await
    }

    #[wasm_bindgen(js_name = newWebgpu)]
    pub async fn new_webgpu(
        canvas: web_sys::HtmlCanvasElement,
        logical_width: f32,
        logical_height: f32,
        device_pixel_ratio: f32,
    ) -> Result<SceneHost, JsValue> {
        build_from_canvas(
            BrowserBackend::WebGpu,
            canvas,
            logical_width,
            logical_height,
            device_pixel_ratio,
        )
        .await
    }

    #[wasm_bindgen(js_name = rootHandle)]
    pub fn root_handle(&self) -> u64 {
        self.core.root_handle()
    }

    pub fn backend(&self) -> String {
        serde_json::to_value(self.core.backend())
            .ok()
            .and_then(|value| value.as_str().map(str::to_owned))
            .unwrap_or_else(|| format!("{:?}", self.core.backend()))
    }

    pub fn resize(
        &mut self,
        logical_width: f32,
        logical_height: f32,
        device_pixel_ratio: f32,
    ) -> Result<(), JsValue> {
        self.core
            .resize(logical_width, logical_height, device_pixel_ratio)
            .map_err(js_error)
    }

    #[wasm_bindgen(js_name = attachCanvasWebgl2)]
    pub async fn attach_canvas_webgl2(
        &mut self,
        canvas: web_sys::HtmlCanvasElement,
        logical_width: f32,
        logical_height: f32,
        device_pixel_ratio: f32,
    ) -> Result<(), JsValue> {
        self.attach_canvas(
            BrowserBackend::WebGl2,
            canvas,
            logical_width,
            logical_height,
            device_pixel_ratio,
        )
        .await
    }

    #[wasm_bindgen(js_name = attachCanvasWebgpu)]
    pub async fn attach_canvas_webgpu(
        &mut self,
        canvas: web_sys::HtmlCanvasElement,
        logical_width: f32,
        logical_height: f32,
        device_pixel_ratio: f32,
    ) -> Result<(), JsValue> {
        self.attach_canvas(
            BrowserBackend::WebGpu,
            canvas,
            logical_width,
            logical_height,
            device_pixel_ratio,
        )
        .await
    }

    #[wasm_bindgen(js_name = setTag)]
    pub fn set_tag(&mut self, node: u64, tag: String) -> Result<(), JsValue> {
        self.core.set_tag(node, &tag).map_err(js_error)
    }

    #[wasm_bindgen(js_name = clearTag)]
    pub fn clear_tag(&mut self, node: u64, tag: String) -> Result<bool, JsValue> {
        self.core.clear_tag(node, &tag).map_err(js_error)
    }

    #[wasm_bindgen(js_name = findByTag)]
    pub fn find_by_tag(&mut self, tag: String) -> Vec<u64> {
        self.core.find_by_tag(&tag)
    }

    #[wasm_bindgen(js_name = instantiateUrl)]
    pub async fn instantiate_url(&mut self, url: String) -> Result<u64, JsValue> {
        self.core.instantiate_url(url).await.map_err(js_error)
    }

    #[wasm_bindgen(js_name = instantiateUrlUnder)]
    pub async fn instantiate_url_under(
        &mut self,
        parent: u64,
        url: String,
    ) -> Result<u64, JsValue> {
        self.core
            .instantiate_url_under(parent, url)
            .await
            .map_err(js_error)
    }

    #[wasm_bindgen(js_name = instantiateGlb)]
    pub async fn instantiate_glb(&mut self, bytes: Box<[u8]>) -> Result<u64, JsValue> {
        self.core
            .instantiate_glb(bytes.as_ref())
            .await
            .map_err(js_error)
    }

    #[wasm_bindgen(js_name = instantiateGlbUnder)]
    pub async fn instantiate_glb_under(
        &mut self,
        parent: u64,
        bytes: Box<[u8]>,
    ) -> Result<u64, JsValue> {
        self.core
            .instantiate_glb_under(parent, bytes.as_ref())
            .await
            .map_err(js_error)
    }

    #[wasm_bindgen(js_name = importRoots)]
    pub fn import_roots(&mut self, import: u64) -> Result<Vec<u64>, JsValue> {
        self.core.import_roots(import).map_err(js_error)
    }

    #[wasm_bindgen(js_name = nodeHandle)]
    pub fn node_handle(&mut self, import: u64, path: String) -> Result<u64, JsValue> {
        self.core.node_handle(import, &path).map_err(js_error)
    }

    #[wasm_bindgen(js_name = nodeHandleByName)]
    pub fn node_handle_by_name(&mut self, import: u64, name: String) -> Result<u64, JsValue> {
        self.core
            .node_handle_by_name(import, &name)
            .map_err(js_error)
    }

    #[wasm_bindgen(js_name = nodeHandleFromInspection)]
    pub fn node_handle_from_inspection(&self, handle: u64) -> Result<u64, JsValue> {
        self.core
            .node_handle_from_inspection(handle)
            .map_err(js_error)
    }

    #[wasm_bindgen(js_name = setNodeAnnotation)]
    pub fn set_node_annotation(
        &mut self,
        id: String,
        node: u64,
        local_offset: Box<[f32]>,
    ) -> Result<(), JsValue> {
        self.core
            .set_node_annotation(
                &id,
                node,
                vec3_array_from_slice("localOffset", &local_offset).map_err(js_error)?,
            )
            .map_err(js_error)
    }

    #[wasm_bindgen(js_name = setWorldAnnotation)]
    pub fn set_world_annotation(
        &mut self,
        id: String,
        position: Box<[f32]>,
    ) -> Result<(), JsValue> {
        self.core
            .set_world_annotation(
                &id,
                vec3_array_from_slice("position", &position).map_err(js_error)?,
            )
            .map_err(js_error)
    }

    #[wasm_bindgen(js_name = clearAnnotation)]
    pub fn clear_annotation(&mut self, id: String) -> bool {
        self.core.clear_annotation(&id)
    }

    #[wasm_bindgen(js_name = removeNode)]
    pub fn remove_node(&mut self, node: u64) -> Result<(), JsValue> {
        self.core.remove_node(node).map_err(js_error)
    }

    #[wasm_bindgen(js_name = removeImport)]
    pub fn remove_import(&mut self, import: u64) -> Result<(), JsValue> {
        self.core.remove_import(import).map_err(js_error)
    }

    pub fn prepare(&mut self) -> Result<(), JsValue> {
        self.core.prepare().map_err(js_error)
    }

    pub fn render(&mut self) -> Result<String, JsValue> {
        self.core
            .render()
            .map(render_outcome_json)
            .map_err(js_error)
    }

    #[wasm_bindgen(js_name = readPixels)]
    pub fn read_pixels(&self) -> Vec<u8> {
        self.browser_canvas
            .as_ref()
            .and_then(|canvas| browser_canvas_rgba8(canvas).ok().flatten())
            .map(|(_width, _height, rgba8)| rgba8)
            .unwrap_or_else(|| self.core.read_pixels())
    }

    #[wasm_bindgen(js_name = capture)]
    pub fn capture(&self) -> Result<JsValue, JsValue> {
        let capture = match self
            .browser_canvas
            .as_ref()
            .map(browser_canvas_rgba8)
            .transpose()
            .map_err(js_error)?
            .flatten()
        {
            Some((width, height, rgba8)) => self
                .core
                .capture_from_rgba8(width, height, rgba8)
                .map_err(js_error)?,
            None => self.core.capture().map_err(js_error)?,
        };
        let descriptor_json = serde_json::to_string(&capture.descriptor).map_err(|error| {
            js_error(SceneHostError::new(
                super::SceneHostErrorCode::Capture,
                format!("capture descriptor serialization failed: {error}"),
            ))
        })?;
        let object = js_sys::Object::new();
        let rgba8 = js_sys::Uint8Array::from(capture.rgba8.as_slice());
        let _ = js_sys::Reflect::set(
            &object,
            &JsValue::from_str("descriptorJson"),
            &JsValue::from_str(&descriptor_json),
        );
        let _ = js_sys::Reflect::set(&object, &JsValue::from_str("rgba8"), &rgba8);
        Ok(object.into())
    }

    #[wasm_bindgen(js_name = captureJson)]
    pub fn capture_json(&self) -> Result<String, JsValue> {
        self.core.capture_json().map_err(js_error)
    }

    pub fn pick(&mut self, x: f32, y: f32) -> Result<Option<u64>, JsValue> {
        self.core.pick(x, y).map_err(js_error)
    }

    #[wasm_bindgen(js_name = frameNode)]
    pub fn frame_node(&mut self, node: u64) -> Result<(), JsValue> {
        self.core.frame_node(node).map_err(js_error)
    }

    #[wasm_bindgen(js_name = frameNodeProductView)]
    pub fn frame_node_product_view(&mut self, node: u64) -> Result<(), JsValue> {
        self.core.frame_node_product_view(node).map_err(js_error)
    }

    #[wasm_bindgen(js_name = frameNodeWithPreset)]
    pub fn frame_node_with_preset(&mut self, node: u64, preset: String) -> Result<(), JsValue> {
        self.core
            .frame_node_with_preset(node, &preset)
            .map_err(js_error)
    }

    #[wasm_bindgen(js_name = frameAll)]
    pub fn frame_all(&mut self) -> Result<(), JsValue> {
        self.core.frame_all().map_err(js_error)
    }

    #[wasm_bindgen(js_name = worldDistance)]
    pub fn world_distance(&self, a: u64, b: u64) -> Result<f32, JsValue> {
        self.core.world_distance(a, b).map_err(js_error)
    }

    #[wasm_bindgen(js_name = nodeWorldBoundsJson)]
    pub fn node_world_bounds_json(&self, node: u64) -> Result<String, JsValue> {
        self.core.node_world_bounds_json(node).map_err(js_error)
    }

    #[wasm_bindgen(js_name = inspectJson)]
    pub fn inspect_json(&self) -> Result<String, JsValue> {
        self.core.inspect_json().map_err(js_error)
    }

    #[wasm_bindgen(js_name = annotationProjectionsJson)]
    pub fn annotation_projections_json(&self) -> Result<String, JsValue> {
        self.core.annotation_projections_json().map_err(js_error)
    }

    #[wasm_bindgen(js_name = capabilitiesJson)]
    pub fn capabilities_json(&self) -> Result<String, JsValue> {
        self.core.capabilities_json().map_err(js_error)
    }

    #[wasm_bindgen(js_name = diagnosticsJson)]
    pub fn diagnostics_json(&self) -> String {
        self.core.diagnostics_json()
    }

    #[wasm_bindgen(js_name = statsJson)]
    pub fn stats_json(&self) -> String {
        self.core.stats_json()
    }
}

impl SceneHost {
    async fn attach_canvas(
        &mut self,
        backend: BrowserBackend,
        canvas: web_sys::HtmlCanvasElement,
        logical_width: f32,
        logical_height: f32,
        device_pixel_ratio: f32,
    ) -> Result<(), JsValue> {
        let browser_canvas = canvas.clone();
        let (surface, viewport) = surface_from_canvas(
            backend,
            canvas,
            logical_width,
            logical_height,
            device_pixel_ratio,
        )?;
        self.core
            .resize(
                viewport.logical_width(),
                viewport.logical_height(),
                viewport.device_pixel_ratio(),
            )
            .map_err(js_error)?;
        self.core.attach_surface(surface).await.map_err(js_error)?;
        self.browser_canvas = Some(browser_canvas);
        Ok(())
    }
}

#[derive(Debug, Clone, Copy)]
enum BrowserBackend {
    WebGpu,
    WebGl2,
}

async fn build_from_canvas(
    backend: BrowserBackend,
    canvas: web_sys::HtmlCanvasElement,
    logical_width: f32,
    logical_height: f32,
    device_pixel_ratio: f32,
) -> Result<SceneHost, JsValue> {
    let browser_canvas = canvas.clone();
    let (surface, viewport) = surface_from_canvas(
        backend,
        canvas,
        logical_width,
        logical_height,
        device_pixel_ratio,
    )?;
    let renderer = Renderer::from_surface_async(surface)
        .await
        .map_err(js_error)?;
    let core = SceneHostCore::from_renderer(Assets::new(), renderer, viewport).map_err(js_error)?;
    Ok(SceneHost {
        core,
        browser_canvas: Some(browser_canvas),
    })
}

fn surface_from_canvas(
    backend: BrowserBackend,
    canvas: web_sys::HtmlCanvasElement,
    logical_width: f32,
    logical_height: f32,
    device_pixel_ratio: f32,
) -> Result<(PlatformSurface, SurfaceViewport), JsValue> {
    let viewport = SurfaceViewport::new(logical_width, logical_height, device_pixel_ratio)
        .ok_or_else(|| {
            js_error(SceneHostError::new(
                super::SceneHostErrorCode::InvalidViewport,
                format!(
                    "invalid viewport {logical_width}x{logical_height} at DPR {device_pixel_ratio}"
                ),
            ))
        })?;
    let size = viewport.physical_size();
    let surface = match backend {
        BrowserBackend::WebGpu => {
            PlatformSurface::browser_webgpu_canvas_element(canvas, size.width, size.height)
        }
        BrowserBackend::WebGl2 => {
            PlatformSurface::browser_webgl2_canvas_element(canvas, size.width, size.height)
        }
    };
    Ok((surface, viewport))
}

fn render_outcome_json(outcome: RenderOutcome) -> String {
    serde_json::json!({
        "width": outcome.width,
        "height": outcome.height,
        "draw_calls": outcome.draw_calls,
        "primitives": outcome.primitives,
        "skipped": outcome.skipped,
    })
    .to_string()
}

pub(super) fn js_error(error: impl Into<SceneHostError>) -> JsValue {
    let error = error.into();
    let object = js_sys::Object::new();
    let _ = js_sys::Reflect::set(
        &object,
        &JsValue::from_str("code"),
        &JsValue::from_str(&format!("{:?}", error.code())),
    );
    let _ = js_sys::Reflect::set(
        &object,
        &JsValue::from_str("message"),
        &JsValue::from_str(error.message()),
    );
    object.into()
}