roamwright 0.61.3 → 0.61.4

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.
@@ -0,0 +1,32 @@
1
+ /**
2
+ * A limb of two bones in one plane: a leg from hip to ankle, an arm from shoulder to hand. For an end point (forward, up) from
3
+ * the root joint, it finds how far the first bone swings from hanging straight down and how far the middle joint bends.
4
+ * @module
5
+ */
6
+ import { clamp } from './scalar.js';
7
+
8
+ /**
9
+ * Solves a two-bone limb in its plane, writing [swing, bend] into `out`.
10
+ *
11
+ * The swing is the first bone's angle from straight down, positive toward `forward`; the bend is the middle joint's, 0 when
12
+ * the limb is straight. A middle joint that bends backward (an elbow: `bendsForward` false) leaves the first bone pointing
13
+ * ahead of the target; one that bends forward (a knee) leaves it behind. The reach is clamped to `[nearest, farthest]`, so a
14
+ * target out of reach gets the straightest limb pointing at it, and one too near gets the tightest bend allowed.
15
+ * @param {number[]} out - Written: [swing, bend], radians.
16
+ * @param {number} first - The first bone's length (thigh, upper arm).
17
+ * @param {number} second - The second bone's length, to the end point (shin to the ankle, forearm to the hand).
18
+ * @param {number} forward - The end point ahead of the root joint, in the bones' units.
19
+ * @param {number} up - The end point above the root joint (negative below).
20
+ * @param {number} nearest - The shortest reach allowed.
21
+ * @param {number} farthest - The longest reach allowed (at most `first + second`).
22
+ * @param {boolean} [bendsForward=false] - True for a joint that bends forward, like a knee.
23
+ * @returns {number[]} `out`.
24
+ */
25
+ export function solveTwoBone(out, first, second, forward, up, nearest, farthest, bendsForward = false) {
26
+ const reach = clamp(Math.sqrt(forward * forward + up * up), nearest, farthest),
27
+ bend = Math.acos(clamp((reach * reach - first * first - second * second) / (2 * first * second), -1, 1)),
28
+ lag = Math.atan2(second * Math.sin(bend), first + second * Math.cos(bend));
29
+ out[0] = bendsForward ? Math.atan2(forward, -up) + lag : Math.atan2(forward, -up) - lag;
30
+ out[1] = bend;
31
+ return out;
32
+ }
@@ -1,12 +1,11 @@
1
- import { clamp, lerp, sstep } from '../math/scalar.js';
1
+ import { lerp, sstep } from '../math/scalar.js';
2
+ import { solveTwoBone } from '../math/two-bone.js';
2
3
 
3
4
  // Two-bone leg from hip to ankle in the fore-and-aft plane: for an ankle at (forward, up) from the hip, the thigh's and the
4
5
  // knee's rotation, written into out.
5
6
  export function solveBipedLeg(out, rig, forward, up, minReach = Math.abs(rig.thigh - rig.shin)) {
6
- const reach = clamp(Math.sqrt(forward * forward + up * up), minReach, rig.thigh + rig.shin - 1e-4),
7
- bend = Math.acos(clamp((reach * reach - rig.thigh * rig.thigh - rig.shin * rig.shin) / (2 * rig.thigh * rig.shin), -1, 1));
8
- out[0] = -(Math.atan2(forward, -up) + Math.atan2(rig.shin * Math.sin(bend), rig.thigh + rig.shin * Math.cos(bend)));
9
- out[1] = bend;
7
+ solveTwoBone(out, rig.thigh, rig.shin, forward, up, minReach, rig.thigh + rig.shin - 1e-4, true);
8
+ out[0] = -out[0]; // the thigh's rotation turns it back for a positive angle
10
9
  }
11
10
  // The pitch of a foot's sole (toes down is more) at share u of its stride: it lands on its heel with the toes up by `strike`,
12
11
  // lies flat while the body passes over it, rolls up onto its toes until the heel has risen by `push`, and in the air comes
@@ -230,7 +230,7 @@ export function createCrowdPoser({
230
230
  }
231
231
  }
232
232
  if (cloak) cloak.rotation.set(-0.07 * walking - 0.05 * eased, 0.035 * rest, 0.04 * over);
