@plupin/character 0.0.0-stage → 0.2.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/dist/canvas.d.ts +79 -0
- package/dist/canvas.js +315 -0
- package/dist/chunk-aa5nstet.js +39 -0
- package/dist/chunk-ae7gp5cc.js +101 -0
- package/dist/chunk-k5gwaq6t.js +315 -0
- package/dist/chunk-wpbgtn0y.js +179 -0
- package/dist/clip-bake.d.ts +48 -0
- package/dist/clip-bake.js +132 -0
- package/dist/emote.d.ts +23 -0
- package/dist/faces.d.ts +10 -0
- package/dist/faces.js +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +102 -0
- package/dist/listing.d.ts +195 -0
- package/dist/listing.js +753 -0
- package/dist/named-faces.d.ts +22 -0
- package/dist/plup2d.d.ts +29 -0
- package/dist/plup3d.d.ts +73 -0
- package/dist/png.d.ts +61 -0
- package/dist/pose.d.ts +43 -0
- package/dist/pose.js +18 -0
- package/dist/rig.d.ts +29 -0
- package/dist/skin-native.d.ts +252 -0
- package/dist/skin.d.ts +35 -0
- package/dist/three.d.ts +22 -0
- package/dist/three.js +581 -0
- package/package.json +58 -4
- package/README.md +0 -3
package/dist/three.js
ADDED
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
import {
|
|
2
|
+
emoteOf
|
|
3
|
+
} from "./chunk-ae7gp5cc.js";
|
|
4
|
+
import {
|
|
5
|
+
boneSkin,
|
|
6
|
+
clampBodyScale,
|
|
7
|
+
nativePartOf,
|
|
8
|
+
scaleBone
|
|
9
|
+
} from "./chunk-k5gwaq6t.js";
|
|
10
|
+
import {
|
|
11
|
+
FACE_STAMP_PALETTE,
|
|
12
|
+
NAMED_FACE_STAMPS
|
|
13
|
+
} from "./chunk-aa5nstet.js";
|
|
14
|
+
import {
|
|
15
|
+
RIGS,
|
|
16
|
+
clipLift,
|
|
17
|
+
mergePoses,
|
|
18
|
+
resolvePose,
|
|
19
|
+
sampleClip
|
|
20
|
+
} from "./chunk-wpbgtn0y.js";
|
|
21
|
+
|
|
22
|
+
// src/three.ts
|
|
23
|
+
import {
|
|
24
|
+
BoxGeometry,
|
|
25
|
+
CanvasTexture as CanvasTexture2,
|
|
26
|
+
Group as Group2,
|
|
27
|
+
Mesh as Mesh2,
|
|
28
|
+
MeshStandardMaterial,
|
|
29
|
+
NearestFilter as NearestFilter2,
|
|
30
|
+
SRGBColorSpace as SRGBColorSpace2
|
|
31
|
+
} from "three";
|
|
32
|
+
|
|
33
|
+
// src/plup3d.ts
|
|
34
|
+
import {
|
|
35
|
+
BackSide,
|
|
36
|
+
CanvasTexture,
|
|
37
|
+
CapsuleGeometry,
|
|
38
|
+
Color,
|
|
39
|
+
Group,
|
|
40
|
+
Mesh,
|
|
41
|
+
MeshBasicMaterial,
|
|
42
|
+
NearestFilter,
|
|
43
|
+
RepeatWrapping,
|
|
44
|
+
ShaderMaterial,
|
|
45
|
+
SphereGeometry,
|
|
46
|
+
SRGBColorSpace
|
|
47
|
+
} from "three";
|
|
48
|
+
var SKIN_PALETTE = {
|
|
49
|
+
B: "#2f6fed",
|
|
50
|
+
L: "#6a96ff",
|
|
51
|
+
D: "#1f4fc0",
|
|
52
|
+
l: "#a9c4ff",
|
|
53
|
+
k: "#161923",
|
|
54
|
+
w: "#ffffff",
|
|
55
|
+
a: "#8b93a8",
|
|
56
|
+
r: "#e2574c",
|
|
57
|
+
o: "#e6a23c",
|
|
58
|
+
y: "#f2d24b",
|
|
59
|
+
g: "#3fae74",
|
|
60
|
+
c: "#4cc9e2",
|
|
61
|
+
v: "#8a5ad8",
|
|
62
|
+
p: "#f08aa8",
|
|
63
|
+
n: "#8a5a3c",
|
|
64
|
+
s: "#f0b184"
|
|
65
|
+
};
|
|
66
|
+
var BODY_SHAPES = {
|
|
67
|
+
plup: { x: 1, y: 1 },
|
|
68
|
+
tall: { x: 0.88, y: 1.25 },
|
|
69
|
+
wide: { x: 1.2, y: 0.85 },
|
|
70
|
+
mini: { x: 0.82, y: 0.82 },
|
|
71
|
+
bean: { x: 0.72, y: 1.35 }
|
|
72
|
+
};
|
|
73
|
+
var EYES = {
|
|
74
|
+
dot: {
|
|
75
|
+
open: [".ee..ee.", ".ee..ee."],
|
|
76
|
+
blink: ["........", ".ee..ee."],
|
|
77
|
+
happy: ["........", ".ee..ee."],
|
|
78
|
+
wide: [".ee..ee.", ".ee..ee."],
|
|
79
|
+
sad: ["........", ".ee..ee."]
|
|
80
|
+
},
|
|
81
|
+
sparkle: {
|
|
82
|
+
open: [".we..we.", ".ee..ee."],
|
|
83
|
+
blink: ["........", ".ee..ee."],
|
|
84
|
+
happy: ["........", ".ee..ee."],
|
|
85
|
+
wide: [".ee..ee.", ".ee..ee."],
|
|
86
|
+
sad: ["........", ".ee..ee."]
|
|
87
|
+
},
|
|
88
|
+
sleepy: {
|
|
89
|
+
open: ["........", ".ee..ee."],
|
|
90
|
+
blink: ["........", ".ee..ee."],
|
|
91
|
+
happy: ["........", ".ee..ee."],
|
|
92
|
+
wide: [".ee..ee.", ".ee..ee."],
|
|
93
|
+
sad: ["........", ".ee..ee."]
|
|
94
|
+
},
|
|
95
|
+
derp: {
|
|
96
|
+
open: [".ee.....", ".ee..e.."],
|
|
97
|
+
blink: ["........", ".ee..ee."],
|
|
98
|
+
happy: ["........", ".ee..ee."],
|
|
99
|
+
wide: [".ee..ee.", ".ee..ee."],
|
|
100
|
+
sad: ["........", ".ee..ee."]
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
var MOUTHS = {
|
|
104
|
+
idle: ["...mm...", "........"],
|
|
105
|
+
open: ["...mm...", "...tt..."],
|
|
106
|
+
o: ["...mm...", "...mm..."],
|
|
107
|
+
frown: ["...mm...", "..m..m.."]
|
|
108
|
+
};
|
|
109
|
+
var PARTS = {
|
|
110
|
+
idle: { eyes: "open", mouth: "idle" },
|
|
111
|
+
blink: { eyes: "blink", mouth: "idle" },
|
|
112
|
+
happy: { eyes: "happy", mouth: "open" },
|
|
113
|
+
surprised: { eyes: "wide", mouth: "o" },
|
|
114
|
+
sad: { eyes: "sad", mouth: "frown" },
|
|
115
|
+
wee: { eyes: "happy", mouth: "o" }
|
|
116
|
+
};
|
|
117
|
+
function stampFor(face, type) {
|
|
118
|
+
if (Object.hasOwn(NAMED_FACE_STAMPS, face))
|
|
119
|
+
return NAMED_FACE_STAMPS[face];
|
|
120
|
+
const parts = PARTS[face] ?? PARTS.idle;
|
|
121
|
+
const [eyeTop, eyeBottom] = (EYES[type] ?? EYES.dot)[parts.eyes];
|
|
122
|
+
const [mouthTop, mouthBottom] = MOUTHS[parts.mouth];
|
|
123
|
+
const blush = type === "derp" ? "........" : "p......p";
|
|
124
|
+
const merged = Array.from(mouthTop, (pixel, i) => pixel === "." ? blush[i] ?? "." : pixel).join("");
|
|
125
|
+
return [eyeTop, eyeBottom, merged, mouthBottom];
|
|
126
|
+
}
|
|
127
|
+
var BODY_WIDTH = 48;
|
|
128
|
+
var BODY_HEIGHT = 24;
|
|
129
|
+
var FACE_COLUMN = 20;
|
|
130
|
+
var FACE_ROW = 9;
|
|
131
|
+
var RADIUS_X = 0.54;
|
|
132
|
+
var RADIUS_Y = 0.64;
|
|
133
|
+
var RADIUS_Z = 0.54;
|
|
134
|
+
var TOON_VERTEX = `
|
|
135
|
+
varying vec3 vNormal;
|
|
136
|
+
varying vec2 vUv;
|
|
137
|
+
uniform vec2 uvOffset;
|
|
138
|
+
void main() {
|
|
139
|
+
vNormal = normalize(normalMatrix * (length(normal) > 0.001 ? normal : vec3(0.0, 0.0, 1.0)));
|
|
140
|
+
vUv = uv + uvOffset;
|
|
141
|
+
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
|
142
|
+
}`;
|
|
143
|
+
var TOON_FRAGMENT = `
|
|
144
|
+
varying vec3 vNormal;
|
|
145
|
+
varying vec2 vUv;
|
|
146
|
+
uniform sampler2D map;
|
|
147
|
+
uniform float useMap;
|
|
148
|
+
uniform vec3 color;
|
|
149
|
+
void main() {
|
|
150
|
+
vec3 n = normalize(vNormal);
|
|
151
|
+
float light = dot(n, normalize(vec3(-0.35, 0.75, 0.55)));
|
|
152
|
+
float band = light > 0.2 ? 1.0 : light > -0.3 ? 0.8 : 0.6;
|
|
153
|
+
vec3 base = useMap > 0.5 ? texture2D(map, vUv).rgb : color;
|
|
154
|
+
vec3 lit = base * band;
|
|
155
|
+
float rim = 1.0 - max(n.z, 0.0);
|
|
156
|
+
if (rim > 0.82 && n.y > -0.2) lit = mix(lit, vec3(1.0), 0.35);
|
|
157
|
+
gl_FragColor = vec4(lit, 1.0);
|
|
158
|
+
#include <colorspace_fragment>
|
|
159
|
+
}`;
|
|
160
|
+
function toon(options) {
|
|
161
|
+
return new ShaderMaterial({
|
|
162
|
+
vertexShader: TOON_VERTEX,
|
|
163
|
+
fragmentShader: TOON_FRAGMENT,
|
|
164
|
+
toneMapped: false,
|
|
165
|
+
uniforms: {
|
|
166
|
+
map: { value: options.map ?? null },
|
|
167
|
+
useMap: { value: options.map ? 1 : 0 },
|
|
168
|
+
color: { value: new Color(options.color ?? "#ffffff") },
|
|
169
|
+
uvOffset: { value: [options.map?.offset.x ?? 0, 0] }
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
function hull(color, width) {
|
|
174
|
+
return new ShaderMaterial({
|
|
175
|
+
side: BackSide,
|
|
176
|
+
toneMapped: false,
|
|
177
|
+
uniforms: { color: { value: new Color(color) }, width: { value: width } },
|
|
178
|
+
vertexShader: `
|
|
179
|
+
uniform float width;
|
|
180
|
+
void main() {
|
|
181
|
+
gl_Position = projectionMatrix * modelViewMatrix * vec4(position + normal * width, 1.0);
|
|
182
|
+
}`,
|
|
183
|
+
fragmentShader: `
|
|
184
|
+
uniform vec3 color;
|
|
185
|
+
void main() {
|
|
186
|
+
gl_FragColor = vec4(color, 1.0);
|
|
187
|
+
#include <colorspace_fragment>
|
|
188
|
+
}`
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
function blobGeometry() {
|
|
192
|
+
const geometry = new SphereGeometry(1, 40, 28);
|
|
193
|
+
const position = geometry.attributes.position;
|
|
194
|
+
let minY = Number.POSITIVE_INFINITY;
|
|
195
|
+
for (let i = 0;i < position.count; i += 1) {
|
|
196
|
+
let y = position.getY(i);
|
|
197
|
+
const spread = 1 + 0.16 * -y;
|
|
198
|
+
if (y < -0.62)
|
|
199
|
+
y = -0.62 + (y + 0.62) * 0.3;
|
|
200
|
+
position.setXYZ(i, position.getX(i) * spread * RADIUS_X, y * RADIUS_Y, position.getZ(i) * spread * RADIUS_Z);
|
|
201
|
+
minY = Math.min(minY, y * RADIUS_Y);
|
|
202
|
+
}
|
|
203
|
+
geometry.translate(0, -minY, 0);
|
|
204
|
+
geometry.computeVertexNormals();
|
|
205
|
+
return geometry;
|
|
206
|
+
}
|
|
207
|
+
var channel = (hex, at) => Number.parseInt(hex.slice(at, at + 2), 16);
|
|
208
|
+
var shade = (hex, toward, amount) => `#${[1, 3, 5].map((at) => Math.round(channel(hex, at) + (toward - channel(hex, at)) * amount).toString(16).padStart(2, "0")).join("")}`;
|
|
209
|
+
function paintBody(canvas, color, face, skin) {
|
|
210
|
+
const ctx = canvas.getContext("2d");
|
|
211
|
+
if (!ctx)
|
|
212
|
+
return;
|
|
213
|
+
const px = canvas.width / BODY_WIDTH;
|
|
214
|
+
const base = color;
|
|
215
|
+
const light = shade(color, 255, 0.32);
|
|
216
|
+
const belly = shade(color, 255, 0.62);
|
|
217
|
+
const dark = shade(color, 0, 0.3);
|
|
218
|
+
const palette = {
|
|
219
|
+
...SKIN_PALETTE,
|
|
220
|
+
B: color,
|
|
221
|
+
L: shade(color, 255, 0.32),
|
|
222
|
+
D: shade(color, 0, 0.3)
|
|
223
|
+
};
|
|
224
|
+
for (let y = 0;y < BODY_HEIGHT; y += 1) {
|
|
225
|
+
for (let x = 0;x < BODY_WIDTH; x += 1) {
|
|
226
|
+
const drawn = skin?.body[y]?.[x];
|
|
227
|
+
if (drawn) {
|
|
228
|
+
ctx.fillStyle = palette[drawn] ?? color;
|
|
229
|
+
ctx.fillRect(x * px, y * px, px, px);
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
let fill = base;
|
|
233
|
+
if (y >= 16)
|
|
234
|
+
fill = belly;
|
|
235
|
+
else if (y === 15)
|
|
236
|
+
fill = light;
|
|
237
|
+
else if (x >= 29 && x <= 35)
|
|
238
|
+
fill = dark;
|
|
239
|
+
ctx.fillStyle = fill;
|
|
240
|
+
ctx.fillRect(x * px, y * px, px, px);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
if (!skin) {
|
|
244
|
+
ctx.fillStyle = shade(color, 255, 0.8);
|
|
245
|
+
for (const [x, y] of [
|
|
246
|
+
[19, 4],
|
|
247
|
+
[20, 4],
|
|
248
|
+
[21, 4],
|
|
249
|
+
[19, 5],
|
|
250
|
+
[20, 5]
|
|
251
|
+
])
|
|
252
|
+
ctx.fillRect(x * px, y * px, px, px);
|
|
253
|
+
}
|
|
254
|
+
const stamp = stampFor(face, skin?.faceType ?? "dot");
|
|
255
|
+
stamp.forEach((row, dy) => {
|
|
256
|
+
for (let dx = 0;dx < row.length; dx += 1) {
|
|
257
|
+
const key = row[dx] ?? ".";
|
|
258
|
+
const paint = FACE_STAMP_PALETTE[key];
|
|
259
|
+
if (!paint)
|
|
260
|
+
continue;
|
|
261
|
+
ctx.fillStyle = paint;
|
|
262
|
+
ctx.fillRect((FACE_COLUMN + dx) * px, (FACE_ROW + dy) * px, px, px);
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
var inkHull = null;
|
|
267
|
+
var neonHull = null;
|
|
268
|
+
function createPlup(options = {}) {
|
|
269
|
+
inkHull ??= hull("#14102e", 0.022);
|
|
270
|
+
neonHull ??= hull("#b9a6ff", 0.04);
|
|
271
|
+
const ink = inkHull;
|
|
272
|
+
const neon = neonHull;
|
|
273
|
+
let color = options.color ?? "#3d7bff";
|
|
274
|
+
let face = options.face ?? "smug";
|
|
275
|
+
const skin = options.skin ?? null;
|
|
276
|
+
const shape = BODY_SHAPES[options.shape ?? "plup"] ?? BODY_SHAPES.plup;
|
|
277
|
+
const canvas = document.createElement("canvas");
|
|
278
|
+
canvas.width = BODY_WIDTH * 4;
|
|
279
|
+
canvas.height = BODY_HEIGHT * 4;
|
|
280
|
+
paintBody(canvas, color, face, skin);
|
|
281
|
+
const texture = new CanvasTexture(canvas);
|
|
282
|
+
texture.magFilter = NearestFilter;
|
|
283
|
+
texture.minFilter = NearestFilter;
|
|
284
|
+
texture.generateMipmaps = false;
|
|
285
|
+
texture.colorSpace = SRGBColorSpace;
|
|
286
|
+
texture.wrapS = RepeatWrapping;
|
|
287
|
+
texture.offset.x = (FACE_COLUMN + 4) / BODY_WIDTH - 0.25;
|
|
288
|
+
const bodyMaterial = toon({ map: texture });
|
|
289
|
+
const limbMaterial = toon({ color: shade(color, 0, 0.12) });
|
|
290
|
+
const tipMaterial = toon({ color: shade(color, 0, 0.3) });
|
|
291
|
+
const root = new Group;
|
|
292
|
+
const lifted = new Group;
|
|
293
|
+
root.add(lifted);
|
|
294
|
+
const geometry = blobGeometry();
|
|
295
|
+
if (!options.model) {
|
|
296
|
+
const bodyMesh = new Mesh(geometry, bodyMaterial);
|
|
297
|
+
lifted.add(bodyMesh, new Mesh(geometry, ink), new Mesh(geometry, neon));
|
|
298
|
+
for (const mesh of lifted.children)
|
|
299
|
+
mesh.raycast = () => {};
|
|
300
|
+
}
|
|
301
|
+
const limbGeometry = new CapsuleGeometry(0.075, 0.2, 4, 10);
|
|
302
|
+
const handGeometry = new SphereGeometry(0.11, 12, 10);
|
|
303
|
+
const footGeometry = new SphereGeometry(0.13, 12, 10);
|
|
304
|
+
const pivots = {
|
|
305
|
+
armL: new Group,
|
|
306
|
+
armR: new Group,
|
|
307
|
+
legL: new Group,
|
|
308
|
+
legR: new Group
|
|
309
|
+
};
|
|
310
|
+
const limb = (name, x, y, length, tip, flat) => {
|
|
311
|
+
const pivot = pivots[name];
|
|
312
|
+
pivot.position.set(x, y, 0);
|
|
313
|
+
const tube = new Mesh(limbGeometry, limbMaterial);
|
|
314
|
+
tube.position.y = -length / 2;
|
|
315
|
+
tube.scale.y = length / 0.35;
|
|
316
|
+
const end = new Mesh(tip, tipMaterial);
|
|
317
|
+
end.position.y = -length;
|
|
318
|
+
if (flat)
|
|
319
|
+
end.scale.set(1.15, 0.5, 1.7);
|
|
320
|
+
const inkTube = new Mesh(limbGeometry, ink);
|
|
321
|
+
inkTube.position.copy(tube.position);
|
|
322
|
+
inkTube.scale.copy(tube.scale);
|
|
323
|
+
const inkEnd = new Mesh(tip, ink);
|
|
324
|
+
inkEnd.position.copy(end.position);
|
|
325
|
+
inkEnd.scale.copy(end.scale);
|
|
326
|
+
pivot.add(tube, end, inkTube, inkEnd);
|
|
327
|
+
for (const mesh of pivot.children)
|
|
328
|
+
mesh.raycast = () => {};
|
|
329
|
+
lifted.add(pivot);
|
|
330
|
+
};
|
|
331
|
+
if (options.model) {
|
|
332
|
+
lifted.add(options.model);
|
|
333
|
+
for (const name of Object.keys(pivots)) {
|
|
334
|
+
const found = options.model.getObjectByName(name);
|
|
335
|
+
if (found)
|
|
336
|
+
pivots[name] = found;
|
|
337
|
+
}
|
|
338
|
+
} else {
|
|
339
|
+
limb("armL", -0.6, 0.62, 0.3, handGeometry, false);
|
|
340
|
+
limb("armR", 0.6, 0.62, 0.3, handGeometry, false);
|
|
341
|
+
limb("legL", -0.22, 0.2, 0.16, footGeometry, true);
|
|
342
|
+
limb("legR", 0.22, 0.2, 0.16, footGeometry, true);
|
|
343
|
+
}
|
|
344
|
+
let shadow = null;
|
|
345
|
+
if (options.shadow !== false) {
|
|
346
|
+
shadow = new Mesh(new SphereGeometry(0.6, 20, 6), new MeshBasicMaterial({
|
|
347
|
+
color: "#0b0f18",
|
|
348
|
+
transparent: true,
|
|
349
|
+
opacity: 0.4,
|
|
350
|
+
depthWrite: false
|
|
351
|
+
}));
|
|
352
|
+
shadow.scale.set(1, 0.02, 0.9);
|
|
353
|
+
shadow.position.y = 0.005;
|
|
354
|
+
shadow.raycast = () => {};
|
|
355
|
+
root.add(shadow);
|
|
356
|
+
}
|
|
357
|
+
let clip = "idle";
|
|
358
|
+
let held = {};
|
|
359
|
+
let seconds = 0;
|
|
360
|
+
let clock = 0;
|
|
361
|
+
const apply = () => {
|
|
362
|
+
const pose = resolvePose("plup", mergePoses(sampleClip(clip, seconds), held));
|
|
363
|
+
const byAlias = {
|
|
364
|
+
armL: "LeftArm",
|
|
365
|
+
armR: "RightArm",
|
|
366
|
+
legL: "LeftLeg",
|
|
367
|
+
legR: "RightLeg"
|
|
368
|
+
};
|
|
369
|
+
for (const name of Object.keys(pivots)) {
|
|
370
|
+
const rotation = pose.get(byAlias[name]);
|
|
371
|
+
pivots[name].rotation.set(rotation?.x ?? 0, rotation?.y ?? 0, rotation?.z ?? 0);
|
|
372
|
+
}
|
|
373
|
+
const body = pose.get("Body");
|
|
374
|
+
lifted.rotation.set(body?.x ?? 0, body?.y ?? 0, body?.z ?? 0);
|
|
375
|
+
const breath = Math.sin(clock * 3.2) * 0.018;
|
|
376
|
+
lifted.scale.set(shape.x * (1 - breath), shape.y * (1 + breath), shape.x * (1 - breath));
|
|
377
|
+
lifted.position.y = clipLift(clip, seconds) * 0.32;
|
|
378
|
+
if (shadow) {
|
|
379
|
+
const air = Math.min(1, lifted.position.y / 0.5);
|
|
380
|
+
shadow.scale.set(1 - air * 0.3, 0.02, 0.9 - air * 0.3);
|
|
381
|
+
shadow.material.opacity = 0.4 - air * 0.18;
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
apply();
|
|
385
|
+
const redraw = () => {
|
|
386
|
+
paintBody(canvas, color, face, skin);
|
|
387
|
+
texture.needsUpdate = true;
|
|
388
|
+
};
|
|
389
|
+
return {
|
|
390
|
+
object: root,
|
|
391
|
+
play(next) {
|
|
392
|
+
if (next !== clip)
|
|
393
|
+
seconds = 0;
|
|
394
|
+
clip = next;
|
|
395
|
+
},
|
|
396
|
+
emote(name, custom) {
|
|
397
|
+
const found = custom ?? emoteOf(name);
|
|
398
|
+
if (!found)
|
|
399
|
+
return;
|
|
400
|
+
const next = found.anim ?? found.clip;
|
|
401
|
+
if (next !== clip)
|
|
402
|
+
seconds = 0;
|
|
403
|
+
clip = next;
|
|
404
|
+
if (found.face !== face) {
|
|
405
|
+
face = found.face;
|
|
406
|
+
redraw();
|
|
407
|
+
}
|
|
408
|
+
return found;
|
|
409
|
+
},
|
|
410
|
+
pose(next) {
|
|
411
|
+
held = next;
|
|
412
|
+
},
|
|
413
|
+
bone(name) {
|
|
414
|
+
return name === "body" ? lifted : pivots[name];
|
|
415
|
+
},
|
|
416
|
+
setFace(next) {
|
|
417
|
+
if (next === face)
|
|
418
|
+
return;
|
|
419
|
+
face = next;
|
|
420
|
+
redraw();
|
|
421
|
+
},
|
|
422
|
+
setColor(next) {
|
|
423
|
+
if (next === color)
|
|
424
|
+
return;
|
|
425
|
+
color = next;
|
|
426
|
+
redraw();
|
|
427
|
+
limbMaterial.uniforms.color = { value: new Color(shade(color, 0, 0.12)) };
|
|
428
|
+
tipMaterial.uniforms.color = { value: new Color(shade(color, 0, 0.3)) };
|
|
429
|
+
},
|
|
430
|
+
update(delta) {
|
|
431
|
+
seconds += delta;
|
|
432
|
+
clock += delta;
|
|
433
|
+
apply();
|
|
434
|
+
},
|
|
435
|
+
dispose() {
|
|
436
|
+
geometry.dispose();
|
|
437
|
+
limbGeometry.dispose();
|
|
438
|
+
handGeometry.dispose();
|
|
439
|
+
footGeometry.dispose();
|
|
440
|
+
texture.dispose();
|
|
441
|
+
bodyMaterial.dispose();
|
|
442
|
+
limbMaterial.dispose();
|
|
443
|
+
tipMaterial.dispose();
|
|
444
|
+
if (shadow) {
|
|
445
|
+
shadow.geometry.dispose();
|
|
446
|
+
shadow.material.dispose();
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
// src/three.ts
|
|
452
|
+
var BOX_FACES = ["left", "right", "up", "down", "front", "back"];
|
|
453
|
+
function faceTexture(sheet, source, fill) {
|
|
454
|
+
const canvas = document.createElement("canvas");
|
|
455
|
+
canvas.width = Math.max(1, Math.round(source.w));
|
|
456
|
+
canvas.height = Math.max(1, Math.round(source.h));
|
|
457
|
+
const context = canvas.getContext("2d");
|
|
458
|
+
if (context) {
|
|
459
|
+
context.imageSmoothingEnabled = false;
|
|
460
|
+
context.beginPath();
|
|
461
|
+
context.rect(0, 0, canvas.width, canvas.height);
|
|
462
|
+
context.clip();
|
|
463
|
+
if (fill) {
|
|
464
|
+
context.fillStyle = fill;
|
|
465
|
+
context.fillRect(0, 0, canvas.width, canvas.height);
|
|
466
|
+
}
|
|
467
|
+
context.drawImage(sheet, source.x, source.y, source.w, source.h, 0, 0, canvas.width, canvas.height);
|
|
468
|
+
}
|
|
469
|
+
const texture = new CanvasTexture2(canvas);
|
|
470
|
+
texture.magFilter = NearestFilter2;
|
|
471
|
+
texture.minFilter = NearestFilter2;
|
|
472
|
+
texture.colorSpace = SRGBColorSpace2;
|
|
473
|
+
return texture;
|
|
474
|
+
}
|
|
475
|
+
function plupHandle(look, stature) {
|
|
476
|
+
const plup = createPlup({ color: look.bodyColor });
|
|
477
|
+
plup.object.scale.setScalar(3.4 / 1.1 * stature);
|
|
478
|
+
return {
|
|
479
|
+
object: plup.object,
|
|
480
|
+
play: plup.play,
|
|
481
|
+
emote: (name) => plup.emote(name),
|
|
482
|
+
pose: plup.pose,
|
|
483
|
+
update: plup.update,
|
|
484
|
+
bone: (name) => plup.bone(name),
|
|
485
|
+
dispose: plup.dispose
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
function createCharacter(rigId = "plup", look = { bodyColor: "#3d7bff" }, bodyScale) {
|
|
489
|
+
if (rigId === "plup")
|
|
490
|
+
return plupHandle(look, clampBodyScale(bodyScale).stature);
|
|
491
|
+
const rig = RIGS[rigId];
|
|
492
|
+
const body = clampBodyScale(bodyScale);
|
|
493
|
+
const root = new Group2;
|
|
494
|
+
const joints = new Map;
|
|
495
|
+
const materials = [];
|
|
496
|
+
const geometries = [];
|
|
497
|
+
for (const bone of rig.bones) {
|
|
498
|
+
const box = scaleBone(bone, nativePartOf(bone.name), bone.parent ? nativePartOf(bone.parent) : null, body);
|
|
499
|
+
const joint = new Group2;
|
|
500
|
+
joint.position.set(box.joint[0], box.joint[1], box.joint[2]);
|
|
501
|
+
joints.set(bone.name, joint);
|
|
502
|
+
(bone.parent ? joints.get(bone.parent) : root)?.add(joint);
|
|
503
|
+
const geometry = new BoxGeometry(box.size[0], box.size[1], box.size[2]);
|
|
504
|
+
geometries.push(geometry);
|
|
505
|
+
const color = bone.name === "Head" ? look.headColor ?? look.bodyColor : look.bodyColor;
|
|
506
|
+
const faces = BOX_FACES.map((face) => {
|
|
507
|
+
const skin = boneSkin(bone.name, face, look);
|
|
508
|
+
if (skin)
|
|
509
|
+
return new MeshStandardMaterial({
|
|
510
|
+
map: faceTexture(skin.image, skin.source, skin.redraw ? null : color),
|
|
511
|
+
roughness: 0.8,
|
|
512
|
+
...skin.redraw ? { transparent: true, alphaTest: 0.5 } : {}
|
|
513
|
+
});
|
|
514
|
+
if (look.face && (bone.name === "Head" || bone.name === "Body") && face === "front") {
|
|
515
|
+
return new MeshStandardMaterial({
|
|
516
|
+
map: faceTexture(look.face, { x: 0, y: 0, w: 64, h: 64 }, color),
|
|
517
|
+
roughness: 0.8
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
return new MeshStandardMaterial({ color, roughness: 0.8 });
|
|
521
|
+
});
|
|
522
|
+
materials.push(...faces);
|
|
523
|
+
const mesh = new Mesh2(geometry, faces);
|
|
524
|
+
mesh.position.set(box.center[0], box.center[1], box.center[2]);
|
|
525
|
+
joint.add(mesh);
|
|
526
|
+
}
|
|
527
|
+
let clip = "idle";
|
|
528
|
+
let held = {};
|
|
529
|
+
let seconds = 0;
|
|
530
|
+
const apply = () => {
|
|
531
|
+
const pose = resolvePose(rigId, mergePoses(sampleClip(clip, seconds), held));
|
|
532
|
+
for (const [name, joint] of joints) {
|
|
533
|
+
const rotation = pose.get(name);
|
|
534
|
+
joint.rotation.set(rotation?.x ?? 0, rotation?.y ?? 0, rotation?.z ?? 0);
|
|
535
|
+
}
|
|
536
|
+
root.position.y = clipLift(clip, seconds) * body.stature;
|
|
537
|
+
};
|
|
538
|
+
apply();
|
|
539
|
+
return {
|
|
540
|
+
object: root,
|
|
541
|
+
play(next) {
|
|
542
|
+
if (next !== clip)
|
|
543
|
+
seconds = 0;
|
|
544
|
+
clip = next;
|
|
545
|
+
},
|
|
546
|
+
emote(name) {
|
|
547
|
+
const found = emoteOf(name);
|
|
548
|
+
if (found) {
|
|
549
|
+
const next = found.anim ?? found.clip;
|
|
550
|
+
if (next !== clip)
|
|
551
|
+
seconds = 0;
|
|
552
|
+
clip = next;
|
|
553
|
+
}
|
|
554
|
+
return found;
|
|
555
|
+
},
|
|
556
|
+
pose(next) {
|
|
557
|
+
held = next;
|
|
558
|
+
},
|
|
559
|
+
update(delta) {
|
|
560
|
+
seconds += delta;
|
|
561
|
+
apply();
|
|
562
|
+
},
|
|
563
|
+
bone(name) {
|
|
564
|
+
const resolved = rig.aliases[name] ?? name;
|
|
565
|
+
return joints.get(resolved);
|
|
566
|
+
},
|
|
567
|
+
dispose() {
|
|
568
|
+
for (const material of materials) {
|
|
569
|
+
const map = material.map;
|
|
570
|
+
map?.dispose();
|
|
571
|
+
material.dispose();
|
|
572
|
+
}
|
|
573
|
+
for (const geometry of geometries)
|
|
574
|
+
geometry.dispose();
|
|
575
|
+
}
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
export {
|
|
579
|
+
createPlup,
|
|
580
|
+
createCharacter
|
|
581
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plupin/character",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Plup, the Plupin mascot, in three.js and canvas: createPlup, drawPlup2D, clips, emotes and the listing validator.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./three": {
|
|
16
|
+
"types": "./dist/three.d.ts",
|
|
17
|
+
"import": "./dist/three.js"
|
|
18
|
+
},
|
|
19
|
+
"./canvas": {
|
|
20
|
+
"types": "./dist/canvas.d.ts",
|
|
21
|
+
"import": "./dist/canvas.js"
|
|
22
|
+
},
|
|
23
|
+
"./faces": {
|
|
24
|
+
"types": "./dist/faces.d.ts",
|
|
25
|
+
"import": "./dist/faces.js"
|
|
26
|
+
},
|
|
27
|
+
"./listing": {
|
|
28
|
+
"types": "./dist/listing.d.ts",
|
|
29
|
+
"import": "./dist/listing.js"
|
|
30
|
+
},
|
|
31
|
+
"./pose": {
|
|
32
|
+
"types": "./dist/pose.d.ts",
|
|
33
|
+
"import": "./dist/pose.js"
|
|
34
|
+
},
|
|
35
|
+
"./clip-bake": {
|
|
36
|
+
"types": "./dist/clip-bake.d.ts",
|
|
37
|
+
"import": "./dist/clip-bake.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"fflate": "^0.8.3"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"three": ">=0.160.0"
|
|
45
|
+
},
|
|
46
|
+
"peerDependenciesMeta": {
|
|
47
|
+
"three": {
|
|
48
|
+
"optional": true
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"repository": {
|
|
52
|
+
"type": "git",
|
|
53
|
+
"url": "git+https://github.com/himanusia/plupin.git",
|
|
54
|
+
"directory": "packages/character"
|
|
55
|
+
},
|
|
56
|
+
"homepage": "https://plupin.com/docs",
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public"
|
|
59
|
+
}
|
|
60
|
+
}
|
package/README.md
DELETED