import * as THREE from "./vendor/three.module.js";
// The profile of each digit runs from the palm into a rounded tip.
// Dimensions are in the glove's local units, with +Y toward the fingertips.
const digits = [
{ root: [-.50, -.16, .075], spread: 56, lengths: [.36, .285, .155], radii: [.226, .176], delay: .14 },
{ root: [-.48, .31, .015], spread: 12, lengths: [.355, .26, .17], radii: [.199, .166], delay: 0 },
{ root: [-.15, .37, 0], spread: 3, lengths: [.398, .284, .19], radii: [.206, .171], delay: .075 },
{ root: [.19, .35, -.012], spread: -6, lengths: [.375, .265, .17], radii: [.196, .163], delay: .15 },
{ root: [.49, .27, -.032], spread: -15, lengths: [.30, .21, .13], radii: [.178, .146], delay: .23 },
];
const samplesPerDigit = 5;
const pointCount = digits.length * samplesPerDigit;
const vertexShader = `
varying vec3 localPosition;
void main() {
localPosition = position;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`;
const fragmentShader = `
uniform vec4 digitPoints[${pointCount}];
uniform float unfold;
uniform float curls[${digits.length}];
uniform float thumbWrap;
uniform float wrappedThumb;
uniform float fistThumbWrap;
uniform float shadowAccent;
uniform vec3 viewRay;
uniform vec3 localLight;
uniform mat4 clipFromLocal;
varying vec3 localPosition;
float ellipsoid(vec3 p, vec3 radii) {
float k0 = length(p / radii);
float k1 = length(p / (radii * radii));
return k0 * (k0 - 1.0) / max(k1, 0.001);
}
float join(float a, float b, float size) {
float overlap = max(size - abs(a - b), 0.0) / size;
return min(a, b) - overlap * overlap * size * 0.25;
}
float segment(vec3 p, vec4 a, vec4 b) {
vec3 ab = b.xyz - a.xyz;
vec3 ap = p - a.xyz;
float t = clamp(dot(ap, ab) / max(dot(ab, ab), 0.0001), 0.0, 1.0);
vec3 offset = ap - ab * t;
offset.z *= 1.14;
return length(offset) - mix(a.w, b.w, t);
}
float digitDistance(vec3 p, int base) {
float d = segment(p, digitPoints[base], digitPoints[base + 1]);
for (int j = 1; j < ${samplesPerDigit - 1}; j++) {
d = min(d, segment(p, digitPoints[base + j], digitPoints[base + j + 1]));
}
return d;
}
float contactShadow(float a, float b, float reach) {
// The farther of the two surfaces controls the ambient shadow.
return 1.0 - smoothstep(.012, reach, max(a, b));
}
float fingerShadows(vec3 p) {
float thumb = digitDistance(p, 0);
float palm = ellipsoid(p - vec3(-.025, .045, 0.0), vec3(.728, .54, .365));
float palmSide = smoothstep(.08, .23, p.z);
float thumbHeight = smoothstep(-.23, .14, p.y);
float shadow = contactShadow(thumb, palm, .18) * palmSide * thumbHeight *
thumbWrap * (1.0 - wrappedThumb);
if (wrappedThumb > .002) {
float index = digitDistance(p, ${samplesPerDigit});
float middle = digitDistance(p, ${2 * samplesPerDigit});
float underThumb = join(palm, join(index, middle, .052), .060);
float reach = .19 + fistThumbWrap * .048;
float wrap = contactShadow(thumb, underThumb, reach) * thumbHeight;
shadow = max(shadow, wrap * wrappedThumb * (1.0 + fistThumbWrap * .13));
}
float previous = thumb;
for (int i = 1; i < ${digits.length}; i++) {
float next = digitDistance(p, i * ${samplesPerDigit});
float fold = max(curls[i - 1], curls[i]);
float placement = i == 1 ? palmSide * smoothstep(.02, .24, p.y) * (1.0 - wrappedThumb)
: smoothstep(mix(.32, .29, shadowAccent), .51, p.y);
float reach = mix(.152, .179, shadowAccent);
shadow = max(shadow, contactShadow(previous, next, reach) * fold * placement);
previous = next;
}
return shadow * smoothstep(.78, 1.0, unfold);
}
float palmBack(vec2 p) {
vec2 xy = (p - vec2(-.025, mix(-.09, .045, unfold))) /
mix(vec2(.68), vec2(.728, .54), unfold);
return -mix(.68, .365, unfold) * sqrt(max(.035, 1.0 - dot(xy, xy)));
}
float slot(vec3 p, float x, float top) {
float y = clamp(p.y, -.10, top);
float axis = x * (1.0 + .36 * y);
float depth = p.z - palmBack(vec2(axis, y)) + .030;
return length(vec3(p.x - axis, p.y - y, depth)) - .052;
}
float slotField(vec3 p) {
if (p.z > -.13 || p.y < -.20 || p.y > .38 || abs(p.x) > .35) return 1.0;
float cut = min(slot(p, -.214, .177), slot(p, .0, .242));
cut = min(cut, slot(p, .214, .198));
return cut + (1.0 - smoothstep(.70, .96, unfold)) * .085;
}
float surface(vec3 p) {
vec3 center = vec3(-.025, mix(-.09, .045, unfold), 0.0);
const float restRadius = .68;
if (unfold < .002) return length(p - center) - restRadius;
vec3 radii = mix(vec3(restRadius), vec3(.728, .54, .365), unfold);
float shape = ellipsoid(p - center, radii);
vec3 wristCenter = vec3(.04, mix(-.19, -.49, unfold), -.01);
vec3 wristRadii = mix(vec3(.27, .21, .23), vec3(.42, .34, .295), unfold);
float wrist = ellipsoid(p - wristCenter, wristRadii);
vec3 cuffCenter = vec3(.04, mix(-.26, -.70, unfold), -.012);
vec3 cuffRadii = mix(vec3(.22,.11,.17), vec3(.46,.18,.335), unfold);
float cuff = ellipsoid(p - cuffCenter, cuffRadii);
shape = join(shape, join(wrist, cuff, .095), .16);
float blend = mix(.245, .125, smoothstep(.23, .76, p.y)) * unfold + .025;
for (int i = 0; i < ${pointCount}; i += ${samplesPerDigit}) {
float joinWidth = i == 0 ? mix(blend, .146, wrappedThumb) - .018 * fistThumbWrap : blend;
shape = join(shape, digitDistance(p, i), joinWidth);
}
// Shallow cavities in the dorsal surface disappear into the sphere.
if (unfold > .70) shape = -join(-shape, slotField(p), .027);
return shape;
}
void main() {
vec3 direction = normalize(viewRay);
vec3 p = localPosition;
vec3 side = mix(vec3(-1.0), vec3(1.0), step(vec3(0.0), direction));
vec3 rayLimit = (side * vec3(1.83, 1.76, 1.34) - p) /
(side * max(abs(direction), vec3(.00001)));
float limit = min(min(rayLimit.x, rayLimit.y), rayLimit.z) + .015;
float t = 0.0;
bool hit = false;
for (int i = 0; i < 46; i++) {
float d = surface(p);
if (d < .0035) {
hit = true;
break;
}
t += max(d * .90, .004);
if (t > limit) break;
p = localPosition + direction * t;
}
if (!hit) discard;
// Shade the glove as a whole so finger bends don't create small highlights.
float cuff = smoothstep(-.72, -.65, p.y) - smoothstep(-.64, -.57, p.y);
float lightness = smoothstep(-1.03, .85, dot(vec3(p.xy, p.z * .24), localLight));
vec3 color = vec3(mix(.805, 1.0, .27 + lightness * .73));
color *= 1.0 - cuff * smoothstep(.76, 1.0, unfold) * .053;
// Soft directional shading shows the part of each finger folded over the palm.
float palmSide = smoothstep(.10, .30, p.z) * smoothstep(.78, 1.0, unfold);
if (palmSide > .002) {
vec2 e = vec2(.024, -.024);
vec3 n = normalize(
e.xyy * surface(p + e.xyy) + e.yxy * surface(p + e.yxy) +
e.yyx * surface(p + e.yyx) + e.xxx * surface(p + e.xxx)
);
float foldLight = smoothstep(-.65, .85, dot(n, localLight));
color *= 1.0 - palmSide * (.055 + (1.0 - foldLight) * .12);
}
if (unfold > .70 && p.z < -.13) {
float nearRecess = 1.0 - smoothstep(0.0, .043, abs(slotField(p)));
float depth = p.z - palmBack(p.xy);
float recessedShadow = smoothstep(.001, .028, depth) * nearRecess;
color *= 1.0 - recessedShadow * .145;
}
if (unfold > .78 && p.y > -.23) color *= 1.0 - fingerShadows(p) * mix(.145, .167, shadowAccent);
gl_FragColor = vec4(color, 1.0);
vec4 depth = clipFromLocal * vec4(p, 1.0);
gl_FragDepth = depth.z / depth.w * .5 + .5;
}
`;
const X = new THREE.Vector3(1, 0, 0);
const Z = new THREE.Vector3(0, 0, 1);
const rootOfBlob = new THREE.Vector3(-.04, -.02, .02);
const toRadians = THREE.MathUtils.degToRad;
export function makeSoftGlove() {
const points = Array.from({ length: pointCount }, () => new THREE.Vector4());
// In Point the thumb rounds the side below the index and rests across the middle finger.
const pointingThumbCurve = [
new THREE.Vector3(-.89, -.065, .05),
new THREE.Vector3(-.93, .405, .32),
new THREE.Vector3(-.33, .36, .51),
];
// In Fist the thumb lies across the curled index and middle fingers.
const fistThumbCurve = [
new THREE.Vector3(-.96, -.095, .15),
new THREE.Vector3(-.78, .47, .60),
new THREE.Vector3(-.045, .435, .55),
];
const uniforms = {
digitPoints: { value: points },
unfold: { value: 0 },
curls: { value: new Float32Array(digits.length) },
thumbWrap: { value: 0 },
wrappedThumb: { value: 0 },
fistThumbWrap: { value: 0 },
shadowAccent: { value: 0 },
viewRay: { value: new THREE.Vector3() },
localLight: { value: new THREE.Vector3() },
clipFromLocal: { value: new THREE.Matrix4() },
};
const hand = new THREE.Mesh(new THREE.BoxGeometry(3.66, 3.52, 2.68), new THREE.ShaderMaterial({
vertexShader, fragmentShader, uniforms, toneMapped: false,
}));
hand.rotation.order = "YXZ";
const root = new THREE.Vector3();
const cursor = new THREE.Vector3();
const curve = new THREE.CubicBezierCurve3();
const controls = [curve.v1, curve.v2, curve.v3];
const step = new THREE.Vector3();
const spread = new THREE.Quaternion();
const flex = new THREE.Quaternion();
const orientation = new THREE.Quaternion();
const inverse = new THREE.Matrix4();
const key = new THREE.Vector3(-.49, .71, .75);
const mix = THREE.MathUtils.lerp;
function pose({
x, y, z, width, height, tilt, turn, roll, open, fingers,
shadowAccent = 0, indexExtension = 0, pointThumbWrap = 0, fistThumbWrap = 0,
}) {
hand.position.set(x, y, z);
hand.rotation.set(toRadians(tilt), toRadians(turn), toRadians(roll));
hand.scale.set(width, height, mix(1, width, .60));
uniforms.unfold.value = open;
uniforms.shadowAccent.value = shadowAccent;
const wrappedThumb = pointThumbWrap + fistThumbWrap;
uniforms.wrappedThumb.value = wrappedThumb;
uniforms.fistThumbWrap.value = fistThumbWrap;
uniforms.thumbWrap.value = THREE.MathUtils.smoothstep(-fingers[0][3], 20, 56);
digits.forEach((digit, index) => {
const [first, second, third, flair] = fingers[index];
uniforms.curls.value[index] = THREE.MathUtils.smoothstep(second + third, 55, 117);
const emerge = THREE.MathUtils.smoothstep(open, digit.delay, .94);
root.fromArray(digit.root);
cursor.copy(rootOfBlob).lerp(root, emerge);
const base = index * samplesPerDigit;
const radiusScale = mix(.44, 1, emerge);
const [baseRadius, tipRadius] = digit.radii;
curve.v0.copy(cursor);
spread.setFromAxisAngle(Z, toRadians(digit.spread + flair));
orientation.copy(spread);
[first, second, third].forEach((bend, joint) => {
flex.setFromAxisAngle(X, toRadians(bend));
orientation.multiply(flex);
// Extend the pointing tip while preserving its contact with the thumb at the base.
const length = digit.lengths[joint] + (index === 1 && joint > 0 ? indexExtension * .5 : 0);
step.set(0, length * emerge, 0).applyQuaternion(orientation);
cursor.add(step);
controls[joint].copy(cursor);
});
if (index === 0 && wrappedThumb > 0) {
const fistWeight = fistThumbWrap / wrappedThumb;
for (let joint = 0; joint < controls.length; joint++) {
cursor.copy(pointingThumbCurve[joint]).lerp(fistThumbCurve[joint], fistWeight);
cursor.lerp(rootOfBlob, 1 - emerge);
controls[joint].lerp(cursor, wrappedThumb);
}
}
// The pose defines a continuous bend from base to tip.
for (let sample = 0; sample < samplesPerDigit; sample++) {
const t = sample / (samplesPerDigit - 1);
curve.getPoint(t, cursor);
const thumbPadding = index === 0 ? wrappedThumb * (.10 * t * (1 - t) + .014 * t) : 0;
const radius = (mix(baseRadius, tipRadius, t) + thumbPadding) * radiusScale;
points[base + sample].set(cursor.x, cursor.y, cursor.z, radius);
}
});
}
function fingertip(index, target) {
const tip = points[(index + 1) * samplesPerDigit - 1];
hand.updateMatrix();
target.set(tip.x, tip.y, tip.z).applyMatrix4(hand.matrix);
return tip.w / 1.14;
}
function updateCamera(camera) {
hand.updateWorldMatrix(true, false);
camera.updateMatrixWorld();
inverse.copy(hand.matrixWorld).invert();
camera.getWorldDirection(uniforms.viewRay.value).transformDirection(inverse);
uniforms.localLight.value.copy(key).transformDirection(camera.matrixWorld).transformDirection(inverse);
uniforms.clipFromLocal.value.copy(camera.projectionMatrix).multiply(camera.matrixWorldInverse).multiply(hand.matrixWorld);
}
return { hand, pose, fingertip, updateCamera };
}