Expand description
This crate adds Three.js 3D scenes to Autumn apps. It works with Maud and htmx.
Add ThreePlugin to the app. Put three_stylesheet() and
three_script() in the page <head>. Then write a Scene:
use autumn_plugin_three::{
Color, Controls, Environment, Mesh, Scene, ThreePlugin, three_script, three_stylesheet,
};
use autumn_web::prelude::*;
#[get("/")]
async fn index() -> Markup {
html! {
html {
head { (three_stylesheet()) (three_script()) }
body {
(Scene::new()
.label("An orange torus knot")
.environment(Environment::Room)
.controls(Controls::Orbit)
.turntable(20.0)
.add(Mesh::torus_knot(0.8, 0.25).color(Color::hex(0xff7a18)).metalness(0.6)))
}
}
}
}
autumn_web::app()
.plugin(ThreePlugin::new())
.routes(routes![index])
.run()
.await;§How it works
- The crate contains Three.js 0.185.1 (MIT) and
five addons. It does not use npm or a bundler.
THREE_ASSETSserves them under/static/_plugins/three/. SRI coversinit.jsand the core modules. The addons load from the same origin without SRI. - The builder renders
data-three-*attributes.init.jsreads them and builds the scene. You can also write the attributes by hand. init.jsscans on load, onhtmx:afterSwap, and on each DOM insertion. It disposes a scene when its element leaves the document.- The
three:readyevent gives your own JavaScript the scene, camera, renderer, and the shared Three.js module.
§Limits
- The plugin uses WebGL only. It does not support the WebGPU renderer.
- The declarative layer has no shadows, post-processing, or physics. Use
the
three:readyevent for custom code. - Each scene has its own WebGL context. Browsers keep about 16 contexts. When the browser drops a context, the scene shows its fallback.
- The plugin loads only glTF 2.0 and GLB files. It does not decode Draco, KTX2, or Meshopt compression. Images inside a GLB work with the default CSP. External model and image URLs need a CSP that allows them.
- Do not let user content keep
data-three-*attributes. User markup could then start scenes and load model URLs.
Structs§
- Camera
- A perspective camera.
- Color
- An sRGB color.
- Light
- A light.
- Mesh
- A primitive mesh: a
Geometrywith aMaterial. - Model
- A glTF / GLB model, loaded with the vendored
GLTFLoader. - Scene
- A 3D scene. Renders one
<div data-three="scene">element. - Three
Plugin - Installs the Three.js assets in an Autumn app.
- Vec3
- A 3D vector: position, rotation (degrees), scale, or spin (degrees per second).
- Vendored
File - Provenance of one vendored upstream file.
Enums§
- Aspect
- The aspect ratio (width / height) of a scene.
- Clip
- Which animation clips of a
Modelplay. - Controls
- Camera controls.
- Environment
- Image-based scene light.
- Geometry
- A primitive geometry. Sizes are in Three.js units.
- Light
Kind - The kind of a
Light. - Material
- A mesh material.
- Scene
Object - One object in a
Scene.
Constants§
- ASSETS_
NAMESPACE - URL namespace of the bundle. Files are served under
/static/_plugins/three/. - PLUGIN_
NAME - The plugin name in Autumn diagnostics.
- THREE_
MODULE_ URL - Plain URL of the Three.js ES module.
- THREE_
VERSION - Pinned Three.js version.
- VENDORED
- The vendored upstream files and their pins.
Statics§
- THREE_
ASSETS - The plugin asset bundle.
Functions§
- three_
script - Renders the tags that load Three.js and the plugin runtime.
- three_
stylesheet - Renders the
<link>tag for the plugin stylesheet.