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
//! Port of `three.js/examples/webgpu_materials_basic.html`, calling the three-rs
//! API in the same order the page's `init()` / `animate()` do.
//!
//! Under the e2e harness the viewport is `400 * 2` x `250 * 2` with no
//! `deviceScaleFactor`, so `window.innerWidth` / `innerHeight` /
//! `devicePixelRatio` are 800, 500 and 1, and `Date.now()` is 0.
//!
//! `CubeTextureLoader.load()` is asynchronous on the page, but the harness only
//! fires its single RAF once the network is idle, so the six faces are always
//! present for the graded frame; here they are decoded synchronously.
//!
//! The example's GUI (`renderer.inspector.createParameters()`) only registers
//! `onChange` callbacks, so it changes nothing about the graded frame.

use std::rc::Rc;

use three_rs::addons::controls::OrbitControls;
use three_rs::testing::DeterministicRandom;
use three_rs::utils::date_now_ms;
use three_rs::{
    sphere_geometry, Color, CubeTextureLoader, Mesh, MeshBasicNodeMaterial, PerspectiveCamera,
    Renderer, RendererParameters, Scene, Vector3,
};

pub const INNER_WIDTH: f64 = 800.0;
pub const INNER_HEIGHT: f64 = 500.0;
/// `renderer.setPixelRatio( window.devicePixelRatio )`
pub const DPR: f64 = 1.0;

pub struct App {
    pub renderer: Renderer,
    pub scene: Scene,
    pub camera: PerspectiveCamera,
}

fn examples_dir() -> std::path::PathBuf {
    let three = three_rs::testing::three_js_dir();
    three.join("examples")
}

pub fn init() -> App {
    let camera = PerspectiveCamera::new(60.0, INNER_WIDTH / INNER_HEIGHT, 0.01, 100.0);
    camera.node.borrow_mut().position.z = 3.0;

    let path = examples_dir().join("textures/cube/pisa");
    let format = "png";
    let urls = [
        path.join(format!("px.{format}")),
        path.join(format!("nx.{format}")),
        path.join(format!("py.{format}")),
        path.join(format!("ny.{format}")),
        path.join(format!("pz.{format}")),
        path.join(format!("nz.{format}")),
    ];

    let texture_cube = CubeTextureLoader::new().load(urls).unwrap();

    let mut scene = Scene::new();
    scene.set_background(texture_cube.clone());

    let geometry = Rc::new(sphere_geometry(0.1, 32, 16));

    let mut material = MeshBasicNodeMaterial::new();
    material.color = Color::from_hex(0xffffff);
    material.env_map = Some(texture_cube);

    // `Math.random()` is the harness' deterministic sequence: four draws per
    // mesh, in the order x, y, z, scale.
    let mut random = DeterministicRandom::new();

    for _ in 0..500 {
        let mesh = Mesh::new(geometry.clone(), material.clone());
        {
            let mut object = mesh.borrow_mut();
            object.position.x = random.next() * 10.0 - 5.0;
            object.position.y = random.next() * 10.0 - 5.0;
            object.position.z = random.next() * 10.0 - 5.0;

            let scale = random.next() * 3.0 + 1.0;
            object.scale.set(scale, scale, scale);
        }
        scene.add(&mesh);
    }

    //

    let mut renderer = Renderer::new(RendererParameters::default()).unwrap();
    renderer.set_pixel_ratio(DPR);
    renderer.set_size(INNER_WIDTH, INNER_HEIGHT);

    App {
        renderer,
        scene,
        camera,
    }
}

/// The page's `animate()`.
pub fn animate(app: &mut App) {
    // `const timer = 0.0001 * Date.now();`
    let timer = 0.0001 * date_now_ms();

    // `mouseX` / `mouseY` are 0 with no pointer events, and the camera starts at
    // x = y = 0, so both of these are no-ops.
    {
        let mut camera_object = app.camera.node.borrow_mut();
        let position = &mut camera_object.position;
        position.x += (0.0 - position.x) * 0.05;
        position.y += (0.0 - position.y) * 0.05;
    }

    app.camera.look_at(&Vector3::ZERO);

    // `for ( let i = 0 … spheres.length )` — the scene holds nothing but the
    // spheres, so `scene.children` *is* the page's `spheres` array.
    for (i, sphere) in app.scene.children().iter().enumerate() {
        let mut sphere = sphere.borrow_mut();
        sphere.position.x = 5.0 * (timer + i as f64).cos();
        sphere.position.y = 5.0 * (timer + i as f64 * 1.1).sin();
    }

    app.renderer.render(&mut app.scene, &mut app.camera);
}

/// 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.
pub fn resize(app: &mut App, width: f64, height: f64) {
    // The page also sets its module-level `windowHalfX` / `windowHalfY` here.
    // They exist only to recentre the `pointermove` handler's `mouseX` /
    // `mouseY`, which this port has no equivalent of, so there is nothing
    // here to update.
    app.camera.aspect = width / height;
    app.camera.update_projection_matrix();
    app.renderer.set_size(width, height);
}

/// The example's controls, for a host that has a pointer. `None` here:
/// the page creates none.
pub fn controls(_app: &mut App) -> Option<&mut OrbitControls> {
    None
}

/// The controls and the camera at once, for a host delivering pointer events.
/// `None` here: the page creates no controls.
pub fn controls_and_camera(_app: &mut App) -> Option<(&mut OrbitControls, &mut PerspectiveCamera)> {
    None
}

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_materials_basic.png".to_string());
    three_rs::testing::write_png(&path, width, height, &pixels);
    println!("wrote {path} ({width}x{height})");
}