Tear: irregular fibre-elongated tears, stricter stretch guard, visible flakes and dust; remove debug hooks
Browse files
app.js
CHANGED
|
@@ -351,9 +351,6 @@ function setTear(on) {
|
|
| 351 |
if (hover) { hover.userData.own.uHi.value = 0; hover = null; }
|
| 352 |
}
|
| 353 |
}
|
| 354 |
-
window.__tear = () => tear;
|
| 355 |
-
window.__focus = (id) => openFocus(meshes.find((m) => m.userData.w.id === id));
|
| 356 |
-
window.__unroll = () => ({ t, target, playing, input: +scrubIn.value });
|
| 357 |
|
| 358 |
// unwrap the peel angle across windings so the roll turns continuously
|
| 359 |
let lastEnd = null;
|
|
|
|
| 351 |
if (hover) { hover.userData.own.uHi.value = 0; hover = null; }
|
| 352 |
}
|
| 353 |
}
|
|
|
|
|
|
|
|
|
|
| 354 |
|
| 355 |
// unwrap the peel angle across windings so the roll turns continuously
|
| 356 |
let lastEnd = null;
|
tear.js
CHANGED
|
@@ -38,8 +38,8 @@ const P = {
|
|
| 38 |
peelProb: 0.25,
|
| 39 |
shatterSpeed: 700, // mm/s impact speed that shatters links at a node
|
| 40 |
tipGrow: 0.8, // a crack tip advances while nearby links are above this fraction of failure
|
| 41 |
-
fibreBias: 0.
|
| 42 |
-
turnJitter: 0.
|
| 43 |
branch: 0.025, // chance per step that a crack forks
|
| 44 |
stepsPerTip: 4, // propagation steps per tip per frame, at most
|
| 45 |
stepBudget: 100, // propagation steps per frame, all cracks
|
|
@@ -85,7 +85,7 @@ export function createTear(ctx) {
|
|
| 85 |
N += 2 * R * C;
|
| 86 |
ground = Math.min(ground, o.y - H);
|
| 87 |
}
|
| 88 |
-
ground -=
|
| 89 |
const pos = new Float32Array(N * 3), prev = new Float32Array(N * 3), rest = new Float32Array(N * 3);
|
| 90 |
const inv = new Float32Array(N), stuck = new Uint8Array(N), valid = new Uint8Array(N);
|
| 91 |
const sheetOf = new Int16Array(N);
|
|
@@ -390,7 +390,7 @@ void main() {
|
|
| 390 |
} else if (off) z -= off;
|
| 391 |
p[v * 3] = x; p[v * 3 + 1] = y; p[v * 3 + 2] = z;
|
| 392 |
}
|
| 393 |
-
// guard: a triangle stretched past
|
| 394 |
// Then flat normals, written straight into the attribute (cheaper than computeVertexNormals).
|
| 395 |
const m = F.tm, nr = F.na.array;
|
| 396 |
for (let t = 0; t < F.n; t++) {
|
|
@@ -402,7 +402,7 @@ void main() {
|
|
| 402 |
const rx = m[q + i * 2] - m[q + j * 2], ry = m[q + i * 2 + 1] - m[q + j * 2 + 1];
|
| 403 |
const r = Math.sqrt(rx * rx + ry * ry) + thick;
|
| 404 |
const dx = p[o + i * 3] - p[o + j * 3], dy = p[o + i * 3 + 1] - p[o + j * 3 + 1], dz = p[o + i * 3 + 2] - p[o + j * 3 + 2];
|
| 405 |
-
const lim =
|
| 406 |
if (!(dx * dx + dy * dy + dz * dz <= lim * lim)) bad = true; // also catches NaN
|
| 407 |
}
|
| 408 |
if (bad) {
|
|
@@ -721,7 +721,7 @@ void main() {
|
|
| 721 |
fragmentShader: `varying float vPh;
|
| 722 |
void main(){ vec2 d = gl_PointCoord - 0.5; float a = atan(d.y, d.x) * 3.0 + vPh * 6.28;
|
| 723 |
float r = 0.36 + 0.12 * sin(a) + 0.06 * sin(a * 2.3); if (length(d) > r) discard;
|
| 724 |
-
vec3 c = mix(vec3(0.
|
| 725 |
}));
|
| 726 |
flakes.frustumCulled = false;
|
| 727 |
group.add(flakes);
|
|
@@ -735,8 +735,8 @@ void main() {
|
|
| 735 |
vertexShader: `attribute float aLife; varying float vL; uniform float uScale;
|
| 736 |
void main(){ vL = aLife; vec4 mv = modelViewMatrix * vec4(position,1.0); gl_Position = projectionMatrix * mv; gl_PointSize = (1.0 + (1.0 - aLife) * 2.5) * 9.0 * uScale / -mv.z; }`,
|
| 737 |
fragmentShader: `varying float vL;
|
| 738 |
-
void main(){ float d = length(gl_PointCoord - 0.5) * 2.0; float a = smoothstep(1.0, 0.0, d) * vL * 0.
|
| 739 |
-
gl_FragColor = vec4(vec3(0.
|
| 740 |
}));
|
| 741 |
dust.frustumCulled = false;
|
| 742 |
group.add(dust);
|
|
@@ -1078,8 +1078,9 @@ void main() {
|
|
| 1078 |
n *= sign(dot(n, v));
|
| 1079 |
float diff = 0.3 + 0.7 * max(dot(n, l), 0.0);
|
| 1080 |
// black carbon with a faint glassy sheen on fresh fracture faces
|
| 1081 |
-
float spec = pow(max(dot(reflect(-l, n), v), 0.0),
|
| 1082 |
-
vec3 base = mix(vec3(0.
|
|
|
|
| 1083 |
gl_FragColor = vec4(base * diff + spec, 1.0);
|
| 1084 |
}`,
|
| 1085 |
});
|
|
@@ -1249,11 +1250,15 @@ void main() {
|
|
| 1249 |
active[sheetOf[best]] = 1; calm[sheetOf[best]] = 0;
|
| 1250 |
// pinch everything within about 1.6 nodes of the hit, on the layer that was touched
|
| 1251 |
pinch = []; const offs = [];
|
| 1252 |
-
|
|
|
|
| 1253 |
for (let k = s.base[L]; k < s.base[L] + s.R * s.C; k++) {
|
| 1254 |
if (!valid[k]) continue;
|
| 1255 |
const o = k * 3, dx = pos[o] - pos[bo], dy = pos[o + 1] - pos[bo + 1], dz = pos[o + 2] - pos[bo + 2];
|
| 1256 |
-
|
|
|
|
|
|
|
|
|
|
| 1257 |
pinch.push(k); offs.push(dx, dy, dz);
|
| 1258 |
unstick(k); inv[k] = 0; // held by the pointer
|
| 1259 |
}
|
|
@@ -1345,15 +1350,8 @@ void main() {
|
|
| 1345 |
return {
|
| 1346 |
group, step, reset, shake,
|
| 1347 |
setCrush(v) { crushOn = v; },
|
| 1348 |
-
crushTest: (n = 20) => { let made = 0; const s = sheets[Math.floor(sheets.length / 2)], lay = s.lay[0]; for (let i = 0; i < n; i++) { const k = lay.base + Math.floor(s.R / 2) * s.C + Math.floor(s.C / 2) + (i % 5) - 2; made += crushAt(lay, k, new THREE.Vector3(pos[k * 3], pos[k * 3 + 1], pos[k * 3 + 2])); } return { made, crumbs: cCount }; },
|
| 1349 |
setSound(v) { soundOn = v; if (v && audio && audio.state === 'suspended') audio.resume(); },
|
| 1350 |
stats: () => ({ nodes: N, links: NC, activeSheets: active.reduce((a, b) => a + b, 0), simMs: +simMs.toFixed(2), lastMs: +lastMs.toFixed(2), prof, breaks, debris: spawned }),
|
| 1351 |
isGrabbing: () => grabbed >= 0,
|
| 1352 |
-
debug: () => sheets.map((s, si) => s.cracked && s.lay.map((l) => {
|
| 1353 |
-
const a = l.F.pa.array; let nan = 0, far = 0, mnY = 1e9, mxY = -1e9;
|
| 1354 |
-
for (let i = 0; i < l.F.n * 9; i += 3) { const v = i / 3, tt = Math.floor(v / 3), q = v % 3; const rx = s.x0 + l.F.tm[tt * 6 + q * 2], ry = s.y0 - l.F.tm[tt * 6 + q * 2 + 1]; if (Math.hypot(a[i] - rx, a[i + 1] - ry, a[i + 2] - l.z) > 5) far++; if (!isFinite(a[i])) nan++; mnY = Math.min(mnY, a[i + 1]); mxY = Math.max(mxY, a[i + 1]); }
|
| 1355 |
-
let stuckN = 0, ny = 0, my = 0; for (let k = l.base; k < l.base + s.R * s.C; k++) { if (valid[k]) { ny++; my += pos[k * 3 + 1]; stuckN += stuck[k]; } }
|
| 1356 |
-
return { si, far, sample: Array.from(a.slice(0, 9)).map((x) => +x.toFixed(1)), tm: Array.from(l.F.tm.slice(0, 6)), x0: s.x0, y0: s.y0, z: l.z, tris: l.F.n, segs: l.nseg, tips: l.tips && l.tips.length, nan, mnY, mxY, nodeMeanY: my / ny, restY: s.y0 - s.H / 2, stuckN, ny, vis: l.F.mesh.visible, coarseVis: l.coarse.visible, draw: l.F.geo.drawRange.count };
|
| 1357 |
-
})).filter(Boolean),
|
| 1358 |
};
|
| 1359 |
}
|
|
|
|
| 38 |
peelProb: 0.25,
|
| 39 |
shatterSpeed: 700, // mm/s impact speed that shatters links at a node
|
| 40 |
tipGrow: 0.8, // a crack tip advances while nearby links are above this fraction of failure
|
| 41 |
+
fibreBias: 0.62, // how strongly cracks follow the fibres of their layer
|
| 42 |
+
turnJitter: 0.38, // radians of random wander per step
|
| 43 |
branch: 0.025, // chance per step that a crack forks
|
| 44 |
stepsPerTip: 4, // propagation steps per tip per frame, at most
|
| 45 |
stepBudget: 100, // propagation steps per frame, all cracks
|
|
|
|
| 85 |
N += 2 * R * C;
|
| 86 |
ground = Math.min(ground, o.y - H);
|
| 87 |
}
|
| 88 |
+
ground -= 8; // floor close below the strip, so fallen pieces and dust stay in view
|
| 89 |
const pos = new Float32Array(N * 3), prev = new Float32Array(N * 3), rest = new Float32Array(N * 3);
|
| 90 |
const inv = new Float32Array(N), stuck = new Uint8Array(N), valid = new Uint8Array(N);
|
| 91 |
const sheetOf = new Int16Array(N);
|
|
|
|
| 390 |
} else if (off) z -= off;
|
| 391 |
p[v * 3] = x; p[v * 3 + 1] = y; p[v * 3 + 2] = z;
|
| 392 |
}
|
| 393 |
+
// guard: a triangle stretched past 2.2x its rest size, or with a bad number, is a binding glitch: hide it.
|
| 394 |
// Then flat normals, written straight into the attribute (cheaper than computeVertexNormals).
|
| 395 |
const m = F.tm, nr = F.na.array;
|
| 396 |
for (let t = 0; t < F.n; t++) {
|
|
|
|
| 402 |
const rx = m[q + i * 2] - m[q + j * 2], ry = m[q + i * 2 + 1] - m[q + j * 2 + 1];
|
| 403 |
const r = Math.sqrt(rx * rx + ry * ry) + thick;
|
| 404 |
const dx = p[o + i * 3] - p[o + j * 3], dy = p[o + i * 3 + 1] - p[o + j * 3 + 1], dz = p[o + i * 3 + 2] - p[o + j * 3 + 2];
|
| 405 |
+
const lim = 2.2 * r + 0.05; // brittle: real carbon never stretches this far, so it is a glitch
|
| 406 |
if (!(dx * dx + dy * dy + dz * dz <= lim * lim)) bad = true; // also catches NaN
|
| 407 |
}
|
| 408 |
if (bad) {
|
|
|
|
| 721 |
fragmentShader: `varying float vPh;
|
| 722 |
void main(){ vec2 d = gl_PointCoord - 0.5; float a = atan(d.y, d.x) * 3.0 + vPh * 6.28;
|
| 723 |
float r = 0.36 + 0.12 * sin(a) + 0.06 * sin(a * 2.3); if (length(d) > r) discard;
|
| 724 |
+
vec3 c = mix(vec3(0.06,0.045,0.032), vec3(0.22,0.16,0.11), fract(vPh * 7.3)); gl_FragColor = vec4(c, 1.0); }`,
|
| 725 |
}));
|
| 726 |
flakes.frustumCulled = false;
|
| 727 |
group.add(flakes);
|
|
|
|
| 735 |
vertexShader: `attribute float aLife; varying float vL; uniform float uScale;
|
| 736 |
void main(){ vL = aLife; vec4 mv = modelViewMatrix * vec4(position,1.0); gl_Position = projectionMatrix * mv; gl_PointSize = (1.0 + (1.0 - aLife) * 2.5) * 9.0 * uScale / -mv.z; }`,
|
| 737 |
fragmentShader: `varying float vL;
|
| 738 |
+
void main(){ float d = length(gl_PointCoord - 0.5) * 2.0; float a = smoothstep(1.0, 0.0, d) * vL * 0.55; if (a < 0.01) discard;
|
| 739 |
+
gl_FragColor = vec4(vec3(0.34,0.29,0.24), a); }`,
|
| 740 |
}));
|
| 741 |
dust.frustumCulled = false;
|
| 742 |
group.add(dust);
|
|
|
|
| 1078 |
n *= sign(dot(n, v));
|
| 1079 |
float diff = 0.3 + 0.7 * max(dot(n, l), 0.0);
|
| 1080 |
// black carbon with a faint glassy sheen on fresh fracture faces
|
| 1081 |
+
float spec = pow(max(dot(reflect(-l, n), v), 0.0), 24.0) * 0.38;
|
| 1082 |
+
vec3 base = mix(vec3(0.035, 0.026, 0.019), vec3(0.17, 0.12, 0.085), vTone);
|
| 1083 |
+
diff += 0.25 * pow(1.0 - abs(dot(n, v)), 2.0); // rim light so dark flakes read against the dark floor
|
| 1084 |
gl_FragColor = vec4(base * diff + spec, 1.0);
|
| 1085 |
}`,
|
| 1086 |
});
|
|
|
|
| 1250 |
active[sheetOf[best]] = 1; calm[sheetOf[best]] = 0;
|
| 1251 |
// pinch everything within about 1.6 nodes of the hit, on the layer that was touched
|
| 1252 |
pinch = []; const offs = [];
|
| 1253 |
+
// an irregular blob, longer along the layer's fibres, so the torn piece is not a neat disc
|
| 1254 |
+
const bo = best * 3, fib = lay.fib, ph = Math.random() * 6.3;
|
| 1255 |
for (let k = s.base[L]; k < s.base[L] + s.R * s.C; k++) {
|
| 1256 |
if (!valid[k]) continue;
|
| 1257 |
const o = k * 3, dx = pos[o] - pos[bo], dy = pos[o + 1] - pos[bo + 1], dz = pos[o + 2] - pos[bo + 2];
|
| 1258 |
+
const along = Math.abs(dx * fib[0] - dy * fib[1]), across = Math.abs(dx * fib[1] + dy * fib[0]);
|
| 1259 |
+
const a = Math.atan2(dy, dx);
|
| 1260 |
+
const r = cell * (1.25 + 0.45 * Math.sin(2 * a + ph) + 0.3 * Math.sin(5 * a + ph * 2) + 0.35 * Math.random());
|
| 1261 |
+
if ((along / 1.45) ** 2 + across ** 2 + dz * dz > r * r) continue;
|
| 1262 |
pinch.push(k); offs.push(dx, dy, dz);
|
| 1263 |
unstick(k); inv[k] = 0; // held by the pointer
|
| 1264 |
}
|
|
|
|
| 1350 |
return {
|
| 1351 |
group, step, reset, shake,
|
| 1352 |
setCrush(v) { crushOn = v; },
|
|
|
|
| 1353 |
setSound(v) { soundOn = v; if (v && audio && audio.state === 'suspended') audio.resume(); },
|
| 1354 |
stats: () => ({ nodes: N, links: NC, activeSheets: active.reduce((a, b) => a + b, 0), simMs: +simMs.toFixed(2), lastMs: +lastMs.toFixed(2), prof, breaks, debris: spawned }),
|
| 1355 |
isGrabbing: () => grabbed >= 0,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1356 |
};
|
| 1357 |
}
|