@adatechnology/game-fluids 0.6.3 → 0.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -935,6 +935,37 @@ var stepStain = (s, def, surface, ambientC, dt) => {
935
935
  };
936
936
  var stainDirection = (s, i) => sampleDir(makeBasis(s.normal ?? [0, 1, 0], s.tangent), i);
937
937
 
938
+ // src/surface-pose.ts
939
+ var IDENTITY_POSE = { position: [0, 0, 0], rotation: [0, 0, 0, 1] };
940
+ var RUNOFF_TILT = 6 * Math.PI / 180;
941
+ var RUNOFF_TILT_MAX = 60 * Math.PI / 180;
942
+ var RUNOFF_CLEARANCE = 0.02;
943
+ var RUNOFF_SPEED = 0.6;
944
+ var UPSIDE_DOWN_Y = -0.2;
945
+ var applyPose = (pose, point) => add(pose.position, quatRotate(pose.rotation, point));
946
+ var undoPose = (pose, point) => quatRotate(quatConjugate(pose.rotation), sub(point, pose.position));
947
+ function posedFrame(rest, pose) {
948
+ return { point: applyPose(pose, rest.point), normal: quatRotate(pose.rotation, rest.normal), tangent: quatRotate(pose.rotation, rest.tangent) };
949
+ }
950
+ function carryStain({ stain, from, to }) {
951
+ const turn = quatMultiply(to.rotation, quatConjugate(from.rotation));
952
+ stain.center = applyPose(to, undoPose(from, stain.center));
953
+ stain.normal = quatRotate(turn, stain.normal);
954
+ stain.tangent = quatRotate(turn, stain.tangent);
955
+ if (stain.flow) stain.flow = quatRotate(turn, stain.flow);
956
+ }
957
+ var runoffTilt = (def) => clamp(RUNOFF_TILT * Math.sqrt(Math.max(1, def.viscosity)), RUNOFF_TILT, RUNOFF_TILT_MAX);
958
+ var tiltOf = (rest, normal) => Math.acos(clamp(dot(rest.normal, normal), -1, 1));
959
+ function runoffStart({ center, surface }) {
960
+ const { dir, slope } = downhill(surface.normal);
961
+ if (surface.normal[1] < UPSIDE_DOWN_Y || slope < 1e-3) return { origin: add(center, scale(surface.normal, RUNOFF_CLEARANCE)), velocity: [0, -RUNOFF_SPEED, 0] };
962
+ const rel = sub(center, surface.point);
963
+ const room = (half, at, along) => along > 1e-6 ? (half - at) / along : along < -1e-6 ? (-half - at) / along : Infinity;
964
+ const reach = Math.max(0, Math.min(room(surface.half[0], dot(rel, surface.t1), dot(dir, surface.t1)), room(surface.half[1], dot(rel, surface.t2), dot(dir, surface.t2))));
965
+ const rim = add(center, scale(dir, Number.isFinite(reach) ? reach + RUNOFF_CLEARANCE : RUNOFF_CLEARANCE));
966
+ return { origin: add(rim, scale(surface.normal, RUNOFF_CLEARANCE)), velocity: scale(dir, RUNOFF_SPEED) };
967
+ }
968
+
938
969
  // src/world.ts
939
970
  var GROUND_POROSITY = 0.2;
940
971
  var MAX_STEP = 0.1;
@@ -985,11 +1016,29 @@ function createFluids(options = {}) {
985
1016
  c.d = levelFor(c.cols, c.u, c.spec.capacity > 0 ? c.liters / c.spec.capacity : 0);
986
1017
  };
987
1018
  const surfaceFor = (id) => id === GROUND_ID ? groundSurface : surfaces.get(id) ?? groundSurface;
