SharpMeow commited on
Commit
9728790
·
1 Parent(s): 01b454f

Sandbox debris: triangles bound to removed nodes are hidden instead of hanging frozen; tiny loose pieces become crumbs, small pieces held by a glued node are freed, a sheet only sleeps when no free piece is off the floor; crumbs and flakes settle and fade like the main view; cache-busting ?v= on the JS modules; sandboxStats/sandboxSim test hooks

Browse files
Files changed (3) hide show
  1. app.js +4 -2
  2. index.html +3 -3
  3. tear.js +101 -10
app.js CHANGED
@@ -4,12 +4,12 @@ import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
4
  import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
5
  import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js';
6
  import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
7
- import { createTear } from './tear.js';
8
  import { loadRegistry, listLocal, getLocal, localKey, remoteSource, fileSource, findManifest, buildMeshScroll, decodeImage } from './lib/scrolls.js';
9
  import { validateManifest, peelOrder, layerWindings, layerFile, roleOf, fill, REGISTRY_LIMITS, LOCAL_LIMITS } from './lib/manifest.js';
10
  import { setupOpenOwn } from './lib/openown.js';
11
  import { unpackInt16 } from './lib/formats.js';
12
- import { createDamage, injectFrag, DMG_HEAD } from './damage.js';
13
 
14
  const $ = (id) => document.getElementById(id);
15
  const params = new URLSearchParams(location.hash.slice(1));
@@ -1430,6 +1430,8 @@ requestAnimationFrame(() => loadOverlay());
1430
  // camera hooks for the keyboard shortcuts in polish.js
