Media and drawing
Things in space: canvas scene=
Media and drawing
scene.md

Things in space: canvas scene=

A canvas with scene= draws things in three dimensions with WebGL 2, in the words three.js uses: a scene of meshes and groups, materials, lights, a camera. It draws them plainly at once, and with render= works out a lit picture (sun, sky, shadows, light bouncing, glass, metal, things that glow) with a path tracer written in Mote's runtime. canvas boxes= (in the card) is the same engine with a plainer way of saying boxes; both stay.

z is up (x across, y back), unlike three.js, where y is up. Lengths are the page's own units. A position is a geometry's middle (for an outline pushed up, three.extrude, it is the outline's own origin: below). Rotations are degrees about x, then y, then z.

mote
derive oak = three.material({color: "#c8a878", roughness: 0.6})
derive steel = three.material({color: "#c9ccd1", roughness: 0.2, metalness: 1})
// A table: a top on four legs, as one group that turns and moves together.
derive table = three.group([
three.mesh(three.box(120, 70, 4), oak, {position: [0, 0, 72], id: "top", name: "Top"}),
three.mesh(three.cylinder(3, 70), steel, {position: [-52, -28, 35]}),
three.mesh(three.cylinder(3, 70), steel, {position: [52, -28, 35]}),
three.mesh(three.cylinder(3, 70), steel, {position: [-52, 28, 35]}),
three.mesh(three.cylinder(3, 70), steel, {position: [52, 28, 35]})
], {rotation: [0, 0, 25]})
derive world = three.scene([table], {lights: [three.sun(300, 50, 1), three.sky(1)]})
derive eye = three.perspective({position: [220, -300, 200], target: [0, 0, 40], fov: 40})
row width=640 height=420
canvas scene=world camera=eye render="real" samples=64 change=picked flex=1 min-width=0

Everything three.* makes is a plain record, so a page may write the records itself, build them in a fn, or keep them in state:

the record
three.box(width, depth, height){type: "box", size: [width, depth, height]} (sizes along x, y, z)
three.sphere(radius){type: "sphere", radius}
three.cylinder(radius, height){type: "cylinder", radius, height} (upright along z until turned)
three.plane(width, depth){type: "plane", width, depth} (flat on x and y)
three.extrude(points, height, {holes}){type: "extrude", points: [[x, y], …], height, holes} (an outline on x and y, pushed up along z)
three.material({…}){color, roughness, metalness, transmission, emissive, edges}, and for a picture {map, mapSize, mapOffset, mapTurn, tint}
three.mesh(geometry, material, {…}){geometry, material, position, rotation, scale, id, name}, and for a picture {grain, sides, faces}
three.group(children, {…}){children, position, rotation, scale}
three.scene(objects, {…}){objects, lights, ground}
three.sun(turn, height, intensity){type: "sun", turn, height, intensity}
three.sky(intensity){type: "sky", intensity}
three.perspective({…}){type: "perspective", position, target, fov}
three.orthographic({…}){type: "orthographic", position, target, size}

Materials. color is "#rrggbb". roughness runs from 0 (a mirror) to 1 (matte; 0.7 if not said). metalness is 0 or 1. transmission (0 to 1) is how much light passes: glass. emissive is light given off, in the sun's units: a lamp. edges: false leaves the outline of a box or an extrusion off the plain drawing (edges: "#333333" colours it). The plain drawing uses the colour and edges; the lit picture uses all of it. map puts a picture on it: below.

Meshes and groups. position: [x, y, z], rotation: [x, y, z] in degrees, scale (a number, or [x, y, z] on a mesh). A group places everything in it together, groups inside groups too; a group's scale is one number. id is what a press reports; name shows on hover with the thing's size.

Lights. three.sun(turn, height, intensity): where the sun stands, in degrees round (0 is along x, 90 along y) and up from the horizon. With no sun in the scene (and no sun= on the canvas) it stands over the left shoulder of whoever is looking. lights: [three.sky(1)] alone is an overcast day: no sun. ground: false takes the floor away; ground: {color: "#b9b2a6", height: 0} says what it is. Anything emissive lights what is near it.

The camera. camera= takes {position, target, fov} (fov in degrees, up and down: 40 if not said) or {type: "orthographic", size} (how much of the scene's own units it sees up and down). What is not said is the scene's own: it looks at the scene's middle from far enough to see all of it. view= ("iso", "front", "back", "left", "right", "plan", or "cam:yaw,pitch,zoom,ortho") still works without camera=. A drag orbits about what the camera looks at; the wheel zooms.

What the canvas tells the page, exactly as canvas boxes= does: a press on a thing sends change with its id (change=picked with action picked(id: string)); tool=, render= ("still" or "real"), samples=, exposure=, sun= and the input events ({phase: "cam" | "gpu" | "render" | "done" | "saved", …}) are as the card says for boxes. A scene with pictures says two more, "materials" and "limit": below.

Outlines pushed up: three.extrude#

three.extrude(points, height) is a flat outline on x and y, pushed up along z: a top shaped like an L, a cabinet whose front is cut across a corner, a panel with a notch, a wedge. The outline is a list of [x, y] corners going round either way, and may bend inward as well as outward (it need not be convex); the last corner joins the first. {holes: [[[x, y], …], …]} cuts outlines out of it: a sink out of a worktop.

Examplea whole program
state picked = ""
derive oak = three.material({color: "#c8a878", roughness: 0.6})
derive stone = three.material({color: "#d8d6d0", roughness: 0.3})
// A cabinet in the corner of two walls, from above. The corner of the walls is 0, 0;
// the front is cut across from one arm to the other.
derive plan = [[0, 0], [90, 0], [90, 58], [58, 90], [0, 90]]
// The worktop over it and its neighbours: an L, with a sink cut out of one arm.
derive top = [[0, 0], [170, 0], [170, 62], [62, 62], [62, 170], [0, 170]]
derive sink = [[92, 12], [150, 12], [150, 50], [92, 50]]
derive corner = three.group([
three.mesh(three.extrude(plan, 2), oak, {position: [0, 0, 11], id: "bottom", name: "Bottom"}),
three.mesh(three.extrude(plan, 2), oak, {position: [0, 0, 46], id: "shelf", name: "Shelf"}),
three.mesh(three.box(90, 2, 74), oak, {position: [45, 1, 47], id: "back", name: "Back"}),
three.mesh(three.box(2, 90, 74), oak, {position: [1, 45, 47], id: "side", name: "Side"}),
three.mesh(three.box(80, 58, 74), oak, {position: [130, 29, 47], id: "right", name: "Drawers"}),
three.mesh(three.box(58, 80, 74), oak, {position: [29, 130, 47], id: "left", name: "Doors"}),
three.mesh(three.extrude(top, 4, {holes: [sink]}), stone, {position: [0, 0, 86], id: "top", name: "Worktop"})
])
derive world = three.scene([corner], {lights: [three.sun(20, 50, 1), three.sky(1)]})
derive eye = three.perspective({position: [400, 360, 320], target: [70, 70, 40], fov: 40})
action pick(id: string)
picked = id
column gap=8 padding=16 height=480
canvas scene=world camera=eye render="real" samples=64 change=pick flex=1
text (picked == "" ? "Press a piece" : `Picked ${picked}`)

Pictures on materials: map#

Examplea whole program
// A picture 64 by 64: red and blue squares. A shop's own is a photograph of the board.
derive veneer = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAAW0lEQVR42u3QMQ0AQAwDsUIplIdQ/lOhPIYO2SzdHjm1r6P1bLQCAAAAAAAAAAAAAAAAAAAAAAAAAAAAADgD0gPpgwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAzn0sY4EtWbO6uAAAAABJRU5ErkJggg=="
// One copy covers 600 by 600 of the scene, whatever the panel's size.
derive oak = three.material({color: "#c8a878", map: veneer, mapSize: [600, 600], roughness: 0.6})
derive band = three.material({color: "#f2efe8"})
derive cabinet = three.group([
// A door: the grain runs up it (z, as it is unsaid); its four thin sides are a plain band.
three.mesh(three.box(450, 18, 720), oak, {position: [225, 9, 360], sides: band, id: "door", name: "Door"}),
// A rail: the same material, the grain along its length.
three.mesh(three.box(900, 18, 90), oak, {position: [450, 9, 780], grain: "x", sides: band, id: "rail", name: "Rail"}),
// A top shaped like an L: the picture on its two caps, the band round its sides.
three.mesh(three.extrude([[0, 0], [900, 0], [900, 300], [300, 300], [300, 600], [0, 600]], 30), oak, {position: [0, 0, 850], sides: band, id: "top", name: "Top"})
])
derive world = three.scene([cabinet], {lights: [three.sun(300, 50, 1), three.sky(1)]})
derive eye = three.perspective({position: [-700, -1500, 1300], target: [450, 150, 420], fov: 40})
column gap=8 padding=16 height=480
canvas scene=world camera=eye render="real" samples=64 flex=1

Loading, and what input hears#

esc
1072 sections of 111 pages, ranked as you type.A heading's words count for more than what stands under it.
↑↓move
↵open
ranked by BM25, in the page