-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffl.js
3802 lines (3503 loc) · 126 KB
/
ffl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @ts-check
'use strict';
/*!
* Bindings for FFL, a Mii renderer, in JavaScript.
* https://github.com/ariankordi/FFL.js
* @author Arian Kordi <https://github.com/ariankordi>
*/
// ------------------ ESM imports, uncomment if you use ESM ------------------
// Also see the bottom of the script for corresponding exports.
/*
import * as THREE from 'three';
// import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm';
import * as _ from './struct-fu';
*/
// // ---------------------------------------------------------------------
// // Emscripten Types, JSDoc Helpers
// // ---------------------------------------------------------------------
/**
* Emscripten "Module" type.
* https://github.com/DefinitelyTyped/DefinitelyTyped/blob/c03bddd4d3c7774d00fa256a9e165d68c7534ccc/types/emscripten/index.d.ts#L26
* @typedef {Object} Module
*
* @property {function(): void} onRuntimeInitialized
* @property {function(object): void} destroy
* @property {boolean|null} calledRun
* // USE_TYPED_ARRAYS == 2
* @property {Int8Array} HEAP8
* @property {Uint8Array} HEAPU8
* @property {Uint16Array} HEAPU16
* @property {Uint32Array} HEAPU32
* @property {Float32Array} HEAPF32
* Runtime methods:
* @property {function(number): number} _malloc
* @property {function(number): void} _free
* @property {function((...args: *[]) => *, string=): number} addFunction
* @property {function(number): void} removeFunction
*
* ------------------------------- FFL Bindings -------------------------------
* @property {function(number, number, number, number): *} _FFLInitCharModelCPUStepWithCallback
* @property {function(number, number, number): *} _FFLInitCharModelCPUStep
* @property {function(number): *} _FFLDeleteCharModel
* @property {function(number): *} _FFLGetDrawParamOpaFaceline
* @property {function(number): *} _FFLGetDrawParamOpaBeard
* @property {function(number): *} _FFLGetDrawParamOpaNose
* @property {function(number): *} _FFLGetDrawParamOpaForehead
* @property {function(number): *} _FFLGetDrawParamOpaHair
* @property {function(number): *} _FFLGetDrawParamOpaCap
* @property {function(number): *} _FFLGetDrawParamXluMask
* @property {function(number): *} _FFLGetDrawParamXluNoseLine
* @property {function(number): *} _FFLGetDrawParamXluGlass
* @property {function(number, number): *} _FFLSetExpression
* @property {function(number): *} _FFLGetExpression
* @property {function(number, number): *} _FFLSetViewModelType
* @property {function(number, number): *} _FFLGetBoundingBox
* @property {function(number, number): *} _FFLIsAvailableExpression
* @property {function(number, number): *} _FFLSetCoordinate
* @property {function(number): *} _FFLSetScale
* @property {function(number, number, number, number): *} _FFLiGetRandomCharInfo
* @property {function(number, number): *} _FFLpGetStoreDataFromCharInfo
* @property {function(number, number): *} _FFLpGetCharInfoFromStoreData
* @property {function(number, number, number, number, boolean): *} _FFLGetAdditionalInfo
* @property {function(number, number): *} _FFLInitRes
* @property {function(): *} _FFLInitResGPUStep
* @property {function(): *} _FFLExit
* @property {function(): *} _FFLIsAvailable
* @property {function(number, number): *} _FFLGetFavoriteColor
* @property {function(number): *} _FFLSetLinearGammaMode
* @property {function(number, number): *} _FFLGetFacelineColor
* @property {function(boolean): *} _FFLSetTextureFlipY
* @property {function(boolean): *} _FFLSetNormalIsSnorm8_8_8_8
* @property {function(boolean): *} _FFLSetFrontCullForFlipX
* @property {function(number): *} _FFLSetTextureCallback
* @property {function(number): *} _FFLiDeleteTextureTempObject
* @property {function(number, number, number): *} _FFLiDeleteTempObjectMaskTextures
* @property {function(number, number, number): *} _FFLiDeleteTempObjectFacelineTexture
* @property {function(number): *} _FFLiiGetEyeRotateOffset
* @property {function(number): *} _FFLiiGetEyebrowRotateOffset
* @property {function(number): *} _FFLiInvalidateTempObjectFacelineTexture
* @property {function(number): *} _FFLiInvalidatePartsTextures
* @property {function(number): *} _FFLiInvalidateRawMask
* @property {function(number, boolean): *} _FFLiVerifyCharInfoWithReason
* @property {function(): void} _exit
*/
/**
* Generate JSDoc typedef from an object, supporting nested types up to depth 5.
* Ignores keys starting with "_".
* @param {Object.<string, *>} obj - The object to analyze.
* @param {string} typeName - The name of the type.
* @param {number} depth - Current recursion depth (default: 0).
* @param {Set<string>} definedTypes - Tracks already defined types to avoid duplicates.
* @returns {string} JSDoc typedef output.
*/
function generateJSDoc(obj, typeName = 'GeneratedType', depth = 0, definedTypes = new Set()) {
// Stop at max depth 5.
if (depth > 5) {
return '';
}
let text = `/**\n * @typedef {Object} ${typeName}\n`;
let nestedDefinitions = '';
Object.keys(obj).forEach((key) => {
// Ignore private keys beginning with underscore.
if (key.startsWith('_')) {
// Ignore private keys beginning with underscore.
return;
}
const value = obj[key];
const valueType = typeof value;
let propType;
if (value === null) {
propType = 'null';
} else if (Array.isArray(value)) {
let arrayType = '*'; // Assume first element type.
if (value.length > 0) {
const firstElem = value[0];
if (typeof firstElem === 'object' && firstElem !== null) {
const subType = `${typeName}_${key}_Item`;
if (!definedTypes.has(subType)) {
nestedDefinitions += generateJSDoc(firstElem, subType, depth + 1, definedTypes) + '\n';
definedTypes.add(subType);
}
arrayType = subType;
} else {
arrayType = typeof firstElem;
}
}
propType = `Array<${arrayType}>`;
} else if (valueType === 'object') {
const subType = `${typeName}_${key}`;
if (!definedTypes.has(subType)) {
nestedDefinitions += generateJSDoc(value, subType, depth + 1, definedTypes) + '\n';
definedTypes.add(subType);
}
propType = subType;
} else {
propType = valueType;
}
text += ` * @property {${propType}} ${key}\n`;
});
text += ' */';
return nestedDefinitions + text;
}
/**
* Generates JSDoc, calling {@link generateJSDoc}, from the name
* of a type defined as a struct-fu struct.
* The result will be logged to console.
* @param {string} typeName - Name of the type.
* @throws {Error} typeName must be a string
*/
function generateJSDocStructFu(typeName) {
if (typeof typeName !== 'string') {
throw new Error('typeName must be a string.');
}
eval(`
const empty = new Uint8Array(${typeName}.size);
const ret = generateJSDoc(${typeName}.unpack(empty), typeName);
console.log(ret);
`);
}
/**
* @typedef {Object} WindowCustom
* @property {Module} Module
* @property {Object} THREE
* @property {TextureManager} FFLTextures
* List shader materials:
* @property {Object} FFLShaderMaterial
* @property {Object} LUTShaderMaterial
*/
/** @type {WindowCustom} */
window;
// // ---------------------------------------------------------------------
// // Enum Definitions
// // ---------------------------------------------------------------------
/**
* enum {number}
*/
const FFLResult = {
OK: 0,
MAX: 20
// To be completed.
};
/**
* @enum {number}
*/
const FFLiShapeType = {
OPA_BEARD: 0,
OPA_FACELINE: 1,
OPA_HAIR_NORMAL: 2,
OPA_FOREHEAD_NORMAL: 3,
XLU_MASK: 4,
XLU_NOSELINE: 5,
OPA_NOSE: 6,
OPA_HAT_NORMAL: 7,
XLU_GLASS: 8,
OPA_HAIR_CAP: 9,
OPA_FOREHEAD_CAP: 10,
OPA_HAT_CAP: 11,
MAX: 12
};
/**
* @enum {number}
*/
const FFLAttributeBufferType = {
POSITION: 0,
TEXCOORD: 1,
NORMAL: 2,
TANGENT: 3,
COLOR: 4,
MAX: 5
};
/**
* @enum {number}
*/
const FFLCullMode = {
NONE: 0,
BACK: 1,
FRONT: 2,
MAX: 3
};
/**
* @enum {number}
*/
const FFLModulateMode = {
CONSTANT: 0, // No Texture, Has Color (R)
TEXTURE_DIRECT: 1, // Has Texture, No Color
RGB_LAYERED: 2, // Has Texture, Has Color (R + G + B)
ALPHA: 3, // Has Texture, Has Color (R)
LUMINANCE_ALPHA: 4, // Has Texture, Has Color (R)
ALPHA_OPA: 5 // Has Texture, Has Color (R)
};
/**
* @enum {number}
*/
const FFLModulateType = {
SHAPE_FACELINE: 0,
SHAPE_BEARD: 1,
SHAPE_NOSE: 2,
SHAPE_FOREHEAD: 3,
SHAPE_HAIR: 4,
SHAPE_CAP: 5,
SHAPE_MASK: 6,
SHAPE_NOSELINE: 7,
SHAPE_GLASS: 8,
MUSTACHE: 9,
MOUTH: 10,
EYEBROW: 11,
EYE: 12,
MOLE: 13,
FACE_MAKE: 14,
FACE_LINE: 15,
FACE_BEARD: 16,
FILL: 17,
SHAPE_MAX: 9
};
/**
* @enum {number}
*/
const FFLResourceType = {
MIDDLE: 0,
HIGH: 1,
MAX: 2
};
/**
* @enum {number}
*/
const FFLExpression = {
NORMAL: 0,
MAX: 70
};
/**
* @enum {number}
*/
const FFLModelFlag = {
NORMAL: 1 << 0,
HAT: 1 << 1, // Uses a variant of hair designed for hats.
FACE_ONLY: 1 << 2, // Discards hair from the model for helmets, etc.
FLATTEN_NOSE: 1 << 3, // Limits the Z depth on a nose for helmets, etc.
NEW_EXPRESSIONS: 1 << 4, // Enables expression flag to use beyond 32 expressions.
// This flag will only make new textures
// when initializing a CharModel and not
// initialize shapes. Note that this means
// you cannot DrawOpa/Xlu when this is set.
NEW_MASK_ONLY: 1 << 5
};
// // ---------------------------------------------------------------------
// // Struct Definitions (struct-fu)
// // ---------------------------------------------------------------------
// Mostly leading up to FFLDrawParam.
// Define _uintptr as a mirror for _.uint32le.
const _uintptr = _.uint32le;
/**
* @typedef {Object} FFLAttributeBuffer
* @property {number} size
* @property {number} stride
* @property {number} ptr
*/
/** @type {_.StructInstance<FFLAttributeBuffer>} */
const FFLAttributeBuffer = _.struct([
_.uint32le('size'),
_.uint32le('stride'),
_uintptr('ptr')
]);
/**
* @typedef {Object} FFLAttributeBufferParam
* @property {Array<FFLAttributeBuffer>} attributeBuffers
*/
/** @type {_.StructInstance<FFLAttributeBufferParam>} */
const FFLAttributeBufferParam = _.struct([
_.struct('attributeBuffers', [FFLAttributeBuffer], 5)
]);
/**
* @typedef {Object} FFLPrimitiveParam
* @property {number} primitiveType
* @property {number} indexCount
* @property {number} pAdjustMatrix
* @property {number} pIndexBuffer
*/
/** @type {_.StructInstance<FFLPrimitiveParam>} */
const FFLPrimitiveParam = _.struct([
_.uint32le('primitiveType'),
_.uint32le('indexCount'),
_uintptr('pAdjustMatrix'), // TODO: Not uintptr in 64-bit.
_uintptr('pIndexBuffer')
]);
/**
* @typedef {Object} FFLColor
* @property {number} r
* @property {number} g
* @property {number} b
* @property {number} a
*/
/** @type {_.StructInstance<FFLColor>} */
const FFLColor = _.struct([
_.float32le('r'),
_.float32le('g'),
_.float32le('b'),
_.float32le('a')
]);
/**
* @typedef {Object} FFLVec3
* @property {number} x
* @property {number} y
* @property {number} z
*/
/** @type {_.StructInstance<FFLVec3>} */
const FFLVec3 = _.struct([
_.float32le('x'),
_.float32le('y'),
_.float32le('z')
]);
/**
* @typedef {Object} FFLModulateParam
* @property {FFLModulateMode} mode
* @property {FFLModulateType} type
* @property {number} pColorR - Pointer to FFLColor
* @property {number} pColorG - Pointer to FFLColor
* @property {number} pColorB - Pointer to FFLColor
* @property {number} pTexture2D
*/
/** @type {_.StructInstance<FFLModulateParam>} */
const FFLModulateParam = _.struct([
_.uint32le('mode'), // enum FFLModulateMode
_.uint32le('type'), // enum FFLModulateType
_uintptr('pColorR'),
_uintptr('pColorG'),
_uintptr('pColorB'),
_uintptr('pTexture2D')
]);
/**
* @typedef {Object} FFLDrawParam
* @property {FFLAttributeBufferParam} attributeBufferParam
* @property {FFLModulateParam} modulateParam
* @property {FFLCullMode} cullMode
* @property {FFLPrimitiveParam} primitiveParam
*/
/** @type {_.StructInstance<FFLDrawParam>} */
const FFLDrawParam = _.struct([
_.struct('attributeBufferParam', [FFLAttributeBufferParam]),
_.struct('modulateParam', [FFLModulateParam]),
_.uint32le('cullMode'),
_.struct('primitiveParam', [FFLPrimitiveParam])
]);
// ---------------------- Begin FFLiCharInfo Definition ----------------------
const FFLCreateID = _.struct([
_.uint8('data', 10)
]);
/**
* @typedef {Object} FFLiCharInfo_faceline
* @property {number} type
* @property {number} color
* @property {number} texture
* @property {number} make
*/
/**
* @typedef {Object} FFLiCharInfo_hair
* @property {number} type
* @property {number} color
* @property {number} flip
*/
/**
* @typedef {Object} FFLiCharInfo_eye
* @property {number} type
* @property {number} color
* @property {number} scale
* @property {number} aspect
* @property {number} rotate
* @property {number} x
* @property {number} y
*/
/**
* @typedef {Object} FFLiCharInfo_eyebrow
* @property {number} type
* @property {number} color
* @property {number} scale
* @property {number} aspect
* @property {number} rotate
* @property {number} x
* @property {number} y
*/
/**
* @typedef {Object} FFLiCharInfo_nose
* @property {number} type
* @property {number} scale
* @property {number} y
*/
/**
* @typedef {Object} FFLiCharInfo_mouth
* @property {number} type
* @property {number} color
* @property {number} scale
* @property {number} aspect
* @property {number} y
*/
/**
* @typedef {Object} FFLiCharInfo_beard
* @property {number} mustache
* @property {number} type
* @property {number} color
* @property {number} scale
* @property {number} y
*/
/**
* @typedef {Object} FFLiCharInfo_glass
* @property {number} type
* @property {number} color
* @property {number} scale
* @property {number} y
*/
/**
* @typedef {Object} FFLiCharInfo_mole
* @property {number} type
* @property {number} scale
* @property {number} x
* @property {number} y
*/
/**
* @typedef {Object} FFLiCharInfo_body
* @property {number} height
* @property {number} build
*/
/**
* @typedef {Object} FFLiCharInfo_personal
* @property {string} name
* @property {string} creator
* @property {number} gender
* @property {number} birthMonth
* @property {number} birthDay
* @property {number} favoriteColor
* @property {number} favorite
* @property {number} copyable
* @property {number} ngWord
* @property {number} localonly
* @property {number} regionMove
* @property {number} fontRegion
* @property {number} roomIndex
* @property {number} positionInRoom
* @property {number} birthPlatform
*/
/**
* @typedef {Object} FFLiCharInfo_createID
* @property {Array<number>} data
*/
/**
* @typedef {Object} FFLiCharInfo
* @property {number} miiVersion
* @property {FFLiCharInfo_faceline} faceline
* @property {FFLiCharInfo_hair} hair
* @property {FFLiCharInfo_eye} eye
* @property {FFLiCharInfo_eyebrow} eyebrow
* @property {FFLiCharInfo_nose} nose
* @property {FFLiCharInfo_mouth} mouth
* @property {FFLiCharInfo_beard} beard
* @property {FFLiCharInfo_glass} glass
* @property {FFLiCharInfo_mole} mole
* @property {FFLiCharInfo_body} body
* @property {FFLiCharInfo_personal} personal
* @property {FFLiCharInfo_createID} createID
* @property {number} padding_0
* @property {number} authorType
* @property {Array<number>} authorID
*/
/** @type {_.StructInstance<FFLiCharInfo>} */
const FFLiCharInfo = _.struct([
_.int32le('miiVersion'),
_.struct('faceline', [_.int32le('type'), _.int32le('color'),
_.int32le('texture'), _.int32le('make')]),
_.struct('hair', [_.int32le('type'), _.int32le('color'), _.int32le('flip')]),
_.struct('eye', [_.int32le('type'), _.int32le('color'), _.int32le('scale'),
_.int32le('aspect'), _.int32le('rotate'),
_.int32le('x'), _.int32le('y')]),
_.struct('eyebrow', [_.int32le('type'), _.int32le('color'),
_.int32le('scale'), _.int32le('aspect'),
_.int32le('rotate'), _.int32le('x'),
_.int32le('y')]),
_.struct('nose', [_.int32le('type'), _.int32le('scale'),
_.int32le('y')]),
_.struct('mouth', [_.int32le('type'), _.int32le('color'),
_.int32le('scale'), _.int32le('aspect'),
_.int32le('y')]),
_.struct('beard', [_.int32le('mustache'), _.int32le('type'),
_.int32le('color'), _.int32le('scale'),
_.int32le('y')]),
_.struct('glass', [_.int32le('type'), _.int32le('color'),
_.int32le('scale'), _.int32le('y')]),
_.struct('mole', [_.int32le('type'), _.int32le('scale'),
_.int32le('x'), _.int32le('y')]),
_.struct('body', [_.int32le('height'), _.int32le('build')]),
_.struct('personal', [
_.char16le('name', 22),
_.char16le('creator', 22),
_.int32le('gender'),
_.int32le('birthMonth'),
_.int32le('birthDay'),
_.int32le('favoriteColor'),
_.uint8('favorite'),
_.uint8('copyable'),
_.uint8('ngWord'),
_.uint8('localonly'),
_.int32le('regionMove'),
_.int32le('fontRegion'),
_.int32le('roomIndex'),
_.int32le('positionInRoom'),
_.int32le('birthPlatform')
]),
_.struct('createID', [FFLCreateID]),
_.uint16le('padding_0'),
_.int32le('authorType'),
_.uint8('authorID', 8) // stub
]);
/**
* @public
* Size of FFLStoreData, a structure not included currently.
*/
const FFLStoreData_size = 96; // sizeof(FFLStoreData)
// ---------------------- Common Color Mask Definitions ----------------------
/** @package */
const commonColorEnableMask = (1 << 31);
/**
* Applies (unofficial) mask: FFLI_NN_MII_COMMON_COLOR_ENABLE_MASK
* to a common color index to indicate to FFL which color table it should use.
* @param {number} color - The color index to flag.
* @returns {number} The flagged color index to use in FFLiCharinfo.
*/
const commonColorMask = color => color | commonColorEnableMask;
/**
* Removes (unofficial) mask: FFLI_NN_MII_COMMON_COLOR_ENABLE_MASK
* to a common color index to reveal the original common color index.
* @param {number} color - The flagged color index.
* @returns {number} The original color index before flagging.
*/
const commonColorUnmask = color => (color & ~commonColorEnableMask) === 0
// Only unmask color if the mask is enabled.
? color
: color & ~commonColorEnableMask;
// --------------------- Begin FFLiCharModel Definitions ---------------------
const FFLAdditionalInfo = _.struct([
_.char16le('name', 22),
_.char16le('creator', 22),
_.struct('createID', [FFLCreateID]),
_.byte('_padding0', 2), // alignment
_.struct('skinColor', [FFLColor]),
_.uint32le('flags'),
// _.ubitLE('hairFlip', 1),
// _.ubitLE('fontRegion', 2),
// _.ubitLE('ngWord', 1),
// _.ubitLE('build', 7),
// _.ubitLE('height', 7),
// _.ubitLE('favoriteColor', 4),
// _.ubitLE('birthDay', 5),
// _.ubitLE('birthMonth', 4),
// _.ubitLE('gender', 1),
_.uint8('facelineType'),
_.uint8('hairType'),
_.byte('_padding1', 2) // alignment
]);
const FFLiRenderTexture = _.struct([
// STUB: four pointers in one field
_uintptr('pTexture2DRenderBufferColorTargetDepthTarget', 4)
]);
/**
* @typedef {Object} FFLiFacelineTextureTempObject
* @property {number} pTextureFaceLine
* @property {FFLDrawParam} drawParamFaceLine
* @property {number} pTextureFaceMake
* @property {FFLDrawParam} drawParamFaceMake
* @property {number} pTextureFaceBeard
* @property {FFLDrawParam} drawParamFaceBeard
* @property {Array<number>} pRenderTextureCompressorParam
*/
/** @type {_.StructInstance<FFLiFacelineTextureTempObject>} */
const FFLiFacelineTextureTempObject = _.struct([
_uintptr('pTextureFaceLine'),
_.struct('drawParamFaceLine', [FFLDrawParam]),
_uintptr('pTextureFaceMake'),
_.struct('drawParamFaceMake', [FFLDrawParam]),
_uintptr('pTextureFaceBeard'),
_.struct('drawParamFaceBeard', [FFLDrawParam]),
_uintptr('pRenderTextureCompressorParam', 2) // stub
]);
/**
* @typedef {Object} FFLiRawMaskDrawParam
* @property {Array<FFLDrawParam>} drawParamRawMaskPartsEye - 2
* @property {Array<FFLDrawParam>} drawParamRawMaskPartsEyebrow - 2
* @property {FFLDrawParam} drawParamRawMaskPartsMouth
* @property {Array<FFLDrawParam>} drawParamRawMaskPartsMustache - 2
* @property {FFLDrawParam} drawParamRawMaskPartsMole
* @property {FFLDrawParam} drawParamRawMaskPartsFill
*/
/** @type {_.StructInstance<FFLiRawMaskDrawParam>} */
const FFLiRawMaskDrawParam = _.struct([
_.struct('drawParamRawMaskPartsEye', [FFLDrawParam], 2),
_.struct('drawParamRawMaskPartsEyebrow', [FFLDrawParam], 2),
_.struct('drawParamRawMaskPartsMouth', [FFLDrawParam]),
_.struct('drawParamRawMaskPartsMustache', [FFLDrawParam], 2),
_.struct('drawParamRawMaskPartsMole', [FFLDrawParam]),
_.struct('drawParamRawMaskPartsFill', [FFLDrawParam])
]);
/**
* @typedef {Object} FFLiMaskTexturesTempObject
* @property {Array<number>} partsTextures
* @property {Array<number>} pRawMaskDrawParam
* @property {Uint8Array} _remaining
*/
/** @type {_.StructInstance<FFLiMaskTexturesTempObject>} */
const FFLiMaskTexturesTempObject = _.struct([
_.uint8('partsTextures', 0x154),
_uintptr('pRawMaskDrawParam', FFLExpression.MAX),
_.byte('_remaining', 0x388 - 620) // stub
]);
/**
* @typedef {Object} FFLiTextureTempObject
* @property {FFLiMaskTexturesTempObject} maskTextures
* @property {FFLiFacelineTextureTempObject} facelineTexture
*/
/** @type {_.StructInstance<FFLiTextureTempObject>} */
const FFLiTextureTempObject = _.struct([
_.struct('maskTextures', [FFLiMaskTexturesTempObject]),
_.struct('facelineTexture', [FFLiFacelineTextureTempObject])
]);
/**
* @typedef {Object} FFLiMaskTextures
* @property {Array<number>} pRenderTextures
*/
/** @type {_.StructInstance<FFLiMaskTextures>} */
const FFLiMaskTextures = _.struct([
_uintptr('pRenderTextures', FFLExpression.MAX)
]);
/** @package */
const FFL_RESOLUTION_MASK = 0x3fffffff;
/**
* @typedef {Object} FFLCharModelDesc
* @property {number} resolution
* @property {Uint32Array} allExpressionFlag
* @property {number} modelFlag
* @property {number} resourceType
*/
/** @type {_.StructInstance<FFLCharModelDesc>} */
const FFLCharModelDesc = _.struct([
_.uint32le('resolution'),
_.uint32le('allExpressionFlag', 3),
_.uint32le('modelFlag'),
_.uint32le('resourceType')
]);
/**
* @public
* Static default for FFLCharModelDesc.
* @type {FFLCharModelDesc}
*/
const FFLCharModelDescDefault = {
resolution: 512, // Typical default.
// Choose normal expression.
allExpressionFlag: new Uint32Array([1, 0, 0]), // Normal expression.
modelFlag: FFLModelFlag.NORMAL,
resourceType: FFLResourceType.HIGH // Default resource type.
};
/**
* @typedef {Object.<string, FFLVec3>} FFLBoundingBox
* @property {FFLVec3} min
* @property {FFLVec3} max
*/
/** @type {_.StructInstance<FFLBoundingBox>} */
const FFLBoundingBox = _.struct([
_.struct('min', [FFLVec3]),
_.struct('max', [FFLVec3])
]);
/**
* @typedef {Object.<string, FFLVec3>} FFLPartsTransform
* @property {FFLVec3} hatTranslate
* @property {FFLVec3} headFrontRotate
* @property {FFLVec3} headFrontTranslate
* @property {FFLVec3} headSideRotate
* @property {FFLVec3} headSideTranslate
* @property {FFLVec3} headTopRotate
* @property {FFLVec3} headTopTranslate
*/
/** @type {_.StructInstance<FFLPartsTransform>} */
const FFLPartsTransform = _.struct([
_.struct('hatTranslate', [FFLVec3]),
_.struct('headFrontRotate', [FFLVec3]),
_.struct('headFrontTranslate', [FFLVec3]),
_.struct('headSideRotate', [FFLVec3]),
_.struct('headSideTranslate', [FFLVec3]),
_.struct('headTopRotate', [FFLVec3]),
_.struct('headTopTranslate', [FFLVec3])
]);
/**
* @typedef {Object} FFLiCharModel
* @property {FFLiCharInfo} charInfo
* @property {FFLCharModelDesc} charModelDesc
* @property {FFLExpression} expression
* @property {number} pTextureTempObject
* @property {Array<FFLDrawParam>} drawParam
* @property {Array<number>} pShapeData
* @property {Array<Object>} facelineRenderTexture
* @property {Array<number>} pCapGlassNoselineTextures
* @property {FFLiMaskTextures} maskTextures
* @property {Array<FFLVec3>} beardHairFaceCenterPos
* @property {FFLPartsTransform} partsTransform
* @property {number} modelType
* @property {Array<FFLBoundingBox>} boundingBox
*/
/** @type {_.StructInstance<FFLiCharModel>} */
const FFLiCharModel = _.struct([
_.struct('charInfo', [FFLiCharInfo]),
_.struct('charModelDesc', [FFLCharModelDesc]),
_.uint32le('expression'), // enum FFLExpression
_uintptr('pTextureTempObject'), // stub
_.struct('drawParam', [FFLDrawParam], FFLiShapeType.MAX),
_uintptr('pShapeData', FFLiShapeType.MAX),
_.struct('facelineRenderTexture', [FFLiRenderTexture]),
_uintptr('pCapGlassNoselineTextures', 3),
_.struct('maskTextures', [FFLiMaskTextures]),
_.struct('beardHairFaceCenterPos', [FFLVec3], 3),
_.struct('partsTransform', [FFLPartsTransform]),
_.uint32le('modelType'), // enum FFLModelType
// FFLBoundingBox[FFL_MODEL_TYPE_MAX = 3]
_.struct('boundingBox', [FFLBoundingBox], 3)
]);
/**
* @enum {number}
*/
const FFLDataSource = {
OFFICIAL: 0,
DEFAULT: 1,
MIDDLE_DB: 2,
STORE_DATA_OFFICIAL: 3,
STORE_DATA: 4,
BUFFER: 5,
DIRECT_POINTER: 6
};
/**
* @typedef {Object} FFLCharModelSource
* @property {number} dataSource
* @property {number} pBuffer
* @property {number} index
*/
/** @type {_.StructInstance<FFLCharModelSource>} */
const FFLCharModelSource = _.struct([
_.uint32le('dataSource'),
_uintptr('pBuffer'),
_.uint16le('index')
]);
// The enums below are only for FFLiGetRandomCharInfo.
// Hence, why each one has a value called ALL.
/**
* @enum {number}
*/
const FFLGender = {
MALE: 0,
FEMALE: 1,
ALL: 2
};
/**
* @enum {number}
*/
const FFLAge = {
CHILD: 0,
ADULT: 1,
ELDER: 2,
ALL: 3
};
/**
* @enum {number}
*/
const FFLRace = {
BLACK: 0,
WHITE: 1,
ASIAN: 2,
ALL: 3
};
/**
* @typedef {Object} FFLResourceDesc
* @property {Array<number>} pData
* @property {Array<number>} size
*/
/**
* @type {_.StructInstance<FFLResourceDesc>}
*/
const FFLResourceDesc = _.struct([
_uintptr('pData', FFLResourceType.MAX),
_.uint32le('size', FFLResourceType.MAX)
]);
// // ---------------------------------------------------------------------
// // Texture Management
// // ---------------------------------------------------------------------
// ------------------------- Texture Related Structs -------------------------
/**
* @enum {number}
*/
const FFLTextureFormat = {
R8_UNORM: 0,
R8_G8_UNORM: 1,
R8_G8_B8_A8_UNORM: 2,
MAX: 3
};
/**
* @typedef {Object} FFLTextureInfo
* @property {number} width
* @property {number} height
* @property {number} mipCount
* @property {FFLTextureFormat} format
* @property {number} isGX2Tiled
* @property {number} imageSize
* @property {number} imagePtr
* @property {number} mipSize
* @property {number} mipPtr
* @property {Array<number>} mipLevelOffset
*/
/**
* @type {_.StructInstance<FFLTextureInfo>}
*/
const FFLTextureInfo = _.struct([
_.uint16le('width'),
_.uint16le('height'),
_.uint8('mipCount'),
_.uint8('format'),
_.uint8('isGX2Tiled'),
_.byte('_padding', 1),
_.uint32le('imageSize'),
_uintptr('imagePtr'),
_.uint32le('mipSize'),
_uintptr('mipPtr'),
_.uint32le('mipLevelOffset', 13)
]);
const FFLTextureCallback = _.struct([
_uintptr('pObj'),
_.uint8('useOriginalTileMode'),
_.byte('_padding', 3), // alignment
_uintptr('pCreateFunc'),
_uintptr('pDeleteFunc')
]);
// ------------------------ Class: TextureManager -----------------------------
/**
* Manages THREE.Texture objects created via FFL.
* Must be instantiated after FFL is fully initialized.
*/
class TextureManager {
/**
* @constructor
* Constructs the TextureManager. This MUST be created after initializing FFL.
* @param {Module} [module=window.Module] - The Emscripten module.
*/
constructor(module = window.Module) {
/** @private */
this._module = module;
/** @private */
this._textures = new Map(); // Internal map of texture id -> THREE.Texture.
/** @private */
this._textureCallbackPtr = 0;
/**
* @public
* Controls whether or not the TextureManager
* will log creations and deletions of textures
* in order to better track memory allocations.
*/
this.logging = false;
this._setupTextureCallbacks();
}
/**
* @private
* Sets up texture creation and deletion callbacks
* with the module. Called by the constructor.
*/
_setupTextureCallbacks() {
const mod = this._module;
// Bind the callbacks to this instance.
/** @private */
this._createCallback = mod.addFunction(this._textureCreateFunc.bind(this), 'vppp');
/** @private */
this._deleteCallback = mod.addFunction(this._textureDeleteFunc.bind(this), 'vpp');
const textureCallback = {
pObj: 0,
useOriginalTileMode: false,
_padding: [0, 0, 0],
pCreateFunc: this._createCallback,
pDeleteFunc: this._deleteCallback
};
const packed = FFLTextureCallback.pack(textureCallback);
/** @private */
this._textureCallbackPtr = mod._malloc(FFLTextureCallback.size);
mod.HEAPU8.set(new Uint8Array(packed), this._textureCallbackPtr);
mod._FFLSetTextureCallback(this._textureCallbackPtr);
// TODO: module.removeFunction(this._createCallback);
}
/**
* @private
* @param {number} format - Enum value for FFLTextureFormat.
* @returns {THREE.PixelFormat} Three.js texture format constant.
* @throws {Error} Unexpected FFLTextureFormat value
*
* Note that this function won't work on WebGL1Renderer in Three.js r137-r162 since R and RG textures need to use Luminance(Alpha)Format
* - (you'd somehow need to detect which renderer is used)
*/
_getTextureFormat(format) {