@needle-tools/three-mesh-effects 0.1.0-alpha → 0.1.0-alpha.0
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/LICENSE +21 -21
- package/README.md +94 -88
- package/build/deformation-runtime.js.map +1 -1
- package/build/impostor-runtime-needle-engine.js.map +1 -1
- package/build/impostor-runtime.js.map +1 -1
- package/build/internal/deformation/cloth-surface.js.map +1 -1
- package/build/internal/deformation/fur-surface.js.map +1 -1
- package/build/internal/deformation/ik-constraints.js.map +1 -1
- package/build/internal/deformation/joints-json.js.map +1 -1
- package/build/internal/deformation/limits.js.map +1 -1
- package/build/internal/deformation/motion-settings.js.map +1 -1
- package/build/internal/deformation/pin-cloth.js.map +1 -1
- package/build/internal/deformation/pin-ik.js.map +1 -1
- package/build/internal/deformation/pin-types.js.map +1 -1
- package/build/internal/deformation/pin-wiggle.js.map +1 -1
- package/build/internal/deformation/pose-slots.js.map +1 -1
- package/build/internal/deformation/pose-timeline.js.map +1 -1
- package/build/internal/deformation/rig-drivers.js.map +1 -1
- package/build/internal/deformation/rig-extension.js.map +1 -1
- package/build/internal/deformation/rig-runtime.js.map +1 -1
- package/build/internal/deformation/wind.js.map +1 -1
- package/build/internal/impostor/impostor-metadata.js.map +1 -1
- package/build/internal/impostor/impostor-node-material.js.map +1 -1
- package/build/internal/impostor/impostor-runtime-support.js.map +1 -1
- package/build/internal/impostor/octahedral.js.map +1 -1
- package/build/internal/pixel-impostor/pixel-impostor-display.js.map +1 -1
- package/build/internal/pixel-impostor/pixel-impostor-metadata.js.map +1 -1
- package/build/internal/pixel-impostor/pixel-impostor-runtime.js.map +1 -1
- package/build/internal/rendering/skin-projection.js +52 -52
- package/build/internal/rendering/skin-projection.js.map +2 -2
- package/build/internal/rendering/surface-query.js +52 -52
- package/build/internal/rendering/surface-query.js.map +2 -2
- package/build/internal/rendering/wireframe.js.map +1 -1
- package/build/internal/rendering/wrap-preview-settings.js.map +1 -1
- package/build/internal/rendering/wrap-preview.js +52 -52
- package/build/internal/rendering/wrap-preview.js.map +2 -2
- package/build/internal/rendering/wrap-skin.js +52 -52
- package/build/internal/rendering/wrap-skin.js.map +2 -2
- package/build/pixel-impostor-runtime.js.map +1 -1
- package/build/wrap-effects.js +52 -52
- package/build/wrap-effects.js.map +2 -2
- package/package.json +107 -94
- package/src/deformation/cloth-surface.ts +223 -223
- package/src/deformation/fur-surface.ts +129 -129
- package/src/deformation/ik-constraints.ts +37 -37
- package/src/deformation/joints-json.ts +94 -94
- package/src/deformation/limits.ts +25 -25
- package/src/deformation/motion-settings.ts +9 -9
- package/src/deformation/pin-cloth.ts +149 -149
- package/src/deformation/pin-ik.ts +582 -582
- package/src/deformation/pin-types.ts +22 -22
- package/src/deformation/pin-wiggle.ts +119 -119
- package/src/deformation/pose-slots.ts +106 -106
- package/src/deformation/pose-timeline.ts +242 -242
- package/src/deformation/rig-drivers.ts +32 -32
- package/src/deformation/rig-extension.ts +87 -87
- package/src/deformation/rig-runtime.ts +416 -416
- package/src/deformation/wind.ts +36 -36
- package/src/impostor/impostor-metadata.js +137 -137
- package/src/impostor/impostor-node-material.js +1581 -1581
- package/src/impostor/impostor-runtime-support.js +139 -139
- package/src/impostor/octahedral.js +280 -280
- package/src/pixel-impostor/pixel-impostor-display.js +794 -794
- package/src/pixel-impostor/pixel-impostor-metadata.js +74 -74
- package/src/pixel-impostor/pixel-impostor-runtime.js +78 -78
- package/src/public/deformation-runtime.ts +3 -3
- package/src/public/impostor-runtime-needle-engine.js +56 -56
- package/src/public/impostor-runtime.js +21 -21
- package/src/public/pixel-impostor-runtime.js +5 -5
- package/src/public/wrap-effects.js +21 -21
- package/src/rendering/skin-projection.js +113 -113
- package/src/rendering/surface-query.js +88 -88
- package/src/rendering/wireframe.js +115 -115
- package/src/rendering/wrap-preview-settings.js +123 -123
- package/src/rendering/wrap-preview.js +645 -645
- package/src/rendering/wrap-skin.js +109 -109
- package/build/internal/public/deformation-runtime.js +0 -2143
- package/build/internal/public/deformation-runtime.js.map +0 -7
- package/build/internal/public/impostor-runtime-needle-engine.js +0 -42
- package/build/internal/public/impostor-runtime-needle-engine.js.map +0 -7
- package/build/internal/public/impostor-runtime.js +0 -1333
- package/build/internal/public/impostor-runtime.js.map +0 -7
- package/build/internal/public/pixel-impostor-runtime.js +0 -876
- package/build/internal/public/pixel-impostor-runtime.js.map +0 -7
- package/build/internal/public/wrap-effects.js +0 -5640
- package/build/internal/public/wrap-effects.js.map +0 -7
|
@@ -1,223 +1,223 @@
|
|
|
1
|
-
import { Vector3 } from 'three';
|
|
2
|
-
import type { PinRecord, Point } from './pin-types.js';
|
|
3
|
-
import type { ClothCollider } from './pin-cloth.js';
|
|
4
|
-
import { DEFAULT_MOTION, type MotionSettings } from './motion-settings.js';
|
|
5
|
-
import { windAcceleration, windProfile } from './wind.js';
|
|
6
|
-
|
|
7
|
-
type Edge = { a: number; b: number; length: number; bend: boolean };
|
|
8
|
-
type Triangle = { a: number; b: number; c: number };
|
|
9
|
-
type Particle = { position: Vector3; previous: Vector3; base: Vector3; share: number; anchor: number };
|
|
10
|
-
|
|
11
|
-
export function clothSurfaceMotion(pins: readonly PinRecord[]): MotionSettings {
|
|
12
|
-
if (!pins.length) return { ...DEFAULT_MOTION };
|
|
13
|
-
const values = pins.map(pin => pin.motion ?? DEFAULT_MOTION);
|
|
14
|
-
return { ...DEFAULT_MOTION,
|
|
15
|
-
damping: values.reduce((sum, motion) => sum + motion.damping, 0) / values.length,
|
|
16
|
-
strength: values.reduce((sum, motion) => sum + motion.strength, 0) / values.length };
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function distances(graph: readonly { to: number; length: number }[][], seeds: readonly number[]) {
|
|
20
|
-
const distance = new Float64Array(graph.length).fill(Infinity);
|
|
21
|
-
const heap: { node: number; distance: number }[] = [];
|
|
22
|
-
const push = (node: number, value: number) => {
|
|
23
|
-
let i = heap.length; heap.push({ node, distance: value });
|
|
24
|
-
while (i) { const parent = (i - 1) >> 1; if (heap[parent].distance <= value) break; heap[i] = heap[parent]; i = parent; }
|
|
25
|
-
heap[i] = { node, distance: value };
|
|
26
|
-
};
|
|
27
|
-
const pop = () => {
|
|
28
|
-
const top = heap[0], last = heap.pop()!;
|
|
29
|
-
if (heap.length) {
|
|
30
|
-
let i = 0;
|
|
31
|
-
for (;;) {
|
|
32
|
-
let child = i * 2 + 1; if (child >= heap.length) break;
|
|
33
|
-
if (child + 1 < heap.length && heap[child + 1].distance < heap[child].distance) child++;
|
|
34
|
-
if (last.distance <= heap[child].distance) break;
|
|
35
|
-
heap[i] = heap[child]; i = child;
|
|
36
|
-
}
|
|
37
|
-
heap[i] = last;
|
|
38
|
-
}
|
|
39
|
-
return top;
|
|
40
|
-
};
|
|
41
|
-
for (const seed of seeds) if (distance[seed] > 0) { distance[seed] = 0; push(seed, 0); }
|
|
42
|
-
while (heap.length) {
|
|
43
|
-
const next = pop(); if (next.distance !== distance[next.node]) continue;
|
|
44
|
-
for (const edge of graph[next.node]) {
|
|
45
|
-
const value = next.distance + edge.length;
|
|
46
|
-
if (value < distance[edge.to]) { distance[edge.to] = value; push(edge.to, value); }
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return distance;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** A topology-connected fabric layer over the portable skinned/voxel result.
|
|
53
|
-
* Vertices at UV seams share one particle; all other vertices keep the source
|
|
54
|
-
* triangle topology. Body controls attach the sheet, cloth controls release it. */
|
|
55
|
-
export class ClothSurface {
|
|
56
|
-
readonly particles: Particle[];
|
|
57
|
-
readonly vertexParticle: Uint32Array;
|
|
58
|
-
readonly triangles: Triangle[];
|
|
59
|
-
readonly edges: Edge[];
|
|
60
|
-
readonly scale: number;
|
|
61
|
-
private time = 0;
|
|
62
|
-
|
|
63
|
-
constructor(restVertices: Float32Array, indices: ArrayLike<number>, pins: readonly PinRecord[]) {
|
|
64
|
-
const vertexCount = restVertices.length / 3;
|
|
65
|
-
const min = new Vector3(Infinity, Infinity, Infinity), max = new Vector3(-Infinity, -Infinity, -Infinity);
|
|
66
|
-
for (let i = 0; i < vertexCount; i++) {
|
|
67
|
-
const p = new Vector3().fromArray(restVertices, i * 3); min.min(p); max.max(p);
|
|
68
|
-
}
|
|
69
|
-
this.scale = Math.max(1e-4, max.clone().sub(min).length());
|
|
70
|
-
const tolerance = this.scale * 1e-6;
|
|
71
|
-
const welded = new Map<string, number>(), particles: Particle[] = [];
|
|
72
|
-
this.vertexParticle = new Uint32Array(vertexCount);
|
|
73
|
-
for (let i = 0; i < vertexCount; i++) {
|
|
74
|
-
const p = new Vector3().fromArray(restVertices, i * 3);
|
|
75
|
-
const key = p.toArray().map(value => Math.round(value / tolerance)).join(',');
|
|
76
|
-
let node = welded.get(key);
|
|
77
|
-
if (node === undefined) {
|
|
78
|
-
node = particles.length; welded.set(key, node);
|
|
79
|
-
particles.push({ position: p.clone(), previous: p.clone(), base: p, share: 0, anchor: 0 });
|
|
80
|
-
}
|
|
81
|
-
this.vertexParticle[i] = node;
|
|
82
|
-
}
|
|
83
|
-
const edgeMap = new Map<string, { a: number; b: number; opposites: number[] }>();
|
|
84
|
-
const graph: { to: number; length: number }[][] = particles.map(() => []);
|
|
85
|
-
const triangles: Triangle[] = [];
|
|
86
|
-
const edge = (a: number, b: number, opposite: number) => {
|
|
87
|
-
if (a === b) return;
|
|
88
|
-
const key = a < b ? `${a}:${b}` : `${b}:${a}`;
|
|
89
|
-
const existing = edgeMap.get(key);
|
|
90
|
-
if (existing) existing.opposites.push(opposite);
|
|
91
|
-
else edgeMap.set(key, { a, b, opposites: [opposite] });
|
|
92
|
-
};
|
|
93
|
-
for (let i = 0; i + 2 < indices.length; i += 3) {
|
|
94
|
-
const a = this.vertexParticle[indices[i]], b = this.vertexParticle[indices[i + 1]], c = this.vertexParticle[indices[i + 2]];
|
|
95
|
-
if (a === b || b === c || c === a) continue;
|
|
96
|
-
triangles.push({ a, b, c }); edge(a, b, c); edge(b, c, a); edge(c, a, b);
|
|
97
|
-
}
|
|
98
|
-
this.triangles = triangles;
|
|
99
|
-
const edges: Edge[] = [];
|
|
100
|
-
for (const value of edgeMap.values()) {
|
|
101
|
-
const length = particles[value.a].base.distanceTo(particles[value.b].base);
|
|
102
|
-
edges.push({ a: value.a, b: value.b, length, bend: false });
|
|
103
|
-
graph[value.a].push({ to: value.b, length }); graph[value.b].push({ to: value.a, length });
|
|
104
|
-
if (value.opposites.length === 2 && value.opposites[0] !== value.opposites[1]) {
|
|
105
|
-
const a = value.opposites[0], b = value.opposites[1];
|
|
106
|
-
edges.push({ a, b, length: particles[a].base.distanceTo(particles[b].base), bend: true });
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
this.edges = edges;
|
|
110
|
-
const nearest = (point: Point) => {
|
|
111
|
-
const p = new Vector3().fromArray(point); let found = 0, best = Infinity;
|
|
112
|
-
for (let i = 0; i < particles.length; i++) {
|
|
113
|
-
const d = particles[i].base.distanceToSquared(p); if (d < best) { best = d; found = i; }
|
|
114
|
-
}
|
|
115
|
-
return found;
|
|
116
|
-
};
|
|
117
|
-
const cloth = pins.filter(pin => pin.cloth && !pin.guide).map(pin => nearest(pin.rest));
|
|
118
|
-
const body = pins.filter(pin => !pin.cloth && !pin.guide).map(pin => nearest(pin.rest));
|
|
119
|
-
const clothDistance = distances(graph, cloth), bodyDistance = distances(graph, body);
|
|
120
|
-
for (let i = 0; i < particles.length; i++) {
|
|
121
|
-
const a = clothDistance[i], b = bodyDistance[i];
|
|
122
|
-
particles[i].share = Number.isFinite(a) && Number.isFinite(b) ? b / Math.max(1e-6, a + b) : Number.isFinite(a) ? 1 : 0;
|
|
123
|
-
particles[i].anchor = Number.isFinite(b) ? Math.exp(-b / (.12 * this.scale)) : 0;
|
|
124
|
-
}
|
|
125
|
-
this.particles = particles;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
get movableCount() { return this.particles.filter(p => p.share > .25).length; }
|
|
129
|
-
|
|
130
|
-
reset() {
|
|
131
|
-
this.time = 0;
|
|
132
|
-
for (const particle of this.particles) particle.position.copy(particle.base), particle.previous.copy(particle.base);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
step(baseVertices: Float32Array, seconds: number, windStrength: number, colliders: readonly ClothCollider[] = [], motion: Readonly<MotionSettings> = DEFAULT_MOTION) {
|
|
136
|
-
const count = this.particles.length, base = Array.from({ length: count }, () => new Vector3()), samples = new Uint32Array(count);
|
|
137
|
-
for (let i = 0; i < this.vertexParticle.length; i++) {
|
|
138
|
-
const node = this.vertexParticle[i]; base[node].add(new Vector3().fromArray(baseVertices, i * 3)); samples[node]++;
|
|
139
|
-
}
|
|
140
|
-
for (let i = 0; i < count; i++) {
|
|
141
|
-
base[i].divideScalar(samples[i] || 1);
|
|
142
|
-
const particle = this.particles[i], delta = base[i].clone().sub(particle.base);
|
|
143
|
-
particle.position.add(delta); particle.previous.add(delta); particle.base.copy(base[i]);
|
|
144
|
-
}
|
|
145
|
-
const dt = Math.min(Math.max(seconds, 0), .05), steps = Math.max(1, Math.ceil(dt * 60)), h = dt / steps;
|
|
146
|
-
const profile = windProfile(windStrength), pressureScale = profile.load ? .025 * profile.surfaceLoad / profile.load : 0;
|
|
147
|
-
const radius = Math.max(1e-4, ...colliders.map(collider => collider.radius));
|
|
148
|
-
const cell = radius * 2;
|
|
149
|
-
const bins = new Map<number, ClothCollider[]>();
|
|
150
|
-
// Hash collisions only add candidates; the distance test below remains exact.
|
|
151
|
-
const key = (x: number, y: number, z: number) => (Math.imul(x, 73856093) ^ Math.imul(y, 19349663) ^ Math.imul(z, 83492791)) | 0;
|
|
152
|
-
for (const collider of colliders) {
|
|
153
|
-
const p = collider.position, id = key(Math.floor(p[0] / cell), Math.floor(p[1] / cell), Math.floor(p[2] / cell));
|
|
154
|
-
const bin = bins.get(id) ?? []; bin.push(collider); bins.set(id, bin);
|
|
155
|
-
}
|
|
156
|
-
for (let step = 0; step < steps && h > 0; step++) {
|
|
157
|
-
this.time += h;
|
|
158
|
-
const force = Array.from({ length: count }, () => new Vector3());
|
|
159
|
-
const area = new Float32Array(count);
|
|
160
|
-
if (pressureScale && motion.strength) for (const triangle of this.triangles) {
|
|
161
|
-
const a = this.particles[triangle.a].position, b = this.particles[triangle.b].position, c = this.particles[triangle.c].position;
|
|
162
|
-
const normal = b.clone().sub(a).cross(c.clone().sub(a));
|
|
163
|
-
const surface = normal.length(); if (surface < 1e-10) continue;
|
|
164
|
-
normal.divideScalar(surface);
|
|
165
|
-
const center = a.clone().add(b).add(c).divideScalar(3);
|
|
166
|
-
const wind = windAcceleration(this.time, windStrength, this.scale, center.toArray());
|
|
167
|
-
// Normal pressure bends the sheet; a small tangential drag carries
|
|
168
|
-
// near-parallel fabric rather than letting it ignore the wind entirely.
|
|
169
|
-
const pressure = normal.multiplyScalar(wind.dot(normal)).addScaledVector(wind, .12)
|
|
170
|
-
.multiplyScalar(pressureScale);
|
|
171
|
-
for (const node of [triangle.a, triangle.b, triangle.c]) { force[node].addScaledVector(pressure, surface); area[node] += surface; }
|
|
172
|
-
}
|
|
173
|
-
for (let i = 0; i < count; i++) {
|
|
174
|
-
const particle = this.particles[i], share = particle.share;
|
|
175
|
-
if (share < 1e-4) { particle.position.copy(particle.base); particle.previous.copy(particle.base); continue; }
|
|
176
|
-
const velocity = particle.position.clone().sub(particle.previous).multiplyScalar(Math.exp(-motion.damping * 3 * h));
|
|
177
|
-
particle.previous.copy(particle.position);
|
|
178
|
-
particle.position.add(velocity).y -= 2.5 * this.scale * share * h * h;
|
|
179
|
-
if (area[i]) particle.position.addScaledVector(force[i], motion.strength * share * h * h / area[i]);
|
|
180
|
-
const anchor = 1 - Math.exp(-(1.5 + 60 * particle.anchor ** 2) * h);
|
|
181
|
-
particle.position.lerp(particle.base, anchor);
|
|
182
|
-
}
|
|
183
|
-
for (let iteration = 0; iteration < 5; iteration++) {
|
|
184
|
-
for (const edge of this.edges) {
|
|
185
|
-
const a = this.particles[edge.a], b = this.particles[edge.b];
|
|
186
|
-
const difference = b.position.clone().sub(a.position), length = difference.length();
|
|
187
|
-
if (length < 1e-8) continue;
|
|
188
|
-
const wa = a.share * a.share, wb = b.share * b.share, total = wa + wb;
|
|
189
|
-
if (total < 1e-8) continue;
|
|
190
|
-
const strength = edge.bend ? .14 : .72;
|
|
191
|
-
difference.multiplyScalar((length - edge.length) * strength / (length * total));
|
|
192
|
-
a.position.addScaledVector(difference, wa); b.position.addScaledVector(difference, -wb);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
if (colliders.length) for (const particle of this.particles) {
|
|
196
|
-
if (particle.share < .35) continue;
|
|
197
|
-
const p = particle.position, px = p.x / cell, py = p.y / cell, pz = p.z / cell;
|
|
198
|
-
const x = Math.floor(px), y = Math.floor(py), z = Math.floor(pz);
|
|
199
|
-
const sx = px - x < .5 ? -1 : 1, sy = py - y < .5 ? -1 : 1, sz = pz - z < .5 ? -1 : 1;
|
|
200
|
-
let chosen: ClothCollider | undefined, penetration = 0;
|
|
201
|
-
// Cell width is twice the largest radius, so only the nearest neighbor
|
|
202
|
-
// on each axis can contain a sphere reaching this particle.
|
|
203
|
-
for (let iz = 0; iz < 2; iz++) for (let iy = 0; iy < 2; iy++) for (let ix = 0; ix < 2; ix++) {
|
|
204
|
-
for (const collider of bins.get(key(x + ix * sx, y + iy * sy, z + iz * sz)) ?? []) {
|
|
205
|
-
const center = collider.position, bx = particle.base.x - center[0], by = particle.base.y - center[1], bz = particle.base.z - center[2];
|
|
206
|
-
if (bx * bx + by * by + bz * bz < collider.radius * collider.radius * 1.04) continue;
|
|
207
|
-
const vx = p.x - center[0], vy = p.y - center[1], vz = p.z - center[2];
|
|
208
|
-
const depth = collider.radius - Math.hypot(vx, vy, vz);
|
|
209
|
-
if (depth > penetration) { penetration = depth; chosen = collider; }
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
if (chosen) {
|
|
213
|
-
const center = chosen.position, delta = p.clone().sub(new Vector3().fromArray(center));
|
|
214
|
-
if (delta.lengthSq() < 1e-12) delta.copy(particle.base).sub(new Vector3().fromArray(center));
|
|
215
|
-
p.copy(new Vector3().fromArray(center).add(delta.setLength(chosen.radius)));
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
const result = new Float32Array(baseVertices.length);
|
|
220
|
-
for (let i = 0; i < this.vertexParticle.length; i++) result.set(this.particles[this.vertexParticle[i]].position.toArray(), i * 3);
|
|
221
|
-
return result;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
1
|
+
import { Vector3 } from 'three';
|
|
2
|
+
import type { PinRecord, Point } from './pin-types.js';
|
|
3
|
+
import type { ClothCollider } from './pin-cloth.js';
|
|
4
|
+
import { DEFAULT_MOTION, type MotionSettings } from './motion-settings.js';
|
|
5
|
+
import { windAcceleration, windProfile } from './wind.js';
|
|
6
|
+
|
|
7
|
+
type Edge = { a: number; b: number; length: number; bend: boolean };
|
|
8
|
+
type Triangle = { a: number; b: number; c: number };
|
|
9
|
+
type Particle = { position: Vector3; previous: Vector3; base: Vector3; share: number; anchor: number };
|
|
10
|
+
|
|
11
|
+
export function clothSurfaceMotion(pins: readonly PinRecord[]): MotionSettings {
|
|
12
|
+
if (!pins.length) return { ...DEFAULT_MOTION };
|
|
13
|
+
const values = pins.map(pin => pin.motion ?? DEFAULT_MOTION);
|
|
14
|
+
return { ...DEFAULT_MOTION,
|
|
15
|
+
damping: values.reduce((sum, motion) => sum + motion.damping, 0) / values.length,
|
|
16
|
+
strength: values.reduce((sum, motion) => sum + motion.strength, 0) / values.length };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function distances(graph: readonly { to: number; length: number }[][], seeds: readonly number[]) {
|
|
20
|
+
const distance = new Float64Array(graph.length).fill(Infinity);
|
|
21
|
+
const heap: { node: number; distance: number }[] = [];
|
|
22
|
+
const push = (node: number, value: number) => {
|
|
23
|
+
let i = heap.length; heap.push({ node, distance: value });
|
|
24
|
+
while (i) { const parent = (i - 1) >> 1; if (heap[parent].distance <= value) break; heap[i] = heap[parent]; i = parent; }
|
|
25
|
+
heap[i] = { node, distance: value };
|
|
26
|
+
};
|
|
27
|
+
const pop = () => {
|
|
28
|
+
const top = heap[0], last = heap.pop()!;
|
|
29
|
+
if (heap.length) {
|
|
30
|
+
let i = 0;
|
|
31
|
+
for (;;) {
|
|
32
|
+
let child = i * 2 + 1; if (child >= heap.length) break;
|
|
33
|
+
if (child + 1 < heap.length && heap[child + 1].distance < heap[child].distance) child++;
|
|
34
|
+
if (last.distance <= heap[child].distance) break;
|
|
35
|
+
heap[i] = heap[child]; i = child;
|
|
36
|
+
}
|
|
37
|
+
heap[i] = last;
|
|
38
|
+
}
|
|
39
|
+
return top;
|
|
40
|
+
};
|
|
41
|
+
for (const seed of seeds) if (distance[seed] > 0) { distance[seed] = 0; push(seed, 0); }
|
|
42
|
+
while (heap.length) {
|
|
43
|
+
const next = pop(); if (next.distance !== distance[next.node]) continue;
|
|
44
|
+
for (const edge of graph[next.node]) {
|
|
45
|
+
const value = next.distance + edge.length;
|
|
46
|
+
if (value < distance[edge.to]) { distance[edge.to] = value; push(edge.to, value); }
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return distance;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A topology-connected fabric layer over the portable skinned/voxel result.
|
|
53
|
+
* Vertices at UV seams share one particle; all other vertices keep the source
|
|
54
|
+
* triangle topology. Body controls attach the sheet, cloth controls release it. */
|
|
55
|
+
export class ClothSurface {
|
|
56
|
+
readonly particles: Particle[];
|
|
57
|
+
readonly vertexParticle: Uint32Array;
|
|
58
|
+
readonly triangles: Triangle[];
|
|
59
|
+
readonly edges: Edge[];
|
|
60
|
+
readonly scale: number;
|
|
61
|
+
private time = 0;
|
|
62
|
+
|
|
63
|
+
constructor(restVertices: Float32Array, indices: ArrayLike<number>, pins: readonly PinRecord[]) {
|
|
64
|
+
const vertexCount = restVertices.length / 3;
|
|
65
|
+
const min = new Vector3(Infinity, Infinity, Infinity), max = new Vector3(-Infinity, -Infinity, -Infinity);
|
|
66
|
+
for (let i = 0; i < vertexCount; i++) {
|
|
67
|
+
const p = new Vector3().fromArray(restVertices, i * 3); min.min(p); max.max(p);
|
|
68
|
+
}
|
|
69
|
+
this.scale = Math.max(1e-4, max.clone().sub(min).length());
|
|
70
|
+
const tolerance = this.scale * 1e-6;
|
|
71
|
+
const welded = new Map<string, number>(), particles: Particle[] = [];
|
|
72
|
+
this.vertexParticle = new Uint32Array(vertexCount);
|
|
73
|
+
for (let i = 0; i < vertexCount; i++) {
|
|
74
|
+
const p = new Vector3().fromArray(restVertices, i * 3);
|
|
75
|
+
const key = p.toArray().map(value => Math.round(value / tolerance)).join(',');
|
|
76
|
+
let node = welded.get(key);
|
|
77
|
+
if (node === undefined) {
|
|
78
|
+
node = particles.length; welded.set(key, node);
|
|
79
|
+
particles.push({ position: p.clone(), previous: p.clone(), base: p, share: 0, anchor: 0 });
|
|
80
|
+
}
|
|
81
|
+
this.vertexParticle[i] = node;
|
|
82
|
+
}
|
|
83
|
+
const edgeMap = new Map<string, { a: number; b: number; opposites: number[] }>();
|
|
84
|
+
const graph: { to: number; length: number }[][] = particles.map(() => []);
|
|
85
|
+
const triangles: Triangle[] = [];
|
|
86
|
+
const edge = (a: number, b: number, opposite: number) => {
|
|
87
|
+
if (a === b) return;
|
|
88
|
+
const key = a < b ? `${a}:${b}` : `${b}:${a}`;
|
|
89
|
+
const existing = edgeMap.get(key);
|
|
90
|
+
if (existing) existing.opposites.push(opposite);
|
|
91
|
+
else edgeMap.set(key, { a, b, opposites: [opposite] });
|
|
92
|
+
};
|
|
93
|
+
for (let i = 0; i + 2 < indices.length; i += 3) {
|
|
94
|
+
const a = this.vertexParticle[indices[i]], b = this.vertexParticle[indices[i + 1]], c = this.vertexParticle[indices[i + 2]];
|
|
95
|
+
if (a === b || b === c || c === a) continue;
|
|
96
|
+
triangles.push({ a, b, c }); edge(a, b, c); edge(b, c, a); edge(c, a, b);
|
|
97
|
+
}
|
|
98
|
+
this.triangles = triangles;
|
|
99
|
+
const edges: Edge[] = [];
|
|
100
|
+
for (const value of edgeMap.values()) {
|
|
101
|
+
const length = particles[value.a].base.distanceTo(particles[value.b].base);
|
|
102
|
+
edges.push({ a: value.a, b: value.b, length, bend: false });
|
|
103
|
+
graph[value.a].push({ to: value.b, length }); graph[value.b].push({ to: value.a, length });
|
|
104
|
+
if (value.opposites.length === 2 && value.opposites[0] !== value.opposites[1]) {
|
|
105
|
+
const a = value.opposites[0], b = value.opposites[1];
|
|
106
|
+
edges.push({ a, b, length: particles[a].base.distanceTo(particles[b].base), bend: true });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
this.edges = edges;
|
|
110
|
+
const nearest = (point: Point) => {
|
|
111
|
+
const p = new Vector3().fromArray(point); let found = 0, best = Infinity;
|
|
112
|
+
for (let i = 0; i < particles.length; i++) {
|
|
113
|
+
const d = particles[i].base.distanceToSquared(p); if (d < best) { best = d; found = i; }
|
|
114
|
+
}
|
|
115
|
+
return found;
|
|
116
|
+
};
|
|
117
|
+
const cloth = pins.filter(pin => pin.cloth && !pin.guide).map(pin => nearest(pin.rest));
|
|
118
|
+
const body = pins.filter(pin => !pin.cloth && !pin.guide).map(pin => nearest(pin.rest));
|
|
119
|
+
const clothDistance = distances(graph, cloth), bodyDistance = distances(graph, body);
|
|
120
|
+
for (let i = 0; i < particles.length; i++) {
|
|
121
|
+
const a = clothDistance[i], b = bodyDistance[i];
|
|
122
|
+
particles[i].share = Number.isFinite(a) && Number.isFinite(b) ? b / Math.max(1e-6, a + b) : Number.isFinite(a) ? 1 : 0;
|
|
123
|
+
particles[i].anchor = Number.isFinite(b) ? Math.exp(-b / (.12 * this.scale)) : 0;
|
|
124
|
+
}
|
|
125
|
+
this.particles = particles;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
get movableCount() { return this.particles.filter(p => p.share > .25).length; }
|
|
129
|
+
|
|
130
|
+
reset() {
|
|
131
|
+
this.time = 0;
|
|
132
|
+
for (const particle of this.particles) particle.position.copy(particle.base), particle.previous.copy(particle.base);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
step(baseVertices: Float32Array, seconds: number, windStrength: number, colliders: readonly ClothCollider[] = [], motion: Readonly<MotionSettings> = DEFAULT_MOTION) {
|
|
136
|
+
const count = this.particles.length, base = Array.from({ length: count }, () => new Vector3()), samples = new Uint32Array(count);
|
|
137
|
+
for (let i = 0; i < this.vertexParticle.length; i++) {
|
|
138
|
+
const node = this.vertexParticle[i]; base[node].add(new Vector3().fromArray(baseVertices, i * 3)); samples[node]++;
|
|
139
|
+
}
|
|
140
|
+
for (let i = 0; i < count; i++) {
|
|
141
|
+
base[i].divideScalar(samples[i] || 1);
|
|
142
|
+
const particle = this.particles[i], delta = base[i].clone().sub(particle.base);
|
|
143
|
+
particle.position.add(delta); particle.previous.add(delta); particle.base.copy(base[i]);
|
|
144
|
+
}
|
|
145
|
+
const dt = Math.min(Math.max(seconds, 0), .05), steps = Math.max(1, Math.ceil(dt * 60)), h = dt / steps;
|
|
146
|
+
const profile = windProfile(windStrength), pressureScale = profile.load ? .025 * profile.surfaceLoad / profile.load : 0;
|
|
147
|
+
const radius = Math.max(1e-4, ...colliders.map(collider => collider.radius));
|
|
148
|
+
const cell = radius * 2;
|
|
149
|
+
const bins = new Map<number, ClothCollider[]>();
|
|
150
|
+
// Hash collisions only add candidates; the distance test below remains exact.
|
|
151
|
+
const key = (x: number, y: number, z: number) => (Math.imul(x, 73856093) ^ Math.imul(y, 19349663) ^ Math.imul(z, 83492791)) | 0;
|
|
152
|
+
for (const collider of colliders) {
|
|
153
|
+
const p = collider.position, id = key(Math.floor(p[0] / cell), Math.floor(p[1] / cell), Math.floor(p[2] / cell));
|
|
154
|
+
const bin = bins.get(id) ?? []; bin.push(collider); bins.set(id, bin);
|
|
155
|
+
}
|
|
156
|
+
for (let step = 0; step < steps && h > 0; step++) {
|
|
157
|
+
this.time += h;
|
|
158
|
+
const force = Array.from({ length: count }, () => new Vector3());
|
|
159
|
+
const area = new Float32Array(count);
|
|
160
|
+
if (pressureScale && motion.strength) for (const triangle of this.triangles) {
|
|
161
|
+
const a = this.particles[triangle.a].position, b = this.particles[triangle.b].position, c = this.particles[triangle.c].position;
|
|
162
|
+
const normal = b.clone().sub(a).cross(c.clone().sub(a));
|
|
163
|
+
const surface = normal.length(); if (surface < 1e-10) continue;
|
|
164
|
+
normal.divideScalar(surface);
|
|
165
|
+
const center = a.clone().add(b).add(c).divideScalar(3);
|
|
166
|
+
const wind = windAcceleration(this.time, windStrength, this.scale, center.toArray());
|
|
167
|
+
// Normal pressure bends the sheet; a small tangential drag carries
|
|
168
|
+
// near-parallel fabric rather than letting it ignore the wind entirely.
|
|
169
|
+
const pressure = normal.multiplyScalar(wind.dot(normal)).addScaledVector(wind, .12)
|
|
170
|
+
.multiplyScalar(pressureScale);
|
|
171
|
+
for (const node of [triangle.a, triangle.b, triangle.c]) { force[node].addScaledVector(pressure, surface); area[node] += surface; }
|
|
172
|
+
}
|
|
173
|
+
for (let i = 0; i < count; i++) {
|
|
174
|
+
const particle = this.particles[i], share = particle.share;
|
|
175
|
+
if (share < 1e-4) { particle.position.copy(particle.base); particle.previous.copy(particle.base); continue; }
|
|
176
|
+
const velocity = particle.position.clone().sub(particle.previous).multiplyScalar(Math.exp(-motion.damping * 3 * h));
|
|
177
|
+
particle.previous.copy(particle.position);
|
|
178
|
+
particle.position.add(velocity).y -= 2.5 * this.scale * share * h * h;
|
|
179
|
+
if (area[i]) particle.position.addScaledVector(force[i], motion.strength * share * h * h / area[i]);
|
|
180
|
+
const anchor = 1 - Math.exp(-(1.5 + 60 * particle.anchor ** 2) * h);
|
|
181
|
+
particle.position.lerp(particle.base, anchor);
|
|
182
|
+
}
|
|
183
|
+
for (let iteration = 0; iteration < 5; iteration++) {
|
|
184
|
+
for (const edge of this.edges) {
|
|
185
|
+
const a = this.particles[edge.a], b = this.particles[edge.b];
|
|
186
|
+
const difference = b.position.clone().sub(a.position), length = difference.length();
|
|
187
|
+
if (length < 1e-8) continue;
|
|
188
|
+
const wa = a.share * a.share, wb = b.share * b.share, total = wa + wb;
|
|
189
|
+
if (total < 1e-8) continue;
|
|
190
|
+
const strength = edge.bend ? .14 : .72;
|
|
191
|
+
difference.multiplyScalar((length - edge.length) * strength / (length * total));
|
|
192
|
+
a.position.addScaledVector(difference, wa); b.position.addScaledVector(difference, -wb);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (colliders.length) for (const particle of this.particles) {
|
|
196
|
+
if (particle.share < .35) continue;
|
|
197
|
+
const p = particle.position, px = p.x / cell, py = p.y / cell, pz = p.z / cell;
|
|
198
|
+
const x = Math.floor(px), y = Math.floor(py), z = Math.floor(pz);
|
|
199
|
+
const sx = px - x < .5 ? -1 : 1, sy = py - y < .5 ? -1 : 1, sz = pz - z < .5 ? -1 : 1;
|
|
200
|
+
let chosen: ClothCollider | undefined, penetration = 0;
|
|
201
|
+
// Cell width is twice the largest radius, so only the nearest neighbor
|
|
202
|
+
// on each axis can contain a sphere reaching this particle.
|
|
203
|
+
for (let iz = 0; iz < 2; iz++) for (let iy = 0; iy < 2; iy++) for (let ix = 0; ix < 2; ix++) {
|
|
204
|
+
for (const collider of bins.get(key(x + ix * sx, y + iy * sy, z + iz * sz)) ?? []) {
|
|
205
|
+
const center = collider.position, bx = particle.base.x - center[0], by = particle.base.y - center[1], bz = particle.base.z - center[2];
|
|
206
|
+
if (bx * bx + by * by + bz * bz < collider.radius * collider.radius * 1.04) continue;
|
|
207
|
+
const vx = p.x - center[0], vy = p.y - center[1], vz = p.z - center[2];
|
|
208
|
+
const depth = collider.radius - Math.hypot(vx, vy, vz);
|
|
209
|
+
if (depth > penetration) { penetration = depth; chosen = collider; }
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (chosen) {
|
|
213
|
+
const center = chosen.position, delta = p.clone().sub(new Vector3().fromArray(center));
|
|
214
|
+
if (delta.lengthSq() < 1e-12) delta.copy(particle.base).sub(new Vector3().fromArray(center));
|
|
215
|
+
p.copy(new Vector3().fromArray(center).add(delta.setLength(chosen.radius)));
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const result = new Float32Array(baseVertices.length);
|
|
220
|
+
for (let i = 0; i < this.vertexParticle.length; i++) result.set(this.particles[this.vertexParticle[i]].position.toArray(), i * 3);
|
|
221
|
+
return result;
|
|
222
|
+
}
|
|
223
|
+
}
|