Exporting
Save what a scene shows as an image or a video, or its meshes as an OBJ file that Blender and other 3D programs can open.
import { exportOBJ, download } from "trid3d";
download(scene.screenshot(), "scene.png"); // an image
download(exportOBJ(scene), "scene.obj"); // the 3D models
const clip = scene.record({ duration: 3 }); // a 3-second video
download(clip.done, "clip.webm");import { Scene, makeShape, OrbitCamera, FpsMeter, exportOBJ, download } from "./trid/index.js";
export default function (canvas) {
const scene = new Scene(canvas, { width: 800, height: 320, camera: { z: -2.2, fov: 0.7 } });
const box = scene.add({
name: "box", mesh: makeShape("cube", { size: 0.5, radius: 0.05, segments: 2 }), pos: { x: -0.7, y: 0, z: 0 },
material: { fill: "#E9806E", smooth: true },
});
const ball = scene.add({
name: "ball", mesh: makeShape("sphere", { r: 0.3 }), pos: { x: 0.1, y: 0, z: 0 },
material: { fill: "#8ab4f8", smooth: true },
});
const ring = scene.add({
name: "ring", mesh: makeShape("torus", { R: 0.22, tube: 0.07 }), pos: { x: 0.85, y: 0, z: 0 },
material: { fill: "#7ae96e", smooth: true },
});
scene.use(new OrbitCamera({ pitch: -0.3 }));
scene.use(new FpsMeter({ color: "#888888" }));
// Buttons over the top-right corner of the canvas
const bar = document.createElement("div");
bar.className = "trid-demo-bar";
const status = document.createElement("span");
const button = (text, onclick) => {
const b = document.createElement("button");
b.textContent = text;
b.onclick = onclick;
bar.append(b);
return b;
};
bar.append(status);
// 1. A still image: a new frame at twice the size, without the FPS meter
button("Screenshot", () => {
download(scene.screenshot({ width: 1600, height: 640, tools: false }), "trid.png");
});
// 2. A 3-second video of the canvas
const rec = button("Record 3 s", async () => {
let recording;
try {
recording = scene.record({ duration: 3 });
} catch (err) {
status.textContent = err.message;
return;
}
rec.disabled = true;
status.textContent = "● recording…";
const video = await recording.done;
rec.disabled = false;
status.textContent = `${(video.size / 1e6).toFixed(1)} MB`;
download(video, video.type.includes("mp4") ? "trid.mp4" : "trid.webm");
});
// 3. The objects as an OBJ file, placed where they are in the scene
button("Export .obj", () => download(exportOBJ(scene), "trid.obj"));
canvas.parentElement.append(bar);
scene.start((dt) => {
box.rot.y += dt * 0.6;
box.rot.x += dt * 0.3;
ring.rot.z += dt * 0.8;
ball.pos.y = Math.sin(performance.now() / 600) * 0.15;
});
return () => {
scene.destroy();
bar.remove();
};
}Saving files
download(data, filename) saves something in the browser's downloads. data can be a file (Blob), text, or a promise for either, so you can pass scene.screenshot() or recording.done straight in.
download(scene.screenshot(), "scene.png");
download(exportOBJ(mesh), "model.obj");Browsers only allow downloads that start from a user action, like a click, so call it from a button.
If you'd rather keep the file (to upload it, or show it in an <img>), use the Blob directly:
const image = await scene.screenshot();
img.src = URL.createObjectURL(image);Screenshots
scene.screenshot(options) draws a new frame and returns it as an image file (a promise for a Blob).
The frame is drawn on a separate canvas, so the page doesn't flicker, and it works whether or not the scene is running.
| Option | Default | Meaning |
|---|---|---|
type | "image/png" | "image/png", "image/jpeg" or "image/webp" |
quality | 0.92 | For JPEG and WebP, from 0 to 1 |
width, height | the canvas's | Size of the image in pixels. A bigger size gives a sharper image |
tools | true | Include what tools draw on top, like the FPS meter |
// A 4K wallpaper, without the FPS meter
download(scene.screenshot({ width: 3840, height: 2160, tools: false }), "wallpaper.png");Line widths and tool overlays are in pixels, so they look thinner on a bigger image. Raise lineWidth for the shot if you need to.
Videos
scene.record(options) starts recording the canvas as a video, tools included. The scene has to be running (scene.start()), because only new frames are recorded.
| Option | Default | Meaning |
|---|---|---|
duration | none | Stop by itself after this many seconds |
fps | 60 | Frames per second |
bitsPerSecond | 8000000 | Quality: higher is sharper and bigger |
type | the best one available | Video format, like "video/webm" |
It returns a recording:
| Property | Meaning |
|---|---|
stop() | Stops recording. Returns a promise for the video |
done | A promise for the video, once recording stops |
recording | Whether it's still recording |
type | The video format |
const recording = scene.record();
stopButton.onclick = async () => download(await recording.stop(), "clip.webm");Chrome, Edge and Firefox record WebM; Safari records MP4. Check recording.type to name the file. record throws an Error in browsers that can't record a canvas at all.
OBJ files
exportOBJ(input, options) writes meshes as the text of an OBJ file, the format Trid loads. input can be:
| Input | What's written |
|---|---|
| A mesh | The mesh as it is |
A scene object ({ mesh, pos, rot, scale }) | The mesh where the object is: its position, rotation and scale are applied |
| A list of meshes or objects | One object (o) each in the file |
A Scene | Every object in it |
Objects are named after their name field when they have one (scene.add({ name: "box", mesh })).
What's written:
- Vertices, faces, texture coordinates and normals (when the mesh has them).
- Lines (
l) for edges that aren't the side of a face, so wireframe-only meshes are kept too. - Material names (
usemtl) for meshes with a material per face: the names from the OBJ file it came from (mesh.materialNames), or the materials'namefields. - Texture coordinates as they're drawn: an object whose material has a
mapping,textureRepeat,textureOffsetortextureRotationgets the coordinates the renderer uses, so its texture lands the same way in other programs.
Trid flips Z when it loads an OBJ file (see Loading OBJ files), and exportOBJ flips it back. A model loaded and exported again comes out the same as the original.
| Option | Default | Meaning |
|---|---|---|
uvs | true | Write texture coordinates |
normals | true | Write the mesh's normals, when it has them |
lines | true | Write edges that aren't the side of a face |
precision | 6 | Decimals kept in numbers. Fewer makes smaller files |
header | "Exported by Trid" | Comment at the top of the file; "" for none |
mtllib | none | Name of an .mtl file to link. Every material then gets a name (see below) |
Colors and textures (MTL)
OBJ files only hold shapes; colors and textures go in a separate .mtl file next to it, and the textures in image files. exportModel gives you all of them:
const files = await exportModel(scene, { name: "sword" });
// sword.obj, sword.mtl, sword_blade.png...
for (const f of files) await download(f.data, f.name);Save them in the same folder (or zip them into one download) and the model opens with its colors and textures in Blender and other 3D programs.
Or write the two text files yourself:
download(exportOBJ(scene, { mtllib: "scene.mtl" }), "scene.obj");
download(exportMTL(scene), "scene.mtl");- Each material object gets one name, used by the OBJ's
usemtllines and the MTL'snewmtlblocks: itsnamefield, or the object's name (sword, orsword_1for an object's second material). Materials shared by several objects are written once. - The MTL has each material's fill (
Kd; white under a texture), see-through fills asd, and the texture's file name (map_Kd),name.pngby default. PasstextureFile: (name, texture) => "textures/" + name + ".png"to change it. objMaterials(input)lists the materials with their names, textures and file names, to save the textures yourself.textureToBlob(texture)turns one into a PNG.exportMTLalso takes plain descriptions:exportMTL([{ name: "red", fill: "#f00" }, { name: "skin", texture: "skin.png" }]).