glove.js
At e51515aCopy the commit ID
e51515aHistory
Preview
Code8.3 KiBimport * as THREE from "./vendor/three.module.js";
import { makeSoftGlove } from "./soft-glove.js?grid-loops";
import { floorY } from "./finger-loops.js";
function starShape(kind) {
const shape = new THREE.Shape();
if (kind === "diamond") {
shape.moveTo(0, .27);
shape.lineTo(.155, 0);
shape.lineTo(0, -.27);
shape.lineTo(-.155, 0);
} else if (kind === "moon") {
shape.moveTo(.10, .245);
shape.bezierCurveTo(-.24, .33, -.38, -.15, -.11, -.235);
shape.quadraticCurveTo(.065, -.30, .22, -.135);
shape.bezierCurveTo(-.003, -.21, -.14, .05, .10, .245);
} else {
shape.moveTo(0, .28);
shape.quadraticCurveTo(.023, .056, .22, 0);
shape.quadraticCurveTo(.027, -.039, 0, -.28);
shape.quadraticCurveTo(-.031, -.048, -.22, 0);
shape.quadraticCurveTo(-.031, .053, 0, .28);
}
shape.closePath();
return shape;
}
function makeProp(kind) {
const group = new THREE.Group();
const material = new THREE.MeshBasicMaterial({ color: 0xd9d9d9, transparent: true, toneMapped: false });
const geometry = kind === "pearl" ? new THREE.SphereGeometry(.18, 32, 24)
: new THREE.ExtrudeGeometry(starShape(kind), {
depth: .075, bevelSize: .026, bevelThickness: .022, bevelSegments: 4,
});
const prop = new THREE.Mesh(geometry, material);
group.add(prop);
const dust = Array.from({ length: 7 }, (_, index) => {
const speck = new THREE.Mesh(new THREE.SphereGeometry(.018 + (index % 3) * .006, 8, 6), material);
group.add(speck);
return speck;
});
return { group, material, prop, dust };
}
function makeShadow() {
const canvas = document.createElement("canvas");
canvas.width = canvas.height = 128;
const ctx = canvas.getContext("2d");
const gradient = ctx.createRadialGradient(64, 64, 0, 64, 64, 64);
gradient.addColorStop(0, "rgba(0,0,0,.62)");
gradient.addColorStop(.45, "rgba(0,0,0,.28)");
gradient.addColorStop(1, "rgba(0,0,0,0)");
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, 128, 128);
const map = new THREE.CanvasTexture(canvas);
const shadow = new THREE.Mesh(new THREE.PlaneGeometry(2.7, .82), new THREE.MeshBasicMaterial({
map, transparent: true, depthWrite: false,
}));
shadow.rotation.x = -Math.PI / 2;
shadow.position.set(0, floorY + .008, .22);
return shadow;
}
function makeGrid() {
const material = new THREE.ShaderMaterial({
transparent: true, depthWrite: false, toneMapped: false,
uniforms: { emphasis: { value: 0 } },
vertexShader: `
varying vec2 gridPosition;
void main() {
gridPosition = position.xy;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
varying vec2 gridPosition;
uniform float emphasis;
float lines(float spacing) {
vec2 p = gridPosition / spacing;
vec2 width = max(fwidth(p), vec2(.0001));
vec2 gap = abs(fract(p + .5) - .5);
vec2 line = 1.0 - smoothstep(width * .16, width * .87, gap);
return max(line.x, line.y);
}
void main() {
float fine = lines(.48);
float major = lines(1.92);
vec2 width = max(fwidth(gridPosition), vec2(.0001));
vec2 axes = 1.0 - smoothstep(width * .2, width, abs(gridPosition));
float ink = fine * .17 + major * .08 + max(axes.x, axes.y) * .12;
float fade = 1.0 - smoothstep(1.4, 5.4, length(gridPosition));
gl_FragColor = vec4(vec3(.76), ink * fade * mix(.64, 1.0, emphasis));
}
`,
});
const grid = new THREE.Mesh(new THREE.PlaneGeometry(12, 12), material);
grid.rotation.x = -Math.PI / 2;
grid.position.y = floorY;
return grid;
}
export async function createStage(element, variant) {
const glove = makeSoftGlove();
const hand = glove.hand;
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setPixelRatio(Math.min(1.4, window.devicePixelRatio));
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.04;
element.append(renderer.domElement);
const scene = new THREE.Scene();
// Mirror the hand and its choreography together for a right-hand performance.
scene.scale.x = -1;
const camera = new THREE.OrthographicCamera(-3, 3, 2, -2, .1, 40);
const grid = makeGrid();
const shadow = makeShadow();
shadow.renderOrder = 1;
const tip = new THREE.Vector3();
const fingertipShadows = Array.from({ length: 4 }, () => {
const detail = shadow.clone();
detail.material = shadow.material.clone();
detail.visible = false;
detail.renderOrder = 2;
return detail;
});
const effect = makeProp(variant.prop);
const cue = new THREE.Mesh(new THREE.SphereGeometry(.032, 16, 12), new THREE.MeshBasicMaterial({
color: 0xd4d4d4, transparent: true, depthWrite: false, toneMapped: false,
}));
cue.position.set(1.17, .52, .50);
scene.add(grid, shadow, ...fingertipShadows, hand, effect.group, cue);
const defaultOrbit = Math.PI;
let orbit = defaultOrbit;
let elevationOffset = 0;
let drag;
let lastPose;
const baseElevation = () => THREE.MathUtils.lerp(.22, .54, lastPose?.floorView ?? 0);
function redraw() {
const distance = 8;
const floorView = lastPose?.floorView ?? 0;
const elevation = baseElevation() + elevationOffset;
const azimuth = orbit + floorView * .34;
camera.position.set(
Math.sin(azimuth) * Math.cos(elevation) * distance,
Math.sin(elevation) * distance,
Math.cos(azimuth) * Math.cos(elevation) * distance,
);
camera.lookAt(
THREE.MathUtils.lerp(-.13, -.05, floorView),
THREE.MathUtils.lerp(.13, -1.02, floorView), 0,
);
glove.updateCamera(camera);
renderer.render(scene, camera);
}
new ResizeObserver(([entry]) => {
const { width, height } = entry.contentRect;
if (!width || !height) return;
const half = Math.max(1.89, 2.3 * height / width);
camera.top = half;
camera.bottom = -half;
camera.left = -half * width / height;
camera.right = half * width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
if (lastPose) redraw();
}).observe(element);
const canvas = renderer.domElement;
canvas.setAttribute("aria-hidden", "true");
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
drag = { x: event.clientX, y: event.clientY };
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!drag) return;
orbit -= (event.clientX - drag.x) * .009;
const base = baseElevation();
elevationOffset = THREE.MathUtils.clamp(
elevationOffset + (event.clientY - drag.y) * .006, -.43 - base, 1.08 - base,
);
drag = { x: event.clientX, y: event.clientY };
redraw();
});
canvas.addEventListener("lostpointercapture", () => { drag = undefined; });
element.parentElement.querySelector(".reset-view").addEventListener("click", () => {
orbit = defaultOrbit;
elevationOffset = 0;
redraw();
});
function draw(pose) {
lastPose = pose;
glove.pose(pose);
const floorView = pose.floorView ?? 0;
grid.material.uniforms.emphasis.value = floorView;
shadow.position.x = pose.x * .75;
shadow.position.z = .22 - floorView * .35;
shadow.scale.y = 1 + floorView * .60;
shadow.material.opacity = .66 - pose.y * .20;
fingertipShadows.forEach((detail, index) => {
detail.visible = floorView === 1;
if (!detail.visible) return;
const radius = glove.fingertip(index + 1, tip);
const clearance = Math.max(0, tip.y - floorY - radius);
const contact = 1 - THREE.MathUtils.smoothstep(clearance, .007, .25);
detail.position.set(tip.x, floorY + .014, tip.z);
detail.scale.set(.14 + clearance * .18, .46 + clearance * .20, 1);
detail.material.opacity = .20 + contact * .72;
});
cue.visible = pose.cue > .004;
cue.material.opacity = pose.cue;
effect.group.visible = pose.spark > .004;
effect.group.position.set(pose.sparkX, pose.sparkY, .57);
effect.group.scale.setScalar(.39 + pose.rise * .98);
effect.material.opacity = pose.spark;
effect.prop.rotation.set(pose.rise * .12, pose.rise * .33, pose.rise * -.32);
effect.dust.forEach((speck, index) => {
const angle = index * 2.39996;
const reach = pose.rise * (.25 + (index % 3) * .075);
speck.position.set(reach * Math.cos(angle), reach * Math.sin(angle), Math.sin(angle * 2) * reach * .8);
});
redraw();
}
return draw;
}