233
- if (focus === 'crank' && crankPose(b, head, t, g.ph, wellStand, wellArm, wellCrank) && cloak) cloak.rotation.set(0.15, 0, 0);
233
+ if (focus === 'crank' && crankPose(b, head, t, g.ph, wellStand, wellArm, wellCrank, g.s) && cloak) cloak.rotation.set(0.15, 0, 0);
234
234
  if (focus === 'sit') {
235
235
  const lean = 0.16 + 0.03 * rest;
236
236
  b[0].position.set(0, -0.47, 0.03);
@@ -5,9 +5,10 @@
5
5
  * @module
6
6
  */
7
7
 
8
- import { clamp, lerp, sstep, TAU } from '../math/scalar.js';
8
+ import { lerp, sstep, TAU } from '../math/scalar.js';
9
9
  import { createHash } from '../math/random.js';
10
- import { ANKLE, BOOT_HEEL, BOOT_TOE, HIPS, SHANK, THIGH } from './figure-bones.js';
10
+ import { solveTwoBone } from '../math/two-bone.js';
11
+ import { ANKLE, BOOT_HEEL, BOOT_TOE, FOREARM, HIPS, PALM, SHANK, SHOULDERS, THIGH, UPPER_ARM } from './figure-bones.js';
11
12
  import { solveBipedLeg } from './biped-leg.js';
12
13
  import { CROWD_BONES } from '../geometry/crowd-weld.js';
13
14
 
@@ -49,9 +50,9 @@ export function windlass(t, phase) {
49
50
  return wellState;
50
51
  }
51
52
 
52
- const SHOULDER_Y = 1.36;
53
- const UPPER_ARM = 0.3;
54
- const FOREARM = 0.3;
53
+ // The arm as the crank solves it, in figure units: the upper arm, and the forearm to the middle of the palm on the handle.
54
+ const HAND_ON_HANDLE = FOREARM + PALM,
55
+ crankReach = [0, 0];
55
56
 
56
57
  /**
57
58
  * Solves a two-bone reach placing both hands on a rotating well crank handle in front of the figure.
@@ -63,22 +64,23 @@ const FOREARM = 0.3;
63
64
  * @param {number} [wellStand=WELL_STAND] Distance from crank axle (metres).
64
65
  * @param {number} [wellArm=WELL_ARM] Crank radius (metres).
65
66
  * @param {number} [wellCrank=WELL_CRANK] Crank axle height (metres).
67
+ * @param {number} [scale=1] The figure's size (its bones' figure units are metres at 1), so a smaller figure reaches up more.
66
68
  * @returns {boolean} True while actively turning the crank.
67
69
  */
68
- export function crankPose(b, head, t, phase, wellStand = WELL_STAND, wellArm = WELL_ARM, wellCrank = WELL_CRANK) {
70
+ export function crankPose(b, head, t, phase, wellStand = WELL_STAND, wellArm = WELL_ARM, wellCrank = WELL_CRANK, scale = 1) {
69
71
  const w = windlass(t, phase);
70
72
  if (!w.turning) return false;
71
- const forward = wellStand - wellArm * Math.cos(w.angle),
72
- rise = wellCrank + wellArm * Math.sin(w.angle) - SHOULDER_Y,
73
- reach = clamp(Math.hypot(forward, rise), 0.15, UPPER_ARM + FOREARM - 0.01),
74
- bend = Math.PI - Math.acos(clamp((UPPER_ARM ** 2 + FOREARM ** 2 - reach ** 2) / (2 * UPPER_ARM * FOREARM), -1, 1)),
75
- toward = Math.atan2(forward, -rise),
76
- lag = Math.atan2(FOREARM * Math.sin(bend), UPPER_ARM + FOREARM * Math.cos(bend));
73
+ // the handle from the shoulder, in figure units of this figure's size
74
+ const forward = (wellStand - wellArm * Math.cos(w.angle)) / scale,
75
+ rise = (wellCrank + wellArm * Math.sin(w.angle)) / scale - SHOULDERS;
76
+ solveTwoBone(crankReach, UPPER_ARM, HAND_ON_HANDLE, forward, rise, 0.15, UPPER_ARM + HAND_ON_HANDLE - 0.01);
77
+ const swing = crankReach[0],
78
+ bend = crankReach[1];
77
79
  b[0].rotation.x = 0.12 + 0.06 * Math.cos(w.angle);
78
80
  head.rotation.x = 0.2;
79
81
  for (let i = 0; i < 2; i++) {
80
82
  const s = i ? 1 : -1;
81
- b[2 + i * 2].rotation.set(-(toward - lag), -s * 0.05, -s * 0.12);
83
+ b[2 + i * 2].rotation.set(-swing, -s * 0.05, -s * 0.12);
82
84
  b[3 + i * 2].rotation.x = -bend;
83
85
  b[13 + i].rotation.set(-0.1, 0, 0);
84
86
  }
@@ -59,6 +59,7 @@ const SHOULDERS = 1.43,
59
59
  BOOT_TOE = 0.206,
60
60
  UPPER_ARM = 0.33, // the elbow below the shoulder, the wrist below the elbow,
61
61
  FOREARM = 0.315,
62
+ PALM = 0.055, // the middle of the palm below the wrist (where a hand rests on what it holds)
62
63
  GRIP = 0.09; // and the bone of what a hand holds below the wrist
63
64
 
64
65
  export {
@@ -102,6 +103,7 @@ export {
102
103
  MOUTH_R,
103
104
  OPEN_L,
104
105
  OPEN_R,
106
+ PALM,
105
107
  PELVIS,
106
108
  ROOT,
107
109
  SHANK,
@@ -8,7 +8,15 @@
8
8
  */
9
9
  import { clamp, lerp } from '../math/scalar.js';
10
10
  import { restingPostureDrift, solveCrawlSlope, solveRecumbentSlope } from './biped-posture.js';
11
- import { HIPS as HIP_HEIGHT, SHOULDERS as SHOULDERS_ABOVE_HIPS, THIGH, UPPER_ARM } from './figure-bones.js';
11
+ import { solveTwoBone } from '../math/two-bone.js';
12
+ import {
13
+ FOREARM as WRIST_BELOW_ELBOW,
14
+ HIPS as HIP_HEIGHT,
15
+ PALM,
16
+ SHOULDERS as SHOULDERS_ABOVE_HIPS,
17
+ THIGH,
18
+ UPPER_ARM,
19
+ } from './figure-bones.js';
12
20
  import { HIPS, LIE, THIGH_L, THIGH_R, TRUNK, editable } from './figure-pose-table.js';
13
21
 
14
22
  const KNEE_L = 0,
@@ -21,9 +29,9 @@ const KNEE_L = 0,
21
29
  TABLE_R = 7;
22
30
  // The figure's proportions, in figure units, as the seat solves them: the thigh, how high the hips stand, how far above them
23
31
  // the shoulders are and the upper arm are the bones' (figure-bones.js); the shin here is the knee to the sole (the bones'
24
- // shank and the ankle's height), and the forearm the elbow to the middle of the hand (the bones' forearm ends at the wrist).
32
+ // shank and the ankle's height), and the forearm the elbow to the middle of the hand (the bones' forearm and the palm).
25
33
  const SHIN = 0.474,
26
- FOREARM = 0.37;
34
+ FOREARM = WRIST_BELOW_ELBOW + PALM;
27
35
  const FLOOR_SEAT = 0.44, // the height of a seat where none is given, and of the hip joint above what it sits on
28
36
  HIP_ABOVE_SEAT = 0.092,
29
37
  KNEES_APART = 0.24, // how differently one person's knees splay from another's
@@ -176,22 +184,7 @@ function createFigureSeat({ ground = FLAT, seatHere = ANY_SEAT } = {}) {
176
184
  const reach = [0, 0],
177
185
  FULL_REACH = 0.995, // a target further than this share of the arm's length is brought in to it
178
186
  CLOSEST = 0.14; // and one nearer the shoulder than this is taken as this far
179
- function armIK(forward, up) {
180
- const upper = UPPER_ARM,
181
- fore = FOREARM;
182
- let distance = Math.hypot(forward, up);
183
- const most = (upper + fore) * FULL_REACH;
184
- if (distance > most) {
185
- forward *= most / distance;
186
- up *= most / distance;
187
- distance = most;
188
- }
189
- if (distance < CLOSEST) distance = CLOSEST;
190
- const bend = Math.acos(clamp((distance * distance - upper * upper - fore * fore) / (2 * upper * fore), -1, 1));
191
- reach[0] = Math.atan2(forward, -up) - Math.atan2(fore * Math.sin(bend), upper + fore * Math.cos(bend));
192
- reach[1] = bend;
193
- return reach;
194
- }
187
+ const armIK = (forward, up) => solveTwoBone(reach, UPPER_ARM, FOREARM, forward, up, CLOSEST, (UPPER_ARM + FOREARM) * FULL_REACH);
195
188
  // A seated figure's arms reach for where its hands rest (seatTarget), as far as it is seated.
196
189
  function seatedArms(a, w, lean) {
197
190
  const joints = a.joints,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "roamwright",
3
- "version": "0.61.3",
3
+ "version": "0.61.4",
4
4
  "description": "A procedural worldbuilding engine built on top of Three.js. Generate landscapes with rivers and changing weather, animate crowds, and explore on foot or in flight.",
5
5
  "keywords": [
6
6
  "threejs",