ugly-game 0.15.40 → 0.15.42

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.
@@ -1,4 +1,5 @@
1
1
  import type { CrowdBucket, GpuCrowdRenderer } from './assets/gpuCrowd.js';
2
+ import type { GpuSkinRenderer, SkinCreature } from './assets/gpuSkin.js';
2
3
  export declare const GPU_CHARACTER_FACE_CHANNELS = 47;
3
4
  export declare const GPU_CHARACTER_VISIBLE = 1;
4
5
  export interface GpuCharacterTiers {
@@ -15,15 +16,28 @@ export interface GpuCharactersOptions {
15
16
  maxCharacters: number;
16
17
  boneCount: number;
17
18
  faceChannels?: number;
19
+ /** Fixed socket rows packed per actor in each hot frame. Defaults to zero. */
20
+ socketCount?: number;
21
+ }
22
+ export interface GpuCharacterSocketAttachment {
23
+ /** Stable authored asset id; negative ids mean no attachment. */
24
+ asset: number;
25
+ /** Row in `GpuCharacterFrame.socketTransforms`. */
26
+ socket: number;
18
27
  }
19
28
  export interface GpuCharacterIdentity {
20
29
  revision: number;
21
30
  morphWeights: Float32Array;
31
+ /** Optional for compatibility with single-kit renderers where the body is implicit. */
32
+ bodyAsset?: number;
33
+ /** Defaults to bodyAsset when the face is part of the body kit. */
34
+ faceAsset?: number;
22
35
  material: number;
23
36
  skinLayer: number;
24
37
  eyeLayer: number;
25
38
  hairAsset: number;
26
39
  garmentAssets: readonly number[];
40
+ socketAttachments?: readonly GpuCharacterSocketAttachment[];
27
41
  }
28
42
  export interface GpuCharacterIdentityUpdate extends GpuCharacterIdentity {
29
43
  slot: number;
@@ -37,6 +51,8 @@ export interface GpuCharacterFrame {
37
51
  palettes: Float32Array;
38
52
  /** count × faceChannels. */
39
53
  faceWeights: Float32Array;
54
+ /** count × socketCount × 16 world matrices. */
55
+ socketTransforms?: Float32Array;
40
56
  /** count × xyzr, in world space. */
41
57
  bounds: Float32Array;
42
58
  /** count packed visibility/tier flags. */
@@ -65,9 +81,146 @@ export interface GpuCharactersBackend {
65
81
  export interface GpuCrowdCharacterBinding {
66
82
  poolSlots: readonly number[];
67
83
  facePoolSlots?: readonly (number | null)[];
84
+ /** Caller-owned draw/allocation handles, selected by the corresponding hot tier. */
85
+ hair?: GpuCrowdCharacterPartBinding;
86
+ /** One binding per identity.garmentAssets entry, in the same order. */
87
+ garments?: readonly (GpuCrowdCharacterPartBinding | null)[];
88
+ /** One binding per identity.socketAttachments entry, in the same order. */
89
+ socketAttachments?: readonly (GpuCrowdCharacterPartBinding | null)[];
68
90
  tint?: Float32Array;
69
91
  layer?: number;
70
92
  }
93
+ /** A retained caller-owned part allocation. The numeric slots are opaque to this adapter. */
94
+ export interface GpuCrowdCharacterPartBinding {
95
+ asset: number;
96
+ lodSlots: readonly (number | null)[];
97
+ }
98
+ export declare const GPU_CHARACTER_PART_KIND: {
99
+ readonly body: 0;
100
+ readonly face: 1;
101
+ readonly hair: 2;
102
+ readonly garment: 3;
103
+ readonly socketAttachment: 4;
104
+ };
105
+ export type GpuCharacterPartKind = (typeof GPU_CHARACTER_PART_KIND)[keyof typeof GPU_CHARACTER_PART_KIND];
106
+ /**
107
+ * Packed same-frame handoff for caller-owned part passes. Body and face rows are included so
108
+ * model/palette/LOD agreement across every composed part is directly observable and testable.
109
+ */
110
+ export interface GpuCrowdCharacterCompositionFrame {
111
+ count: number;
112
+ actorSlots: Uint32Array;
113
+ kinds: Uint8Array;
114
+ assets: Int32Array;
115
+ bindingSlots: Uint32Array;
116
+ lods: Uint8Array;
117
+ tiers: Uint8Array;
118
+ /** Animation sampling tier selected for the actor in this same hot frame. */
119
+ animationTiers: Uint8Array;
120
+ paletteRows: Uint32Array;
121
+ /** -1 for actor-root parts; otherwise the identity's canonical socket row. */
122
+ socketIndices: Int32Array;
123
+ /** count × 16 world matrices. */
124
+ transforms: Float32Array;
125
+ /** count × xyzr world bounds. */
126
+ bounds: Float32Array;
127
+ }
128
+ export interface GpuCrowdCharacterCompositionBackend {
129
+ submitComposition(frame: GpuCrowdCharacterCompositionFrame, resources: GpuCrowdCharacterCompositionResources): void;
130
+ }
131
+ /** Shared skin data referenced by `paletteRows`; passed by reference, never copied. */
132
+ export interface GpuCrowdCharacterCompositionResources {
133
+ palettes: Float32Array;
134
+ boneCount: number;
135
+ }
136
+ /**
137
+ * One directly drawable instanced part batch. Rows are grouped by kind, authored asset and LOD;
138
+ * models and palettes are packed instance-major for `GpuSkin` or an equivalent caller renderer.
139
+ * `bindingSlots` remain available to a pooled renderer and are deliberately opaque here.
140
+ */
141
+ export interface GpuCrowdCharacterPartDrawBatch {
142
+ kind: GpuCharacterPartKind;
143
+ asset: number;
144
+ lod: number;
145
+ count: number;
146
+ actorSlots: Uint32Array;
147
+ bindingSlots: Uint32Array;
148
+ socketIndices: Int32Array;
149
+ models: Float32Array;
150
+ palettes: Float32Array;
151
+ bounds: Float32Array;
152
+ }
153
+ /** The game owns the actual render pass; this receives executable, asset-grouped draw inputs. */
154
+ export interface GpuCrowdCharacterPartDrawBackend {
155
+ submitPartDraws(batches: readonly GpuCrowdCharacterPartDrawBatch[]): void;
156
+ }
157
+ export interface GpuCrowdCharacterPartDispatcherOptions {
158
+ backend: GpuCrowdCharacterPartDrawBackend;
159
+ /** Body and face already use `GpuCrowdRenderer`; include them only for a replacement renderer. */
160
+ includeBodyAndFace?: boolean;
161
+ }
162
+ /**
163
+ * Converts the unified composition table into concrete instanced draw submissions. This is the
164
+ * generic boundary the engine can provide without inventing a mesh format: the caller maps each
165
+ * authored `asset` to its own GPU renderer and encodes those prepared batches in its scene pass.
166
+ */
167
+ export declare class GpuCrowdCharacterPartDispatcher implements GpuCrowdCharacterCompositionBackend {
168
+ private readonly backend;
169
+ private readonly includeBodyAndFace;
170
+ constructor(options: GpuCrowdCharacterPartDispatcherOptions);
171
+ submitComposition(frame: GpuCrowdCharacterCompositionFrame, resources: GpuCrowdCharacterCompositionResources): void;
172
+ }
173
+ export declare function createGpuCrowdCharacterPartDispatcher(options: GpuCrowdCharacterPartDispatcherOptions): GpuCrowdCharacterPartDispatcher;
174
+ /** Immutable mesh/material fields for one authored part; pose and placement come from the batch. */
175
+ export type GpuSkinCharacterPartAsset = Omit<SkinCreature, 'palette' | 'model' | 'instances'>;
176
+ export interface GpuSkinCharacterPartDrawBackendOptions {
177
+ renderer: Pick<GpuSkinRenderer, 'setCreatures'>;
178
+ resolveAsset: (kind: GpuCharacterPartKind, asset: number, lod: number) => GpuSkinCharacterPartAsset | null;
179
+ }
180
+ /**
181
+ * Executable adapter for the engine's existing skinned-mesh renderer. After submission, the game's
182
+ * normal `GpuSkinRenderer.frame(...)` call deforms and draws every grouped part. Asset GPU resources
183
+ * remain caller-owned; a body-conformed garment must resolve to its conformed mesh (or use a pooled
184
+ * backend) because this adapter intentionally shares one immutable mesh per asset/LOD batch.
185
+ */
186
+ export declare class GpuSkinCharacterPartDrawBackend implements GpuCrowdCharacterPartDrawBackend {
187
+ private readonly renderer;
188
+ private readonly resolveAsset;
189
+ constructor(options: GpuSkinCharacterPartDrawBackendOptions);
190
+ submitPartDraws(batches: readonly GpuCrowdCharacterPartDrawBatch[]): void;
191
+ }
192
+ export declare function createGpuSkinCharacterPartDrawBackend(options: GpuSkinCharacterPartDrawBackendOptions): GpuSkinCharacterPartDrawBackend;
193
+ export interface GpuCrowdCharacterPartAllocation {
194
+ actorSlot: number;
195
+ revision: number;
196
+ kind: GpuCharacterPartKind;
197
+ asset: number;
198
+ /** Index within the corresponding hair/garment/attachment list. */
199
+ partIndex: number;
200
+ lod: number;
201
+ slot: number;
202
+ }
203
+ export interface GpuCrowdCharacterPartBindingLifecycleOptions {
204
+ /** Resolve the existing body/face crowd allocations. Their format and ownership stay unchanged. */
205
+ resolveBase: (identity: Readonly<GpuCharacterIdentityUpdate>) => Omit<GpuCrowdCharacterBinding, 'hair' | 'garments' | 'socketAttachments'>;
206
+ releaseBase?: (binding: GpuCrowdCharacterBinding, identity: Readonly<GpuCharacterIdentityUpdate>) => void;
207
+ /** Number of authored LODs for a part asset. */
208
+ lodCount: (kind: GpuCharacterPartKind, asset: number) => number;
209
+ /** Allocate one caller-owned opaque slot. Null explicitly marks an unsupported LOD. */
210
+ allocate: (request: Omit<GpuCrowdCharacterPartAllocation, 'slot'>) => number | null;
211
+ release: (allocation: GpuCrowdCharacterPartAllocation) => void;
212
+ }
213
+ export interface GpuCrowdCharacterPartBindingLifecycle {
214
+ resolveBinding: GpuCrowdCharactersBackendOptions['resolveBinding'];
215
+ releaseBinding: NonNullable<GpuCrowdCharactersBackendOptions['releaseBinding']>;
216
+ }
217
+ /**
218
+ * Cold-path allocator for hair, garments and socket attachments. It gives every actor/part/LOD a
219
+ * retained caller-owned slot, rolls partial failures back, and releases exactly those slots on
220
+ * identity replacement, removal or backend disposal. Body and face remain delegated to the
221
+ * existing crowd allocator so current games do not have to change their pool layout.
222
+ */
223
+ export declare function createGpuCrowdCharacterPartBindingLifecycle(options: GpuCrowdCharacterPartBindingLifecycleOptions): GpuCrowdCharacterPartBindingLifecycle;
71
224
  /** The narrow renderer surface used by the concrete crowd backend (and by its CPU-only tests). */
72
225
  export type GpuCrowdCharacterRenderer = Pick<GpuCrowdRenderer, 'morph' | 'enableFaceRig' | 'disableFaceRig' | 'setFaceWeights' | 'faceFrame' | 'setInstances' | 'setPalettes'>;
73
226
  export interface GpuCrowdCharactersBackendOptions {
@@ -79,6 +232,10 @@ export interface GpuCrowdCharactersBackendOptions {
79
232
  releaseBinding?: (binding: GpuCrowdCharacterBinding, identity: Readonly<GpuCharacterIdentityUpdate>) => void;
80
233
  /** Highest face quality tier that receives the per-frame face rig. Defaults to 1. */
81
234
  maxRiggedFaceTier?: number;
235
+ /** Must match GpuCharactersOptions.socketCount when attachments are used. */
236
+ socketCount?: number;
237
+ /** Explicit handoff to caller-owned hair, garment and attachment render passes. */
238
+ composition?: GpuCrowdCharacterCompositionBackend;
82
239
  }
83
240
  export interface GpuCrowdCharactersPreparedFrame {
84
241
  renderedCount: number;
@@ -87,6 +244,7 @@ export interface GpuCrowdCharactersPreparedFrame {
87
244
  records: Float32Array;
88
245
  palettes: Float32Array;
89
246
  buckets: readonly CrowdBucket[];
247
+ composition: GpuCrowdCharacterCompositionFrame;
90
248
  }
91
249
  export interface GpuCharactersReport {
92
250
  capacity: number;
@@ -100,7 +258,7 @@ export interface GpuCharactersReport {
100
258
  lastTierCounts: Record<'body' | 'face' | 'animation' | 'hair' | 'cloth', number[]>;
101
259
  }
102
260
  /** Validate a hot frame without allocating or converting it. */
103
- export declare function validateGpuCharacterFrame(frame: GpuCharacterFrame, options: Required<GpuCharactersOptions>): void;
261
+ export declare function validateGpuCharacterFrame(frame: GpuCharacterFrame, options: Omit<Required<GpuCharactersOptions>, 'socketCount'> & Pick<GpuCharactersOptions, 'socketCount'>): void;
104
262
  export declare class GpuCharacters {
105
263
  readonly options: Required<GpuCharactersOptions>;
106
264
  private readonly backend;
@@ -136,7 +294,9 @@ export declare class GpuCrowdCharactersBackend implements GpuCharactersBackend {
136
294
  private readonly renderer;
137
295
  private readonly boneCount;
138
296
  private readonly faceChannels;
297
+ private readonly socketCount;
139
298
  private readonly maxRiggedFaceTier;
299
+ private readonly compositionBackend;
140
300
  private readonly resolveBinding;
141
301
  private readonly releaseBinding;
142
302
  private readonly states;
@@ -147,6 +307,7 @@ export declare class GpuCrowdCharactersBackend implements GpuCharactersBackend {
147
307
  dispose(): void;
148
308
  private applyRemovals;
149
309
  private applyIdentityUpdates;
310
+ private validateBinding;
150
311
  private releaseState;
151
312
  }
152
313
  export declare function createGpuCrowdCharactersBackend(options: GpuCrowdCharactersBackendOptions): GpuCrowdCharactersBackend;
package/dist/character.js CHANGED
@@ -42,11 +42,239 @@ export function unpackGpuCharacterFlags(flags) {
42
42
  cloth: (flags >>> TIER_SHIFT.cloth) & TIER_MASK,
43
43
  };
44
44
  }
45
+ export const GPU_CHARACTER_PART_KIND = {
46
+ body: 0,
47
+ face: 1,
48
+ hair: 2,
49
+ garment: 3,
50
+ socketAttachment: 4,
51
+ };
52
+ /**
53
+ * Converts the unified composition table into concrete instanced draw submissions. This is the
54
+ * generic boundary the engine can provide without inventing a mesh format: the caller maps each
55
+ * authored `asset` to its own GPU renderer and encodes those prepared batches in its scene pass.
56
+ */
57
+ export class GpuCrowdCharacterPartDispatcher {
58
+ backend;
59
+ includeBodyAndFace;
60
+ constructor(options) {
61
+ this.backend = options.backend;
62
+ this.includeBodyAndFace = options.includeBodyAndFace ?? false;
63
+ }
64
+ submitComposition(frame, resources) {
65
+ const paletteStride = positiveInt(resources.boneCount, 'boneCount') * 16;
66
+ if (resources.palettes.length % paletteStride !== 0)
67
+ throw new Error('composition palettes do not contain whole rows');
68
+ const groups = new Map();
69
+ for (let row = 0; row < frame.count; row++) {
70
+ const kind = frame.kinds[row];
71
+ if (!this.includeBodyAndFace &&
72
+ (kind === GPU_CHARACTER_PART_KIND.body ||
73
+ kind === GPU_CHARACTER_PART_KIND.face))
74
+ continue;
75
+ const paletteRow = frame.paletteRows[row];
76
+ if ((paletteRow + 1) * paletteStride > resources.palettes.length)
77
+ throw new Error(`composition row ${row} references unavailable palette ${paletteRow}`);
78
+ const key = `${kind}:${frame.assets[row]}:${frame.lods[row]}`;
79
+ const rows = groups.get(key);
80
+ if (rows)
81
+ rows.push(row);
82
+ else
83
+ groups.set(key, [row]);
84
+ }
85
+ const batches = [];
86
+ for (const rows of groups.values()) {
87
+ const first = rows[0];
88
+ const count = rows.length;
89
+ const actorSlots = new Uint32Array(count);
90
+ const bindingSlots = new Uint32Array(count);
91
+ const socketIndices = new Int32Array(count);
92
+ const models = new Float32Array(count * 16);
93
+ const palettes = new Float32Array(count * paletteStride);
94
+ const bounds = new Float32Array(count * 4);
95
+ rows.forEach((row, instance) => {
96
+ actorSlots[instance] = frame.actorSlots[row];
97
+ bindingSlots[instance] = frame.bindingSlots[row];
98
+ socketIndices[instance] = frame.socketIndices[row];
99
+ models.set(frame.transforms.subarray(row * 16, row * 16 + 16), instance * 16);
100
+ const paletteRow = frame.paletteRows[row];
101
+ palettes.set(resources.palettes.subarray(paletteRow * paletteStride, (paletteRow + 1) * paletteStride), instance * paletteStride);
102
+ bounds.set(frame.bounds.subarray(row * 4, row * 4 + 4), instance * 4);
103
+ });
104
+ batches.push({
105
+ kind: frame.kinds[first],
106
+ asset: frame.assets[first],
107
+ lod: frame.lods[first],
108
+ count,
109
+ actorSlots,
110
+ bindingSlots,
111
+ socketIndices,
112
+ models,
113
+ palettes,
114
+ bounds,
115
+ });
116
+ }
117
+ this.backend.submitPartDraws(batches);
118
+ }
119
+ }
120
+ export function createGpuCrowdCharacterPartDispatcher(options) {
121
+ return new GpuCrowdCharacterPartDispatcher(options);
122
+ }
123
+ /**
124
+ * Executable adapter for the engine's existing skinned-mesh renderer. After submission, the game's
125
+ * normal `GpuSkinRenderer.frame(...)` call deforms and draws every grouped part. Asset GPU resources
126
+ * remain caller-owned; a body-conformed garment must resolve to its conformed mesh (or use a pooled
127
+ * backend) because this adapter intentionally shares one immutable mesh per asset/LOD batch.
128
+ */
129
+ export class GpuSkinCharacterPartDrawBackend {
130
+ renderer;
131
+ resolveAsset;
132
+ constructor(options) {
133
+ this.renderer = options.renderer;
134
+ this.resolveAsset = options.resolveAsset;
135
+ }
136
+ submitPartDraws(batches) {
137
+ this.renderer.setCreatures(batches.map((batch) => {
138
+ positiveInt(batch.count, 'part draw count');
139
+ if (batch.models.length !== batch.count * 16)
140
+ throw new Error('part draw models do not match count');
141
+ if (batch.palettes.length === 0 ||
142
+ batch.palettes.length % (batch.count * 16) !== 0)
143
+ throw new Error('part draw palettes do not contain whole instance rows');
144
+ const asset = this.resolveAsset(batch.kind, batch.asset, batch.lod);
145
+ if (!asset)
146
+ throw new Error(`no GpuSkin asset for character part ${batch.kind}:${batch.asset}:LOD${batch.lod}`);
147
+ return {
148
+ ...asset,
149
+ // GpuSkin's legacy required fields remain valid; `instances` is the active path.
150
+ palette: batch.palettes.subarray(0, batch.palettes.length / batch.count),
151
+ model: batch.models.subarray(0, 16),
152
+ instances: {
153
+ count: batch.count,
154
+ palettes: batch.palettes,
155
+ models: batch.models,
156
+ },
157
+ };
158
+ }));
159
+ }
160
+ }
161
+ export function createGpuSkinCharacterPartDrawBackend(options) {
162
+ return new GpuSkinCharacterPartDrawBackend(options);
163
+ }
164
+ /**
165
+ * Cold-path allocator for hair, garments and socket attachments. It gives every actor/part/LOD a
166
+ * retained caller-owned slot, rolls partial failures back, and releases exactly those slots on
167
+ * identity replacement, removal or backend disposal. Body and face remain delegated to the
168
+ * existing crowd allocator so current games do not have to change their pool layout.
169
+ */
170
+ export function createGpuCrowdCharacterPartBindingLifecycle(options) {
171
+ const allocationFor = (identity, kind, asset, partIndex) => {
172
+ if (asset < 0)
173
+ return null;
174
+ const count = positiveInt(options.lodCount(kind, asset), 'part lodCount');
175
+ const lodSlots = [];
176
+ try {
177
+ for (let lod = 0; lod < count; lod++) {
178
+ const slot = options.allocate({
179
+ actorSlot: identity.slot,
180
+ revision: identity.revision,
181
+ kind,
182
+ asset,
183
+ partIndex,
184
+ lod,
185
+ });
186
+ if (slot !== null)
187
+ nonNegativeInt(slot, 'part allocation slot');
188
+ lodSlots.push(slot);
189
+ }
190
+ }
191
+ catch (error) {
192
+ lodSlots.forEach((slot, lod) => {
193
+ if (slot === null)
194
+ return;
195
+ options.release({
196
+ actorSlot: identity.slot,
197
+ revision: identity.revision,
198
+ kind,
199
+ asset,
200
+ partIndex,
201
+ lod,
202
+ slot,
203
+ });
204
+ });
205
+ throw error;
206
+ }
207
+ return { asset, lodSlots };
208
+ };
209
+ const releaseParts = (binding, identity) => {
210
+ const releaseOne = (part, kind, partIndex) => {
211
+ if (!part)
212
+ return;
213
+ part.lodSlots.forEach((slot, lod) => {
214
+ if (slot === null)
215
+ return;
216
+ options.release({
217
+ actorSlot: identity.slot,
218
+ revision: identity.revision,
219
+ kind,
220
+ asset: part.asset,
221
+ partIndex,
222
+ lod,
223
+ slot,
224
+ });
225
+ });
226
+ };
227
+ releaseOne(binding.hair, GPU_CHARACTER_PART_KIND.hair, 0);
228
+ binding.garments?.forEach((part, index) => {
229
+ releaseOne(part, GPU_CHARACTER_PART_KIND.garment, index);
230
+ });
231
+ binding.socketAttachments?.forEach((part, index) => {
232
+ releaseOne(part, GPU_CHARACTER_PART_KIND.socketAttachment, index);
233
+ });
234
+ };
235
+ const resolveBinding = (identity) => {
236
+ const base = options.resolveBase(identity);
237
+ const binding = {
238
+ ...base,
239
+ garments: [],
240
+ socketAttachments: [],
241
+ };
242
+ try {
243
+ binding.hair =
244
+ allocationFor(identity, GPU_CHARACTER_PART_KIND.hair, identity.hairAsset, 0) ?? undefined;
245
+ binding.garments = identity.garmentAssets.map((asset, index) => allocationFor(identity, GPU_CHARACTER_PART_KIND.garment, asset, index));
246
+ binding.socketAttachments = (identity.socketAttachments ?? []).map((attachment, index) => allocationFor(identity, GPU_CHARACTER_PART_KIND.socketAttachment, attachment.asset, index));
247
+ return binding;
248
+ }
249
+ catch (error) {
250
+ releaseParts(binding, identity);
251
+ options.releaseBase?.(binding, identity);
252
+ throw error;
253
+ }
254
+ };
255
+ return {
256
+ resolveBinding,
257
+ releaseBinding: (binding, identity) => {
258
+ releaseParts(binding, identity);
259
+ options.releaseBase?.(binding, identity);
260
+ },
261
+ };
262
+ }
45
263
  function positiveInt(value, name) {
46
264
  if (!Number.isInteger(value) || value <= 0)
47
265
  throw new Error(`${name} must be a positive integer`);
48
266
  return value;
49
267
  }
268
+ function nonNegativeInt(value, name) {
269
+ if (!Number.isInteger(value) || value < 0)
270
+ throw new Error(`${name} must be a non-negative integer`);
271
+ return value;
272
+ }
273
+ function integer(value, name) {
274
+ if (!Number.isInteger(value))
275
+ throw new Error(`${name} must be an integer`);
276
+ return value;
277
+ }
50
278
  function finiteArray(value, name) {
51
279
  for (let i = 0; i < value.length; i++)
52
280
  if (!Number.isFinite(value[i]))
@@ -56,6 +284,10 @@ function cloneIdentity(slot, identity) {
56
284
  if (!Number.isInteger(identity.revision) || identity.revision < 0)
57
285
  throw new Error('identity revision must be a non-negative integer');
58
286
  finiteArray(identity.morphWeights, 'morphWeights');
287
+ if (identity.bodyAsset !== undefined)
288
+ integer(identity.bodyAsset, 'bodyAsset');
289
+ if (identity.faceAsset !== undefined)
290
+ integer(identity.faceAsset, 'faceAsset');
59
291
  for (const [name, value] of [
60
292
  ['material', identity.material],
61
293
  ['skinLayer', identity.skinLayer],
@@ -67,15 +299,32 @@ function cloneIdentity(slot, identity) {
67
299
  for (const [i, value] of identity.garmentAssets.entries())
68
300
  if (!Number.isInteger(value))
69
301
  throw new Error(`garmentAssets[${i}] must be an integer`);
302
+ for (const [i, attachment] of (identity.socketAttachments ?? []).entries()) {
303
+ integer(attachment.asset, `socketAttachments[${i}].asset`);
304
+ nonNegativeInt(attachment.socket, `socketAttachments[${i}].socket`);
305
+ }
70
306
  return {
71
307
  slot,
72
308
  revision: identity.revision,
73
309
  morphWeights: identity.morphWeights.slice(),
310
+ ...(identity.bodyAsset === undefined
311
+ ? {}
312
+ : { bodyAsset: identity.bodyAsset }),
313
+ ...(identity.faceAsset === undefined
314
+ ? {}
315
+ : { faceAsset: identity.faceAsset }),
74
316
  material: identity.material,
75
317
  skinLayer: identity.skinLayer,
76
318
  eyeLayer: identity.eyeLayer,
77
319
  hairAsset: identity.hairAsset,
78
320
  garmentAssets: [...identity.garmentAssets],
321
+ ...(identity.socketAttachments === undefined
322
+ ? {}
323
+ : {
324
+ socketAttachments: identity.socketAttachments.map((attachment) => ({
325
+ ...attachment,
326
+ })),
327
+ }),
79
328
  };
80
329
  }
81
330
  /** Validate a hot frame without allocating or converting it. */
@@ -93,6 +342,11 @@ export function validateGpuCharacterFrame(frame, options) {
93
342
  frame.faceWeights.length,
94
343
  frame.count * options.faceChannels,
95
344
  ],
345
+ [
346
+ 'socketTransforms',
347
+ frame.socketTransforms?.length ?? 0,
348
+ frame.count * (options.socketCount ?? 0) * 16,
349
+ ],
96
350
  ['bounds', frame.bounds.length, frame.count * 4],
97
351
  ['flags', frame.flags.length, frame.count],
98
352
  ];
@@ -102,6 +356,8 @@ export function validateGpuCharacterFrame(frame, options) {
102
356
  finiteArray(frame.transforms, 'transforms');
103
357
  finiteArray(frame.palettes, 'palettes');
104
358
  finiteArray(frame.faceWeights, 'faceWeights');
359
+ if (frame.socketTransforms)
360
+ finiteArray(frame.socketTransforms, 'socketTransforms');
105
361
  finiteArray(frame.bounds, 'bounds');
106
362
  }
107
363
  export class GpuCharacters {
@@ -122,6 +378,7 @@ export class GpuCharacters {
122
378
  maxCharacters: positiveInt(options.maxCharacters, 'maxCharacters'),
123
379
  boneCount: positiveInt(options.boneCount, 'boneCount'),
124
380
  faceChannels: positiveInt(options.faceChannels ?? GPU_CHARACTER_FACE_CHANNELS, 'faceChannels'),
381
+ socketCount: nonNegativeInt(options.socketCount ?? 0, 'socketCount'),
125
382
  };
126
383
  // pop() returns 0, 1, 2… so stable low slots are reused first.
127
384
  this.freeSlots = Array.from({ length: this.options.maxCharacters }, (_, i) => this.options.maxCharacters - i - 1);
@@ -254,6 +511,47 @@ function bindingFaceSlot(binding, lod) {
254
511
  throw new Error(`character binding has an invalid face pool slot for LOD ${lod}`);
255
512
  return slot;
256
513
  }
514
+ function partBindingSlot(binding, lod, label) {
515
+ const slot = binding.lodSlots[lod];
516
+ if (typeof slot !== 'number' || !Number.isInteger(slot) || slot < 0)
517
+ throw new Error(`${label} binding has no valid slot for LOD ${lod}`);
518
+ return slot;
519
+ }
520
+ function indexedPartBinding(bindings, index, label) {
521
+ const binding = bindings?.[index];
522
+ if (!binding)
523
+ throw new Error(`${label} binding is unavailable`);
524
+ return binding;
525
+ }
526
+ function emptyComposition() {
527
+ return {
528
+ count: 0,
529
+ actorSlots: new Uint32Array(0),
530
+ kinds: new Uint8Array(0),
531
+ assets: new Int32Array(0),
532
+ bindingSlots: new Uint32Array(0),
533
+ lods: new Uint8Array(0),
534
+ tiers: new Uint8Array(0),
535
+ animationTiers: new Uint8Array(0),
536
+ paletteRows: new Uint32Array(0),
537
+ socketIndices: new Int32Array(0),
538
+ transforms: new Float32Array(0),
539
+ bounds: new Float32Array(0),
540
+ };
541
+ }
542
+ function validatePartBinding(binding, asset, label) {
543
+ if (asset < 0) {
544
+ if (binding)
545
+ throw new Error(`${label} binding was supplied for disabled asset ${asset}`);
546
+ return;
547
+ }
548
+ if (!binding)
549
+ throw new Error(`${label} binding is required for asset ${asset}`);
550
+ if (binding.asset !== asset)
551
+ throw new Error(`${label} binding asset ${binding.asset} does not match identity asset ${asset}`);
552
+ if (binding.lodSlots.length === 0)
553
+ throw new Error(`${label} binding must declare at least one LOD slot`);
554
+ }
257
555
  /**
258
556
  * Concrete adapter from the retained character batch to {@link GpuCrowdRenderer}.
259
557
  *
@@ -266,7 +564,9 @@ export class GpuCrowdCharactersBackend {
266
564
  renderer;
267
565
  boneCount;
268
566
  faceChannels;
567
+ socketCount;
269
568
  maxRiggedFaceTier;
569
+ compositionBackend;
270
570
  resolveBinding;
271
571
  releaseBinding;
272
572
  states = new Map();
@@ -276,6 +576,7 @@ export class GpuCrowdCharactersBackend {
276
576
  records: new Float32Array(0),
277
577
  palettes: new Float32Array(0),
278
578
  buckets: [],
579
+ composition: emptyComposition(),
279
580
  };
280
581
  constructor(options) {
281
582
  this.renderer = options.renderer;
@@ -283,6 +584,8 @@ export class GpuCrowdCharactersBackend {
283
584
  this.faceChannels = positiveInt(options.faceChannels ?? GPU_CHARACTER_FACE_CHANNELS, 'faceChannels');
284
585
  if (this.faceChannels !== GPU_CHARACTER_FACE_CHANNELS)
285
586
  throw new Error(`GpuCrowdRenderer requires ${GPU_CHARACTER_FACE_CHANNELS} face channels`);
587
+ this.socketCount = nonNegativeInt(options.socketCount ?? 0, 'socketCount');
588
+ this.compositionBackend = options.composition;
286
589
  this.resolveBinding = options.resolveBinding;
287
590
  this.releaseBinding = options.releaseBinding;
288
591
  const faceTier = options.maxRiggedFaceTier ?? 1;
@@ -291,6 +594,10 @@ export class GpuCrowdCharactersBackend {
291
594
  this.maxRiggedFaceTier = faceTier;
292
595
  }
293
596
  submit(batch) {
597
+ const expectedSocketValues = batch.frame.count * this.socketCount * 16;
598
+ const socketValues = batch.frame.socketTransforms?.length ?? 0;
599
+ if (socketValues !== expectedSocketValues)
600
+ throw new Error(`socketTransforms length ${socketValues} does not match backend requirement ${expectedSocketValues}`);
294
601
  this.applyRemovals(batch.removedSlots);
295
602
  this.applyIdentityUpdates(batch.identityUpdates);
296
603
  const entries = [];
@@ -318,6 +625,29 @@ export class GpuCrowdCharactersBackend {
318
625
  const actorSlots = new Uint32Array(entries.length);
319
626
  const buckets = [];
320
627
  const paletteStride = this.boneCount * 16;
628
+ const compositionCount = entries.reduce((count, entry) => {
629
+ const identity = entry.state.identity;
630
+ return (count +
631
+ 2 +
632
+ (identity.hairAsset < 0 ? 0 : 1) +
633
+ identity.garmentAssets.filter((asset) => asset >= 0).length +
634
+ (identity.socketAttachments ?? []).filter((attachment) => attachment.asset >= 0).length);
635
+ }, 0);
636
+ const composition = {
637
+ count: compositionCount,
638
+ actorSlots: new Uint32Array(compositionCount),
639
+ kinds: new Uint8Array(compositionCount),
640
+ assets: new Int32Array(compositionCount),
641
+ bindingSlots: new Uint32Array(compositionCount),
642
+ lods: new Uint8Array(compositionCount),
643
+ tiers: new Uint8Array(compositionCount),
644
+ animationTiers: new Uint8Array(compositionCount),
645
+ paletteRows: new Uint32Array(compositionCount),
646
+ socketIndices: new Int32Array(compositionCount).fill(-1),
647
+ transforms: new Float32Array(compositionCount * 16),
648
+ bounds: new Float32Array(compositionCount * 4),
649
+ };
650
+ let partIndex = 0;
321
651
  for (const [recordIndex, entry] of entries.entries()) {
322
652
  const { actorSlot, sourceIndex, lod, tiers, state } = entry;
323
653
  const sourcePoolSlot = bindingPoolSlot(state.binding, lod);
@@ -350,6 +680,50 @@ export class GpuCrowdCharactersBackend {
350
680
  writeCrowdRecord(records, recordIndex * CROWD_RECORD_FLOATS, transform, tint, state.binding.layer ?? state.identity.skinLayer, recordIndex, facePoolSlot ?? sourcePoolSlot, state.identity.material);
351
681
  palettes.set(batch.frame.palettes.subarray(sourceIndex * paletteStride, (sourceIndex + 1) * paletteStride), recordIndex * paletteStride);
352
682
  actorSlots[recordIndex] = actorSlot;
683
+ const sourceBounds = batch.frame.bounds.subarray(sourceIndex * 4, (sourceIndex + 1) * 4);
684
+ const writePart = (kind, asset, bindingSlot, partLod, tier, partTransform, socketIndex = -1) => {
685
+ const index = partIndex++;
686
+ composition.actorSlots[index] = actorSlot;
687
+ composition.kinds[index] = kind;
688
+ composition.assets[index] = asset;
689
+ composition.bindingSlots[index] = bindingSlot;
690
+ composition.lods[index] = partLod;
691
+ composition.tiers[index] = tier;
692
+ composition.animationTiers[index] = tiers.animation;
693
+ composition.paletteRows[index] = recordIndex;
694
+ composition.socketIndices[index] = socketIndex;
695
+ composition.transforms.set(partTransform, index * 16);
696
+ composition.bounds.set(sourceBounds, index * 4);
697
+ };
698
+ const bodyAsset = state.identity.bodyAsset ?? -1;
699
+ const faceAsset = state.identity.faceAsset ?? bodyAsset;
700
+ writePart(GPU_CHARACTER_PART_KIND.body, bodyAsset, sourcePoolSlot, lod, tiers.body, transform);
701
+ writePart(GPU_CHARACTER_PART_KIND.face, faceAsset, facePoolSlot ?? sourcePoolSlot, lod, tiers.face, transform);
702
+ if (state.identity.hairAsset >= 0) {
703
+ const binding = state.binding.hair;
704
+ if (!binding)
705
+ throw new Error('hair binding is unavailable');
706
+ writePart(GPU_CHARACTER_PART_KIND.hair, state.identity.hairAsset, partBindingSlot(binding, tiers.hair, 'hair'), tiers.hair, tiers.hair, transform);
707
+ }
708
+ for (const [garmentIndex, asset,] of state.identity.garmentAssets.entries()) {
709
+ if (asset < 0)
710
+ continue;
711
+ const binding = indexedPartBinding(state.binding.garments, garmentIndex, `garment ${garmentIndex}`);
712
+ writePart(GPU_CHARACTER_PART_KIND.garment, asset, partBindingSlot(binding, tiers.cloth, `garment ${garmentIndex}`), tiers.cloth, tiers.cloth, transform);
713
+ }
714
+ for (const [attachmentIndex, attachment] of (state.identity.socketAttachments ?? []).entries()) {
715
+ if (attachment.asset < 0)
716
+ continue;
717
+ const socketTransforms = batch.frame.socketTransforms;
718
+ if (!socketTransforms)
719
+ throw new Error('socketTransforms are required for socket attachments');
720
+ const offset = (sourceIndex * this.socketCount + attachment.socket) * 16;
721
+ const socketTransform = socketTransforms.subarray(offset, offset + 16);
722
+ if (socketTransform.length !== 16)
723
+ throw new Error(`socket transform ${attachment.socket} is unavailable for actor ${actorSlot}`);
724
+ const binding = indexedPartBinding(state.binding.socketAttachments, attachmentIndex, `socket attachment ${attachmentIndex}`);
725
+ writePart(GPU_CHARACTER_PART_KIND.socketAttachment, attachment.asset, partBindingSlot(binding, tiers.body, `socket attachment ${attachmentIndex}`), tiers.body, tiers.body, socketTransform, attachment.socket);
726
+ }
353
727
  const bucket = buckets[buckets.length - 1];
354
728
  if (buckets.length > 0 && bucket.lod === lod)
355
729
  bucket.count++;
@@ -365,7 +739,12 @@ export class GpuCrowdCharactersBackend {
365
739
  records,
366
740
  palettes,
367
741
  buckets: buckets.map((bucket) => ({ ...bucket })),
742
+ composition,
368
743
  };
744
+ this.compositionBackend?.submitComposition(composition, {
745
+ palettes,
746
+ boneCount: this.boneCount,
747
+ });
369
748
  }
370
749
  lastPreparedFrame() {
371
750
  return this.prepared;
@@ -387,7 +766,18 @@ export class GpuCrowdCharactersBackend {
387
766
  applyIdentityUpdates(updates) {
388
767
  for (const identity of updates) {
389
768
  const previous = this.states.get(identity.slot);
769
+ // A failed hot submit leaves cold updates pending in GpuCharacters. If this revision was
770
+ // already installed before the failure, retry must not allocate/release it a second time.
771
+ if (previous?.identity === identity)
772
+ continue;
390
773
  const binding = this.resolveBinding(identity);
774
+ try {
775
+ this.validateBinding(binding, identity);
776
+ }
777
+ catch (error) {
778
+ this.releaseBinding?.(binding, identity);
779
+ throw error;
780
+ }
391
781
  if (previous)
392
782
  this.releaseState(previous);
393
783
  this.states.set(identity.slot, {
@@ -398,6 +788,32 @@ export class GpuCrowdCharactersBackend {
398
788
  });
399
789
  }
400
790
  }
791
+ validateBinding(binding, identity) {
792
+ if (binding.poolSlots.length === 0)
793
+ throw new Error('character binding must declare at least one body LOD slot');
794
+ for (const [lod, slot] of binding.poolSlots.entries())
795
+ nonNegativeInt(slot, `body binding LOD ${lod}`);
796
+ if (binding.tint) {
797
+ if (binding.tint.length !== 4)
798
+ throw new Error('character binding tint must contain four values');
799
+ finiteArray(binding.tint, 'character binding tint');
800
+ }
801
+ validatePartBinding(binding.hair, identity.hairAsset, 'hair');
802
+ const garments = binding.garments ?? [];
803
+ if (garments.length !== identity.garmentAssets.length)
804
+ throw new Error(`garment binding count ${garments.length} does not match identity count ${identity.garmentAssets.length}`);
805
+ for (const [index, asset] of identity.garmentAssets.entries())
806
+ validatePartBinding(garments[index], asset, `garment ${index}`);
807
+ const attachments = identity.socketAttachments ?? [];
808
+ const attachmentBindings = binding.socketAttachments ?? [];
809
+ if (attachmentBindings.length !== attachments.length)
810
+ throw new Error(`socket attachment binding count ${attachmentBindings.length} does not match identity count ${attachments.length}`);
811
+ for (const [index, attachment] of attachments.entries()) {
812
+ if (attachment.socket >= this.socketCount)
813
+ throw new Error(`socket attachment ${index} names socket ${attachment.socket}, but socketCount is ${this.socketCount}`);
814
+ validatePartBinding(attachmentBindings[index], attachment.asset, `socket attachment ${index}`);
815
+ }
816
+ }
401
817
  releaseState(state) {
402
818
  if (state.rigSource !== null)
403
819
  this.renderer.disableFaceRig(state.rigSource);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.40",
3
+ "version": "0.15.42",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {