@eskais/character 0.0.0-stage → 0.1.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/CHANGELOG.md +29 -0
- package/LICENSE +21 -0
- package/LICENSE-APACHE-2.0.txt +202 -0
- package/LICENSES.md +17 -0
- package/README.md +136 -2
- package/assets/anny/anny.bin +0 -0
- package/assets/hair/hair.bin +0 -0
- package/dist/engine.d.ts +302 -0
- package/dist/errors.js +49 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +525 -0
- package/dist/index.js.map +1 -0
- package/dist/motion.d.ts +1 -0
- package/dist/motion.js +5 -0
- package/dist/motion.js.map +1 -0
- package/dist/react.d.ts +62 -0
- package/dist/react.js +168 -0
- package/dist/react.js.map +1 -0
- package/dist/style.d.ts +32 -0
- package/dist/style.js +152 -0
- package/dist/style.js.map +1 -0
- package/dist/three.d.ts +14 -0
- package/dist/three.js +100 -0
- package/dist/three.js.map +1 -0
- package/dist/worker.d.ts +3 -0
- package/dist/worker.js +42 -0
- package/dist/worker.js.map +1 -0
- package/package.json +147 -4
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { _ as ScanInput, h as ScanEvent, m as RestyleOptions, o as Character, r as ScanEngine, v as ScanOptions, y as ScanStage } from "./engine.js";
|
|
2
|
+
/** A person mask: confidence 0–255, row-major. */
|
|
3
|
+
interface ScanMask {
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
data: Uint8Array;
|
|
7
|
+
}
|
|
8
|
+
type ScanStatus = "idle" | "scanning" | "done" | "error" | "cancelled";
|
|
9
|
+
/** Props-shaped scan state for ModelScan / ModelViewer. */
|
|
10
|
+
interface CharacterScanState {
|
|
11
|
+
status: ScanStatus;
|
|
12
|
+
stage: ScanStage | null;
|
|
13
|
+
/** Models downloading before the first scan; null before and after. Bytes after decompression. */
|
|
14
|
+
download: {
|
|
15
|
+
loaded: number;
|
|
16
|
+
total: number;
|
|
17
|
+
} | null;
|
|
18
|
+
/** 0..1. */
|
|
19
|
+
progress: number;
|
|
20
|
+
landmarks: {
|
|
21
|
+
face: Float32Array | null;
|
|
22
|
+
body: Float32Array | null;
|
|
23
|
+
};
|
|
24
|
+
/** Person masks for the photo-to-character transition (see the `mask` event). */
|
|
25
|
+
masks: {
|
|
26
|
+
face: ScanMask | null;
|
|
27
|
+
body: ScanMask | null;
|
|
28
|
+
};
|
|
29
|
+
/** The base GLB to load once; morph it by setting `weights`. */
|
|
30
|
+
model: {
|
|
31
|
+
glb: Uint8Array;
|
|
32
|
+
targetNames: string[];
|
|
33
|
+
} | null;
|
|
34
|
+
weights: Float32Array | null;
|
|
35
|
+
character: Character | null;
|
|
36
|
+
error: Error | null;
|
|
37
|
+
}
|
|
38
|
+
export declare const initialScanState: CharacterScanState;
|
|
39
|
+
type Action = {
|
|
40
|
+
type: "start";
|
|
41
|
+
} | {
|
|
42
|
+
type: "restyled";
|
|
43
|
+
character: Character;
|
|
44
|
+
} | {
|
|
45
|
+
type: "event";
|
|
46
|
+
event: ScanEvent;
|
|
47
|
+
} | {
|
|
48
|
+
type: "error";
|
|
49
|
+
error: Error;
|
|
50
|
+
} | {
|
|
51
|
+
type: "cancelled";
|
|
52
|
+
};
|
|
53
|
+
/** Pure: how one scan event changes the state. Exported for tests and for hosts that don't use React. */
|
|
54
|
+
export declare function reduceScan(state: CharacterScanState, action: Action): CharacterScanState;
|
|
55
|
+
interface CharacterScan extends CharacterScanState {
|
|
56
|
+
start(input: ScanInput, options?: Omit<ScanOptions, "signal">): Promise<void>;
|
|
57
|
+
/** Re-exports the finished character with another look, hair or colours; no-op before a character exists. */
|
|
58
|
+
restyle(options: RestyleOptions): Promise<void>;
|
|
59
|
+
cancel(): void;
|
|
60
|
+
}
|
|
61
|
+
export declare function useCharacterScan(engine: ScanEngine): CharacterScan;
|
|
62
|
+
export type { CharacterScan, CharacterScanState, ScanMask, ScanStatus };
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { n as ScanError } from "./errors.js";
|
|
2
|
+
import { useCallback, useEffect, useReducer, useRef } from "react";
|
|
3
|
+
// src/react/use-character-scan.ts
|
|
4
|
+
const initialScanState = {
|
|
5
|
+
status: "idle",
|
|
6
|
+
stage: null,
|
|
7
|
+
download: null,
|
|
8
|
+
progress: 0,
|
|
9
|
+
landmarks: {
|
|
10
|
+
face: null,
|
|
11
|
+
body: null
|
|
12
|
+
},
|
|
13
|
+
masks: {
|
|
14
|
+
face: null,
|
|
15
|
+
body: null
|
|
16
|
+
},
|
|
17
|
+
model: null,
|
|
18
|
+
weights: null,
|
|
19
|
+
character: null,
|
|
20
|
+
error: null
|
|
21
|
+
};
|
|
22
|
+
/** Pure: how one scan event changes the state. Exported for tests and for hosts that don't use React. */
|
|
23
|
+
function reduceScan(state, action) {
|
|
24
|
+
switch (action.type) {
|
|
25
|
+
case "start": return {
|
|
26
|
+
...initialScanState,
|
|
27
|
+
status: "scanning"
|
|
28
|
+
};
|
|
29
|
+
case "restyled": return {
|
|
30
|
+
...state,
|
|
31
|
+
character: action.character,
|
|
32
|
+
weights: action.character.weights
|
|
33
|
+
};
|
|
34
|
+
case "error": return {
|
|
35
|
+
...state,
|
|
36
|
+
status: "error",
|
|
37
|
+
error: action.error
|
|
38
|
+
};
|
|
39
|
+
case "cancelled": return {
|
|
40
|
+
...state,
|
|
41
|
+
status: "cancelled",
|
|
42
|
+
stage: null,
|
|
43
|
+
download: null
|
|
44
|
+
};
|
|
45
|
+
case "event": {
|
|
46
|
+
const { event } = action;
|
|
47
|
+
switch (event.type) {
|
|
48
|
+
case "stage":
|
|
49
|
+
if (event.ms === void 0) return {
|
|
50
|
+
...state,
|
|
51
|
+
stage: event.stage
|
|
52
|
+
};
|
|
53
|
+
return event.stage === "load" ? {
|
|
54
|
+
...state,
|
|
55
|
+
download: null
|
|
56
|
+
} : state;
|
|
57
|
+
case "download": return {
|
|
58
|
+
...state,
|
|
59
|
+
download: {
|
|
60
|
+
loaded: event.loaded,
|
|
61
|
+
total: event.total
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
case "mask": return {
|
|
65
|
+
...state,
|
|
66
|
+
masks: {
|
|
67
|
+
...state.masks,
|
|
68
|
+
[event.source]: {
|
|
69
|
+
width: event.width,
|
|
70
|
+
height: event.height,
|
|
71
|
+
data: event.data
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
case "landmarks": return {
|
|
76
|
+
...state,
|
|
77
|
+
landmarks: {
|
|
78
|
+
...state.landmarks,
|
|
79
|
+
[event.source]: event.points
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
case "model": return {
|
|
83
|
+
...state,
|
|
84
|
+
model: {
|
|
85
|
+
glb: event.glb,
|
|
86
|
+
targetNames: event.targetNames
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
case "progress": return {
|
|
90
|
+
...state,
|
|
91
|
+
progress: event.value,
|
|
92
|
+
weights: event.weights
|
|
93
|
+
};
|
|
94
|
+
case "error": return {
|
|
95
|
+
...state,
|
|
96
|
+
status: "error",
|
|
97
|
+
stage: null,
|
|
98
|
+
download: null,
|
|
99
|
+
error: new ScanError(event.error.code, event.error.message)
|
|
100
|
+
};
|
|
101
|
+
case "done": return {
|
|
102
|
+
...state,
|
|
103
|
+
status: "done",
|
|
104
|
+
stage: null,
|
|
105
|
+
progress: 1,
|
|
106
|
+
character: event.character,
|
|
107
|
+
weights: event.character.weights
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return state;
|
|
113
|
+
}
|
|
114
|
+
function useCharacterScan(engine) {
|
|
115
|
+
const [state, dispatch] = useReducer(reduceScan, initialScanState);
|
|
116
|
+
const controller = useRef(null);
|
|
117
|
+
const cancel = useCallback(() => {
|
|
118
|
+
controller.current?.abort();
|
|
119
|
+
controller.current = null;
|
|
120
|
+
}, []);
|
|
121
|
+
const start = async (input, options = {}) => {
|
|
122
|
+
controller.current?.abort();
|
|
123
|
+
const current = new AbortController();
|
|
124
|
+
controller.current = current;
|
|
125
|
+
dispatch({ type: "start" });
|
|
126
|
+
try {
|
|
127
|
+
for await (const event of engine.scan(input, {
|
|
128
|
+
...options,
|
|
129
|
+
signal: current.signal
|
|
130
|
+
})) dispatch({
|
|
131
|
+
type: "event",
|
|
132
|
+
event
|
|
133
|
+
});
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if (current.signal.aborted) dispatch({ type: "cancelled" });
|
|
136
|
+
else dispatch({
|
|
137
|
+
type: "error",
|
|
138
|
+
error: error instanceof Error ? error : /* @__PURE__ */ new Error("The scan failed.")
|
|
139
|
+
});
|
|
140
|
+
} finally {
|
|
141
|
+
if (controller.current === current) controller.current = null;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
useEffect(() => cancel, [cancel]);
|
|
145
|
+
const restyle = async (options) => {
|
|
146
|
+
if (!state.character) return;
|
|
147
|
+
try {
|
|
148
|
+
dispatch({
|
|
149
|
+
type: "restyled",
|
|
150
|
+
character: await engine.restyle(state.character, options)
|
|
151
|
+
});
|
|
152
|
+
} catch (error) {
|
|
153
|
+
dispatch({
|
|
154
|
+
type: "error",
|
|
155
|
+
error: error instanceof Error ? error : /* @__PURE__ */ new Error("Restyling failed.")
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
return {
|
|
160
|
+
...state,
|
|
161
|
+
start,
|
|
162
|
+
cancel,
|
|
163
|
+
restyle
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
export { initialScanState, reduceScan, useCharacterScan };
|
|
167
|
+
|
|
168
|
+
//# sourceMappingURL=react.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.js","names":[],"sources":["../src/react/use-character-scan.ts"],"sourcesContent":["import { useCallback, useEffect, useReducer, useRef } from \"react\"\nimport type { ScanEngine } from \"../scan/engine.ts\"\nimport { ScanError } from \"../scan/errors.ts\"\nimport type { Character, RestyleOptions, ScanEvent, ScanInput, ScanOptions, ScanStage } from \"../scan/types.ts\"\n\n/** A person mask: confidence 0–255, row-major. */\nexport interface ScanMask {\n width: number\n height: number\n data: Uint8Array\n}\n\nexport type ScanStatus = \"idle\" | \"scanning\" | \"done\" | \"error\" | \"cancelled\"\n\n/** Props-shaped scan state for ModelScan / ModelViewer. */\nexport interface CharacterScanState {\n status: ScanStatus\n stage: ScanStage | null\n /** Models downloading before the first scan; null before and after. Bytes after decompression. */\n download: { loaded: number; total: number } | null\n /** 0..1. */\n progress: number\n landmarks: { face: Float32Array | null; body: Float32Array | null }\n /** Person masks for the photo-to-character transition (see the `mask` event). */\n masks: { face: ScanMask | null; body: ScanMask | null }\n /** The base GLB to load once; morph it by setting `weights`. */\n model: { glb: Uint8Array; targetNames: string[] } | null\n weights: Float32Array | null\n character: Character | null\n error: Error | null\n}\n\nexport const initialScanState: CharacterScanState = {\n status: \"idle\",\n stage: null,\n download: null,\n progress: 0,\n landmarks: { face: null, body: null },\n masks: { face: null, body: null },\n model: null,\n weights: null,\n character: null,\n error: null,\n}\n\ntype Action =\n | { type: \"start\" }\n | { type: \"restyled\"; character: Character }\n | { type: \"event\"; event: ScanEvent }\n | { type: \"error\"; error: Error }\n | { type: \"cancelled\" }\n\n/** Pure: how one scan event changes the state. Exported for tests and for hosts that don't use React. */\nexport function reduceScan(state: CharacterScanState, action: Action): CharacterScanState {\n switch (action.type) {\n case \"start\":\n return { ...initialScanState, status: \"scanning\" }\n case \"restyled\":\n return { ...state, character: action.character, weights: action.character.weights }\n case \"error\":\n return { ...state, status: \"error\", error: action.error }\n case \"cancelled\":\n return { ...state, status: \"cancelled\", stage: null, download: null }\n case \"event\": {\n const { event } = action\n switch (event.type) {\n case \"stage\":\n if (event.ms === undefined) return { ...state, stage: event.stage }\n return event.stage === \"load\" ? { ...state, download: null } : state\n case \"download\":\n return { ...state, download: { loaded: event.loaded, total: event.total } }\n case \"mask\":\n return {\n ...state,\n masks: { ...state.masks, [event.source]: { width: event.width, height: event.height, data: event.data } },\n }\n case \"landmarks\":\n return { ...state, landmarks: { ...state.landmarks, [event.source]: event.points } }\n case \"model\":\n return { ...state, model: { glb: event.glb, targetNames: event.targetNames } }\n case \"progress\":\n return { ...state, progress: event.value, weights: event.weights }\n case \"error\":\n return {\n ...state,\n status: \"error\",\n stage: null,\n download: null,\n error: new ScanError(event.error.code, event.error.message),\n }\n case \"done\":\n return {\n ...state,\n status: \"done\",\n stage: null,\n progress: 1,\n character: event.character,\n weights: event.character.weights,\n }\n }\n }\n }\n return state\n}\n\nexport interface CharacterScan extends CharacterScanState {\n start(input: ScanInput, options?: Omit<ScanOptions, \"signal\">): Promise<void>\n /** Re-exports the finished character with another look, hair or colours; no-op before a character exists. */\n restyle(options: RestyleOptions): Promise<void>\n cancel(): void\n}\n\nexport function useCharacterScan(engine: ScanEngine): CharacterScan {\n const [state, dispatch] = useReducer(reduceScan, initialScanState)\n const controller = useRef<AbortController | null>(null)\n\n const cancel = useCallback(() => {\n controller.current?.abort()\n controller.current = null\n }, [])\n\n // Not memoised: it reads `engine` at call time, and hosts call it from event handlers.\n const start = async (input: ScanInput, options: Omit<ScanOptions, \"signal\"> = {}) => {\n controller.current?.abort()\n const current = new AbortController()\n controller.current = current\n dispatch({ type: \"start\" })\n try {\n for await (const event of engine.scan(input, { ...options, signal: current.signal }))\n dispatch({ type: \"event\", event })\n } catch (error) {\n if (current.signal.aborted) dispatch({ type: \"cancelled\" })\n else dispatch({ type: \"error\", error: error instanceof Error ? error : new Error(\"The scan failed.\") })\n } finally {\n if (controller.current === current) controller.current = null\n }\n }\n\n // A scan still running when the component unmounts is abandoned, so its worker stops sending events.\n useEffect(() => cancel, [cancel])\n\n const restyle = async (options: RestyleOptions) => {\n if (!state.character) return\n try {\n dispatch({ type: \"restyled\", character: await engine.restyle(state.character, options) })\n } catch (error) {\n dispatch({ type: \"error\", error: error instanceof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AO,YAA4B;EACjD,IAAI,CAAC,MAAM,WAAW;EACtB,IAAI;GACF,SAAS;IAAE,MAAM;IAAY,WAAW,MAAM,OAAO,QAAQ,MAAM,WAAW,OAAO;GAAE,CAAC;EAC1F,SAAS,OAAO;GACd,SAAS;IAAE,MAAM;IAAS,OAAO,iBAAiB,QAAQ,wBAAQ,IAAI,MAAM,mBAAmB;GAAE,CAAC;EACpG;CACF;CAEA,OAAO;EAAE,GAAG;EAAO;EAAO;EAAQ;CAAQ;AAC5C"}
|
package/dist/style.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
declare const styleIds: readonly ["soft-toon", "cel-outline", "low-poly", "clay"];
|
|
2
|
+
type StyleId = (typeof styleIds)[number];
|
|
3
|
+
interface StylePreset {
|
|
4
|
+
id: StyleId;
|
|
5
|
+
label: string;
|
|
6
|
+
/** Light ramp: `levels[i]` is the brightness for diffuse light above `steps[i]`; `softness` blurs each step. */
|
|
7
|
+
ramp: {
|
|
8
|
+
steps: number[];
|
|
9
|
+
levels: number[];
|
|
10
|
+
softness: number;
|
|
11
|
+
};
|
|
12
|
+
/** Inverted-hull outline, width in metres (0 for none), linear RGB colour. */
|
|
13
|
+
outline: {
|
|
14
|
+
width: number;
|
|
15
|
+
color: [number, number, number];
|
|
16
|
+
};
|
|
17
|
+
/** Rim light added at grazing angles: strength and falloff exponent. */
|
|
18
|
+
rim: {
|
|
19
|
+
strength: number;
|
|
20
|
+
power: number;
|
|
21
|
+
};
|
|
22
|
+
/** Faceted shading from face normals. */
|
|
23
|
+
flatShading: boolean;
|
|
24
|
+
/** 1 keeps colours, 0 greys them; clay also pulls everything towards `tint`. */
|
|
25
|
+
saturation: number;
|
|
26
|
+
tint: [number, number, number] | null;
|
|
27
|
+
tintAmount: number;
|
|
28
|
+
/** Hair highlight strength (skin and eyes get none: on skin a highlight reads as a wet patch). */
|
|
29
|
+
specular: number;
|
|
30
|
+
}
|
|
31
|
+
declare const stylePresets: Record<StyleId, StylePreset>;
|
|
32
|
+
export { stylePresets as i, StylePreset as n, styleIds as r, StyleId as t };
|
package/dist/style.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// src/model/style.ts
|
|
2
|
+
const styleIds = [
|
|
3
|
+
"soft-toon",
|
|
4
|
+
"cel-outline",
|
|
5
|
+
"low-poly",
|
|
6
|
+
"clay"
|
|
7
|
+
];
|
|
8
|
+
const ink = [
|
|
9
|
+
.025,
|
|
10
|
+
.02,
|
|
11
|
+
.03
|
|
12
|
+
];
|
|
13
|
+
const stylePresets = {
|
|
14
|
+
"soft-toon": {
|
|
15
|
+
id: "soft-toon",
|
|
16
|
+
label: "Soft toon",
|
|
17
|
+
ramp: {
|
|
18
|
+
steps: [.15, .55],
|
|
19
|
+
levels: [
|
|
20
|
+
.55,
|
|
21
|
+
.82,
|
|
22
|
+
1
|
|
23
|
+
],
|
|
24
|
+
softness: .12
|
|
25
|
+
},
|
|
26
|
+
outline: {
|
|
27
|
+
width: .0025,
|
|
28
|
+
color: ink
|
|
29
|
+
},
|
|
30
|
+
rim: {
|
|
31
|
+
strength: .22,
|
|
32
|
+
power: 3
|
|
33
|
+
},
|
|
34
|
+
flatShading: false,
|
|
35
|
+
saturation: 1.05,
|
|
36
|
+
tint: null,
|
|
37
|
+
tintAmount: 0,
|
|
38
|
+
specular: .05
|
|
39
|
+
},
|
|
40
|
+
"cel-outline": {
|
|
41
|
+
id: "cel-outline",
|
|
42
|
+
label: "Cel with outline",
|
|
43
|
+
ramp: {
|
|
44
|
+
steps: [.35],
|
|
45
|
+
levels: [.62, 1],
|
|
46
|
+
softness: .02
|
|
47
|
+
},
|
|
48
|
+
outline: {
|
|
49
|
+
width: .0045,
|
|
50
|
+
color: ink
|
|
51
|
+
},
|
|
52
|
+
rim: {
|
|
53
|
+
strength: .12,
|
|
54
|
+
power: 4
|
|
55
|
+
},
|
|
56
|
+
flatShading: false,
|
|
57
|
+
saturation: 1.15,
|
|
58
|
+
tint: null,
|
|
59
|
+
tintAmount: 0,
|
|
60
|
+
specular: 0
|
|
61
|
+
},
|
|
62
|
+
"low-poly": {
|
|
63
|
+
id: "low-poly",
|
|
64
|
+
label: "Low-poly",
|
|
65
|
+
ramp: {
|
|
66
|
+
steps: [
|
|
67
|
+
.2,
|
|
68
|
+
.5,
|
|
69
|
+
.8
|
|
70
|
+
],
|
|
71
|
+
levels: [
|
|
72
|
+
.5,
|
|
73
|
+
.68,
|
|
74
|
+
.86,
|
|
75
|
+
1
|
|
76
|
+
],
|
|
77
|
+
softness: 0
|
|
78
|
+
},
|
|
79
|
+
outline: {
|
|
80
|
+
width: 0,
|
|
81
|
+
color: ink
|
|
82
|
+
},
|
|
83
|
+
rim: {
|
|
84
|
+
strength: .08,
|
|
85
|
+
power: 2
|
|
86
|
+
},
|
|
87
|
+
flatShading: true,
|
|
88
|
+
saturation: 1,
|
|
89
|
+
tint: null,
|
|
90
|
+
tintAmount: 0,
|
|
91
|
+
specular: 0
|
|
92
|
+
},
|
|
93
|
+
clay: {
|
|
94
|
+
id: "clay",
|
|
95
|
+
label: "Clay",
|
|
96
|
+
ramp: {
|
|
97
|
+
steps: [
|
|
98
|
+
.05,
|
|
99
|
+
.4,
|
|
100
|
+
.75
|
|
101
|
+
],
|
|
102
|
+
levels: [
|
|
103
|
+
.45,
|
|
104
|
+
.66,
|
|
105
|
+
.85,
|
|
106
|
+
1
|
|
107
|
+
],
|
|
108
|
+
softness: .25
|
|
109
|
+
},
|
|
110
|
+
outline: {
|
|
111
|
+
width: 0,
|
|
112
|
+
color: ink
|
|
113
|
+
},
|
|
114
|
+
rim: {
|
|
115
|
+
strength: .3,
|
|
116
|
+
power: 2.5
|
|
117
|
+
},
|
|
118
|
+
flatShading: false,
|
|
119
|
+
saturation: .55,
|
|
120
|
+
tint: [
|
|
121
|
+
.78,
|
|
122
|
+
.56,
|
|
123
|
+
.42
|
|
124
|
+
],
|
|
125
|
+
tintAmount: .25,
|
|
126
|
+
specular: .02
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
function stylePreset(id) {
|
|
130
|
+
const preset = stylePresets[id];
|
|
131
|
+
if (!preset) throw new Error(`Unknown style "${id}".`);
|
|
132
|
+
return preset;
|
|
133
|
+
}
|
|
134
|
+
/** Brightness for a Lambert term under a preset's ramp. */
|
|
135
|
+
function rampLevel(preset, lambert) {
|
|
136
|
+
const { steps, levels, softness } = preset.ramp;
|
|
137
|
+
let level = levels[0] ?? 1;
|
|
138
|
+
steps.forEach((step, index) => {
|
|
139
|
+
const next = levels[index + 1] ?? level;
|
|
140
|
+
const previous = levels[index] ?? level;
|
|
141
|
+
const t = softness <= 0 ? lambert >= step ? 1 : 0 : smoothstep(step - softness / 2, step + softness / 2, lambert);
|
|
142
|
+
if (t > 0) level = previous + (next - previous) * t;
|
|
143
|
+
});
|
|
144
|
+
return level;
|
|
145
|
+
}
|
|
146
|
+
function smoothstep(edge0, edge1, x) {
|
|
147
|
+
const t = Math.min(Math.max((x - edge0) / (edge1 - edge0), 0), 1);
|
|
148
|
+
return t * t * (3 - 2 * t);
|
|
149
|
+
}
|
|
150
|
+
export { stylePresets as i, styleIds as n, stylePreset as r, rampLevel as t };
|
|
151
|
+
|
|
152
|
+
//# sourceMappingURL=style.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"style.js","names":[],"sources":["../src/model/style.ts"],"sourcesContent":["// Visual style presets. A preset is data: the GLB carries it in its materials' `extras.eskais`, the reference three.js\n// materials (`@eskais/character/three`) and the repo's software renderer both read the same numbers, so a style looks\n// the same wherever it is drawn.\n\nexport const styleIds = [\"soft-toon\", \"cel-outline\", \"low-poly\", \"clay\"] as const\nexport type StyleId = (typeof styleIds)[number]\n\nexport interface StylePreset {\n id: StyleId\n label: string\n /** Light ramp: `levels[i]` is the brightness for diffuse light above `steps[i]`; `softness` blurs each step. */\n ramp: { steps: number[]; levels: number[]; softness: number }\n /** Inverted-hull outline, width in metres (0 for none), linear RGB colour. */\n outline: { width: number; color: [number, number, number] }\n /** Rim light added at grazing angles: strength and falloff exponent. */\n rim: { strength: number; power: number }\n /** Faceted shading from face normals. */\n flatShading: boolean\n /** 1 keeps colours, 0 greys them; clay also pulls everything towards `tint`. */\n saturation: number\n tint: [number, number, number] | null\n tintAmount: number\n /** Hair highlight strength (skin and eyes get none: on skin a highlight reads as a wet patch). */\n specular: number\n}\n\nconst ink: [number, number, number] = [0.025, 0.02, 0.03]\n\nexport const stylePresets: Record<StyleId, StylePreset> = {\n \"soft-toon\": {\n id: \"soft-toon\",\n label: \"Soft toon\",\n ramp: { steps: [0.15, 0.55], levels: [0.55, 0.82, 1], softness: 0.12 },\n outline: { width: 0.0025, color: ink },\n rim: { strength: 0.22, power: 3 },\n flatShading: false,\n saturation: 1.05,\n tint: null,\n tintAmount: 0,\n specular: 0.05,\n },\n \"cel-outline\": {\n id: \"cel-outline\",\n label: \"Cel with outline\",\n ramp: { steps: [0.35], levels: [0.62, 1], softness: 0.02 },\n outline: { width: 0.0045, color: ink },\n rim: { strength: 0.12, power: 4 },\n flatShading: false,\n saturation: 1.15,\n tint: null,\n tintAmount: 0,\n specular: 0,\n },\n \"low-poly\": {\n id: \"low-poly\",\n label: \"Low-poly\",\n ramp: { steps: [0.2, 0.5, 0.8], levels: [0.5, 0.68, 0.86, 1], softness: 0 },\n outline: { width: 0, color: ink },\n rim: { strength: 0.08, power: 2 },\n flatShading: true,\n saturation: 1,\n tint: null,\n tintAmount: 0,\n specular: 0,\n },\n clay: {\n id: \"clay\",\n label: \"Clay\",\n ramp: { steps: [0.05, 0.4, 0.75], levels: [0.45, 0.66, 0.85, 1], softness: 0.25 },\n outline: { width: 0, color: ink },\n rim: { strength: 0.3, power: 2.5 },\n flatShading: false,\n saturation: 0.55,\n tint: [0.78, 0.56, 0.42],\n tintAmount: 0.25,\n specular: 0.02,\n },\n}\n\nexport function stylePreset(id: string): StylePreset {\n const preset = (stylePresets as Partial<Record<string, StylePreset>>)[id]\n if (!preset) throw new Error(`Unknown style \"${id}\".`)\n return preset\n}\n\n/** Brightness for a Lambert term under a preset's ramp. */\nexport function rampLevel(preset: StylePreset, lambert: number): number {\n const { steps, levels, softness } = preset.ramp\n let level = levels[0] ?? 1\n steps.forEach((step, index) => {\n const next = levels[index + 1] ?? level\n const previous = levels[index] ?? level\n const t = softness <= 0 ? (lambert >= step ? 1 : 0) : smoothstep(step - softness / 2, step + softness / 2, lambert)\n if (t > 0) level = previous + (next - previous) * t\n })\n return level\n}\n\nfunction smoothstep(edge0: number, edge1: number, x: number): number {\n const t = Math.min(Math.max((x - edge0) / (edge1 - edge0), 0), 1)\n return t * t * (3 - 2 * t)\n}\n\n/** A preset's colour treatment on a linear RGB colour (saturation, then the clay tint). */\nexport function styleColor(preset: StylePreset, color: ArrayLike<number>): [number, number, number] {\n const r = color[0] ?? 0\n const g = color[1] ?? 0\n const b = color[2] ?? 0\n const grey = 0.2126 * r + 0.7152 * g + 0.0722 * b\n const saturated = [r, g, b].map((channel) => Math.max(0, grey + (channel - grey) * preset.saturation)) as [\n number,\n number,\n number,\n ]\n const tint = preset.tint\n if (!tint) return saturated\n return saturated.map(\n (channel, index) => channel + ((tint[index] ?? 0) * grey * 2.2 - channel) * preset.tintAmount,\n ) as [number, number, number]\n}\n"],"mappings":";AAIA,MAAa,WAAW;CAAC;CAAa;CAAe;CAAY;AAAM;AAsBvE,MAAM,MAAgC;CAAC;CAAO;CAAM;AAAI;AAExD,MAAa,eAA6C;CACxD,aAAa;EACX,IAAI;EACJ,OAAO;EACP,MAAM;GAAE,OAAO,CAAC,KAAM,GAAI;GAAG,QAAQ;IAAC;IAAM;IAAM;GAAC;GAAG,UAAU;EAAK;EACrE,SAAS;GAAE,OAAO;GAAQ,OAAO;EAAI;EACrC,KAAK;GAAE,UAAU;GAAM,OAAO;EAAE;EAChC,aAAa;EACb,YAAY;EACZ,MAAM;EACN,YAAY;EACZ,UAAU;CACZ;CACA,eAAe;EACb,IAAI;EACJ,OAAO;EACP,MAAM;GAAE,OAAO,CAAC,G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AG,OAAO,WAAW,GAAG,OAAO;EAClH,IAAI,IAAI,GAAG,QAAQ,YAAY,OAAO,YAAY;CACpD,CAAC;CACD,OAAO;AACT;AAEA,SAAS,WAAW,OAAe,OAAe,GAAmB;CACnE,MAAM,IAAI,KAAK,IAAI,KAAK,KAAK,IAAI,UAAU,QAAQ,QAAQ,CAAC,GAAG,CAAC;CAChE,OAAO,IAAI,KAAK,IAAI,IAAI;AAC1B"}
|
package/dist/three.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { i as stylePresets, n as StylePreset, r as styleIds, t as StyleId } from "./style.js";
|
|
2
|
+
import { DataTexture, Object3D } from "three";
|
|
3
|
+
/** The preset's light ramp as a 1D gradient map for MeshToonMaterial. */
|
|
4
|
+
export declare function rampTexture(preset: StylePreset, size?: number): DataTexture;
|
|
5
|
+
export interface StyleOptions {
|
|
6
|
+
/** A preset id or preset; defaults to the style the GLB was exported with. */
|
|
7
|
+
style?: StyleId | StylePreset;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Restyles a loaded character in place (GLTFLoader puts material extras in `material.userData`). Returns a function
|
|
11
|
+
* that removes the outlines and disposes everything this created; it does not restore the original materials.
|
|
12
|
+
*/
|
|
13
|
+
export declare function styleCharacter(root: Object3D, options?: StyleOptions): () => void;
|
|
14
|
+
export { type StyleId, type StylePreset, styleIds, stylePresets };
|
package/dist/three.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { i as stylePresets, n as styleIds, r as stylePreset, t as rampLevel } from "./style.js";
|
|
2
|
+
import { BackSide, Color, DataTexture, LinearFilter, Mesh, MeshBasicMaterial, MeshToonMaterial, NearestFilter, RedFormat } from "three";
|
|
3
|
+
// src/three/index.ts
|
|
4
|
+
function extrasOf(material) {
|
|
5
|
+
return material.userData.eskais;
|
|
6
|
+
}
|
|
7
|
+
/** The preset's light ramp as a 1D gradient map for MeshToonMaterial. */
|
|
8
|
+
function rampTexture(preset, size = 64) {
|
|
9
|
+
const data = new Uint8Array(size);
|
|
10
|
+
for (let index = 0; index < size; index++) data[index] = Math.round(rampLevel(preset, index / (size - 1)) * 255);
|
|
11
|
+
const texture = new DataTexture(data, size, 1, RedFormat);
|
|
12
|
+
const smooth = preset.ramp.softness > .05;
|
|
13
|
+
texture.minFilter = smooth ? LinearFilter : NearestFilter;
|
|
14
|
+
texture.magFilter = smooth ? LinearFilter : NearestFilter;
|
|
15
|
+
texture.needsUpdate = true;
|
|
16
|
+
return texture;
|
|
17
|
+
}
|
|
18
|
+
function toonMaterial(source, preset, ramp) {
|
|
19
|
+
const material = new MeshToonMaterial({
|
|
20
|
+
color: new Color(1, 1, 1),
|
|
21
|
+
vertexColors: true,
|
|
22
|
+
gradientMap: ramp,
|
|
23
|
+
side: source.side
|
|
24
|
+
});
|
|
25
|
+
Object.assign(material, { flatShading: preset.flatShading });
|
|
26
|
+
material.name = `${source.name}-${preset.id}`;
|
|
27
|
+
const rim = preset.rim;
|
|
28
|
+
material.onBeforeCompile = (shader) => {
|
|
29
|
+
shader.uniforms["rimStrength"] = { value: rim.strength };
|
|
30
|
+
shader.uniforms["rimPower"] = { value: rim.power };
|
|
31
|
+
shader.fragmentShader = shader.fragmentShader.replace("void main() {", "uniform float rimStrength;\nuniform float rimPower;\nvoid main() {").replace("#include <opaque_fragment>", [
|
|
32
|
+
"float rimFacing = clamp(dot(normalize(normal), normalize(vViewPosition)), 0.0, 1.0);",
|
|
33
|
+
"outgoingLight += diffuseColor.rgb * (2.0 * rimStrength * pow(1.0 - rimFacing, rimPower));",
|
|
34
|
+
"#include <opaque_fragment>"
|
|
35
|
+
].join("\n"));
|
|
36
|
+
};
|
|
37
|
+
material.customProgramCacheKey = () => `eskais-toon-${String(rim.strength)}-${String(rim.power)}`;
|
|
38
|
+
return material;
|
|
39
|
+
}
|
|
40
|
+
function outlineMaterial(preset) {
|
|
41
|
+
const material = new MeshBasicMaterial({
|
|
42
|
+
color: new Color(...preset.outline.color),
|
|
43
|
+
side: BackSide
|
|
44
|
+
});
|
|
45
|
+
material.name = `outline-${preset.id}`;
|
|
46
|
+
const width = preset.outline.width;
|
|
47
|
+
material.onBeforeCompile = (shader) => {
|
|
48
|
+
shader.uniforms["outlineWidth"] = { value: width };
|
|
49
|
+
shader.vertexShader = shader.vertexShader.replace("void main() {", "uniform float outlineWidth;\nvoid main() {").replace("#include <begin_vertex>", "#include <begin_vertex>\ntransformed += normalize(normal) * outlineWidth;");
|
|
50
|
+
};
|
|
51
|
+
material.customProgramCacheKey = () => "eskais-outline";
|
|
52
|
+
return material;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Restyles a loaded character in place (GLTFLoader puts material extras in `material.userData`). Returns a function
|
|
56
|
+
* that removes the outlines and disposes everything this created; it does not restore the original materials.
|
|
57
|
+
*/
|
|
58
|
+
function styleCharacter(root, options = {}) {
|
|
59
|
+
const created = [];
|
|
60
|
+
const outlines = [];
|
|
61
|
+
const ramps = /* @__PURE__ */ new Map();
|
|
62
|
+
const meshes = [];
|
|
63
|
+
root.traverse((object) => {
|
|
64
|
+
if (object instanceof Mesh && !object.userData.eskaisOutline) meshes.push(object);
|
|
65
|
+
});
|
|
66
|
+
for (const mesh of meshes) {
|
|
67
|
+
const source = Array.isArray(mesh.material) ? mesh.material[0] : mesh.material;
|
|
68
|
+
if (!source) continue;
|
|
69
|
+
const extras = extrasOf(source);
|
|
70
|
+
const requested = options.style;
|
|
71
|
+
const preset = typeof requested === "string" ? stylePreset(requested) : requested ?? extras?.style;
|
|
72
|
+
if (!preset) continue;
|
|
73
|
+
let ramp = ramps.get(preset.id);
|
|
74
|
+
if (!ramp) {
|
|
75
|
+
ramp = rampTexture(preset);
|
|
76
|
+
ramps.set(preset.id, ramp);
|
|
77
|
+
created.push(ramp);
|
|
78
|
+
}
|
|
79
|
+
const material = toonMaterial(source, preset, ramp);
|
|
80
|
+
created.push(material);
|
|
81
|
+
mesh.material = material;
|
|
82
|
+
if (preset.outline.width <= 0 || extras?.role === "eyes") continue;
|
|
83
|
+
const outline = mesh.clone(false);
|
|
84
|
+
outline.morphTargetInfluences = mesh.morphTargetInfluences;
|
|
85
|
+
const hull = outlineMaterial(preset);
|
|
86
|
+
created.push(hull);
|
|
87
|
+
outline.material = hull;
|
|
88
|
+
outline.userData = { eskaisOutline: true };
|
|
89
|
+
outline.renderOrder = mesh.renderOrder - 1;
|
|
90
|
+
mesh.parent?.add(outline);
|
|
91
|
+
outlines.push(outline);
|
|
92
|
+
}
|
|
93
|
+
return () => {
|
|
94
|
+
for (const outline of outlines) outline.removeFromParent();
|
|
95
|
+
for (const item of created) item.dispose();
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
export { rampTexture, styleCharacter, styleIds, stylePresets };
|
|
99
|
+
|
|
100
|
+
//# sourceMappingURL=three.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"three.js","names":[],"sources":["../src/three/index.ts"],"sourcesContent":["// Reference three.js materials for the styles a character GLB carries in `extras.eskais` (three: optional peer).\n// Outlines are inverted hulls: one extra vertex pass per mesh and no full-screen pass, so they stay cheap on phones,\n// where a post-process edge pass would need full-resolution depth and normal targets.\n\nimport {\n BackSide,\n Color,\n DataTexture,\n LinearFilter,\n Mesh,\n MeshBasicMaterial,\n MeshToonMaterial,\n NearestFilter,\n RedFormat,\n type Material,\n type Object3D,\n} from \"three\"\nimport { rampLevel, stylePreset, type StyleId, type StylePreset } from \"../model/style.ts\"\n\nexport { stylePresets, styleIds, type StyleId, type StylePreset } from \"../model/style.ts\"\n\ninterface EskaisExtras {\n role?: string\n style?: StylePreset\n}\n\nfunction extrasOf(material: Material): EskaisExtras | undefined {\n return (material.userData as { eskais?: EskaisExtras }).eskais\n}\n\n/** The preset's light ramp as a 1D gradient map for MeshToonMaterial. */\nexport function rampTexture(preset: StylePreset, size = 64): DataTexture {\n const data = new Uint8Array(size)\n for (let index = 0; index < size; index++) data[index] = Math.round(rampLevel(preset, index / (size - 1)) * 255)\n const texture = new DataTexture(data, size, 1, RedFormat)\n const smooth = preset.ramp.softness > 0.05\n texture.minFilter = smooth ? LinearFilter : NearestFilter\n texture.magFilter = smooth ? LinearFilter : NearestFilter\n texture.needsUpdate = true\n return texture\n}\n\nfunction toonMaterial(source: Material, preset: StylePreset, ramp: DataTexture): MeshToonMaterial {\n const material = new MeshToonMaterial({\n color: new Color(1, 1, 1),\n vertexColors: true,\n gradientMap: ramp,\n side: source.side,\n })\n // three's toon shader honours flatShading (it compiles FLAT_SHADED from any material), but its types omit it.\n Object.assign(material, { flatShading: preset.flatShading })\n material.name = `${source.name}-${preset.id}`\n const rim = preset.rim\n material.onBeforeCompile = (shader) => {\n shader.uniforms[\"rimStrength\"] = { value: rim.strength }\n shader.uniforms[\"rimPower\"] = { value: rim.power }\n shader.fragmentShader = shader.fragmentShader\n .replace(\"void main() {\", \"uniform float rimStrength;\\nuniform float rimPower;\\nvoid main() {\")\n .replace(\n \"#include <opaque_fragment>\",\n [\n \"float rimFacing = clamp(dot(normalize(normal), normalize(vViewPosition)), 0.0, 1.0);\",\n \"outgoingLight += diffuseColor.rgb * (2.0 * rimStrength * pow(1.0 - rimFacing, rimPower));\",\n \"#include <opaque_fragment>\",\n ].join(\"\\n\"),\n )\n }\n material.customProgramCacheKey = () => `eskais-toon-${String(rim.strength)}-${String(rim.power)}`\n return material\n}\n\nfunction outlineMaterial(preset: StylePreset): MeshBasicMaterial {\n const material = new MeshBasicMaterial({ color: new Color(...preset.outline.color), side: BackSide })\n material.name = `outline-${preset.id}`\n const width = preset.outline.width\n material.onBeforeCompile = (shader) => {\n shader.uniforms[\"outlineWidth\"] = { value: width }\n shader.vertexShader = shader.vertexShader\n .replace(\"void main() {\", \"uniform float outlineWidth;\\nvoid main() {\")\n // Pushed out before morphing and skinning, so the hull follows both.\n .replace(\"#include <begin_vertex>\", \"#include <begin_vertex>\\ntransformed += normalize(normal) * outlineWidth;\")\n }\n material.customProgramCacheKey = () => \"eskais-outline\"\n return material\n}\n\nexport interface StyleOptions {\n /** A preset id or preset; defaults to the style the GLB was exported with. */\n style?: StyleId | StylePreset\n}\n\n/**\n * Restyles a loaded character in place (GLTFLoader puts material extras in `material.userData`). Returns a function\n * that removes the outlines and disposes everything this created; it does not restore the original materials.\n */\nexport function styleCharacter(root: Object3D, options: StyleOptions = {}): () => void {\n const created: { dispose(): void }[] = []\n const outlines: Mesh[] = []\n const ramps = new Map<string, DataTexture>()\n const meshes: Mesh[] = []\n root.traverse((object) => {\n if (object instanceof Mesh && !(object.userData as { eskaisOutline?: boolean }).eskaisOutline) meshes.push(object)\n })\n for (const mesh of meshes) {\n const source = Array.isArray(mesh.material) ? mesh.material[0] : mesh.material\n if (!source) continue\n const extras = extrasOf(source)\n const requested = options.style\n const preset = typeof requested === \"string\" ? stylePreset(requested) : (requested ?? extras?.style)\n if (!preset) continue\n let ramp = ramps.get(preset.id)\n if (!ramp) {\n ramp = rampTexture(preset)\n ramps.set(preset.id, ramp)\n created.push(ramp)\n }\n const material = toonMaterial(source, preset, ramp)\n created.push(material)\n mesh.material = material\n if (preset.outline.width <= 0 || extras?.role === \"eyes\") continue\n const outline = mesh.clone(false) as Mesh\n // One influences array for both, so live morph weights move the outline too.\n outline.morphTargetInfluences = mesh.morphTargetInfluences\n const hull = outlineMaterial(preset)\n created.push(hull)\n outline.material = hull\n outline.userData = { eskaisOutline: true }\n outline.renderOrder = mesh.renderOrder - 1\n mesh.parent?.add(outline)\n outlines.push(outline)\n }\n return () => {\n for (const outline of outlines) outline.removeFromParent()\n for (const item of created) item.dispose()\n }\n}\n"],"mappings":";;;AA0BA,SAAS,SAAS,UAA8C;CAC9D,OAAQ,SAAS,SAAuC;AAC1D;;AAGA,SAAgB,YAAY,QAAqB,OAAO,IAAiB;CACvE,MAAM,OAAO,IAAI,WAAW,IAAI;CAChC,KAAK,IAAI,QAAQ,GAAG,QAAQ,MAAM,SAAS,KAAK,SAAS,KAAK,MAAM,UAAU,QAAQ,SAAS,OAAO,EAAE,IAAI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