roamwright 0.61.20 → 0.61.21

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,87 @@
1
+ /**
2
+ * Contact-transition IK for scene props: guides one or two actors' hands to a shared handoff
3
+ * point, a tabletop slide elevation, or a world resting point before transferring grip ownership.
4
+ * @module
5
+ */
6
+
7
+ import * as THREE from 'three';
8
+ import { sstep } from '../math/scalar.js';
9
+ import { createContactTransition } from './contact-transition.js';
10
+
11
+ const _grip = new THREE.Vector3(),
12
+ _origin = new THREE.Vector3(),
13
+ _meeting = new THREE.Vector3(),
14
+ _shoulder = new THREE.Vector3();
15
+
16
+ /**
17
+ * Starts a timed handoff or placement contact transition for prop `p`.
18
+ * @param {object} p Prop registry entry (`hand`, `left`, `grip`, `s`, `obj`, `taken`).
19
+ * @param {number} seconds Duration of the reach-and-transfer transition in seconds.
20
+ * @param {object} options
21
+ * @param {object | null} [options.to] Receiving actor, or `null` when laying the prop down.
22
+ * @param {boolean} [options.toLeft] True when transferring into the receiver's left hand.
23
+ * @param {THREE.Vector3 | null} [options.at] World target point when picking up or laying down.
24
+ * @param {THREE.Quaternion | null} [options.turn] Target world orientation when laying down or handing a blade.
25
+ * @param {number | null} [options.slideHeight] Optional tabletop height above ground for sliding handoffs.
26
+ * @param {object} helpers Actor hand/IK/ground hooks.
27
+ * @param {(actor: object, radius: number, out: THREE.Vector3, left: boolean) => void} helpers.inHand Computes world grip point inside an actor's hand.
28
+ * @param {(actor: object, radius: number, snap: boolean, left: boolean) => boolean} helpers.closeHand Closes an actor's fingers around `radius`.
29
+ * @param {(actor: object, left: boolean) => void} helpers.openHand Opens an actor's hand.
30
+ * @param {Function} helpers.reachArm Two-bone arm IK solver.
31
+ * @param {(x: number, z: number) => number} helpers.groundAt Ground height sampler.
32
+ * @returns {{ from: object | null, to: object | null, turn: THREE.Quaternion | null, phase: number, approach: number, advance: (dt: number) => boolean }} Active contact controller.
33
+ */
34
+ export function createPropContact(
35
+ p,
36
+ seconds,
37
+ { to = null, toLeft = false, at = null, turn = null, slideHeight = null },
38
+ { inHand, closeHand, openHand, reachArm, groundAt },
39
+ ) {
40
+ const from = p.hand,
41
+ fromLeft = p.left,
42
+ radius = p.grip * p.s,
43
+ contact = { from, to, turn, phase: 0, approach: 0, advance: null };
44
+
45
+ function guideGrip(actor, weight, left) {
46
+ if (!actor || !weight) return;
47
+ const wrist = left ? actor.handL : actor.handR,
48
+ fore = left ? actor.foreL : actor.foreR;
49
+ if (!fore || !wrist) return;
50
+ inHand(actor, radius, _grip, left);
51
+ wrist.worldToLocal(_grip);
52
+ reachArm(left ? actor.armL : actor.armR, fore, wrist, _grip, _origin, _meeting, left ? -1 : 1, weight);
53
+ }
54
+
55
+ contact.advance = createContactTransition({
56
+ duration: seconds,
57
+ pose(weight, approach, phase) {
58
+ contact.phase = phase;
59
+ contact.approach = approach;
60
+ if (from && to) {
61
+ const giverArm = fromLeft ? from.armL : from.armR,
62
+ receiverArm = toLeft ? to.armL : to.armR;
63
+ if (!giverArm || !receiverArm) return;
64
+ giverArm.getWorldPosition(_meeting);
65
+ receiverArm.getWorldPosition(_shoulder);
66
+ _meeting.lerp(_shoulder, 0.5);
67
+ if (slideHeight != null) {
68
+ _meeting.lerp(_shoulder, 0.18 * sstep(0.18, 0.54, phase));
69
+ _meeting.y = groundAt(_meeting.x, _meeting.z) + slideHeight;
70
+ } else _meeting.y -= 0.24;
71
+ } else _meeting.copy(at);
72
+ guideGrip(from, weight, fromLeft);
73
+ guideGrip(to, weight, toLeft);
74
+ },
75
+ transfer() {
76
+ if (from) openHand(from, fromLeft);
77
+ p.left = toLeft;
78
+ p.hand = to;
79
+ if (to) p.taken = closeHand(to, radius, false, toLeft);
80
+ else {
81
+ p.obj.position.copy(at);
82
+ p.obj.quaternion.copy(turn);
83
+ }
84
+ },
85
+ });
86
+ return contact;
87
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "roamwright",
3
- "version": "0.61.20",
3
+ "version": "0.61.21",
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",
@@ -0,0 +1,128 @@
1
+ /**
2
+ * Pooled additive ribbon trails that follow the outer edge of swinging blades and fade out over
3
+ * a brief lifetime without per-frame allocation.
4
+ * @module
5
+ */
6
+
7
+ import * as THREE from 'three';
8
+ import { clamp } from '../math/scalar.js';
9
+
10
+ /**
11
+ * Creates a bounded pool of additive weapon-swing ribbon trails attached to `scene`.
12
+ * @param {object} options
13
+ * @param {THREE.Object3D} options.scene Parent scene or group receiving trail meshes.
14
+ * @param {number} [options.capacity] Maximum simultaneous trails (default `6`).
15
+ * @param {number} [options.segments] Number of cross-section rings per trail (default `12`).
16
+ * @param {number} [options.duration] Trail fade lifetime in seconds (default `0.22`).
17
+ * @param {number} [options.glint] Leading-edge additive brightness (default `0.3`).
18
+ * @param {number} [options.layer] Render layer assigned to trail meshes (default `1`).
19
+ * @param {number} [options.renderOrder] Transparent draw order (default `13`).
20
+ * @param {[number, number, number]} [options.color] RGB tint of the outer blade edge (default `[0.85, 0.92, 1.0]`).
21
+ * @returns {{ acquire: () => object | null, push: (trail: object, base: THREE.Vector3, tip: THREE.Vector3) => void, update: (dt: number) => void, clear: () => void, dispose: () => void, trails: object[] }}
22
+ */
23
+ export function createBladeTrailPool({
24
+ scene,
25
+ capacity = 6,
26
+ segments = 12,
27
+ duration = 0.22,
28
+ glint = 0.3,
29
+ layer = 1,
30
+ renderOrder = 13,
31
+ color = [0.85, 0.92, 1.0],
32
+ } = {}) {
33
+ const trails = [];
34
+ const [cr, cg, cb] = color;
35
+
36
+ function acquire() {
37
+ for (let i = 0; i < trails.length; i++) if (trails[i].life <= 0) return trails[i];
38
+ if (trails.length >= capacity) return null;
39
+ const geometry = new THREE.BufferGeometry(),
40
+ pos = new Float32Array(segments * 6),
41
+ col = new Float32Array(segments * 6),
42
+ idx = [];
43
+ for (let i = 0; i < segments - 1; i++) idx.push(i * 2, i * 2 + 1, i * 2 + 2, i * 2 + 1, i * 2 + 3, i * 2 + 2);
44
+ geometry.setAttribute('position', new THREE.BufferAttribute(pos, 3));
45
+ geometry.setAttribute('color', new THREE.BufferAttribute(col, 3));
46
+ geometry.setIndex(idx);
47
+ const mesh = new THREE.Mesh(
48
+ geometry,
49
+ new THREE.MeshBasicMaterial({
50
+ vertexColors: true,
51
+ transparent: true,
52
+ blending: THREE.AdditiveBlending,
53
+ depthWrite: false,
54
+ side: THREE.DoubleSide,
55
+ fog: false,
56
+ }),
57
+ );
58
+ mesh.frustumCulled = false;
59
+ mesh.renderOrder = renderOrder;
60
+ if (layer != null) mesh.layers.set(layer);
61
+ mesh.visible = false;
62
+ scene?.add(mesh);
63
+ const trail = { mesh, pos, col, N: segments, n: 0, life: 0 };
64
+ trails.push(trail);
65
+ return trail;
66
+ }
67
+
68
+ function push(trail, base, tip) {
69
+ const points = trail.pos,
70
+ N = trail.N;
71
+ points.copyWithin(6, 0, (N - 1) * 6);
72
+ points[0] = base.x;
73
+ points[1] = base.y;
74
+ points[2] = base.z;
75
+ points[3] = tip.x;
76
+ points[4] = tip.y;
77
+ points[5] = tip.z;
78
+ if (trail.n === 0) for (let i = 1; i < N; i++) points.copyWithin(i * 6, 0, 6);
79
+ trail.n = Math.min(N, trail.n + 1);
80
+ }
81
+
82
+ function paint(trail) {
83
+ const colours = trail.col,
84
+ N = trail.N,
85
+ k = clamp(trail.life / duration, 0, 1);
86
+ for (let i = 0; i < N; i++) {
87
+ const f = 1 - i / (N - 1),
88
+ v = f * f * f * k * glint * (i < trail.n ? 1 : 0);
89
+ colours[i * 6] = 0;
90
+ colours[i * 6 + 1] = 0;
91
+ colours[i * 6 + 2] = 0;
92
+ colours[i * 6 + 3] = cr * v;
93
+ colours[i * 6 + 4] = cg * v;
94
+ colours[i * 6 + 5] = cb * v;
95
+ }
96
+ trail.mesh.geometry.attributes.position.needsUpdate = true;
97
+ trail.mesh.geometry.attributes.color.needsUpdate = true;
98
+ }
99
+
100
+ function update(dt) {
101
+ for (let i = 0; i < trails.length; i++) {
102
+ const trail = trails[i];
103
+ if (trail.life > 0) {
104
+ trail.life -= dt;
105
+ if (trail.life <= 0) trail.mesh.visible = false;
106
+ else paint(trail);
107
+ }
108
+ }
109
+ }
110
+
111
+ function clear() {
112
+ for (let i = 0; i < trails.length; i++) {
113
+ trails[i].life = 0;
114
+ trails[i].mesh.visible = false;
115
+ }
116
+ }
117
+
118
+ function dispose() {
119
+ for (let i = 0; i < trails.length; i++) {
120
+ trails[i].mesh.removeFromParent();
121
+ trails[i].mesh.geometry.dispose();
122
+ trails[i].mesh.material.dispose();
123
+ }
124
+ trails.length = 0;
125
+ }
126
+
127
+ return { acquire, push, update, clear, dispose, trails };
128
+ }