Shape Zoo
Shape Zoo
Solid and wireframe spheres, cubes, capsules, and arrows demonstrate temporary scene shapes and camera controls. These shapes do not change saved project data.
Add Arrow creates a random arrow and frames the scene. Clear Arrows removes the arrows you added and keeps the original shapes.
Starting SpatialEngine...
const rotation = { x: 0, y: 0, z: 0, w: 1 };
const vec = (x, y, z) => ({ x, y, z });
export async function startDemo({ client, root, enabled, report }) {
const bounds = await loadZoo(client);
const actions = shapeControls(client, bounds);
const messages = {
frame: "Framed the shapes.",
arrow: "Drew an arrow.",
clear: "Cleared the added arrows.",
};
for (const [command, action] of Object.entries(actions)) {
root.querySelector(`[data-command="${command}"]`).addEventListener("click", async () => {
enabled(false);
try {
await action();
report(messages[command]);
} catch (error) {
report("Statement failed: " + error.message, { error: true });
} finally {
enabled(true);
}
});
}
}
export function arrowPose(start, end) {
const [x, y, z] = [end.x - start.x, end.y - start.y, end.z - start.z];
const length = Math.hypot(x, y, z);
if (!Number.isFinite(length) || length === 0)
throw new RangeError("Arrow endpoints must be distinct and finite");
const norm = Math.hypot(z / length, -x / length, 1 + y / length);
return {
length,
translation: start,
rotation:
y / length < -1 + 1e-12
? { x: 1, y: 0, z: 0, w: 0 }
: {
x: z / length / norm,
y: 0,
z: -x / length / norm,
w: (1 + y / length) / norm,
},
};
}
export async function addArrow(client, { start, end, appearance }) {
const pose = arrowPose(start, end);
const node = await client.scene.spawn();
await Promise.all([
client.scene.shape.set({
node,
shape: {
geometry: { kind: "arrow", value: { length: pose.length } },
appearance,
},
}),
client.scene.transform.set({
node,
transform: {
translation: pose.translation,
rotation: pose.rotation,
scale: vec(1, 1, 1),
},
}),
]);
return node;
}
export async function clearAddedArrows(client, nodes) {
await Promise.all(nodes.map((node) => client.scene.despawn({ node })));
nodes.length = 0;
}
export function shapeControls(client, initialBounds = zooRegion) {
const arrows = [];
let bounds = initialBounds;
const frame = async (durationSeconds = 0.6) => {
await client.viewport.camera.zoomToAabb({
...bounds,
viewport: "main",
durationSeconds,
});
};
return {
frame,
async arrow() {
const args = arrowArgs();
arrows.push(await addArrow(client, args));
bounds = includePoints(bounds, [args.start, args.end], 0.25);
await frame();
},
async clear() {
await clearAddedArrows(client, arrows);
bounds = initialBounds;
await frame();
},
};
}
export function includePoints(bounds, points, padding = 0) {
if (!points.length) return bounds;
const next = bounds
? { ...bounds }
: {
minX: Infinity,
minY: Infinity,
minZ: Infinity,
maxX: -Infinity,
maxY: -Infinity,
maxZ: -Infinity,
};
for (const point of points) {
next.minX = Math.min(next.minX, point.x - padding);
next.minY = Math.min(next.minY, point.y - padding);
next.minZ = Math.min(next.minZ, point.z - padding);
next.maxX = Math.max(next.maxX, point.x + padding);
next.maxY = Math.max(next.maxY, point.y + padding);
next.maxZ = Math.max(next.maxZ, point.z + padding);
}
return next;
}
export const arrowRegion = {
minX: -4,
minY: -4,
minZ: -4,
maxX: 4,
maxY: 4,
maxZ: 4,
};
export const zooRegion = {
minX: -1,
minY: -8,
minZ: -1,
maxX: 10,
maxY: -3,
maxZ: 1,
};
export function arrowArgs(random = Math.random) {
const center = vec(random() * 5 - 2.5, random() * 5 - 2.5, random() * 5 - 2.5);
const z = random() * 2 - 1;
const angle = random() * 2 * Math.PI;
const radius = Math.sqrt(1 - z * z);
const length = 1 + random() * 2;
const offset = vec(
(length * radius * Math.cos(angle)) / 2,
(length * radius * Math.sin(angle)) / 2,
(length * z) / 2,
);
const start = vec(center.x - offset.x, center.y - offset.y, center.z - offset.z);
const end = vec(center.x + offset.x, center.y + offset.y, center.z + offset.z);
const channel = () => Math.floor(64 + random() * 192);
return {
start,
end,
appearance: {
color: { r: channel(), g: channel(), b: channel(), a: 255 },
wireframe: false,
},
};
}
export async function loadZoo(client, variedColors = false) {
const palette = [
[255, 16, 32],
[255, 112, 0],
[255, 224, 0],
[32, 255, 0],
[0, 255, 192],
[0, 160, 255],
[80, 0, 255],
[255, 0, 192],
];
let colorIndex = 0;
const spawning = [];
for (const [y, wireframe, color] of [
[-4, true, { r: 196, g: 140, b: 255, a: 255 }],
[-7, false, { r: 91, g: 59, b: 255, a: 255 }],
]) {
const arrow = arrowPose(vec(9, y, -0.7), vec(9, y, 0.7));
const shapes = [
{
geometry: { kind: "sphere", value: { radius: 0.7 } },
translation: vec(0, y, 0),
},
{
geometry: { kind: "cube", value: { halfExtents: 0.7 } },
translation: vec(3, y, 0),
},
{
geometry: { kind: "capsule", value: { radius: 0.35, length: 1 } },
translation: vec(6, y, 0),
},
{
geometry: { kind: "arrow", value: { length: arrow.length } },
translation: arrow.translation,
orientation: arrow.rotation,
},
];
spawning.push(
...shapes.map(async ({ geometry, translation, orientation = rotation }) => {
const [r, g, b] = palette[colorIndex++];
const appearance = {
color: variedColors ? { r, g, b, a: 255 } : color,
wireframe,
};
const node = await client.scene.spawn();
await Promise.all([
client.scene.shape.set({ node, shape: { geometry, appearance } }),
client.scene.transform.set({
node,
transform: {
translation,
rotation: orientation,
scale: vec(1, 1, 1),
},
}),
]);
}),
);
}
await Promise.all(spawning);
return zooRegion;
}