katachi-runtime 0.3.0 → 0.6.1

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/loader.d.ts CHANGED
@@ -5,12 +5,20 @@ export type TextureRole = "color" | "data";
5
5
  export declare function fetchBytes(url: string): Promise<ArrayBuffer>;
6
6
  export declare class TextureLibrary {
7
7
  private readonly baseUrl;
8
+ private readonly fetch;
8
9
  private readonly loaded;
9
- constructor(baseUrl: string);
10
+ /** `fetch` reads a file by its path, so a loader can share what it has already downloaded. */
11
+ constructor(baseUrl: string, fetch?: (path: string) => Promise<ArrayBuffer>);
10
12
  load(path: string, role: TextureRole): Promise<THREE.Texture>;
11
13
  clear(): void;
12
14
  private fetchTexture;
13
15
  }
16
+ /** A texture's texels on the CPU: RGBA bytes, rows from the top. */
17
+ export interface Pixels {
18
+ width: number;
19
+ height: number;
20
+ data: Uint8Array;
21
+ }
14
22
  export interface LoadedModel {
15
23
  scene: THREE.Group;
16
24
  materials: THREE.MeshPhysicalMaterial[];
@@ -20,9 +28,26 @@ export declare class ModelLoader {
20
28
  private readonly gltf;
21
29
  private readonly library;
22
30
  private readonly pieces;
31
+ private readonly averages;
32
+ private readonly decoded;
33
+ private readonly files;
23
34
  private readonly sharedPaints;
24
35
  constructor(baseUrl: string);
25
36
  texture(path: string): Promise<THREE.Texture>;
37
+ /** A texture whose channels are data rather than colour, such as a hair template. */
38
+ dataTexture(path: string): Promise<THREE.Texture>;
39
+ /**
40
+ * A file's bytes, unzipped: one download for every reader of the same texture (the GPU copy, its average colour, its
41
+ * texels for overlays). The last few are kept.
42
+ */
43
+ bytes(path: string): Promise<ArrayBuffer>;
44
+ /**
45
+ * A texture's texels as RGBA bytes, from its largest level no bigger than `size`: for work on the CPU, such as
46
+ * compositing a face's overlays. The last few are kept.
47
+ */
48
+ pixels(path: string, size?: number): Promise<Pixels>;
49
+ /** The typical colour of a texture's opaque texels, in linear light, leaving out the darkest and brightest quarter. */
50
+ averageColor(path: string): Promise<THREE.Color>;
26
51
  json<T>(path: string): Promise<T>;
27
52
  load(path: string, paints?: Paints, diffuse?: string): Promise<LoadedModel>;
28
53
  clear(): void;
package/material.d.ts CHANGED
@@ -15,5 +15,17 @@ export declare function createPaints(): Paints;
15
15
  export interface SecondMap {
16
16
  value: THREE.Texture | null;
17
17
  }
18
+ type Uniforms = Record<string, {
19
+ value: unknown;
20
+ }>;
21
+ /** Whether a converted material is hair, which is drawn in two passes: see `fringeMaterial`. */
22
+ export declare function isHair(material: THREE.Material): boolean;
23
+ /**
24
+ * The second pass of a cut-out material, as the game draws hair: the texels the cutout leaves out, blended over
25
+ * everything solid. Strands end softly instead of in a hard, flickering edge, and the canvas stays opaque behind them.
26
+ */
27
+ export declare function fringeMaterial(base: THREE.MeshPhysicalMaterial): THREE.MeshPhysicalMaterial;
28
+ /** A copy of a converted material for one model, with more shader features switched on and their uniforms. */
29
+ export declare function deriveMaterial(base: THREE.MeshPhysicalMaterial, defines: Record<string, string>, uniforms: Uniforms, map?: THREE.Texture | null): THREE.MeshPhysicalMaterial;
18
30
  export declare function buildMaterial(extras: MaterialExtras, geometry: THREE.BufferGeometry, paints: Paints, library: TextureLibrary): Promise<THREE.MeshPhysicalMaterial>;
19
31
  export {};
package/model.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import * as THREE from "three";
2
+ import { type PlayOptions, type Playback } from "./animation";
3
+ import type { Clip } from "./clip";
2
4
  import type { LoadedModel } from "./loader";
3
5
  export declare function disposeMesh(object: THREE.Object3D): void;
4
6
  export declare function prepareMesh(mesh: THREE.Mesh): void;
@@ -6,6 +8,7 @@ export type RestPose = Map<THREE.Object3D, {
6
8
  world: THREE.Matrix4;
7
9
  }>;
8
10
  export declare function attachModel(model: LoadedModel, bones: Map<string, THREE.Object3D>, root: THREE.Object3D, anchor: THREE.Object3D, rest?: RestPose): THREE.Mesh[];
11
+ /** Takes attached pieces off, freeing what was made for this model alone: blended geometry and derived materials. */
9
12
  export declare function detach(objects: Iterable<THREE.Object3D>): void;
10
13
  /** Any converted 3D model. */
11
14
  export declare class Model {
@@ -17,10 +20,25 @@ export declare class Model {
17
20
  readonly bones: Map<string, THREE.Object3D<THREE.Object3DEventMap>>;
18
21
  /** The direction, from the model's center, that it is best viewed from. */
19
22
  readonly view: readonly [number, number, number];
23
+ private animator;
20
24
  /** Wraps a model returned by `ModelLoader.load`. */
21
25
  constructor(name: string, source: LoadedModel);
22
26
  /** Moves the model to stand centered on the origin, and returns its bounds there. */
23
27
  placeAtOrigin(): THREE.Box3;
28
+ /** Plays a clip on the model's bones, blending in from the current pose. Bones the clip does not move keep their rest pose. */
29
+ play(clip: Clip, options?: PlayOptions): Playback;
30
+ /** Stops the clip, blending back to the rest pose over `blend` seconds. */
31
+ stop({ blend }?: {
32
+ blend?: number;
33
+ }): void;
34
+ /** The clip playing, or null. */
35
+ get playback(): Playback | null;
36
+ /** Moves the model along the playing clip's mover track, as the game moves the ped, or puts it back where it stood. */
37
+ setRootMotion(on: boolean): void;
38
+ /** Advances the playing clip by `seconds` and shows its pose. A `Stage` calls this every frame. */
39
+ update(seconds: number): void;
40
+ /** Shows the playing clip's pose after changing its time while paused. */
41
+ refresh(): void;
24
42
  /** Frees the model's geometry and materials. */
25
43
  dispose(): void;
26
44
  }
package/overlays.d.ts ADDED
@@ -0,0 +1,49 @@
1
+ import * as THREE from "three";
2
+ import type { Pixels } from "./loader";
3
+ /**
4
+ * The game's head overlays, in the order of their numbers (SetPedHeadOverlay's first argument), and the manifest's
5
+ * name for each, which is `facial_overlays.meta`'s. Chest hair and the two body blemish sets cover the torso.
6
+ */
7
+ export declare const OVERLAYS: readonly [readonly ["blemishes", "blemishes"], readonly ["facialHair", "facialHair"], readonly ["eyebrows", "eyebrow"], readonly ["ageing", "aging"], readonly ["makeup", "makeup"], readonly ["blush", "blusher"], readonly ["complexion", "damage"], readonly ["sunDamage", "baseDetail"], readonly ["lipstick", "skinDetail1"], readonly ["freckles", "skinDetail2"], readonly ["chestHair", "bodyOverlay1"], readonly ["bodyBlemishes", "bodyOverlay2"], readonly ["moreBodyBlemishes", "bodyOverlay3"]];
8
+ export type OverlayName = (typeof OVERLAYS)[number][0];
9
+ /** Overlays the game colours with a hair colour (eyebrows, beards, chest hair) or a makeup colour (blush, lipstick). */
10
+ export declare const TINTED: ReadonlySet<OverlayName>;
11
+ /** Overlays drawn on the torso rather than the face. */
12
+ export declare const BODY_OVERLAYS: ReadonlySet<OverlayName>;
13
+ /** One overlay worn: its style number in the game's list, how strong it shows (0 to 1), and for tinted overlays a colour. */
14
+ export interface Overlay {
15
+ style: number;
16
+ opacity?: number;
17
+ color?: string;
18
+ }
19
+ /** A decoded overlay texture, ready to composite: decals carry colour and coverage, details lighten and darken. */
20
+ export interface OverlayLayer {
21
+ pixels: Pixels;
22
+ opacity: number;
23
+ /** Linear colour the texture's covered texels average to, or null to keep the texture's own colours. */
24
+ tint: THREE.Color | null;
25
+ }
26
+ /**
27
+ * The overlays of one surface (the face, or the torso) composited into two textures the skin shader reads in its UVs:
28
+ * the skin is multiplied by the first (stored halved) and the second is added (stored as its square root, so dark
29
+ * hair keeps its shades). Neither holds anything until `compose` runs, so a surface without overlays looks as before.
30
+ */
31
+ export declare class OverlayCanvas {
32
+ readonly uniforms: {
33
+ kOverlayMultiply: {
34
+ value: THREE.Texture;
35
+ };
36
+ kOverlayAdd: {
37
+ value: THREE.Texture;
38
+ };
39
+ };
40
+ private readonly multiply;
41
+ private readonly add;
42
+ private textures;
43
+ /** Draws these layers, bottom first, replacing what was drawn. */
44
+ compose(layers: OverlayLayer[]): void;
45
+ dispose(): void;
46
+ private draw;
47
+ }
48
+ /** Which overlays draw on a surface, bottom first. */
49
+ export declare function overlayOrder(body: boolean): number[];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "katachi-runtime",
3
- "version": "0.3.0",
4
- "description": "Show converted GTA V models in the browser with three.js.",
3
+ "version": "0.6.1",
4
+ "description": "Show converted GTA V models and play their animations in the browser with three.js.",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  "types": "./index.d.ts",
package/stage.d.ts CHANGED
@@ -1,8 +1,14 @@
1
1
  import * as THREE from "three";
2
+ import type { Look } from "./character";
2
3
  import type { Assets } from "./assets";
3
4
  import type { Model } from "./model";
4
5
  import type { VehicleProps } from "./vehicle";
5
6
  export type Direction = readonly [number, number, number];
7
+ /** A part of the model to frame: its middle in the scene and how far it reaches. */
8
+ export interface FocusSphere {
9
+ center: THREE.Vector3;
10
+ radius: number;
11
+ }
6
12
  export type StageLighting = "studio" | "night";
7
13
  export interface StageInset {
8
14
  top?: number;
@@ -35,12 +41,25 @@ export declare class Stage {
35
41
  private night;
36
42
  private flight;
37
43
  private fade;
44
+ private lastFrame;
45
+ private readonly followed;
46
+ /** Where the model stood when it was framed: the whole-model views look at its middle, moved as far as it has since. */
47
+ private readonly framedAt;
38
48
  constructor(canvas: HTMLCanvasElement, options?: StageOptions);
39
49
  /** Loads a model and shows it in place of the current one. Resolves to `undefined` if another model was shown meanwhile. */
40
- show(assets: Assets, name: string, props?: VehicleProps): Promise<Model | undefined>;
50
+ /** Loads and shows a model: a vehicle with `props`, a character straight in `look`. */
51
+ show(assets: Assets, name: string, props?: VehicleProps, look?: Look): Promise<Model | undefined>;
41
52
  view(direction?: Direction, { duration }?: {
42
53
  duration?: number;
43
54
  }): Promise<boolean>;
55
+ /**
56
+ * Flies the camera to frame a sphere around part of the model, such as a face or a hand (`Character.part`), from a
57
+ * direction, or from where the camera looks now. `view` goes back to the whole model.
58
+ */
59
+ focus(sphere: FocusSphere, { direction, duration }?: {
60
+ direction?: Direction;
61
+ duration?: number;
62
+ }): Promise<boolean>;
44
63
  setAutoRotate(on: boolean): void;
45
64
  setLighting(lighting: StageLighting, { duration }?: {
46
65
  duration?: number;
@@ -51,7 +70,11 @@ export declare class Stage {
51
70
  dispose(): void;
52
71
  private readonly yieldToUser;
53
72
  private advance;
73
+ /** Plays the model's clip, and follows the model with the camera, light and ground when the clip moves it. */
74
+ private animate;
54
75
  private fly;
76
+ /** The middle of the model where it stands now, which the whole-model views look at. */
77
+ private middle;
55
78
  private enter;
56
79
  private offset;
57
80
  private frame;