988
- const landingSurfaces = () => {
1019
+ const landingSurfaces = (except) => {
989
1020
  const list = [];
990
- for (const [id, s] of surfaces) list.push({ id, normal: s.normal, t1: s.t1, t2: s.t2, point: s.point, half: s.half });
1021
+ for (const [id, s] of surfaces) if (id !== except) list.push({ id, normal: s.normal, t1: s.t1, t2: s.t2, point: s.point, half: s.half });
991
1022
  return list;
992
1023
  };
1024
+ const poseSurface = (surface, pose, carry) => {
1025
+ const from = surface.pose;
1026
+ const to = { position: copy3(pose.position), rotation: [pose.rotation[0], pose.rotation[1], pose.rotation[2], pose.rotation[3]] };
1027
+ if (carry) {
1028
+ for (const st of stains.values()) if (st.surface === surface.id) carryStain({ stain: st, from, to });
1029
+ }
1030
+ const frame = posedFrame(surface.rest, to);
1031
+ Object.assign(surface, makeBasis(frame.normal, frame.tangent), { point: frame.point, normal: frame.normal, pose: to });
1032
+ };
1033
+ const runOff = (st, def) => {
1034
+ const surface = surfaces.get(st.surface);
1035
+ if (!surface || st.liters <= 1e-6 || tiltOf(surface.rest, surface.normal) <= runoffTilt(def)) return;
1036
+ const { origin, velocity } = runoffStart({ center: st.center, surface });
1037
+ const liters = st.liters;
1038
+ st.liters = 0;
1039
+ const hit = trace(landingSurfaces(surface.id), groundAt, origin, velocity, gravity);
1040
+ if (hit) deposit(st.liquid, hit, liters, hit.velocity, st.tint);
1041
+ };
993
1042
  const stopStream = (id) => {
994
1043
  const s = streams.get(id);
995
1044
  if (!s) return;
@@ -1296,6 +1345,7 @@ function createFluids(options = {}) {
1296
1345
  for (const st of [...stains.values()]) {
1297
1346
  const def = liquids.get(st.liquid);
1298
1347
  if (!def) continue;
1348
+ runOff(st, def);
1299
1349
  const status = stepStain(st, def, surfaceFor(st.surface), ambientC, dt);
1300
1350
  if (status === "dried_mark") emit({ type: "dried", stain: st.id, mark: true });
1301
1351
  else if (status === "dried_gone") {
@@ -1529,7 +1579,9 @@ function createFluids(options = {}) {
1529
1579
  ...clone(spec),
1530
1580
  ...basis,
1531
1581
  point: copy3(spec.point),
1532
- half: [spec.halfSize[0], spec.halfSize[1]]
1582
+ half: [spec.halfSize[0], spec.halfSize[1]],
1583
+ rest: { point: copy3(spec.point), normal: copy3(basis.normal), tangent: copy3(basis.t1) },
1584
+ pose: IDENTITY_POSE
1533
1585
  });
1534
1586
  },
1535
1587
  removeSurface(id) {
@@ -1538,9 +1590,11 @@ function createFluids(options = {}) {
1538
1590
  },
1539
1591
  moveSurface(id, offset) {
1540
1592
  const surface = surfaces.get(id);
1541
- if (!surface) return;
1542
- surface.point = add(surface.point, offset);
1543
- for (const s of stains.values()) if (s.surface === id) s.center = add(s.center, offset);
1593
+ if (surface) poseSurface(surface, { position: add(surface.pose.position, offset), rotation: surface.pose.rotation }, true);
1594
+ },
1595
+ setSurfacePose(id, pose) {
1596
+ const surface = surfaces.get(id);
1597
+ if (surface) poseSurface(surface, pose, true);
1544
1598
  },
1545
1599
  splash(liquid, point, liters, velocity, options2) {
1546
1600
  if (!liquids.has(liquid) || !(liters > 0)) return;
@@ -1598,7 +1652,10 @@ function createFluids(options = {}) {
1598
1652
  const { cols: _cols, ...rest } = c;
1599
1653
  return clone(rest);
1600
1654
  }),
1601
- surfaces: [...surfaces.values()].map((s) => clone({ id: s.id, point: s.point, normal: s.normal, halfSize: s.half, tangent: s.t1, porosity: s.porosity })),
1655
+ surfaces: [...surfaces.values()].map((s) => {
1656
+ const moved = s.pose !== IDENTITY_POSE;
1657
+ return clone({ id: s.id, point: s.rest.point, normal: s.rest.normal, halfSize: s.half, tangent: s.rest.tangent, porosity: s.porosity, ...moved ? { pose: s.pose } : {} });
1658
+ }),
1602
1659
  streams: [...streams.values()].map((s) => clone(s)),
1603
1660
  taps: [...taps.values()].map((t) => clone(t)),
1604
1661
  stains: [...stains.values()].map((s) => clone(s))
@@ -1617,7 +1674,11 @@ function createFluids(options = {}) {
1617
1674
  containers = /* @__PURE__ */ new Map();
1618
1675
  for (const raw of snap.containers) containers.set(raw.id, { ...raw, cols: makeColumns(raw.spec.shape) });
1619
1676
  surfaces = /* @__PURE__ */ new Map();
1620
- for (const s of snap.surfaces) world.addSurface(s);
1677
+ for (const { pose, ...spec } of snap.surfaces) {
1678
+ world.addSurface(spec);
1679
+ const surface = surfaces.get(spec.id);
1680
+ if (pose && surface) poseSurface(surface, pose, false);
1681
+ }
1621
1682
  counters.tap = snap.counters.tap ?? 0;
1622
1683
  streams = new Map(snap.streams.map((s) => [s.id, s]));
1623
1684
  taps = new Map((snap.taps ?? []).map((t) => [t.id, t]));