@yaks/voxel 0.0.0-stage → 0.2.36
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 +202 -0
- package/README.md +79 -2
- package/_dist/boxes.d.ts +80 -0
- package/_dist/boxes.d.ts.map +1 -0
- package/_dist/chartbook.d.ts +14 -0
- package/_dist/chartbook.d.ts.map +1 -0
- package/_dist/chartsheet.d.ts +8 -0
- package/_dist/chartsheet.d.ts.map +1 -0
- package/_dist/light.d.ts +18 -0
- package/_dist/light.d.ts.map +1 -0
- package/_dist/mesh.d.ts +120 -0
- package/_dist/mesh.d.ts.map +1 -0
- package/_dist/mod.d.ts +7 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/stream.d.ts +33 -0
- package/_dist/stream.d.ts.map +1 -0
- package/_dist/three/mod.d.ts +5 -0
- package/_dist/three/mod.d.ts.map +1 -0
- package/_dist/three/parts.d.ts +59 -0
- package/_dist/three/parts.d.ts.map +1 -0
- package/_dist/three/puppet.d.ts +93 -0
- package/_dist/three/puppet.d.ts.map +1 -0
- package/_dist/three/soft.d.ts +27 -0
- package/_dist/three/soft.d.ts.map +1 -0
- package/boxes.js +265 -0
- package/boxes.js.map +1 -0
- package/boxes.ts +266 -0
- package/chartbook.js +63 -0
- package/chartbook.js.map +1 -0
- package/chartbook.ts +88 -0
- package/chartsheet.js +31 -0
- package/chartsheet.js.map +1 -0
- package/chartsheet.ts +32 -0
- package/deno.json +1 -0
- package/light.js +45 -0
- package/light.js.map +1 -0
- package/light.ts +49 -0
- package/mesh.js +577 -0
- package/mesh.js.map +1 -0
- package/mesh.ts +624 -0
- package/mod.js +7 -0
- package/mod.js.map +1 -0
- package/mod.ts +7 -0
- package/package.json +45 -4
- package/stream.js +80 -0
- package/stream.js.map +1 -0
- package/stream.ts +86 -0
- package/three/mod.js +7 -0
- package/three/mod.js.map +1 -0
- package/three/mod.ts +7 -0
- package/three/parts.js +181 -0
- package/three/parts.js.map +1 -0
- package/three/parts.ts +185 -0
- package/three/puppet.js +311 -0
- package/three/puppet.js.map +1 -0
- package/three/puppet.ts +471 -0
- package/three/soft.js +338 -0
- package/three/soft.js.map +1 -0
- package/three/soft.ts +382 -0
package/stream.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// Which chunks the page draws, and how finely. Every chunk whose nearest
|
|
2
|
+
// point lies within sight of where the page looks is drawn, finer the nearer:
|
|
3
|
+
// a voxel's step is a few pixels wherever it is drawn, so the ground near the
|
|
4
|
+
// focus shows fine detail and the hills at the edge of the fog coarser
|
|
5
|
+
// detail. Chunks further off than sight are let go. Plain numbers, so the page
|
|
6
|
+
// asks it every frame.
|
|
7
|
+
/** How near a chunk's nearest point must be, in metres, to be drawn at each
|
|
8
|
+
* detail: the finest within the first ring, the next within the second, and
|
|
9
|
+
* the coarsest out to the edge of sight. */ export let RINGS = [
|
|
10
|
+
40,
|
|
11
|
+
72
|
|
12
|
+
];
|
|
13
|
+
/** How much coarser each detail's voxels are than the finest's. */ export let COARSER = [
|
|
14
|
+
1,
|
|
15
|
+
2,
|
|
16
|
+
4
|
|
17
|
+
];
|
|
18
|
+
// How far past a ring's line a chunk keeps the detail it is drawn at, and
|
|
19
|
+
// past sight how far it stays drawn, in metres, so a chunk on the line does
|
|
20
|
+
// not flicker between two.
|
|
21
|
+
let SLACK = 6;
|
|
22
|
+
// How far (x, z) is from the nearest point of chunk (ci, ck).
|
|
23
|
+
let gap = (chunkSize, x, z, ci, ck)=>{
|
|
24
|
+
let dx = Math.max(ci * chunkSize - x, 0, x - (ci + 1) * chunkSize);
|
|
25
|
+
let dz = Math.max(ck * chunkSize - z, 0, z - (ck + 1) * chunkSize);
|
|
26
|
+
return Math.sqrt(dx * dx + dz * dz);
|
|
27
|
+
};
|
|
28
|
+
// Whether a chunk's index is within a world `per` chunks across.
|
|
29
|
+
let within = (c, per)=>per == Infinity || c >= 0 && c < per;
|
|
30
|
+
// The detail a chunk this far off is drawn at, given the one it has: kept
|
|
31
|
+
// while it is within SLACK of its own ring.
|
|
32
|
+
let detail = (d, has)=>{
|
|
33
|
+
if (has != null) {
|
|
34
|
+
let inner = RINGS[has - 1] ?? -Infinity, outer = RINGS[has] ?? Infinity;
|
|
35
|
+
if (d >= inner - SLACK && d < outer + SLACK) return has;
|
|
36
|
+
}
|
|
37
|
+
let i = RINGS.findIndex((r)=>d < r);
|
|
38
|
+
return i < 0 ? RINGS.length : i;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Every chunk of `chunkSize` metres to draw from (x, z), nearest first: each one within `sight`
|
|
42
|
+
* metres, at its detail, and each drawn one still within `sight` and SLACK,
|
|
43
|
+
* at the detail it has unless it strayed past its ring. `has` says what a
|
|
44
|
+
* chunk is drawn at now; `per`, when the world has edges, keeps to chunks 0
|
|
45
|
+
* to `per` − 1 each way.
|
|
46
|
+
*
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { assertEquals } from '@std/assert'
|
|
49
|
+
* let of = (w: Want[], ci: number, ck: number) =>
|
|
50
|
+
* w.find((c) => c.ci == ci && c.ck == ck)?.lod
|
|
51
|
+
* // Standing in chunk (0, 0) of 32 m chunks: it is drawn finest, a chunk
|
|
52
|
+
* // 48 m off coarser, one 80 m off coarser again, and one past sight not at
|
|
53
|
+
* // all.
|
|
54
|
+
* let w = wanted(32, 16, 16, 110, () => undefined)
|
|
55
|
+
* assertEquals([of(w, 0, 0), of(w, 2, 0), of(w, 3, 0), of(w, 4, 0)], [0, 1, 2, undefined])
|
|
56
|
+
* assertEquals(w[0], { ci: 0, ck: 0, lod: 0, d: 0 })
|
|
57
|
+
* // A world with edges draws nothing past them.
|
|
58
|
+
* assertEquals(wanted(32, 16, 16, 110, () => undefined, 4).every((c) => c.ci >= 0 && c.ci < 4), true)
|
|
59
|
+
* ```
|
|
60
|
+
*/ export let wanted = (chunkSize, x, z, sight, has, per = Infinity)=>{
|
|
61
|
+
let r = Math.ceil((sight + SLACK) / chunkSize);
|
|
62
|
+
let fi = Math.floor(x / chunkSize), fk = Math.floor(z / chunkSize);
|
|
63
|
+
let out = [];
|
|
64
|
+
for(let ck = fk - r; ck <= fk + r; ck++){
|
|
65
|
+
for(let ci = fi - r; ci <= fi + r; ci++){
|
|
66
|
+
if (!within(ci, per) || !within(ck, per)) continue;
|
|
67
|
+
let d = gap(chunkSize, x, z, ci, ck), now = has(ci, ck);
|
|
68
|
+
if (d < sight || now != null && d < sight + SLACK) {
|
|
69
|
+
out.push({
|
|
70
|
+
ci,
|
|
71
|
+
ck,
|
|
72
|
+
lod: detail(d, now),
|
|
73
|
+
d
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return out.sort((a, b)=>a.d - b.d);
|
|
79
|
+
};
|
|
80
|
+
//# sourceMappingURL=stream.js.map
|
package/stream.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.js","sources":["./stream.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,8EAA8E;AAC9E,8EAA8E;AAC9E,uEAAuE;AACvE,+EAA+E;AAC/E,uBAAuB;AAEvB;;0CAE0C,GAC1C,OAAO,IAAI,QAAQ;EAAC;EAAI;CAAG,CAAA;AAC3B,iEAAiE,GACjE,OAAO,IAAI,UAAU;EAAC;EAAG;EAAG;CAAE,CAAA;AAC9B,0EAA0E;AAC1E,4EAA4E;AAC5E,2BAA2B;AAC3B,IAAI,QAAQ;AAMZ,8DAA8D;AAC9D,IAAI,MAAM,CAAC,WAAmB,GAAW,GAAW,IAAY;EAC9D,IAAI,KAAK,KAAK,GAAG,CAAC,KAAK,YAAY,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI;EACxD,IAAI,KAAK,KAAK,GAAG,CAAC,KAAK,YAAY,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI;EACxD,OAAO,KAAK,IAAI,CAAC,KAAK,KAAK,KAAK;AAClC;AAEA,iEAAiE;AACj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, and how finely. Every chunk whose nearest
|
|
2
|
+
// point lies within sight of where the page looks is drawn, finer the nearer:
|
|
3
|
+
// a voxel's step is a few pixels wherever it is drawn, so the ground near the
|
|
4
|
+
// focus shows fine detail and the hills at the edge of the fog coarser
|
|
5
|
+
// detail. Chunks further off than sight are let go. Plain numbers, so the page
|
|
6
|
+
// asks it every frame.
|
|
7
|
+
|
|
8
|
+
/** How near a chunk's nearest point must be, in metres, to be drawn at each
|
|
9
|
+
* detail: the finest within the first ring, the next within the second, and
|
|
10
|
+
* the coarsest out to the edge of sight. */
|
|
11
|
+
export let RINGS = [40, 72]
|
|
12
|
+
/** How much coarser each detail's voxels are than the finest's. */
|
|
13
|
+
export let COARSER = [1, 2, 4]
|
|
14
|
+
// How far past a ring's line a chunk keeps the detail it is drawn at, and
|
|
15
|
+
// past sight how far it stays drawn, in metres, so a chunk on the line does
|
|
16
|
+
// not flicker between two.
|
|
17
|
+
let SLACK = 6
|
|
18
|
+
|
|
19
|
+
/** A chunk to draw, `ci` across and `ck` down, at a detail (0 the finest),
|
|
20
|
+
* and how far its nearest point is, in metres. */
|
|
21
|
+
export type Want = { ci: number; ck: number; lod: number; d: number }
|
|
22
|
+
|
|
23
|
+
// How far (x, z) is from the nearest point of chunk (ci, ck).
|
|
24
|
+
let gap = (chunkSize: number, x: number, z: number, ci: number, ck: number) => {
|
|
25
|
+
let dx = Math.max(ci * chunkSize - x, 0, x - (ci + 1) * chunkSize)
|
|
26
|
+
let dz = Math.max(ck * chunkSize - z, 0, z - (ck + 1) * chunkSize)
|
|
27
|
+
return Math.sqrt(dx * dx + dz * dz)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Whether a chunk's index is within a world `per` chunks across.
|
|
31
|
+
let within = (c: number, per: number) => per == Infinity || (c >= 0 && c < per)
|
|
32
|
+
|
|
33
|
+
// The detail a chunk this far off is drawn at, given the one it has: kept
|
|
34
|
+
// while it is within SLACK of its own ring.
|
|
35
|
+
let detail = (d: number, has?: number) => {
|
|
36
|
+
if (has != null) {
|
|
37
|
+
let inner = RINGS[has - 1] ?? -Infinity, outer = RINGS[has] ?? Infinity
|
|
38
|
+
if (d >= inner - SLACK && d < outer + SLACK) return has
|
|
39
|
+
}
|
|
40
|
+
let i = RINGS.findIndex((r) => d < r)
|
|
41
|
+
return i < 0 ? RINGS.length : i
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Every chunk of `chunkSize` metres to draw from (x, z), nearest first: each one within `sight`
|
|
46
|
+
* metres, at its detail, and each drawn one still within `sight` and SLACK,
|
|
47
|
+
* at the detail it has unless it strayed past its ring. `has` says what a
|
|
48
|
+
* chunk is drawn at now; `per`, when the world has edges, keeps to chunks 0
|
|
49
|
+
* to `per` − 1 each way.
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { assertEquals } from '@std/assert'
|
|
53
|
+
* let of = (w: Want[], ci: number, ck: number) =>
|
|
54
|
+
* w.find((c) => c.ci == ci && c.ck == ck)?.lod
|
|
55
|
+
* // Standing in chunk (0, 0) of 32 m chunks: it is drawn finest, a chunk
|
|
56
|
+
* // 48 m off coarser, one 80 m off coarser again, and one past sight not at
|
|
57
|
+
* // all.
|
|
58
|
+
* let w = wanted(32, 16, 16, 110, () => undefined)
|
|
59
|
+
* assertEquals([of(w, 0, 0), of(w, 2, 0), of(w, 3, 0), of(w, 4, 0)], [0, 1, 2, undefined])
|
|
60
|
+
* assertEquals(w[0], { ci: 0, ck: 0, lod: 0, d: 0 })
|
|
61
|
+
* // A world with edges draws nothing past them.
|
|
62
|
+
* assertEquals(wanted(32, 16, 16, 110, () => undefined, 4).every((c) => c.ci >= 0 && c.ci < 4), true)
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export let wanted = (
|
|
66
|
+
chunkSize: number,
|
|
67
|
+
x: number,
|
|
68
|
+
z: number,
|
|
69
|
+
sight: number,
|
|
70
|
+
has: (ci: number, ck: number) => number | undefined,
|
|
71
|
+
per = Infinity,
|
|
72
|
+
): Want[] => {
|
|
73
|
+
let r = Math.ceil((sight + SLACK) / chunkSize)
|
|
74
|
+
let fi = Math.floor(x / chunkSize), fk = Math.floor(z / chunkSize)
|
|
75
|
+
let out: Want[] = []
|
|
76
|
+
for (let ck = fk - r; ck <= fk + r; ck++) {
|
|
77
|
+
for (let ci = fi - r; ci <= fi + r; ci++) {
|
|
78
|
+
if (!within(ci, per) || !within(ck, per)) continue
|
|
79
|
+
let d = gap(chunkSize, x, z, ci, ck), now = has(ci, ck)
|
|
80
|
+
if (d < sight || (now != null && d < sight + SLACK)) {
|
|
81
|
+
out.push({ ci, ck, lod: detail(d, now), d })
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return out.sort((a, b) => a.d - b.d)
|
|
86
|
+
}
|
package/three/mod.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Three.js rendering of voxel quads and box rigs. The voxel root stays worker-safe. */ export * from "./soft.js";
|
|
2
|
+
export * from "./parts.js";
|
|
3
|
+
export * from "./puppet.js";
|
|
4
|
+
// Published apps import `light` from here, where it lived before
|
|
5
|
+
// @yaks/voxel/light; it keeps answering for them.
|
|
6
|
+
export * from "../light.js";
|
|
7
|
+
//# sourceMappingURL=mod.js.map
|
package/three/mod.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA,sFAAsF,GACtF,0BAAyB;AACzB,2BAA0B;AAC1B,4BAA2B;AAC3B,iEAAiE;AACjE,kDAAkD;AAClD,4BAA2B"}
|
package/three/mod.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Three.js rendering of voxel quads and box rigs. The voxel root stays worker-safe. */
|
|
2
|
+
export * from './soft.js'
|
|
3
|
+
export * from './parts.js'
|
|
4
|
+
export * from './puppet.js'
|
|
5
|
+
// Published apps import `light` from here, where it lived before
|
|
6
|
+
// @yaks/voxel/light; it keeps answering for them.
|
|
7
|
+
export * from '../light.js'
|
package/three/parts.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
// The joinery a rig is built from: a part is
|
|
2
|
+
// a few soft boxes (boxes.ts) turning about a pivot, and a figure is parts
|
|
3
|
+
// hung on parts. A limb is two parts bending at a knee or an elbow (`limb`),
|
|
4
|
+
// its halves overlapping at the joint so a bend opens no gap.
|
|
5
|
+
//
|
|
6
|
+
// A part is a bone, and a figure's parts are drawn together as one skinned
|
|
7
|
+
// mesh (`knit`): one draw call and one shadow for the whole figure, however
|
|
8
|
+
// many joints it bends at. A part's boxes are one solid, each worn over those
|
|
9
|
+
// before it (boxes.ts), and a part is worn over the parts before it where
|
|
10
|
+
// they lie square to each other as the figure is built: a thigh against a
|
|
11
|
+
// flank, a hand round a hilt.
|
|
12
|
+
// @ts-types="npm:@types/three@^0.186.0"
|
|
13
|
+
import * as THREE from "three";
|
|
14
|
+
import { drawn, moved, worn } from "../boxes.js";
|
|
15
|
+
import { out, pack, place } from "../mesh.js";
|
|
16
|
+
import { geometry } from "./soft.js";
|
|
17
|
+
/** A part: boxes turning about `pivot`, tinted in voxels of edge `cell`. It
|
|
18
|
+
* is drawn once its figure is knitted. */ export let partOf = (boxes, pivot, cell = 0.1)=>{
|
|
19
|
+
let b = new THREE.Bone();
|
|
20
|
+
b.position.set(...pivot);
|
|
21
|
+
b.userData.boxes = boxes;
|
|
22
|
+
b.userData.cell = cell;
|
|
23
|
+
return b;
|
|
24
|
+
};
|
|
25
|
+
/** A limb bending partway down, at a knee or an elbow: `upper` turning at
|
|
26
|
+
* `pivot`, and `lower` hung from it, turning at `joint` in the upper's space.
|
|
27
|
+
*/ export let limb = (upper, lower, pivot, joint)=>{
|
|
28
|
+
let a = partOf(upper, pivot), b = partOf(lower, joint);
|
|
29
|
+
a.add(b);
|
|
30
|
+
return [
|
|
31
|
+
a,
|
|
32
|
+
b
|
|
33
|
+
];
|
|
34
|
+
};
|
|
35
|
+
// Where a point in `from`'s space lies in `to`'s, as the figure stands, when
|
|
36
|
+
// the two are turned square to each other, by quarter turns; otherwise null.
|
|
37
|
+
let square = (from, to)=>{
|
|
38
|
+
let m = to.matrixWorld.clone().invert().multiply(from.matrixWorld);
|
|
39
|
+
let e = m.elements;
|
|
40
|
+
for(let c = 0; c < 12; c += 4){
|
|
41
|
+
let col = [
|
|
42
|
+
e[c],
|
|
43
|
+
e[c + 1],
|
|
44
|
+
e[c + 2]
|
|
45
|
+
], n = Math.hypot(...col);
|
|
46
|
+
if (col.filter((x)=>Math.abs(x) > n * 1e-6).length != 1) return null;
|
|
47
|
+
}
|
|
48
|
+
let v = new THREE.Vector3();
|
|
49
|
+
return ([x, y, z])=>{
|
|
50
|
+
v.set(x, y, z).applyMatrix4(m);
|
|
51
|
+
return [
|
|
52
|
+
v.x,
|
|
53
|
+
v.y,
|
|
54
|
+
v.z
|
|
55
|
+
];
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
/** Draw every part under `root` as one skinned mesh in `material`, each part
|
|
59
|
+
* a bone of it. A part's boxes stay in its own space and its bone carries
|
|
60
|
+
* them wherever the part is turned, so a pose is set on the parts as before.
|
|
61
|
+
* Each part is worn over the parts before it that lie square to it, and what
|
|
62
|
+
* it is drawn as is kept on its bone (`userData.part`).
|
|
63
|
+
*/ export let knit = (root, material)=>{
|
|
64
|
+
let bones = [];
|
|
65
|
+
root.traverse((o)=>{
|
|
66
|
+
if (o instanceof THREE.Bone && o.userData.boxes) bones.push(o);
|
|
67
|
+
});
|
|
68
|
+
root.updateMatrixWorld(true);
|
|
69
|
+
let solids = [];
|
|
70
|
+
let all = out(), index = [], weight = [];
|
|
71
|
+
for (let [i, b] of bones.entries()){
|
|
72
|
+
let under = bones.slice(0, i).flatMap((a, j)=>{
|
|
73
|
+
let f = square(a, b);
|
|
74
|
+
return f ? moved(solids[j], f) : [];
|
|
75
|
+
});
|
|
76
|
+
solids.push(worn(b.userData.boxes, undefined, under));
|
|
77
|
+
let part = drawn(out(), solids[i], b.userData.cell);
|
|
78
|
+
b.userData.part = part;
|
|
79
|
+
place(all, part, [
|
|
80
|
+
0,
|
|
81
|
+
0,
|
|
82
|
+
0
|
|
83
|
+
]);
|
|
84
|
+
for(let v = 0; v < part.pos.length; v += 3){
|
|
85
|
+
index.push(i, 0, 0, 0);
|
|
86
|
+
weight.push(1, 0, 0, 0);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
let g = geometry(pack(all));
|
|
90
|
+
g.setAttribute('skinIndex', new THREE.Uint16BufferAttribute(index, 4));
|
|
91
|
+
g.setAttribute('skinWeight', new THREE.Float32BufferAttribute(weight, 4));
|
|
92
|
+
let mesh = new THREE.SkinnedMesh(g, material);
|
|
93
|
+
mesh.castShadow = true;
|
|
94
|
+
// Each bone's inverse is the identity: a part's vertices are already in its
|
|
95
|
+
// bone's space, so the bone's place in the world is the part's.
|
|
96
|
+
mesh.bind(new THREE.Skeleton(bones, bones.map(()=>new THREE.Matrix4())), new THREE.Matrix4());
|
|
97
|
+
root.add(mesh);
|
|
98
|
+
// Its bounds, from the pose it is built in, with room for a limb flung out
|
|
99
|
+
// or a lunge: what the camera judges whether to draw it by.
|
|
100
|
+
root.updateMatrixWorld(true);
|
|
101
|
+
mesh.computeBoundingSphere();
|
|
102
|
+
let bound = mesh.boundingSphere;
|
|
103
|
+
bound.radius = bound.radius * 1.4 + 0.3;
|
|
104
|
+
return mesh;
|
|
105
|
+
};
|
|
106
|
+
/** A box toppled forward, a quarter turn about x: what stood up runs toward
|
|
107
|
+
* +z, and what lay toward +z hangs below.
|
|
108
|
+
*
|
|
109
|
+
* ```ts
|
|
110
|
+
* import { assertEquals } from '@std/assert'
|
|
111
|
+
* assertEquals(topple([[0, 0, 0], [1, 2, 3], 7]), [[0, -3, 0], [1, 3, 2], 7])
|
|
112
|
+
* ```
|
|
113
|
+
*/ export let topple = ([[x, y, z], [w, h, d], ...rest])=>[
|
|
114
|
+
[
|
|
115
|
+
x,
|
|
116
|
+
-z - d,
|
|
117
|
+
y
|
|
118
|
+
],
|
|
119
|
+
[
|
|
120
|
+
w,
|
|
121
|
+
d,
|
|
122
|
+
h
|
|
123
|
+
],
|
|
124
|
+
...rest
|
|
125
|
+
];
|
|
126
|
+
/** Boxes drawn for a part of one size, fit to a part `k` times it along
|
|
127
|
+
* each axis, about the part's pivot.
|
|
128
|
+
*
|
|
129
|
+
* ```ts
|
|
130
|
+
* import { assertEquals } from '@std/assert'
|
|
131
|
+
* assertEquals(fit([[[-0.5, 0, 0], [1, 1, 1], 7]], [2, 1, 0.5]), [
|
|
132
|
+
* [[-1, 0, 0], [2, 1, 0.5], 7],
|
|
133
|
+
* ])
|
|
134
|
+
* ```
|
|
135
|
+
*/ export let fit = (boxes, k)=>boxes.map(([min, size, ...rest])=>[
|
|
136
|
+
[
|
|
137
|
+
min[0] * k[0],
|
|
138
|
+
min[1] * k[1],
|
|
139
|
+
min[2] * k[2]
|
|
140
|
+
],
|
|
141
|
+
[
|
|
142
|
+
size[0] * k[0],
|
|
143
|
+
size[1] * k[1],
|
|
144
|
+
size[2] * k[2]
|
|
145
|
+
],
|
|
146
|
+
...rest
|
|
147
|
+
]);
|
|
148
|
+
/** A colour, darker (`k` under 1) or lighter. */ export let shade = (hex, k)=>{
|
|
149
|
+
let c = new THREE.Color(hex);
|
|
150
|
+
c.multiplyScalar(k);
|
|
151
|
+
return c.getHex();
|
|
152
|
+
};
|
|
153
|
+
export let ease = (t)=>t < 0.5 ? 2 * t * t : 1 - (-2 * t + 2) ** 2 / 2;
|
|
154
|
+
/** How far into a blow: rising to 1 at `peak` of the swing, and back.
|
|
155
|
+
*
|
|
156
|
+
* ```ts
|
|
157
|
+
* import { assertEquals } from '@std/assert'
|
|
158
|
+
* assertEquals([lunge(0), lunge(0.4), lunge(1)], [0, 1, 0])
|
|
159
|
+
* ```
|
|
160
|
+
*/ export let lunge = (swing, peak = 0.4)=>swing < peak ? ease(swing / peak) : 1 - ease((swing - peak) / (1 - peak));
|
|
161
|
+
/** A leg's step in a walk, `s` and `c` the sine and cosine of how far
|
|
162
|
+
* through it, `amp` how far it swings: back and forth from the hip, and bent
|
|
163
|
+
* at the knee while the foot comes forward.
|
|
164
|
+
*
|
|
165
|
+
* ```ts
|
|
166
|
+
* import { assertEquals } from '@std/assert'
|
|
167
|
+
* import * as THREE from 'three'
|
|
168
|
+
* let leg: Limb = [new THREE.Bone(), new THREE.Bone()]
|
|
169
|
+
* stride(leg, 0, -1, 0.5) // passing under the hip, coming forward
|
|
170
|
+
* assertEquals([leg[0].rotation.x, leg[1].rotation.x], [0, 0.75])
|
|
171
|
+
* stride(leg, 0, 1, 0.5) // pushing back: straight
|
|
172
|
+
* assertEquals(leg[1].rotation.x, 0)
|
|
173
|
+
* ```
|
|
174
|
+
*/ export let stride = (leg, s, c, amp)=>{
|
|
175
|
+
let [hip, knee] = Array.isArray(leg) ? leg : [
|
|
176
|
+
leg
|
|
177
|
+
];
|
|
178
|
+
hip.rotation.x = s * amp;
|
|
179
|
+
if (knee) knee.rotation.x = Math.max(0, -c) * amp * 1.5;
|
|
180
|
+
};
|
|
181
|
+
//# sourceMappingURL=parts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parts.js","sources":["./parts.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,2EAA2E;AAC3E,6EAA6E;AAC7E,8DAA8D;AAC9D,EAAE;AACF,2EAA2E;AAC3E,4EAA4E;AAC5E,8EAA8E;AAC9E,0EAA0E;AAC1E,0EAA0E;AAC1E,8BAA8B;AAC9B,wCAAwC;AACxC,YAAY,mBAA+B;AAC3C,SAAmB,KAAK,EAAE,KAAK,EAAc,IAAI,sBAAqB;AACtE,SAAS,GAAG,EAAE,IAAI,EAAE,KAAK,qBAA8B;AACvD,SAAS,QAAQ,oBAAmB;AAQpC;wCACwC,GACxC,OAAO,IAAI,SAAS,CAAC,OAAc,OAAY,OAAO,GAAG;EACvD,IAAI,IAAI,IA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AaC,GACD,OAAO,IAAI,SAAS,CAAC,KAAU,GAAW,GAAW;EACnD,IAAI,CAAC,KAAK,KAAK,GAAG,MAAM,OAAO,CAAC,OAAO,MAAM;IAAC;GAAI;EAClD,IAAI,QAAQ,CAAC,CAAC,GAAG,IAAI;EACrB,IAAI,MAAM,KAAK,QAAQ,CAAC,CAAC,GAAG,KAAK,GAAG,CAAC,GAAG,CAAC,KAAK,MAAM;AACtD,EAAC"}
|
package/three/parts.ts
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// The joinery a rig is built from: a part is
|
|
2
|
+
// a few soft boxes (boxes.ts) turning about a pivot, and a figure is parts
|
|
3
|
+
// hung on parts. A limb is two parts bending at a knee or an elbow (`limb`),
|
|
4
|
+
// its halves overlapping at the joint so a bend opens no gap.
|
|
5
|
+
//
|
|
6
|
+
// A part is a bone, and a figure's parts are drawn together as one skinned
|
|
7
|
+
// mesh (`knit`): one draw call and one shadow for the whole figure, however
|
|
8
|
+
// many joints it bends at. A part's boxes are one solid, each worn over those
|
|
9
|
+
// before it (boxes.ts), and a part is worn over the parts before it where
|
|
10
|
+
// they lie square to each other as the figure is built: a thigh against a
|
|
11
|
+
// flank, a hand round a hilt.
|
|
12
|
+
// @ts-types="npm:@types/three@^0.186.0"
|
|
13
|
+
import * as THREE from 'three'
|
|
14
|
+
import { type Box, drawn, moved, type Solid, worn } from '../boxes.js'
|
|
15
|
+
import { out, pack, place, type Vec } from '../mesh.js'
|
|
16
|
+
import { geometry } from './soft.js'
|
|
17
|
+
|
|
18
|
+
/** A limb: the part at the hip or shoulder, and the one below the joint. */
|
|
19
|
+
export type Limb = [THREE.Bone, THREE.Bone]
|
|
20
|
+
|
|
21
|
+
/** A leg: one part, or a limb. */
|
|
22
|
+
export type Leg = THREE.Object3D | Limb
|
|
23
|
+
|
|
24
|
+
/** A part: boxes turning about `pivot`, tinted in voxels of edge `cell`. It
|
|
25
|
+
* is drawn once its figure is knitted. */
|
|
26
|
+
export let partOf = (boxes: Box[], pivot: Vec, cell = 0.1): THREE.Bone => {
|
|
27
|
+
let b = new THREE.Bone()
|
|
28
|
+
b.position.set(...pivot)
|
|
29
|
+
b.userData.boxes = boxes
|
|
30
|
+
b.userData.cell = cell
|
|
31
|
+
return b
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A limb bending partway down, at a knee or an elbow: `upper` turning at
|
|
35
|
+
* `pivot`, and `lower` hung from it, turning at `joint` in the upper's space.
|
|
36
|
+
*/
|
|
37
|
+
export let limb = (
|
|
38
|
+
upper: Box[],
|
|
39
|
+
lower: Box[],
|
|
40
|
+
pivot: Vec,
|
|
41
|
+
joint: Vec,
|
|
42
|
+
): Limb => {
|
|
43
|
+
let a = partOf(upper, pivot), b = partOf(lower, joint)
|
|
44
|
+
a.add(b)
|
|
45
|
+
return [a, b]
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Where a point in `from`'s space lies in `to`'s, as the figure stands, when
|
|
49
|
+
// the two are turned square to each other, by quarter turns; otherwise null.
|
|
50
|
+
let square = (from: THREE.Object3D, to: THREE.Object3D) => {
|
|
51
|
+
let m = to.matrixWorld.clone().invert().multiply(from.matrixWorld)
|
|
52
|
+
let e = m.elements
|
|
53
|
+
for (let c = 0; c < 12; c += 4) {
|
|
54
|
+
let col = [e[c], e[c + 1], e[c + 2]], n = Math.hypot(...col)
|
|
55
|
+
if (col.filter((x) => Math.abs(x) > n * 1e-6).length != 1) return null
|
|
56
|
+
}
|
|
57
|
+
let v = new THREE.Vector3()
|
|
58
|
+
return ([x, y, z]: Vec): Vec => {
|
|
59
|
+
v.set(x, y, z).applyMatrix4(m)
|
|
60
|
+
return [v.x, v.y, v.z]
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Draw every part under `root` as one skinned mesh in `material`, each part
|
|
65
|
+
* a bone of it. A part's boxes stay in its own space and its bone carries
|
|
66
|
+
* them wherever the part is turned, so a pose is set on the parts as before.
|
|
67
|
+
* Each part is worn over the parts before it that lie square to it, and what
|
|
68
|
+
* it is drawn as is kept on its bone (`userData.part`).
|
|
69
|
+
*/
|
|
70
|
+
export let knit = (
|
|
71
|
+
root: THREE.Object3D,
|
|
72
|
+
material: THREE.Material,
|
|
73
|
+
): THREE.SkinnedMesh<THREE.BufferGeometry, THREE.Material> => {
|
|
74
|
+
let bones: THREE.Bone[] = []
|
|
75
|
+
root.traverse((o) => {
|
|
76
|
+
if (o instanceof THREE.Bone && o.userData.boxes) bones.push(o)
|
|
77
|
+
})
|
|
78
|
+
root.updateMatrixWorld(true)
|
|
79
|
+
let solids: Solid[][] = []
|
|
80
|
+
let all = out(), index: number[] = [], weight: number[] = []
|
|
81
|
+
for (let [i, b] of bones.entries()) {
|
|
82
|
+
let under = bones.slice(0, i).flatMap((a, j) => {
|
|
83
|
+
let f = square(a, b)
|
|
84
|
+
return f ? moved(solids[j], f) : []
|
|
85
|
+
})
|
|
86
|
+
solids.push(worn(b.userData.boxes, undefined, under))
|
|
87
|
+
let part = drawn(out(), solids[i], b.userData.cell)
|
|
88
|
+
b.userData.part = part
|
|
89
|
+
place(all, part, [0, 0, 0])
|
|
90
|
+
for (let v = 0; v < part.pos.length; v += 3) {
|
|
91
|
+
index.push(i, 0, 0, 0)
|
|
92
|
+
weight.push(1, 0, 0, 0)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
let g = geometry(pack(all))
|
|
96
|
+
g.setAttribute('skinIndex', new THREE.Uint16BufferAttribute(index, 4))
|
|
97
|
+
g.setAttribute('skinWeight', new THREE.Float32BufferAttribute(weight, 4))
|
|
98
|
+
let mesh = new THREE.SkinnedMesh(g, material)
|
|
99
|
+
mesh.castShadow = true
|
|
100
|
+
// Each bone's inverse is the identity: a part's vertices are already in its
|
|
101
|
+
// bone's space, so the bone's place in the world is the part's.
|
|
102
|
+
mesh.bind(
|
|
103
|
+
new THREE.Skeleton(bones, bones.map(() => new THREE.Matrix4())),
|
|
104
|
+
new THREE.Matrix4(),
|
|
105
|
+
)
|
|
106
|
+
root.add(mesh)
|
|
107
|
+
// Its bounds, from the pose it is built in, with room for a limb flung out
|
|
108
|
+
// or a lunge: what the camera judges whether to draw it by.
|
|
109
|
+
root.updateMatrixWorld(true)
|
|
110
|
+
mesh.computeBoundingSphere()
|
|
111
|
+
let bound = mesh.boundingSphere!
|
|
112
|
+
bound.radius = bound.radius * 1.4 + 0.3
|
|
113
|
+
return mesh
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** A box toppled forward, a quarter turn about x: what stood up runs toward
|
|
117
|
+
* +z, and what lay toward +z hangs below.
|
|
118
|
+
*
|
|
119
|
+
* ```ts
|
|
120
|
+
* import { assertEquals } from '@std/assert'
|
|
121
|
+
* assertEquals(topple([[0, 0, 0], [1, 2, 3], 7]), [[0, -3, 0], [1, 3, 2], 7])
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
export let topple = ([[x, y, z], [w, h, d], ...rest]: Box): Box => [
|
|
125
|
+
[x, -z - d, y],
|
|
126
|
+
[w, d, h],
|
|
127
|
+
...rest,
|
|
128
|
+
]
|
|
129
|
+
|
|
130
|
+
/** Boxes drawn for a part of one size, fit to a part `k` times it along
|
|
131
|
+
* each axis, about the part's pivot.
|
|
132
|
+
*
|
|
133
|
+
* ```ts
|
|
134
|
+
* import { assertEquals } from '@std/assert'
|
|
135
|
+
* assertEquals(fit([[[-0.5, 0, 0], [1, 1, 1], 7]], [2, 1, 0.5]), [
|
|
136
|
+
* [[-1, 0, 0], [2, 1, 0.5], 7],
|
|
137
|
+
* ])
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
export let fit = (boxes: Box[], k: Vec): Box[] =>
|
|
141
|
+
boxes.map(([min, size, ...rest]) => [
|
|
142
|
+
[min[0] * k[0], min[1] * k[1], min[2] * k[2]],
|
|
143
|
+
[size[0] * k[0], size[1] * k[1], size[2] * k[2]],
|
|
144
|
+
...rest,
|
|
145
|
+
])
|
|
146
|
+
|
|
147
|
+
/** A colour, darker (`k` under 1) or lighter. */
|
|
148
|
+
export let shade = (hex: number, k: number): number => {
|
|
149
|
+
let c = new THREE.Color(hex)
|
|
150
|
+
c.multiplyScalar(k)
|
|
151
|
+
return c.getHex()
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export let ease = (t: number): number =>
|
|
155
|
+
t < 0.5 ? 2 * t * t : 1 - (-2 * t + 2) ** 2 / 2
|
|
156
|
+
|
|
157
|
+
/** How far into a blow: rising to 1 at `peak` of the swing, and back.
|
|
158
|
+
*
|
|
159
|
+
* ```ts
|
|
160
|
+
* import { assertEquals } from '@std/assert'
|
|
161
|
+
* assertEquals([lunge(0), lunge(0.4), lunge(1)], [0, 1, 0])
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
export let lunge = (swing: number, peak = 0.4): number =>
|
|
165
|
+
swing < peak ? ease(swing / peak) : 1 - ease((swing - peak) / (1 - peak))
|
|
166
|
+
|
|
167
|
+
/** A leg's step in a walk, `s` and `c` the sine and cosine of how far
|
|
168
|
+
* through it, `amp` how far it swings: back and forth from the hip, and bent
|
|
169
|
+
* at the knee while the foot comes forward.
|
|
170
|
+
*
|
|
171
|
+
* ```ts
|
|
172
|
+
* import { assertEquals } from '@std/assert'
|
|
173
|
+
* import * as THREE from 'three'
|
|
174
|
+
* let leg: Limb = [new THREE.Bone(), new THREE.Bone()]
|
|
175
|
+
* stride(leg, 0, -1, 0.5) // passing under the hip, coming forward
|
|
176
|
+
* assertEquals([leg[0].rotation.x, leg[1].rotation.x], [0, 0.75])
|
|
177
|
+
* stride(leg, 0, 1, 0.5) // pushing back: straight
|
|
178
|
+
* assertEquals(leg[1].rotation.x, 0)
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
export let stride = (leg: Leg, s: number, c: number, amp: number): void => {
|
|
182
|
+
let [hip, knee] = Array.isArray(leg) ? leg : [leg]
|
|
183
|
+
hip.rotation.x = s * amp
|
|
184
|
+
if (knee) knee.rotation.x = Math.max(0, -c) * amp * 1.5
|
|
185
|
+
}
|