@charivo/render-iki 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Zeikar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,189 @@
1
- # Temporary Holding Version
1
+ # @charivo/render-iki
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [Iki](https://github.com/zeikar/iki) renderer for Charivo. It implements the
4
+ charivo `Renderer` (plus the `MouseTrackable` pair) by driving an `.iki` puppet
5
+ model through the Iki engine, an open 2D rig engine for the web, so a character
6
+ needs no Live2D SDK or license.
7
+
8
+ In most apps you use it together with `@charivo/render`, the same way as
9
+ `@charivo/render-live2d`. To see it running, open the web demo on an Iki
10
+ character: [Twin](https://charivo.vercel.app/?character=twin) (a bust) or
11
+ [Mint](https://charivo.vercel.app/?character=mint) (a full body).
12
+
13
+ ## Install
14
+
15
+ ```bash
16
+ pnpm add @charivo/render-iki @charivo/render
17
+ ```
18
+
19
+ [`@ikijs/engine`](https://www.npmjs.com/package/@ikijs/engine) and
20
+ [`@ikijs/format`](https://www.npmjs.com/package/@ikijs/format) come with it as
21
+ dependencies. The package is browser-only and ESM-only, and MIT like the
22
+ engine. Iki is pre-1.0, so each release of this package pins one engine minor
23
+ (`^0.12` now) and moves to the next one in a release of its own.
24
+
25
+ ## Usage
26
+
27
+ Swap `createLive2DRenderer` for `createIkiRenderer` — the rest of the charivo
28
+ wiring is unchanged:
29
+
30
+ ```ts
31
+ import { createIkiRenderer } from "@charivo/render-iki";
32
+ import { createRenderManager } from "@charivo/render";
33
+
34
+ const renderer = createIkiRenderer({ canvas });
35
+ const renderManager = createRenderManager(renderer, {
36
+ canvas,
37
+ mouseTracking: "document",
38
+ });
39
+
40
+ await renderManager.initialize();
41
+ await renderManager.loadModel("/hero.iki");
42
+
43
+ // The model file is the catalog: ids, counts and descriptions for the LLM.
44
+ const catalog = renderer.getAvatarControlCatalog();
45
+ ```
46
+
47
+ `loadModel()` fetches the path, so serve the `.iki` file from your app (it is
48
+ one JSON file with its textures inside). The models the demo uses are in
49
+ [`examples/web/public/iki/`](https://github.com/zeikar/charivo/tree/main/examples/web/public/iki);
50
+ the [Iki repo](https://github.com/zeikar/iki) documents the format.
51
+
52
+ ## Try it
53
+
54
+ Besides the web demo, a local harness lives in
55
+ [`examples/iki-test`](https://github.com/zeikar/charivo/tree/main/examples/iki-test)
56
+ in the charivo repo — it drives an Iki model through charivo's `RenderManager`
57
+ and this adapter:
58
+ engine idle and hair physics, mouse-follow gaze through the idle's own
59
+ eyes-lead, head-follow model, simulated lip-sync, and a button for every expression and motion the file
60
+ declares (sent as `avatar:expression` / `avatar:motion` events), with the
61
+ catalog printed below. Run `pnpm --filter @charivo/iki-test dev` and open the
62
+ Vite URL. It loads Iki's bust hero by default (twin-tails, whose far tail goes
63
+ behind the hair on a turn through `IkiPart.occlusion`); `?model=mint` loads a
64
+ full body, framed with `setView()` so its `Wave` stays on the canvas. Both
65
+ files are the web demo's, under `examples/web/public/iki/`, so there is one
66
+ copy of each to refresh.
67
+
68
+ ## Public surface
69
+
70
+ The package exports `createIkiRenderer(options?)` and the types
71
+ `IkiRenderer`, `IkiRendererOptions` (`{ canvas }`) and `IkiView`. The renderer
72
+ it returns exposes:
73
+
74
+ - `initialize()` — create the WebGL player (requires a canvas).
75
+ - `loadModel(modelPath)` — fetch + parse an `.iki` model, build the engine's
76
+ `IkiMotion` for it, start the adapter's rAF loop that steps it, then start
77
+ the engine's own render loop. The latest call wins: one whose fetch is still
78
+ out when a later call begins resolves without loading, however late it lands
79
+ (or rejects, if its own fetch or parse fails).
80
+ - `render(message, character?)` — stateless (the engine's rAF draws).
81
+ - `destroy()` — stop that loop and free the player.
82
+ - `setRealtimeLipSync(enabled)` / `updateRealtimeLipSyncRms(rms)` — drive the
83
+ mouth aperture (`ParamMouthOpenY`) from lip-sync RMS; see "Motion".
84
+ - `setView(view?)` — frame an `IkiView` (`{ x, y, width, height }`: centre and
85
+ size in model units, `y` up) instead of the model's box, e.g. a full body
86
+ whose raised arm leaves the box; no argument restores the box. The engine's
87
+ `IkiPlayer.setView`, callable before `initialize()` and kept across loads.
88
+ Not part of the `Renderer` contract, so call it on the renderer, not the
89
+ `RenderManager`. `IkiView` is re-exported.
90
+ - `lookAt({ x, y })` — sets the gaze target (each clamped to `-1..1`, `y=1`
91
+ up; a non-finite pair is ignored); see "Gaze".
92
+ - `setGazeOrigin(origin?)` — `MouseTrackable`'s optional method, the same call
93
+ and units as `@charivo/render-live2d`'s: the point of the model the mouse
94
+ gaze is measured from, as a `GazeOrigin` from `@charivo/render` (fractions of
95
+ the model's canvas, top-left `(0, 0)` to bottom-right `(1, 1)`), e.g. between
96
+ the eyes of a full body; no argument restores the canvas's centre; a
97
+ non-finite `x` or `y` throws. See "Gaze".
98
+ - `updateViewWithMouse` / `handleMouseTap` — the `MouseTrackable` pair for
99
+ cursor-follow. Both are present because `RenderManager` installs mouse
100
+ tracking only when both exist; tap is a no-op (Iki has no tap motions).
101
+ - `playExpression(id)` / `stopExpression()` / `playMotionByGroup(group, index)`
102
+ and `getAvailableExpressions()` / `getAvailableMotionGroups()` /
103
+ `getAvatarControlCatalog()` — see "Expressions and motions".
104
+
105
+ ## Expressions and motions
106
+
107
+ An `.iki` file declares its own expressions (`expressions`: id, description,
108
+ fades, parameters) and motion clips (`motions`: clips by group, each with a
109
+ description). The adapter hands them to `IkiMotion` and reads the catalog off
110
+ the parsed model:
111
+
112
+ - `playExpression(id)` fades the expression in over its `fadeIn`; one is active
113
+ at a time, and a new one replaces it. `stopExpression()` fades back to the
114
+ base face over its `fadeOut`. `RenderManager` calls it when speech ends
115
+ (`tts:audio:end`) or after its hold fallback.
116
+ - `playMotionByGroup(group, index)` plays that clip one-shot, replacing a
117
+ running one. The `Renderer` contract's `{ muteSound }` option is accepted and
118
+ ignored: Iki clips carry no audio.
119
+ - `getAvailableExpressions()` returns the expression ids and
120
+ `getAvailableMotionGroups()` `{ group: clip count }`; `RenderManager` drops
121
+ `avatar:expression` / `avatar:motion` events that name anything else.
122
+ - `getAvatarControlCatalog()` returns the whole `AvatarControlCatalog`:
123
+ those ids and counts plus `expressionDescriptions` (`{ id: description }`)
124
+ and `motionDescriptions` (`{ group: [description by clip index] }`) from the
125
+ file, so `@charivo/avatar`'s tools and instructions describe each choice to
126
+ the LLM with no hand-written descriptions. A map is omitted when the model
127
+ declares nothing of its kind.
128
+
129
+ Before `loadModel()` resolves (and after `destroy()`) the play methods do
130
+ nothing and the catalog is empty, so read the catalog after the load.
131
+
132
+ ## Motion
133
+
134
+ Idle animation, motion clips, expressions, hair-spring physics and chain
135
+ secondary motion are not this adapter's code — they come from
136
+ `@ikijs/engine`'s `IkiMotion`, built fresh in `loadModel()` for the loaded
137
+ model. The adapter steps it once per rAF tick, registered before the engine's
138
+ render loop so the pose is written before the frame is drawn; hands it the
139
+ host's gaze; and writes lip-sync last.
140
+
141
+ Each frame runs in this order, and a later write wins:
142
+
143
+ | Step | Writes |
144
+ | --- | --- |
145
+ | 1. Idle (`IkiMotion`) | blink (and the brows' dip with it, on a model that declares brows), breath, the ambient sway that hair physics follows, and the gaze and head: the eyes hold a point and the head follows it over its own slow drift — unless the model declares an `Idle` motion group, whose looping clips replace the head and gaze |
146
+ | 2. Clips (`IkiMotion`) | the `Idle` loop, then the one-shot from `playMotionByGroup` over it |
147
+ | 3. Expressions (`IkiMotion`) | the active expression, or its fade back to base |
148
+ | 4. Physics and chains (`IkiMotion`) | hair sway outputs, reading the head steps 1–3 wrote |
149
+ | 5. Eyes under an `Idle` group (adapter) | `ParamEyeBallX/Y` at the host gaze — only on a model that declares an `Idle` group, and only once a gaze exists |
150
+ | 6. Lip-sync (adapter) | `ParamMouthOpenY`, while lip-sync is enabled |
151
+
152
+ ### Gaze
153
+
154
+ `lookAt()` and `updateViewWithMouse()` do not write head angles. They hand the
155
+ target to `IkiMotion.setGazeTarget()`, which holds the idle's gaze on it in
156
+ place of the points the idle picks for itself: the eyes go there on stiff
157
+ springs and the head turns and nods after them, 10° per unit of gaze, on top of
158
+ the idle's drift. A gaze at the edge of the range turns the head about a third
159
+ of its ±30°; the eyes carry the rest.
160
+
161
+ - A one-shot clip replaces the head axes it animates for as long as it runs,
162
+ the gaze follow on them with it: a `Nod` (pitch only) plays at its own keys
163
+ while the head keeps its turn toward the target, and the pitch returns to the
164
+ target over the clip's fade-out. The eyes stay on the target unless the clip
165
+ or an expression drives them, in which case that wins.
166
+ - Under a declared `Idle` group the engine's gaze target moves nothing, since
167
+ the group's clips own the head and gaze. The adapter then writes the eyes at
168
+ the target itself (step 5) and leaves the head to the clips. That write is
169
+ the last one, so on such a model the eyes jump to the target with no spring
170
+ and the host gaze beats an expression or clip that moves the eyes — the
171
+ reverse of a model without the group.
172
+ - The mouth is the expression's until lip-sync is enabled; lip-sync, written
173
+ after `IkiMotion`, overrides it, and disabling lip-sync closes the mouth and
174
+ hands it back to the expression or rest.
175
+
176
+ `updateViewWithMouse()` turns the pointer into that target by its offset from
177
+ a point, in half-canvases: one half-width to the right is `x = 1`. The point is
178
+ the canvas's centre unless `setGazeOrigin()` names one on the model, which the
179
+ adapter places on the canvas through the view the engine draws with (so the
180
+ point is a fraction of the model's own box, not of a wider view framing it). On
181
+ a bust the centre is close enough to the face; on a full body it is the waist,
182
+ so a pointer on her face reads as above it and she looks up until the origin is
183
+ set between her eyes.
184
+
185
+ A gaze persists until the next one, across a reload too: there is no "gaze
186
+ released" signal in the `Renderer` contract (`RenderManager` suspends mouse
187
+ tracking; it never tells the renderer an AI gaze ended), so the adapter never
188
+ calls `clearGazeTarget()`. Before the first gaze the character looks where the
189
+ idle picks.
@@ -0,0 +1,240 @@
1
+ import { IkiPlayer, IkiMotion } from '@ikijs/engine';
2
+ import { loadIkiModel, IkiFormatError, IDLE_MOTION_GROUP, StandardParameter } from '@ikijs/format';
3
+
4
+ // src/iki-renderer.ts
5
+ var MOUTH_GAIN = 1.8;
6
+ var IkiRendererImpl = class {
7
+ canvas;
8
+ player;
9
+ /** Kept so a view set before initialize() reaches the player it creates. */
10
+ view;
11
+ /** The loaded model; set together with `motion` once a load completes. */
12
+ model;
13
+ motion;
14
+ /** Counts loadModel() calls, so one a later call overtook can tell. */
15
+ loadCount = 0;
16
+ motionRafId;
17
+ /** Host gaze, each axis -1..1, y up; unset until the first lookAt / mouse. */
18
+ gaze;
19
+ /** Where the mouse gaze is measured from; unset = canvas centre. */
20
+ gazeOrigin;
21
+ lipSyncEnabled = false;
22
+ mouthOpen = 0;
23
+ constructor(options) {
24
+ this.canvas = options?.canvas;
25
+ }
26
+ async initialize() {
27
+ if (!this.canvas) {
28
+ throw new Error("Canvas element is required for Iki rendering");
29
+ }
30
+ this.player = new IkiPlayer(this.canvas);
31
+ if (this.view) this.player.setView(this.view);
32
+ }
33
+ async loadModel(modelPath) {
34
+ if (!this.player) {
35
+ throw new Error("Iki renderer is not initialized");
36
+ }
37
+ const load = ++this.loadCount;
38
+ const res = await fetch(modelPath);
39
+ if (!res.ok) {
40
+ throw new Error(`Failed to fetch iki model: ${res.status}`);
41
+ }
42
+ const json = await res.text();
43
+ let model;
44
+ try {
45
+ model = loadIkiModel(json);
46
+ } catch (err) {
47
+ throw err instanceof IkiFormatError ? new Error(`Invalid .iki model: ${err.message}`) : err;
48
+ }
49
+ if (!this.player || load !== this.loadCount) return;
50
+ const result = await this.player.load(model);
51
+ if (result.superseded || !this.player) return;
52
+ if (result.failedTextures.length > 0) {
53
+ console.warn("Iki: textures failed to load", result.failedTextures);
54
+ }
55
+ const player = this.player;
56
+ this.model = model;
57
+ this.motion = new IkiMotion(
58
+ model,
59
+ (id) => player.getParameter(id),
60
+ (id, value) => player.setParameter(id, value)
61
+ );
62
+ if (this.gaze) this.motion.setGazeTarget(this.gaze.x, this.gaze.y);
63
+ this.startMotionLoop();
64
+ player.start();
65
+ }
66
+ startMotionLoop() {
67
+ if (this.motionRafId !== void 0) return;
68
+ const tick = (now) => {
69
+ this.motionRafId = requestAnimationFrame(tick);
70
+ this.step(now);
71
+ };
72
+ this.motionRafId = requestAnimationFrame(tick);
73
+ }
74
+ step(now) {
75
+ const { player, motion } = this;
76
+ if (!player || !motion) return;
77
+ motion.update(now);
78
+ const gaze = this.gaze;
79
+ if (gaze && this.model?.motions?.[IDLE_MOTION_GROUP]) {
80
+ player.setParameter(StandardParameter.EyeballX, gaze.x);
81
+ player.setParameter(StandardParameter.EyeballY, gaze.y);
82
+ }
83
+ if (this.lipSyncEnabled) {
84
+ player.setParameter(StandardParameter.MouthOpen, this.mouthOpen);
85
+ }
86
+ }
87
+ setRealtimeLipSync(enabled) {
88
+ this.lipSyncEnabled = enabled;
89
+ if (!enabled) {
90
+ this.mouthOpen = 0;
91
+ this.player?.setParameter(StandardParameter.MouthOpen, 0);
92
+ }
93
+ }
94
+ updateRealtimeLipSyncRms(rms) {
95
+ if (!this.lipSyncEnabled) return;
96
+ this.mouthOpen = clamp01(rms * MOUTH_GAIN);
97
+ }
98
+ lookAt(coords) {
99
+ this.setGaze(coords.x, coords.y);
100
+ }
101
+ updateViewWithMouse(coords) {
102
+ if (!this.canvas) return;
103
+ const rect = this.canvas.getBoundingClientRect();
104
+ const origin = this.gazeOriginOnCanvas(rect);
105
+ const x = (coords.clientX - (rect.left + rect.width / 2 + origin.x)) / (rect.width / 2);
106
+ const y = -(coords.clientY - (rect.top + rect.height / 2 + origin.y)) / (rect.height / 2);
107
+ this.setGaze(x, y);
108
+ }
109
+ /**
110
+ * `MouseTrackable.setGazeOrigin`: measure the mouse gaze from a point of the
111
+ * model's canvas (fractions, top-left to bottom-right) instead of the
112
+ * canvas element's centre. Throws on a non-finite `x` or `y`: kept, it would
113
+ * turn every pointer gaze into one the engine drops, and the mouse would
114
+ * stop moving the eyes with nothing said.
115
+ */
116
+ setGazeOrigin(origin) {
117
+ if (origin && !(Number.isFinite(origin.x) && Number.isFinite(origin.y))) {
118
+ throw new Error(
119
+ `Iki: setGazeOrigin() needs a finite x and y, got ${origin.x}, ${origin.y}`
120
+ );
121
+ }
122
+ this.gazeOrigin = origin;
123
+ }
124
+ /**
125
+ * Where the gaze origin lands, in CSS px from the canvas's centre (y down),
126
+ * by the fit the engine draws with: the view, or the model's box, scaled to
127
+ * the canvas with its aspect kept and centred. The centre itself when no
128
+ * origin is set or no model is loaded to place one on.
129
+ */
130
+ gazeOriginOnCanvas(rect) {
131
+ const origin = this.gazeOrigin;
132
+ const model = this.model?.canvas;
133
+ if (!origin || !model) return { x: 0, y: 0 };
134
+ const x = (origin.x - 0.5) * model.width;
135
+ const y = (0.5 - origin.y) * model.height;
136
+ const view = this.view ?? { x: 0, y: 0, ...model };
137
+ const fit = Math.min(rect.width / view.width, rect.height / view.height);
138
+ return { x: (x - view.x) * fit, y: -(y - view.y) * fit };
139
+ }
140
+ /**
141
+ * The eyes go to the target and the head follows through the engine's idle
142
+ * (`IkiMotion.setGazeTarget`), not through head angles written here: those
143
+ * would stack on the idle's own drift and gaze follow.
144
+ */
145
+ setGaze(x, y) {
146
+ if (!Number.isFinite(x) || !Number.isFinite(y)) return;
147
+ this.gaze = { x: clamp(x, -1, 1), y: clamp(y, -1, 1) };
148
+ this.motion?.setGazeTarget(this.gaze.x, this.gaze.y);
149
+ }
150
+ /**
151
+ * Frame `view` (centre and size in model units, +y up) instead of the
152
+ * model's box, e.g. a full body whose raised arm leaves the box; no argument
153
+ * restores the box. The engine keeps it across loads and throws on a
154
+ * non-finite centre or a non-positive size — before initialize(), that
155
+ * throw comes from initialize().
156
+ */
157
+ setView(view) {
158
+ this.player?.setView(view);
159
+ this.view = view;
160
+ }
161
+ // Required by the MouseTrackable duck-type contract; not a stub to flesh out —
162
+ // Iki has no tap-motion concept.
163
+ handleMouseTap(_coords) {
164
+ }
165
+ playExpression(expressionId) {
166
+ this.motion?.playExpression(expressionId);
167
+ }
168
+ stopExpression() {
169
+ this.motion?.stopExpression();
170
+ }
171
+ // No `muteSound` option: Iki motion clips carry no audio.
172
+ playMotionByGroup(group, index) {
173
+ this.motion?.playMotion(group, index);
174
+ }
175
+ getAvailableExpressions() {
176
+ return this.model?.expressions?.map((expression) => expression.id) ?? [];
177
+ }
178
+ getAvailableMotionGroups() {
179
+ return Object.fromEntries(
180
+ Object.entries(this.model?.motions ?? {}).map(([group, clips]) => [
181
+ group,
182
+ clips.length
183
+ ])
184
+ );
185
+ }
186
+ /** The catalog with the descriptions the `.iki` file declares. */
187
+ getAvatarControlCatalog() {
188
+ const catalog = {
189
+ expressions: this.getAvailableExpressions(),
190
+ motions: this.getAvailableMotionGroups()
191
+ };
192
+ const expressions = this.model?.expressions ?? [];
193
+ if (expressions.length > 0) {
194
+ catalog.expressionDescriptions = Object.fromEntries(
195
+ expressions.map((expression) => [
196
+ expression.id,
197
+ expression.description
198
+ ])
199
+ );
200
+ }
201
+ const motionGroups = Object.entries(this.model?.motions ?? {});
202
+ if (motionGroups.length > 0) {
203
+ catalog.motionDescriptions = Object.fromEntries(
204
+ motionGroups.map(([group, clips]) => [
205
+ group,
206
+ clips.map((clip) => clip.description)
207
+ ])
208
+ );
209
+ }
210
+ return catalog;
211
+ }
212
+ async render(_message, _character) {
213
+ }
214
+ async destroy() {
215
+ if (this.motionRafId !== void 0) {
216
+ cancelAnimationFrame(this.motionRafId);
217
+ this.motionRafId = void 0;
218
+ }
219
+ this.motion = void 0;
220
+ this.model = void 0;
221
+ this.gaze = void 0;
222
+ this.player?.destroy();
223
+ this.player = void 0;
224
+ }
225
+ };
226
+ function clamp(v, min, max) {
227
+ return Math.max(min, Math.min(max, v));
228
+ }
229
+ function clamp01(v) {
230
+ return clamp(v, 0, 1);
231
+ }
232
+
233
+ // src/renderer.ts
234
+ function createIkiRenderer(options) {
235
+ return new IkiRendererImpl(options);
236
+ }
237
+
238
+ export { createIkiRenderer };
239
+ //# sourceMappingURL=renderer.js.map
240
+ //# sourceMappingURL=renderer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/iki-renderer.ts","../src/renderer.ts"],"names":[],"mappings":";;;;AAiCA,IAAM,UAAA,GAAa,GAAA;AAIZ,IAAM,kBAAN,MAAuE;AAAA,EACpE,MAAA;AAAA,EACA,MAAA;AAAA;AAAA,EAEA,IAAA;AAAA;AAAA,EAEA,KAAA;AAAA,EACA,MAAA;AAAA;AAAA,EAEA,SAAA,GAAY,CAAA;AAAA,EACZ,WAAA;AAAA;AAAA,EAEA,IAAA;AAAA;AAAA,EAEA,UAAA;AAAA,EACA,cAAA,GAAiB,KAAA;AAAA,EACjB,SAAA,GAAY,CAAA;AAAA,EAEpB,YAAY,OAAA,EAA8B;AACxC,IAAA,IAAA,CAAK,SAAS,OAAA,EAAS,MAAA;AAAA,EACzB;AAAA,EAEA,MAAM,UAAA,GAA4B;AAChC,IAAA,IAAI,CAAC,KAAK,MAAA,EAAQ;AAChB,MAAA,MAAM,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AAS,CAAA,EAAG;AAC3B,MAAA,OAAA,CAAQ,qBAAqB,MAAA,CAAO,WAAA;AAAA,QAClC,aAAa,GAAA,CAAI,CAAC,CAAC,KAAA,EAAO,KAAK,CAAA,KAAM;AAAA,UACnC,KAAA;AAAA,UACA,KAAA,CAAM,GAAA,CAAI,CAAC,IAAA,KAAS,KAAK,WAAW;AAAA,SACrC;AAAA,OACH;AAAA,IACF;AACA,IAAA,OAAO,OAAA;AAAA,EACT;AAAA,EAEA,MAAM,MAAA,CAAO,QAAA,EAAmB,UAAA,EAAuC;AAAA,EAGvE;AAAA,EAEA,MAAM,OAAA,GAAyB;AAC7B,IAAA,IAAI,IAAA,CAAK,gBAAgB,MAAA,EAAW;AAClC,MAAA,oBAAA,CAAqB,KAAK,WAAW,CAAA;AACrC,MAAA,IAAA,CAAK,WAAA,GAAc,MAAA;AAAA,IACrB;AACA,IAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AACd,IAAA,IAAA,CAAK,KAAA,GAAQ,MAAA;AACb,IAAA,IAAA,CAAK,IAAA,GAAO,MAAA;AACZ,IAAA,IAAA,CAAK,QAAQ,OAAA,EAAQ;AACrB,IAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AAAA,EAChB;AACF,CAAA;AAIA,SAAS,KAAA,CAAM,CAAA,EAAW,GAAA,EAAa,GAAA,EAAqB;AAC1D,EAAA,OAAO,KAAK,GAAA,CAAI,GAAA,EAAK,KAAK,GAAA,CAAI,GAAA,EAAK,CAAC,CAAC,CAAA;AACvC;AAEA,SAAS,QAAQ,CAAA,EAAmB;AAClC,EAAA,OAAO,KAAA,CAAM,CAAA,EAAG,CAAA,EAAG,CAAC,CAAA;AACtB;;;AC9RO,SAAS,kBAAkB,OAAA,EAA2C;AAC3E,EAAA,OAAO,IAAI,gBAAgB,OAAO,CAAA;AACpC","file":"renderer.js","sourcesContent":["/**\n * Drives an `.iki` puppet through the Iki engine. Idle, motion clips,\n * expressions, physics and hair-chain motion come from the engine's own\n * `IkiMotion`; this adapter only schedules it, hands it the host's gaze as its\n * gaze target and writes lip-sync last.\n */\n\nimport type {\n AvatarControlCatalog,\n Character,\n GazeCoordinates,\n Message,\n Renderer,\n} from \"@charivo/core\";\nimport type {\n GazeOrigin,\n MouseCoordinates,\n MouseTrackable,\n} from \"@charivo/render\";\nimport { IkiMotion, IkiPlayer, type IkiView } from \"@ikijs/engine\";\nimport {\n IDLE_MOTION_GROUP,\n IkiFormatError,\n loadIkiModel,\n StandardParameter,\n type IkiModel,\n} from \"@ikijs/format\";\n\nimport type { IkiRenderer, IkiRendererOptions } from \"./renderer\";\n\n// ── Tuning constants ─────────────────────────────────────────────────────────\n\n/** Amplify RMS→mouth-open so quiet speech still opens the mouth. */\nconst MOUTH_GAIN = 1.8;\n\n// ─────────────────────────────────────────────────────────────────────────────\n\nexport class IkiRendererImpl implements Renderer, MouseTrackable, IkiRenderer {\n private canvas?: HTMLCanvasElement;\n private player?: IkiPlayer;\n /** Kept so a view set before initialize() reaches the player it creates. */\n private view?: IkiView;\n /** The loaded model; set together with `motion` once a load completes. */\n private model?: IkiModel;\n private motion?: IkiMotion;\n /** Counts loadModel() calls, so one a later call overtook can tell. */\n private loadCount = 0;\n private motionRafId?: number;\n /** Host gaze, each axis -1..1, y up; unset until the first lookAt / mouse. */\n private gaze?: GazeCoordinates;\n /** Where the mouse gaze is measured from; unset = canvas centre. */\n private gazeOrigin?: GazeOrigin;\n private lipSyncEnabled = false;\n private mouthOpen = 0;\n\n constructor(options?: IkiRendererOptions) {\n this.canvas = options?.canvas;\n }\n\n async initialize(): Promise<void> {\n if (!this.canvas) {\n throw new Error(\"Canvas element is required for Iki rendering\");\n }\n this.player = new IkiPlayer(this.canvas);\n if (this.view) this.player.setView(this.view);\n // Do NOT call start() or begin the motion loop here — wait for loadModel().\n }\n\n async loadModel(modelPath: string): Promise<void> {\n if (!this.player) {\n throw new Error(\"Iki renderer is not initialized\");\n }\n const load = ++this.loadCount;\n\n const res = await fetch(modelPath);\n if (!res.ok) {\n throw new Error(`Failed to fetch iki model: ${res.status}`);\n }\n const json = await res.text();\n\n let model: IkiModel;\n try {\n model = loadIkiModel(json);\n } catch (err) {\n throw err instanceof IkiFormatError\n ? new Error(`Invalid .iki model: ${err.message}`)\n : err;\n }\n\n // destroy() may have run while the fetch above was in flight, or a later\n // loadModel() begun. The engine supersedes only the loads that reach it in\n // order, and this one, its fetch the slower, would reach it last and\n // replace the model asked for after it.\n if (!this.player || load !== this.loadCount) return;\n\n // Must be awaited: `load()` decodes the model's textures before it swaps\n // in the new ParameterStore, so building IkiMotion early would read\n // (and physics would seed from) the empty pre-load store; destroy() may\n // also have run during this await, so guard on `this.player` too.\n const result = await this.player.load(model);\n if (result.superseded || !this.player) return;\n if (result.failedTextures.length > 0) {\n console.warn(\"Iki: textures failed to load\", result.failedTextures);\n }\n\n const player = this.player;\n this.model = model;\n this.motion = new IkiMotion(\n model,\n (id) => player.getParameter(id),\n (id, value) => player.setParameter(id, value),\n );\n if (this.gaze) this.motion.setGazeTarget(this.gaze.x, this.gaze.y);\n\n // Motion first: rAF callbacks fire in request order, so starting the\n // motion loop before the engine's own render loop means the pose is\n // written before that frame is drawn, not one frame late.\n this.startMotionLoop();\n player.start();\n }\n\n private startMotionLoop(): void {\n // Idempotent — do nothing if the loop is already running.\n if (this.motionRafId !== undefined) return;\n\n const tick = (now: number): void => {\n this.motionRafId = requestAnimationFrame(tick);\n this.step(now);\n };\n this.motionRafId = requestAnimationFrame(tick);\n }\n\n private step(now: number): void {\n const { player, motion } = this;\n if (!player || !motion) return;\n\n motion.update(now);\n\n // A model's `Idle` clips replace the procedural head and gaze, and the\n // engine's gaze target moves nothing under them: put the eyes there\n // directly, as the engine asks of a host. The head stays the clips'.\n const gaze = this.gaze;\n if (gaze && this.model?.motions?.[IDLE_MOTION_GROUP]) {\n player.setParameter(StandardParameter.EyeballX, gaze.x);\n player.setParameter(StandardParameter.EyeballY, gaze.y);\n }\n\n // After update(), so lip-sync beats an expression that opens the mouth.\n if (this.lipSyncEnabled) {\n player.setParameter(StandardParameter.MouthOpen, this.mouthOpen);\n }\n }\n\n setRealtimeLipSync(enabled: boolean): void {\n this.lipSyncEnabled = enabled;\n if (!enabled) {\n this.mouthOpen = 0;\n // Close what lip-sync left open; from the next frame on, an expression\n // that drives the mouth owns it again.\n this.player?.setParameter(StandardParameter.MouthOpen, 0);\n }\n }\n\n updateRealtimeLipSyncRms(rms: number): void {\n if (!this.lipSyncEnabled) return;\n this.mouthOpen = clamp01(rms * MOUTH_GAIN);\n }\n\n lookAt(coords: GazeCoordinates): void {\n this.setGaze(coords.x, coords.y);\n }\n\n updateViewWithMouse(coords: MouseCoordinates): void {\n if (!this.canvas) return;\n const rect = this.canvas.getBoundingClientRect();\n const origin = this.gazeOriginOnCanvas(rect);\n // Normalize to [-1, 1] with Y inverted (gaze y is up-positive).\n const x =\n (coords.clientX - (rect.left + rect.width / 2 + origin.x)) /\n (rect.width / 2);\n const y =\n -(coords.clientY - (rect.top + rect.height / 2 + origin.y)) /\n (rect.height / 2);\n this.setGaze(x, y);\n }\n\n /**\n * `MouseTrackable.setGazeOrigin`: measure the mouse gaze from a point of the\n * model's canvas (fractions, top-left to bottom-right) instead of the\n * canvas element's centre. Throws on a non-finite `x` or `y`: kept, it would\n * turn every pointer gaze into one the engine drops, and the mouse would\n * stop moving the eyes with nothing said.\n */\n setGazeOrigin(origin?: GazeOrigin): void {\n if (origin && !(Number.isFinite(origin.x) && Number.isFinite(origin.y))) {\n throw new Error(\n `Iki: setGazeOrigin() needs a finite x and y, got ${origin.x}, ${origin.y}`,\n );\n }\n this.gazeOrigin = origin;\n }\n\n /**\n * Where the gaze origin lands, in CSS px from the canvas's centre (y down),\n * by the fit the engine draws with: the view, or the model's box, scaled to\n * the canvas with its aspect kept and centred. The centre itself when no\n * origin is set or no model is loaded to place one on.\n */\n private gazeOriginOnCanvas(rect: DOMRect): { x: number; y: number } {\n const origin = this.gazeOrigin;\n const model = this.model?.canvas;\n if (!origin || !model) return { x: 0, y: 0 };\n // The fractions, in model units: origin at the box's centre, +y up.\n const x = (origin.x - 0.5) * model.width;\n const y = (0.5 - origin.y) * model.height;\n const view = this.view ?? { x: 0, y: 0, ...model };\n const fit = Math.min(rect.width / view.width, rect.height / view.height);\n return { x: (x - view.x) * fit, y: -(y - view.y) * fit };\n }\n\n /**\n * The eyes go to the target and the head follows through the engine's idle\n * (`IkiMotion.setGazeTarget`), not through head angles written here: those\n * would stack on the idle's own drift and gaze follow.\n */\n private setGaze(x: number, y: number): void {\n // The engine ignores a non-finite pair; keep it out of the Idle-group\n // eye write too.\n if (!Number.isFinite(x) || !Number.isFinite(y)) return;\n this.gaze = { x: clamp(x, -1, 1), y: clamp(y, -1, 1) };\n this.motion?.setGazeTarget(this.gaze.x, this.gaze.y);\n }\n\n /**\n * Frame `view` (centre and size in model units, +y up) instead of the\n * model's box, e.g. a full body whose raised arm leaves the box; no argument\n * restores the box. The engine keeps it across loads and throws on a\n * non-finite centre or a non-positive size — before initialize(), that\n * throw comes from initialize().\n */\n setView(view?: IkiView): void {\n this.player?.setView(view);\n this.view = view;\n }\n\n // Required by the MouseTrackable duck-type contract; not a stub to flesh out —\n // Iki has no tap-motion concept.\n handleMouseTap(_coords: MouseCoordinates): void {}\n\n playExpression(expressionId: string): void {\n this.motion?.playExpression(expressionId);\n }\n\n stopExpression(): void {\n this.motion?.stopExpression();\n }\n\n // No `muteSound` option: Iki motion clips carry no audio.\n playMotionByGroup(group: string, index: number): void {\n this.motion?.playMotion(group, index);\n }\n\n getAvailableExpressions(): string[] {\n return this.model?.expressions?.map((expression) => expression.id) ?? [];\n }\n\n getAvailableMotionGroups(): Record<string, number> {\n return Object.fromEntries(\n Object.entries(this.model?.motions ?? {}).map(([group, clips]) => [\n group,\n clips.length,\n ]),\n );\n }\n\n /** The catalog with the descriptions the `.iki` file declares. */\n getAvatarControlCatalog(): AvatarControlCatalog {\n const catalog: AvatarControlCatalog = {\n expressions: this.getAvailableExpressions(),\n motions: this.getAvailableMotionGroups(),\n };\n const expressions = this.model?.expressions ?? [];\n if (expressions.length > 0) {\n catalog.expressionDescriptions = Object.fromEntries(\n expressions.map((expression) => [\n expression.id,\n expression.description,\n ]),\n );\n }\n const motionGroups = Object.entries(this.model?.motions ?? {});\n if (motionGroups.length > 0) {\n catalog.motionDescriptions = Object.fromEntries(\n motionGroups.map(([group, clips]) => [\n group,\n clips.map((clip) => clip.description),\n ]),\n );\n }\n return catalog;\n }\n\n async render(_message: Message, _character?: Character): Promise<void> {\n // Stateless — the engine draws on its own loop; this adapter's rAF only\n // steps IkiMotion.\n }\n\n async destroy(): Promise<void> {\n if (this.motionRafId !== undefined) {\n cancelAnimationFrame(this.motionRafId);\n this.motionRafId = undefined;\n }\n this.motion = undefined;\n this.model = undefined;\n this.gaze = undefined;\n this.player?.destroy();\n this.player = undefined;\n }\n}\n\n// ── Local helpers ─────────────────────────────────────────────────────────────\n\nfunction clamp(v: number, min: number, max: number): number {\n return Math.max(min, Math.min(max, v));\n}\n\nfunction clamp01(v: number): number {\n return clamp(v, 0, 1);\n}\n","import type {\n AvatarControlCatalog,\n GazeCoordinates,\n Renderer,\n} from \"@charivo/core\";\nimport type { GazeOrigin, MouseTrackable } from \"@charivo/render\";\nimport type { IkiView } from \"@ikijs/engine\";\n\nimport { IkiRendererImpl } from \"./iki-renderer\";\n\nexport type { IkiView };\n\nexport interface IkiRendererOptions {\n canvas?: HTMLCanvasElement;\n}\n\nexport interface IkiRenderer extends Renderer, MouseTrackable {\n loadModel(modelPath: string): Promise<void>;\n playExpression(expressionId: string): void;\n stopExpression(): void;\n /** `options.muteSound` is accepted for the `Renderer` contract and ignored: Iki motion clips carry no audio. */\n playMotionByGroup(\n group: string,\n index: number,\n options?: { muteSound?: boolean },\n ): void;\n lookAt(coords: GazeCoordinates): void;\n setGazeOrigin(origin?: GazeOrigin): void;\n getAvailableExpressions(): string[];\n getAvailableMotionGroups(): Record<string, number>;\n /** The catalog with the descriptions the `.iki` file declares. */\n getAvatarControlCatalog(): AvatarControlCatalog;\n /**\n * Frame `view` (centre and size in model units, +y up) instead of the\n * model's box, e.g. a full body whose raised arm leaves the box; no argument\n * restores the box. Kept across loads. Throws on a non-finite centre or a\n * non-positive size — before `initialize()`, that throw comes from\n * `initialize()`.\n */\n setView(view?: IkiView): void;\n}\n\nexport function createIkiRenderer(options?: IkiRendererOptions): IkiRenderer {\n return new IkiRendererImpl(options);\n}\n"]}
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Drives an `.iki` puppet through the Iki engine. Idle, motion clips,
3
+ * expressions, physics and hair-chain motion come from the engine's own
4
+ * `IkiMotion`; this adapter only schedules it, hands it the host's gaze as its
5
+ * gaze target and writes lip-sync last.
6
+ */
7
+ import type { AvatarControlCatalog, Character, GazeCoordinates, Message, Renderer } from "@charivo/core";
8
+ import type { GazeOrigin, MouseCoordinates, MouseTrackable } from "@charivo/render";
9
+ import { type IkiView } from "@ikijs/engine";
10
+ import type { IkiRenderer, IkiRendererOptions } from "./renderer";
11
+ export declare class IkiRendererImpl implements Renderer, MouseTrackable, IkiRenderer {
12
+ private canvas?;
13
+ private player?;
14
+ /** Kept so a view set before initialize() reaches the player it creates. */
15
+ private view?;
16
+ /** The loaded model; set together with `motion` once a load completes. */
17
+ private model?;
18
+ private motion?;
19
+ /** Counts loadModel() calls, so one a later call overtook can tell. */
20
+ private loadCount;
21
+ private motionRafId?;
22
+ /** Host gaze, each axis -1..1, y up; unset until the first lookAt / mouse. */
23
+ private gaze?;
24
+ /** Where the mouse gaze is measured from; unset = canvas centre. */
25
+ private gazeOrigin?;
26
+ private lipSyncEnabled;
27
+ private mouthOpen;
28
+ constructor(options?: IkiRendererOptions);
29
+ initialize(): Promise<void>;
30
+ loadModel(modelPath: string): Promise<void>;
31
+ private startMotionLoop;
32
+ private step;
33
+ setRealtimeLipSync(enabled: boolean): void;
34
+ updateRealtimeLipSyncRms(rms: number): void;
35
+ lookAt(coords: GazeCoordinates): void;
36
+ updateViewWithMouse(coords: MouseCoordinates): void;
37
+ /**
38
+ * `MouseTrackable.setGazeOrigin`: measure the mouse gaze from a point of the
39
+ * model's canvas (fractions, top-left to bottom-right) instead of the
40
+ * canvas element's centre. Throws on a non-finite `x` or `y`: kept, it would
41
+ * turn every pointer gaze into one the engine drops, and the mouse would
42
+ * stop moving the eyes with nothing said.
43
+ */
44
+ setGazeOrigin(origin?: GazeOrigin): void;
45
+ /**
46
+ * Where the gaze origin lands, in CSS px from the canvas's centre (y down),
47
+ * by the fit the engine draws with: the view, or the model's box, scaled to
48
+ * the canvas with its aspect kept and centred. The centre itself when no
49
+ * origin is set or no model is loaded to place one on.
50
+ */
51
+ private gazeOriginOnCanvas;
52
+ /**
53
+ * The eyes go to the target and the head follows through the engine's idle
54
+ * (`IkiMotion.setGazeTarget`), not through head angles written here: those
55
+ * would stack on the idle's own drift and gaze follow.
56
+ */
57
+ private setGaze;
58
+ /**
59
+ * Frame `view` (centre and size in model units, +y up) instead of the
60
+ * model's box, e.g. a full body whose raised arm leaves the box; no argument
61
+ * restores the box. The engine keeps it across loads and throws on a
62
+ * non-finite centre or a non-positive size — before initialize(), that
63
+ * throw comes from initialize().
64
+ */
65
+ setView(view?: IkiView): void;
66
+ handleMouseTap(_coords: MouseCoordinates): void;
67
+ playExpression(expressionId: string): void;
68
+ stopExpression(): void;
69
+ playMotionByGroup(group: string, index: number): void;
70
+ getAvailableExpressions(): string[];
71
+ getAvailableMotionGroups(): Record<string, number>;
72
+ /** The catalog with the descriptions the `.iki` file declares. */
73
+ getAvatarControlCatalog(): AvatarControlCatalog;
74
+ render(_message: Message, _character?: Character): Promise<void>;
75
+ destroy(): Promise<void>;
76
+ }
77
+ //# sourceMappingURL=iki-renderer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"iki-renderer.d.ts","sourceRoot":"","sources":["../../src/iki-renderer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,oBAAoB,EACpB,SAAS,EACT,eAAe,EAC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,GAAG,IAAI;IASxC;;;;;OAKG;IACH,OAAO,CAAC,kBAAkB;IAY1B;;;;OAIG;IACH,OAAO,CAAC,OAAO;IAQf;;;;;;OAMG;IACH,OAAO,CAAC,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI;IAO7B,cAAc,CAAC,OAAO,EAAE,gBAAgB,GAAG,IAAI;IAE/C,cAAc,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI;IAI1C,cAAc,IAAI,IAAI;IAKtB,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAIrD,uBAAuB,IAAI,MAAM,EAAE;IAInC,wBAAwB,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IASlD,kEAAkE;IAClE,uBAAuB,IAAI,oBAAoB;IA0BzC,MAAM,CAAC,QAAQ,EAAE,OAAO,EAAE,UAAU,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;IAKhE,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAW/B"}
@@ -0,0 +1,32 @@
1
+ import type { AvatarControlCatalog, GazeCoordinates, Renderer } from "@charivo/core";
2
+ import type { GazeOrigin, MouseTrackable } from "@charivo/render";
3
+ import type { IkiView } from "@ikijs/engine";
4
+ export type { IkiView };
5
+ export interface IkiRendererOptions {
6
+ canvas?: HTMLCanvasElement;
7
+ }
8
+ export interface IkiRenderer extends Renderer, MouseTrackable {
9
+ loadModel(modelPath: string): Promise<void>;
10
+ playExpression(expressionId: string): void;
11
+ stopExpression(): void;
12
+ /** `options.muteSound` is accepted for the `Renderer` contract and ignored: Iki motion clips carry no audio. */
13
+ playMotionByGroup(group: string, index: number, options?: {
14
+ muteSound?: boolean;
15
+ }): void;
16
+ lookAt(coords: GazeCoordinates): void;
17
+ setGazeOrigin(origin?: GazeOrigin): void;
18
+ getAvailableExpressions(): string[];
19
+ getAvailableMotionGroups(): Record<string, number>;
20
+ /** The catalog with the descriptions the `.iki` file declares. */
21
+ getAvatarControlCatalog(): AvatarControlCatalog;
22
+ /**
23
+ * Frame `view` (centre and size in model units, +y up) instead of the
24
+ * model's box, e.g. a full body whose raised arm leaves the box; no argument
25
+ * restores the box. Kept across loads. Throws on a non-finite centre or a
26
+ * non-positive size — before `initialize()`, that throw comes from
27
+ * `initialize()`.
28
+ */
29
+ setView(view?: IkiView): void;
30
+ }
31
+ export declare function createIkiRenderer(options?: IkiRendererOptions): IkiRenderer;
32
+ //# sourceMappingURL=renderer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"renderer.d.ts","sourceRoot":"","sources":["../../src/renderer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oBAAoB,EACpB,eAAe,EACf,QAAQ,EACT,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAClE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAI7C,YAAY,EAAE,OAAO,EAAE,CAAC;AAExB,MAAM,WAAW,kBAAkB;IACjC,MAAM,CAAC,EAAE,iBAAiB,CAAC;CAC5B;AAED,MAAM,WAAW,WAAY,SAAQ,QAAQ,EAAE,cAAc;IAC3D,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5C,cAAc,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3C,cAAc,IAAI,IAAI,CAAC;IACvB,gHAAgH;IAChH,iBAAiB,CACf,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,OAAO,CAAA;KAAE,GAChC,IAAI,CAAC;IACR,MAAM,CAAC,MAAM,EAAE,eAAe,GAAG,IAAI,CAAC;IACtC,aAAa,CAAC,MAAM,CAAC,EAAE,UAAU,GAAG,IAAI,CAAC;IACzC,uBAAuB,IAAI,MAAM,EAAE,CAAC;IACpC,wBAAwB,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACnD,kEAAkE;IAClE,uBAAuB,IAAI,oBAAoB,CAAC;IAChD;;;;;;OAMG;IACH,OAAO,CAAC,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;CAC/B;AAED,wBAAgB,iBAAiB,CAAC,OAAO,CAAC,EAAE,kBAAkB,GAAG,WAAW,CAE3E"}
package/package.json CHANGED
@@ -1,6 +1,59 @@
1
1
  {
2
2
  "name": "@charivo/render-iki",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.0",
4
+ "description": "Iki puppet renderer for Charivo",
5
+ "type": "module",
6
+ "main": "dist/renderer.js",
7
+ "module": "dist/renderer.js",
8
+ "types": "dist/src/renderer.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/src/renderer.d.ts",
12
+ "import": "./dist/renderer.js",
13
+ "default": "./dist/renderer.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "!dist/metafile-*.json"
19
+ ],
20
+ "keywords": [
21
+ "charivo",
22
+ "iki",
23
+ "avatar",
24
+ "webgl",
25
+ "lipsync"
26
+ ],
27
+ "dependencies": {
28
+ "@charivo/core": "^0.38.0",
29
+ "@charivo/render": "^0.12.1",
30
+ "@ikijs/engine": "^0.12.1",
31
+ "@ikijs/format": "^0.11.1"
32
+ },
33
+ "devDependencies": {
34
+ "tsup": "^8.5.0",
35
+ "typescript": "^5.9.2"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "license": "MIT",
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "https://github.com/zeikar/charivo.git",
44
+ "directory": "packages/render-iki"
45
+ },
46
+ "author": {
47
+ "name": "Zeikar",
48
+ "url": "https://github.com/zeikar"
49
+ },
50
+ "homepage": "https://github.com/zeikar/charivo#readme",
51
+ "bugs": {
52
+ "url": "https://github.com/zeikar/charivo/issues"
53
+ },
54
+ "scripts": {
55
+ "build": "tsup && tsc -p tsconfig.dts.json --emitDeclarationOnly --declarationDir dist --pretty false",
56
+ "dev": "tsup --watch",
57
+ "typecheck": "tsc --noEmit"
58
+ }
6
59
  }