1431
  window.unrollView = {
1432
  damageStats: () => damage.stats(),
 
 
1433
  crumble: () => crumble(), // owner feature: crumble the whole scroll to dust; Reset restores it
1434
  damageSim: (n, dt) => damage.simulate(n, dt),
1435
  surfaceScreen: (j, u, v) => { const p = surfacePoint(meshes[j], u, v, new THREE.Vector3()); if (!p) return null; p.project(camera); return [Math.round((p.x + 1) / 2 * innerWidth), Math.round((1 - p.y) / 2 * innerHeight), meshes[j].userData.own.uP.value]; },
 
4
  import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
5
  import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js';
6
  import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
7
+ import { createTear } from './tear.js?v=20261016b';
8
  import { loadRegistry, listLocal, getLocal, localKey, remoteSource, fileSource, findManifest, buildMeshScroll, decodeImage } from './lib/scrolls.js';
9
  import { validateManifest, peelOrder, layerWindings, layerFile, roleOf, fill, REGISTRY_LIMITS, LOCAL_LIMITS } from './lib/manifest.js';
10
  import { setupOpenOwn } from './lib/openown.js';
11
  import { unpackInt16 } from './lib/formats.js';
12
+ import { createDamage, injectFrag, DMG_HEAD } from './damage.js?v=20261016b';
13
 
14
  const $ = (id) => document.getElementById(id);
15
  const params = new URLSearchParams(location.hash.slice(1));
 
1430
  // camera hooks for the keyboard shortcuts in polish.js
1431
  window.unrollView = {
1432
  damageStats: () => damage.stats(),
1433
+ sandboxStats: () => (tear ? tear.stats() : null),
1434
+ sandboxSim: (n) => { if (!tear) return null; for (let i = 0; i < n; i++) { tear.step(1 / 60); damage.update(1 / 60, shared.uTime.value += 1 / 60, 1 / 60); } return tear.stats(); },
1435
  crumble: () => crumble(), // owner feature: crumble the whole scroll to dust; Reset restores it
1436
  damageSim: (n, dt) => damage.simulate(n, dt),
1437
  surfaceScreen: (j, u, v) => { const p = surfacePoint(meshes[j], u, v, new THREE.Vector3()); if (!p) return null; p.project(camera); return [Math.round((p.x + 1) / 2 * innerWidth), Math.round((1 - p.y) / 2 * innerHeight), meshes[j].userData.own.uP.value]; },
index.html CHANGED
@@ -201,8 +201,8 @@ if (!/(^|[#&])s=/.test(location.hash)) ['scrolls/index.json', 'scrolls/PHerc0139
201
  </div>
202
  </section>
203
 
204
- <script type="module" src="app.js?v=20261016a"></script>
205
- <script type="module" src="polish.js?v=20261016a"></script>
206
- <script type="module" src="lib/bench.js?v=20261016a"></script>
207
  </body>
208
  </html>
 
201
  </div>
202
  </section>
203
 
204
+ <script type="module" src="app.js?v=20261016b"></script>
205
+ <script type="module" src="polish.js?v=20261016b"></script>
206
+ <script type="module" src="lib/bench.js?v=20261016b"></script>
207
  </body>
208
  </html>
tear.js CHANGED
@@ -1,4 +1,4 @@
1
- import { DMG_HEAD as DMG_HEAD_T, DMG_CUT as DMG_CUT_T, DMG_EDGE as DMG_EDGE_T } from './damage.js';
2
  // "Tear apart": a just-for-fun brittle fracture toy on top of the unrolled strip.
3
  // Not a real unrolling. Position based dynamics (Verlet + distance constraints) on typed arrays,
4
  // with real cracks: a crack is a jagged polyline in the sheet's own material space that grows from
@@ -400,6 +400,10 @@ ${DMG_EDGE_T}
400
  const o = t * 9, q = t * 6;
401
  const thick = F.kd[t] === 2 ? P.thickness : 0;
402
  let bad = F.kd[t] === 3; // crushed away
 
 
 
 
403
  for (let e = 0; e < 3 && !bad; e++) {
404
  const i = e, j = e === 2 ? 0 : e + 1;
405
  const rx = m[q + i * 2] - m[q + j * 2], ry = m[q + i * 2 + 1] - m[q + j * 2 + 1];
@@ -765,7 +769,11 @@ ${DMG_EDGE_T}
765
  for (let k = 0; k < FL; k++) {
766
  const o = k * 3;
767
  if (fp[o + 1] < -1e4) continue;
768
- if (fp[o + 1] <= ground + 0.2) { fp[o + 1] = ground + 0.2; continue; }
 
 
 
 
769
  // tiny and light: fall fast at first, then terminal velocity with a little flutter
770
  const drag = 2.5 + 40 / fs[k];
771
  fv[o + 1] -= P.gravity * 0.35 * dt;
@@ -983,6 +991,75 @@ ${DMG_EDGE_T}
983
  test(f0, NC);
984
  }
985
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
986
  let time = 0, frameNo = 0;
987
  let simMs = 0, lastMs = 0, prof = null;
988
  function step(dt) {
@@ -1011,6 +1088,8 @@ ${DMG_EDGE_T}
1011
  impacts.length = 0;
1012
  growCracks();
1013
  const tC = performance.now();
 
 
1014
  stepDebris(dt, time);
1015
  stepCrumbs(dt, time);
1016
  for (let si = 0; si < sheets.length; si++) {
@@ -1022,7 +1101,8 @@ ${DMG_EDGE_T}
1022
  }
1023
  const wake = active[si] || s.lay.some((l) => l.F && (l.F.dirty.size || l.F.attrsDirty));
1024
  if (!wake) continue;
1025
- if (active[si] && calm[si] > 60) { active[si] = 0; calm[si] = 0; }
 
1026
  for (const lay of s.lay) {
1027
  if (lay.F) {
1028
  rebindDirty(lay);
@@ -1050,6 +1130,10 @@ ${DMG_EDGE_T}
1050
  const cp = new Float32Array(CMAX * 3), cv = new Float32Array(CMAX * 3), csz = new Float32Array(CMAX);
1051
  const crot = new Float32Array(CMAX * 3), cav = new Float32Array(CMAX * 3), clive = new Uint8Array(CMAX), crest = new Uint8Array(CMAX);
1052
  let cCount = 0, crushOn = false, crushing = false, lastCrush = 0;
 
 
 
 
1053
  const flakeShape = new THREE.BufferGeometry();
1054
  {
1055
  // an irregular, slightly bent flake: a fan of 7 triangles
@@ -1100,7 +1184,7 @@ ${DMG_EDGE_T}
1100
  if (cCount < CMAX) k = cCount++;
1101
  else { for (let i = 0; i < CMAX; i++) if (!clive[i]) { k = i; break; } }
1102
  if (k < 0) k = Math.floor(Math.random() * CMAX); // full: recycle a random one (usually a settled crumb)
1103
- clive[k] = 1; crest[k] = 0;
1104
  cp[k * 3] = x; cp[k * 3 + 1] = y; cp[k * 3 + 2] = z;
1105
  cv[k * 3] = vx; cv[k * 3 + 1] = vy; cv[k * 3 + 2] = vz;
1106
  csz[k] = size;
@@ -1139,14 +1223,21 @@ ${DMG_EDGE_T}
1139
  crot[o] = Math.PI / 2 + (Math.random() - 0.5) * 0.3; crot[o + 1] = (Math.random() - 0.5) * 0.3;
1140
  cv[o] *= 0.2; cv[o + 2] *= 0.2;
1141
  }
1142
- } else if (Math.abs(cv[o]) + Math.abs(cv[o + 2]) > 1) {
1143
- cp[o] += cv[o] * dt; cp[o + 2] += cv[o + 2] * dt;
1144
- cv[o] *= Math.exp(-8 * dt); cv[o + 2] *= Math.exp(-8 * dt);
 
 
 
 
1145
  }
 
 
 
1146
  _e.set(crot[o], crot[o + 1], crot[o + 2]);
1147
  _q.setFromEuler(_e);
1148
  _p.set(cp[o], cp[o + 1], cp[o + 2]);
1149
- _s.set(sz, sz, sz);
1150
  _m.compose(_p, _q, _s);
1151
  crumbs.setMatrixAt(k, _m);
1152
  }
@@ -1202,7 +1293,7 @@ ${DMG_EDGE_T}
1202
  made++;
1203
  }
1204
  for (let q = 0; q < 3; q++) spawnDust(w[0] + (Math.random() - 0.5) * R0, w[1] + (Math.random() - 0.5) * R0, w[2]);
1205
- active[sheetOf[k0]] = 1; calm[sheetOf[k0]] = 0;
1206
  crunch();
1207
  breaks++;
1208
  return made;
@@ -1350,7 +1441,7 @@ ${DMG_EDGE_T}
1350
  group, step, reset, shake,
1351
  setCrush(v) { crushOn = v; },
1352
  setSound(v) { soundOn = v; if (v && audio && audio.state === 'suspended') audio.resume(); },
1353
- 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 }),
1354
  isGrabbing: () => grabbed >= 0,
1355
  };
1356
  }
 
1
+ import { DMG_HEAD as DMG_HEAD_T, DMG_CUT as DMG_CUT_T, DMG_EDGE as DMG_EDGE_T } from './damage.js?v=20261016b';
2
  // "Tear apart": a just-for-fun brittle fracture toy on top of the unrolled strip.
3
  // Not a real unrolling. Position based dynamics (Verlet + distance constraints) on typed arrays,
4
  // with real cracks: a crack is a jagged polyline in the sheet's own material space that grows from
 
400
  const o = t * 9, q = t * 6;
401
  const thick = F.kd[t] === 2 ? P.thickness : 0;
402
  let bad = F.kd[t] === 3; // crushed away
403
+ // bound to a node that is gone (crushed or retired as debris) or to nothing at all: that node no longer moves,
404
+ // so the triangle would hang frozen in mid-air
405
+ for (let q = t * 12, qe = q + 12; q < qe && !bad; q++) if (F.bw[q] !== 0 && !valid[F.bi[q]]) bad = true;
406
+ if (!bad && F.bw[t * 12] === 0 && F.bw[t * 12 + 1] === 0 && F.bw[t * 12 + 2] === 0 && F.bw[t * 12 + 3] === 0) bad = true;
407
  for (let e = 0; e < 3 && !bad; e++) {
408
  const i = e, j = e === 2 ? 0 : e + 1;
409
  const rx = m[q + i * 2] - m[q + j * 2], ry = m[q + i * 2 + 1] - m[q + j * 2 + 1];
 
769
  for (let k = 0; k < FL; k++) {
770
  const o = k * 3;
771
  if (fp[o + 1] < -1e4) continue;
772
+ if (fp[o + 1] <= ground + 0.2) { // landed: shrink away (same hold and fade as everything else)
773
+ fp[o + 1] = ground + 0.2;
774
+ fs[k] *= Math.exp(-dt * 2.5); if (fs[k] < 0.6) fp[o + 1] = -1e5;
775
+ continue;
776
+ }
777
  // tiny and light: fall fast at first, then terminal velocity with a little flutter
778
  const drag = 2.5 + 40 / fs[k];
779
  fv[o + 1] -= P.gravity * 0.35 * dt;
 
991
  test(f0, NC);
992
  }
993
 
994
+ // ------------------------------------------------------------ loose bits
995
+ // Cracks and crushing cut the sheet into pieces. A piece too small to read as papyrus (a few nodes, often a
996
+ // jagged clump of overlapping triangles) becomes crumbs that fall, settle and fade like all other debris; a
997
+ // small piece still glued in place by a stuck node lets go and falls, so nothing is left hanging in mid-air.
998
+ const ISLAND_MIN = 48; // nodes in both layers together
999
+ const compId = new Int32Array(N).fill(-1), queue = new Int32Array(N);
1000
+ let islandsRetired = 0, islandsReleased = 0;
1001
+ function sweepIslands(si, measure = false) {
1002
+ let floating = 0;
1003
+ const s = sheets[si], k0 = s.base[0], k1 = s.base[1] + s.R * s.C;
1004
+ let total = 0;
1005
+ for (let k = k0; k < k1; k++) { compId[k] = -1; if (valid[k]) total++; }
1006
+ let tag = 0;
1007
+ for (let k = k0; k < k1; k++) {
1008
+ if (!valid[k] || compId[k] >= 0) continue;
1009
+ let qh = 0, qt = 0, stuckN = 0, minY = Infinity;
1010
+ queue[qt++] = k; compId[k] = tag;
1011
+ while (qh < qt) {
1012
+ const n = queue[qh++];
1013
+ if (stuck[n]) stuckN++;
1014
+ if (pos[n * 3 + 1] < minY) minY = pos[n * 3 + 1];
1015
+ // in-layer structural and shear links, plus the glue to the other layer
1016
+ for (let e = deg[n]; e < deg[n + 1]; e++) {
1017
+ const c = adj[e]; if (!alive[c] || kind[c] > 2) continue;
1018
+ const m = ca[c] === n ? cb[c] : ca[c];
1019
+ if (valid[m] && compId[m] < 0) { compId[m] = tag; queue[qt++] = m; }
1020
+ }
1021
+ const other = n < s.base[1] ? n + (s.base[1] - s.base[0]) : n - (s.base[1] - s.base[0]);
1022
+ if (valid[other] && compId[other] < 0) {
1023
+ const c = eid(n, other);
1024
+ if (c >= 0 && alive[c]) { compId[other] = tag; queue[qt++] = other; }
1025
+ }
1026
+ }
1027
+ tag++;
1028
+ if (!stuckN && minY > ground + 1) floating++; // a free piece entirely off the floor
1029
+ if (measure) continue;
1030
+ if (qt < ISLAND_MIN) {
1031
+ // too small: turn it into crumbs at its own position and speed
1032
+ let made = 0;
1033
+ for (let q = 0; q < qt; q++) {
1034
+ const n = queue[q], o = n * 3;
1035
+ if (made < 12 && q % 3 === 0) {
1036
+ const h = 1 / 60;
1037
+ addCrumb(pos[o], pos[o + 1], pos[o + 2] + 0.2, (pos[o] - prev[o]) / h * 0.02, (pos[o + 1] - prev[o + 1]) / h * 0.02, (pos[o + 2] - prev[o + 2]) / h * 0.02,
1038
+ 0.3 + Math.random() * 0.6);
1039
+ made++;
1040
+ }
1041
+ valid[n] = 0; inv[n] = 0; stuck[n] = 0;
1042
+ for (let e = deg[n]; e < deg[n + 1]; e++) alive[adj[e]] = 0;
1043
+ const c = eid(n, n < s.base[1] ? n + (s.base[1] - s.base[0]) : n - (s.base[1] - s.base[0]));
1044
+ if (c >= 0) alive[c] = 0;
1045
+ }
1046
+ islandsRetired++; active[si] = 1; calm[si] = 0;
1047
+ } else if (stuckN && qt < total * 0.12) {
1048
+ // a small piece held only by a few glued nodes: free it so it falls to the floor
1049
+ for (let q = 0; q < qt; q++) { const n = queue[q]; if (stuck[n]) { stuck[n] = 0; inv[n] = 1; } }
1050
+ active[si] = 1; calm[si] = 0;
1051
+ islandsReleased++;
1052
+ }
1053
+ }
1054
+ return floating;
1055
+ }
1056
+
1057
+ // audit: free pieces entirely off the floor on sheets that are asleep (they would hang in mid-air)
1058
+ function hangingNodes() {
1059
+ let n = 0;
1060
+ for (let si = 0; si < sheets.length; si++) if (!active[si] && sheets[si].cracked) n += sweepIslands(si, true);
1061
+ return n;
1062
+ }
1063
  let time = 0, frameNo = 0;
1064
  let simMs = 0, lastMs = 0, prof = null;
1065
  function step(dt) {
 
1088
  impacts.length = 0;
1089
  growCracks();
1090
  const tC = performance.now();
1091
+ // every half second, on sheets that have cracked: retire loose bits, free small held pieces
1092
+ if (frameNo % 30 === 0) for (let si = 0; si < sheets.length; si++) if (sheets[si].cracked && (active[si] || sheets[si].islandDirty)) { sweepIslands(si); sheets[si].islandDirty = false; }
1093
  stepDebris(dt, time);
1094
  stepCrumbs(dt, time);
1095
  for (let si = 0; si < sheets.length; si++) {
 
1101
  }
1102
  const wake = active[si] || s.lay.some((l) => l.F && (l.F.dirty.size || l.F.attrsDirty));
1103
  if (!wake) continue;
1104
+ // sleep only when still and nothing free is left off the floor (a piece caught at the top of a bounce keeps falling)
1105
+ if (active[si] && calm[si] > 60) { const fl = sheets[si].cracked ? sweepIslands(si) : 0; if (calm[si] > 60 && !fl) { active[si] = 0; calm[si] = 0; } else if (fl) calm[si] = 0; }
1106
  for (const lay of s.lay) {
1107
  if (lay.F) {
1108
  rebindDirty(lay);
 
1130
  const cp = new Float32Array(CMAX * 3), cv = new Float32Array(CMAX * 3), csz = new Float32Array(CMAX);
1131
  const crot = new Float32Array(CMAX * 3), cav = new Float32Array(CMAX * 3), clive = new Uint8Array(CMAX), crest = new Uint8Array(CMAX);
1132
  let cCount = 0, crushOn = false, crushing = false, lastCrush = 0;
1133
+ // settle rule shared with the main view (damage.js): lie still for REST_HOLD s, then fade out over REST_FADE s
1134
+ const REST_HOLD = 1.0, REST_FADE = 1.2;
1135
+ const crestT = new Float32Array(CMAX), cStill = new Float32Array(CMAX);
1136
+ let crumbStillAirMax = 0;
1137
  const flakeShape = new THREE.BufferGeometry();
1138
  {
1139
  // an irregular, slightly bent flake: a fan of 7 triangles
 
1184
  if (cCount < CMAX) k = cCount++;
1185
  else { for (let i = 0; i < CMAX; i++) if (!clive[i]) { k = i; break; } }
1186
  if (k < 0) k = Math.floor(Math.random() * CMAX); // full: recycle a random one (usually a settled crumb)
1187
+ clive[k] = 1; crest[k] = 0; crestT[k] = 0; cStill[k] = 0;
1188
  cp[k * 3] = x; cp[k * 3 + 1] = y; cp[k * 3 + 2] = z;
1189
  cv[k * 3] = vx; cv[k * 3 + 1] = vy; cv[k * 3 + 2] = vz;
1190
  csz[k] = size;
 
1223
  crot[o] = Math.PI / 2 + (Math.random() - 0.5) * 0.3; crot[o + 1] = (Math.random() - 0.5) * 0.3;
1224
  cv[o] *= 0.2; cv[o + 2] *= 0.2;
1225
  }
1226
+ } else {
1227
+ if (Math.abs(cv[o]) + Math.abs(cv[o + 2]) > 1) {
1228
+ cp[o] += cv[o] * dt; cp[o + 2] += cv[o + 2] * dt;
1229
+ cv[o] *= Math.exp(-8 * dt); cv[o + 2] *= Math.exp(-8 * dt);
1230
+ }
1231
+ crestT[k] += dt;
1232
+ if (crestT[k] > REST_HOLD + REST_FADE) { clive[k] = 0; _m.makeScale(0, 0, 0); crumbs.setMatrixAt(k, _m); continue; }
1233
  }
1234
+ // audit: airborne and not moving
1235
+ if (!crest[k]) { cStill[k] = Math.abs(cv[o]) + Math.abs(cv[o + 1]) + Math.abs(cv[o + 2]) < 0.05 ? cStill[k] + dt : 0; crumbStillAirMax = Math.max(crumbStillAirMax, cStill[k]); if (cStill[k] > 2.5) { clive[k] = 0; _m.makeScale(0, 0, 0); crumbs.setMatrixAt(k, _m); continue; } }
1236
+ const fadeC = crest[k] ? Math.min(1, Math.max(0, 1 - (crestT[k] - REST_HOLD) / REST_FADE)) : 1;
1237
  _e.set(crot[o], crot[o + 1], crot[o + 2]);
1238
  _q.setFromEuler(_e);
1239
  _p.set(cp[o], cp[o + 1], cp[o + 2]);
1240
+ _s.set(sz * fadeC, sz * fadeC, sz * fadeC);
1241
  _m.compose(_p, _q, _s);
1242
  crumbs.setMatrixAt(k, _m);
1243
  }
 
1293
  made++;
1294
  }
1295
  for (let q = 0; q < 3; q++) spawnDust(w[0] + (Math.random() - 0.5) * R0, w[1] + (Math.random() - 0.5) * R0, w[2]);
1296
+ active[sheetOf[k0]] = 1; calm[sheetOf[k0]] = 0; s.islandDirty = true;
1297
  crunch();
1298
  breaks++;
1299
  return made;
 
1441
  group, step, reset, shake,
1442
  setCrush(v) { crushOn = v; },
1443
  setSound(v) { soundOn = v; if (v && audio && audio.state === 'suspended') audio.resume(); },
1444
+ stats: () => ({ crumbs: clive.reduce((a, b) => a + b, 0), crumbStillAirMax: +crumbStillAirMax.toFixed(2), islandsRetired, islandsReleased, hanging: hangingNodes(), nodes: N, links: NC, activeSheets: active.reduce((a, b) => a + b, 0), simMs: +simMs.toFixed(2), lastMs: +lastMs.toFixed(2), prof, breaks, debris: spawned }),
1445
  isGrabbing: () => grabbed >= 0,
1446
  };
1447
  }