Debian Codegraph @

src/scene/load/pasgltf/CastlePasGLTF.pas

raw
1 (******************************************************************************
2 * PasGLTF *
3 ******************************************************************************
4 * Version 2020-02-12-00-29 *
5 ******************************************************************************
6 * zlib license *
7 *============================================================================*
8 * *
9 * Copyright (C) 2018-2020, Benjamin Rosseaux ([email protected]) *
10 * *
11 * This software is provided 'as-is', without any express or implied *
12 * warranty. In no event will the authors be held liable for any damages *
13 * arising from the use of this software. *
14 * *
15 * Permission is granted to anyone to use this software for any purpose, *
16 * including commercial applications, and to alter it and redistribute it *
17 * freely, subject to the following restrictions: *
18 * *
19 * 1. The origin of this software must not be misrepresented; you must not *
20 * claim that you wrote the original software. If you use this software *
21 * in a product, an acknowledgement in the product documentation would be *
22 * appreciated but is not required. *
23 * 2. Altered source versions must be plainly marked as such, and must not be *
24 * misrepresented as being the original software. *
25 * 3. This notice may not be removed or altered from any source distribution. *
26 * *
27 ******************************************************************************
28 * General guidelines for code contributors *
29 *============================================================================*
30 * *
31 * 1. Make sure you are legally allowed to make a contribution under the zlib *
32 * license. *
33 * 2. The zlib license header goes at the top of each source file, with *
34 * appropriate copyright notice. *
35 * 3. After a pull request, check the status of your pull request on *
36 * http://github.com/BeRo1985/pasGLTF *
37 * 4. Write code which's compatible with newer modern Delphi versions and *
38 * FreePascal >= 3.0.0 *
39 * 5. Don't use Delphi-only, FreePascal-only or Lazarus-only libraries/units, *
40 * but if needed, make it out-ifdef-able. *
41 * 6. No use of third-party libraries/units as possible, but if needed, make *
42 * it out-ifdef-able. *
43 * 7. Try to use const when possible. *
44 * 8. Make sure to comment out writeln, used while debugging. *
45 * 9. Make sure the code compiles on 32-bit and 64-bit platforms (x86-32, *
46 * x86-64, ARM, ARM64, etc.). *
47 * *
48 ******************************************************************************)
49 unit CastlePasGLTF;
50 {$ifdef fpc}
51 {$mode delphi}
52 {$ifdef cpui386}
53 {$define cpu386}
54 {$endif}
55 {$ifdef cpu386}
56 {$asmmode intel}
57 {$endif}
58 {$ifdef cpuamd64}
59 {$asmmode intel}
60 {$endif}
61 {$ifdef FPC_LITTLE_ENDIAN}
62 {$define LITTLE_ENDIAN}
63 {$else}
64 {$ifdef FPC_BIG_ENDIAN}
65 {$define BIG_ENDIAN}
66 {$endif}
67 {$endif}
68 {-$pic off}
69 {$ifdef fpc_has_internal_sar}
70 {$define HasSAR}
71 {$endif}
72 {$ifdef FPC_HAS_TYPE_EXTENDED}
73 {$define HAS_TYPE_EXTENDED}
74 {$else}
75 {$undef HAS_TYPE_EXTENDED}
76 {$endif}
77 {$ifdef FPC_HAS_TYPE_DOUBLE}
78 {$define HAS_TYPE_DOUBLE}
79 {$else}
80 {$undef HAS_TYPE_DOUBLE}
81 {$endif}
82 {$ifdef FPC_HAS_TYPE_SINGLE}
83 {$define HAS_TYPE_SINGLE}
84 {$else}
85 {$undef HAS_TYPE_SINGLE}
86 {$endif}
87 {$define CAN_INLINE}
88 {$define HAS_ADVANCED_RECORDS}
89
90 { CGE: disable FPC warnings at range check errors in constants here.
91 Note that we need to include more than affected lines in warnings off/on
92 section, to effectively disable warnings -- the affected lines are in generic
93 class and it seems they are reported at specialization. }
94 {$warnings off}
95 {$else}
96 {$warn COMBINING_SIGNED_UNSIGNED off} // CGE added
97 {$if CompilerVersion >= 32} {$warn COMBINING_SIGNED_UNSIGNED64 off} {$endif} // only since Delphi 10.2 // CGE added
98 {$warn IMPLICIT_STRING_CAST off} // CGE added
99 {$realcompatibility off}
100 {$localsymbols on}
101 {$define LITTLE_ENDIAN}
102 {$ifndef cpu64}
103 {$define cpu32}
104 {$endif}
105 {$define HAS_TYPE_EXTENDED}
106 {$define HAS_TYPE_DOUBLE}
107 {$define HAS_TYPE_SINGLE}
108 {$undef CAN_INLINE}
109 {$undef HAS_ADVANCED_RECORDS}
110 {$ifndef BCB}
111 {$ifdef ver120}
112 {$define Delphi4or5}
113 {$endif}
114 {$ifdef ver130}
115 {$define Delphi4or5}
116 {$endif}
117 {$ifdef ver140}
118 {$define Delphi6}
119 {$endif}
120 {$ifdef ver150}
121 {$define Delphi7}
122 {$endif}
123 {$ifdef ver170}
124 {$define Delphi2005}
125 {$endif}
126 {$else}
127 {$ifdef ver120}
128 {$define Delphi4or5}
129 {$define BCB4}
130 {$endif}
131 {$ifdef ver130}
132 {$define Delphi4or5}
133 {$endif}
134 {$endif}
135 {$ifdef conditionalexpressions}
136 {$if CompilerVersion>=24.0}
137 {$legacyifend on}
138 {$ifend}
139 {$if CompilerVersion>=14.0}
140 {$if CompilerVersion=14.0}
141 {$define Delphi6}
142 {$ifend}
143 {$define Delphi6AndUp}
144 {$ifend}
145 {$if CompilerVersion>=15.0}
146 {$if CompilerVersion=15.0}
147 {$define Delphi7}
148 {$ifend}
149 {$define Delphi7AndUp}
150 {$ifend}
151 {$if CompilerVersion>=17.0}
152 {$if CompilerVersion=17.0}
153 {$define Delphi2005}
154 {$ifend}
155 {$define Delphi2005AndUp}
156 {$ifend}
157 {$if CompilerVersion>=18.0}
158 {$if CompilerVersion=18.0}
159 {$define BDS2006}
160 {$define Delphi2006}
161 {$ifend}
162 {$define Delphi2006AndUp}
163 {$define CAN_INLINE}
164 {$define HAS_ADVANCED_RECORDS}
165 {$ifend}
166 {$if CompilerVersion>=18.5}
167 {$if CompilerVersion=18.5}
168 {$define Delphi2007}
169 {$ifend}
170 {$define Delphi2007AndUp}
171 {$ifend}
172 {$if CompilerVersion=19.0}
173 {$define Delphi2007Net}
174 {$ifend}
175 {$if CompilerVersion>=20.0}
176 {$if CompilerVersion=20.0}
177 {$define Delphi2009}
178 {$ifend}
179 {$define Delphi2009AndUp}
180 {$ifend}
181 {$if CompilerVersion>=21.0}
182 {$if CompilerVersion=21.0}
183 {$define Delphi2010}
184 {$ifend}
185 {$define Delphi2010AndUp}
186 {$ifend}
187 {$if CompilerVersion>=22.0}
188 {$if CompilerVersion=22.0}
189 {$define DelphiXE}
190 {$ifend}
191 {$define DelphiXEAndUp}
192 {$ifend}
193 {$if CompilerVersion>=23.0}
194 {$if CompilerVersion=23.0}
195 {$define DelphiXE2}
196 {$ifend}
197 {$define DelphiXE2AndUp}
198 {$ifend}
199 {$if CompilerVersion>=24.0}
200 {$legacyifend on}
201 {$if CompilerVersion=24.0}
202 {$define DelphiXE3}
203 {$ifend}
204 {$define DelphiXE3AndUp}
205 {$ifend}
206 {$if CompilerVersion>=25.0}
207 {$if CompilerVersion=25.0}
208 {$define DelphiXE4}
209 {$ifend}
210 {$define DelphiXE4AndUp}
211 {$ifend}
212 {$if CompilerVersion>=26.0}
213 {$if CompilerVersion=26.0}
214 {$define DelphiXE5}
215 {$ifend}
216 {$define DelphiXE5AndUp}
217 {$ifend}
218 {$if CompilerVersion>=27.0}
219 {$if CompilerVersion=27.0}
220 {$define DelphiXE6}
221 {$ifend}
222 {$define DelphiXE6AndUp}
223 {$ifend}
224 {$if CompilerVersion>=28.0}
225 {$if CompilerVersion=28.0}
226 {$define DelphiXE7}
227 {$ifend}
228 {$define DelphiXE7AndUp}
229 {$ifend}
230 {$if CompilerVersion>=29.0}
231 {$if CompilerVersion=29.0}
232 {$define DelphiXE8}
233 {$ifend}
234 {$define DelphiXE8AndUp}
235 {$ifend}
236 {$if CompilerVersion>=30.0}
237 {$if CompilerVersion=30.0}
238 {$define Delphi10Seattle}
239 {$ifend}
240 {$define Delphi10SeattleAndUp}
241 {$ifend}
242 {$if CompilerVersion>=31.0}
243 {$if CompilerVersion=31.0}
244 {$define Delphi10Berlin}
245 {$ifend}
246 {$define Delphi10BerlinAndUp}
247 {$ifend}
248 {$if CompilerVersion>=32.0}
249 {$if CompilerVersion=32.0}
250 {$define Delphi10Tokyo}
251 {$ifend}
252 {$define Delphi10TokyoAndUp}
253 {$ifend}
254 {$if CompilerVersion>=33.0}
255 {$if CompilerVersion=33.0}
256 {$define Delphi10Rio}
257 {$ifend}
258 {$define Delphi10RioAndUp}
259 {$ifend}
260 {$endif}
261 {$ifndef Delphi4or5}
262 {$ifndef BCB}
263 {$define Delphi6AndUp}
264 {$endif}
265 {$ifndef Delphi6}
266 {$define BCB6OrDelphi7AndUp}
267 {$ifndef BCB}
268 {$define Delphi7AndUp}
269 {$endif}
270 {$ifndef BCB}
271 {$ifndef Delphi7}
272 {$ifndef Delphi2005}
273 {$define BDS2006AndUp}
274 {$endif}
275 {$endif}
276 {$endif}
277 {$endif}
278 {$endif}
279 {$ifdef Delphi6AndUp}
280 {$warn symbol_platform off}
281 {$warn symbol_deprecated off}
282 {$endif}
283 {$endif}
284 {$if defined(Win32) or defined(Win64)}
285 {$define Windows}
286 {$ifend}
287 {$rangechecks off}
288 {$extendedsyntax on}
289 {$writeableconst on}
290 {$hints off}
291 {$booleval off}
292 {$typedaddress off}
293 {$stackframes off}
294 {$varstringchecks on}
295 {$typeinfo on}
296 {$overflowchecks off}
297 {$longstrings on}
298 {$openstrings on}
299 {$ifndef HAS_TYPE_SINGLE}
300 {$error No single floating point precision}
301 {$endif}
302 {$ifndef HAS_TYPE_DOUBLE}
303 {$error No double floating point precision}
304 {$endif}
305 {$scopedenums on}
306 {$ifndef fpc}
307 {$ifdef conditionalexpressions}
308 {$if CompilerVersion>=24.0}
309 {$legacyifend on}
310 {$ifend}
311 {$endif}
312 {$endif}
313
314 interface
315
316 uses SysUtils,
317 Classes,
318 Math,
319 CastlePasJSON;
320
321 type PPPasGLTFInt8=^PPasGLTFInt8;
322 PPasGLTFInt8=^TPasGLTFInt8;
323 TPasGLTFInt8={$ifdef fpc}Int8{$else}shortint{$endif};
324
325 PPPasGLTFUInt8=^PPasGLTFUInt8;
326 PPasGLTFUInt8=^TPasGLTFUInt8;
327 TPasGLTFUInt8={$ifdef fpc}UInt8{$else}byte{$endif};
328
329 PPPasGLTFUInt8Array=^PPasGLTFUInt8Array;
330 PPasGLTFUInt8Array=^TPasGLTFUInt8Array;
331 TPasGLTFUInt8Array=array[0..65535] of TPasGLTFUInt8;
332
333 TPasGLTFUInt8DynamicArray=array of TPasGLTFUInt8;
334
335 PPPasGLTFInt16=^PPasGLTFInt16;
336 PPasGLTFInt16=^TPasGLTFInt16;
337 TPasGLTFInt16={$ifdef fpc}Int16{$else}smallint{$endif};
338
339 PPPasGLTFUInt16=^PPasGLTFUInt16;
340 PPasGLTFUInt16=^TPasGLTFUInt16;
341 TPasGLTFUInt16={$ifdef fpc}UInt16{$else}word{$endif};
342
343 PPPasGLTFInt32=^PPasGLTFInt32;
344 PPasGLTFInt32=^TPasGLTFInt32;
345 TPasGLTFInt32={$ifdef fpc}Int32{$else}Integer{$endif};
346
347 TPasGLTFInt32DynamicArray=array of TPasGLTFInt32;
348
349 PPPasGLTFUInt32=^PPasGLTFUInt32;
350 PPasGLTFUInt32=^TPasGLTFUInt32;
351 TPasGLTFUInt32={$ifdef fpc}UInt32{$else}Cardinal{$endif};
352
353 PPPasGLTFUInt32Array=^PPasGLTFUInt32Array;
354 PPasGLTFUInt32Array=^TPasGLTFUInt32Array;
355 TPasGLTFUInt32Array=array[0..65535] of TPasGLTFUInt32;
356
357 TPasGLTFUInt32DynamicArray=array of TPasGLTFUInt32;
358
359 PPPasGLTFInt64=^PPasGLTFInt64;
360 PPasGLTFInt64=^TPasGLTFInt64;
361 TPasGLTFInt64=Int64;
362
363 TPasGLTFInt64DynamicArray=array of TPasGLTFInt64;
364
365 PPPasGLTFUInt64=^PPasGLTFUInt64;
366 PPasGLTFUInt64=^TPasGLTFUInt64;
367 TPasGLTFUInt64=UInt64;
368
369 TPasGLTFUInt64DynamicArray=array of TPasGLTFUInt64;
370
371 PPPasGLTFChar=^PAnsiChar;
372 PPasGLTFChar=PAnsiChar;
373 TPasGLTFChar=AnsiChar;
374
375 PPPasGLTFRawByteChar=^PAnsiChar;
376 PPasGLTFRawByteChar=PAnsiChar;
377 TPasGLTFRawByteChar=AnsiChar;
378
379 PPPasGLTFUTF16Char=^PWideChar;
380 PPasGLTFUTF16Char=PWideChar;
381 TPasGLTFUTF16Char=WideChar;
382
383 PPPasGLTFPointer=^PPasGLTFPointer;
384 PPasGLTFPointer=^TPasGLTFPointer;
385 TPasGLTFPointer=Pointer;
386
387 PPPasGLTFPointers=^PPasGLTFPointers;
388 PPasGLTFPointers=^TPasGLTFPointers;
389 TPasGLTFPointers=array[0..65535] of TPasGLTFPointer;
390
391 PPPasGLTFVoid=^PPasGLTFVoid;
392 PPasGLTFVoid=TPasGLTFPointer;
393
394 PPPasGLTFFloat=^PPasGLTFFloat;
395 PPasGLTFFloat=^TPasGLTFFloat;
396 TPasGLTFFloat=Single;
397
398 TPasGLTFFloatDynamicArray=array of TPasGLTFFloat;
399
400 PPPasGLTFDouble=^PPasGLTFDouble;
401 PPasGLTFDouble=^TPasGLTFDouble;
402 TPasGLTFDouble=Double;
403
404 TPasGLTFDoubleDynamicArray=array of TPasGLTFDouble;
405
406 PPPasGLTFPtrUInt=^PPasGLTFPtrUInt;
407 PPPasGLTFPtrInt=^PPasGLTFPtrInt;
408 PPasGLTFPtrUInt=^TPasGLTFPtrUInt;
409 PPasGLTFPtrInt=^TPasGLTFPtrInt;
410 {$ifdef fpc}
411 TPasGLTFPtrUInt=PtrUInt;
412 TPasGLTFPtrInt=PtrInt;
413 {$undef OldDelphi}
414 {$else}
415 {$ifdef conditionalexpressions}
416 {$if CompilerVersion>=23.0}
417 {$undef OldDelphi}
418 TPasGLTFPtrUInt=NativeUInt;
419 TPasGLTFPtrInt=NativeInt;
420 {$else}
421 {$define OldDelphi}
422 {$ifend}
423 {$else}
424 {$define OldDelphi}
425 {$endif}
426 {$endif}
427 {$ifdef OldDelphi}
428 {$ifdef cpu64}
429 TPasGLTFPtrUInt=uint64;
430 TPasGLTFPtrInt=int64;
431 {$else}
432 TPasGLTFPtrUInt=Cardinal;
433 TPasGLTFPtrInt=Integer;
434 {$endif}
435 {$endif}
436
437 PPPasGLTFSizeUInt=^PPasGLTFSizeUInt;
438 PPasGLTFSizeUInt=^TPasGLTFSizeUInt;
439 TPasGLTFSizeUInt=TPasGLTFPtrUInt;
440
441 TPasGLTFSizeUIntDynamicArray=array of TPasGLTFSizeUInt;
442
443 PPPasGLTFSizeInt=^PPasGLTFSizeInt;
444 PPasGLTFSizeInt=^TPasGLTFSizeInt;
445 TPasGLTFSizeInt=TPasGLTFPtrInt;
446
447 TPasGLTFSizeIntDynamicArray=array of TPasGLTFSizeInt;
448
449 PPPasGLTFNativeUInt=^PPasGLTFNativeUInt;
450 PPasGLTFNativeUInt=^TPasGLTFNativeUInt;
451 TPasGLTFNativeUInt=TPasGLTFPtrUInt;
452
453 PPPasGLTFNativeInt=^PPasGLTFNativeInt;
454 PPasGLTFNativeInt=^TPasGLTFNativeInt;
455 TPasGLTFNativeInt=TPasGLTFPtrInt;
456
457 PPPasGLTFSize=^PPasGLTFSizeUInt;
458 PPasGLTFSize=^TPasGLTFSizeUInt;
459 TPasGLTFSize=TPasGLTFPtrUInt;
460
461 PPPasGLTFPtrDiff=^PPasGLTFPtrDiff;
462 PPasGLTFPtrDiff=^TPasGLTFPtrDiff;
463 TPasGLTFPtrDiff=TPasGLTFPtrInt;
464
465 PPPasGLTFRawByteString=^PPasGLTFRawByteString;
466 PPasGLTFRawByteString=^TPasGLTFRawByteString;
467 TPasGLTFRawByteString={$if declared(RawByteString)}RawByteString{$else}AnsiString{$ifend};
468
469 PPPasGLTFUTF8String=^PPasGLTFUTF8String;
470 PPasGLTFUTF8String=^TPasGLTFUTF8String;
471 TPasGLTFUTF8String={$if declared(UTF8String)}UTF8String{$else}AnsiString{$ifend};
472
473 PPPasGLTFUTF16String=^PPasGLTFUTF16String;
474 PPasGLTFUTF16String=^TPasGLTFUTF16String;
475 TPasGLTFUTF16String={$if declared(UnicodeString)}UnicodeString{$else}WideString{$ifend};
476
477 EPasGLTF=class(Exception);
478
479 EPasGLTFInvalidDocument=class(EPasGLTF);
480
481 EPasGLTFInvalidBase64=class(EPasGLTF);
482
483 TPasGLTFTypedSort<T>=class
484 private
485 type TStackItem=record
486 Left:TPasGLTFSizeInt;
487 Right:TPasGLTFSizeInt;
488 Depth:TPasGLTFInt32;
489 end;
490 PStackItem=^TStackItem;
491 public
492 type TPasGLTFTypedSortCompareFunction=function(const a,b:T):TPasGLTFInt32;
493 {$ifndef fpc}
494 private
495 class function BSRDWord(aValue:TPasGLTFUInt32):TPasGLTFInt32; static;
496 {$endif}
497 public
498 class procedure IntroSort(const pItems:TPasGLTFPointer;const pLeft,pRight:TPasGLTFSizeInt;const pCompareFunc:TPasGLTFTypedSortCompareFunction); static;
499 end;
500
501 TPasGLTFDynamicArray<T>=class
502 private
503 type TValueEnumerator=record
504 private
505 fDynamicArray:TPasGLTFDynamicArray<T>;
506 fIndex:TPasGLTFSizeInt;
507 function GetCurrent:T; inline;
508 public
509 constructor Create(const aDynamicArray:TPasGLTFDynamicArray<T>);
510 function MoveNext:boolean; inline;
511 property Current:T read GetCurrent;
512 end;
513 private
514 fItems:array of T;
515 fCount:TPasGLTFSizeInt;
516 fAllocated:TPasGLTFSizeInt;
517 procedure SetCount(const pNewCount:TPasGLTFSizeInt);
518 function GetItem(const pIndex:TPasGLTFSizeInt):T; inline;
519 procedure SetItem(const pIndex:TPasGLTFSizeInt;const pItem:T); inline;
520 protected
521 public
522 constructor Create;
523 destructor Destroy; override;
524 procedure Clear;
525 function Add(const pItem:T):TPasGLTFSizeInt; overload;
526 function Add(const pItems:array of T):TPasGLTFSizeInt; overload;
527 procedure Insert(const pIndex:TPasGLTFSizeInt;const pItem:T);
528 procedure Delete(const pIndex:TPasGLTFSizeInt);
529 procedure Exchange(const pIndex,pWithIndex:TPasGLTFSizeInt); inline;
530 function GetEnumerator:TValueEnumerator;
531 function Memory:TPasGLTFPointer; inline;
532 property Count:TPasGLTFSizeInt read fCount write SetCount;
533 property Allocated:TPasGLTFSizeInt read fAllocated;
534 property Items[const pIndex:TPasGLTFSizeInt]:T read GetItem write SetItem; default;
535 end;
536
537 TPasGLTFObjectList<T:class>=class
538 private
539 type TValueEnumerator=record
540 private
541 fObjectList:TPasGLTFObjectList<T>;
542 fIndex:TPasGLTFSizeInt;
543 function GetCurrent:T; inline;
544 public
545 constructor Create(const aObjectList:TPasGLTFObjectList<T>);
546 function MoveNext:boolean; inline;
547 property Current:T read GetCurrent;
548 end;
549 private
550 fItems:array of T;
551 fCount:TPasGLTFSizeInt;
552 fAllocated:TPasGLTFSizeInt;
553 fOwnsObjects:boolean;
554 function RoundUpToPowerOfTwoSizeUInt(x:TPasGLTFSizeUInt):TPasGLTFSizeUInt;
555 procedure SetCount(const pNewCount:TPasGLTFSizeInt);
556 function GetItem(const pIndex:TPasGLTFSizeInt):T;
557 procedure SetItem(const pIndex:TPasGLTFSizeInt;const pItem:T);
558 public
559 constructor Create;
560 destructor Destroy; override;
561 procedure Clear;
562 function IndexOf(const pItem:T):TPasGLTFSizeInt;
563 function Add(const pItem:T):TPasGLTFSizeInt;
564 procedure Insert(const pIndex:TPasGLTFSizeInt;const pItem:T);
565 procedure Delete(const pIndex:TPasGLTFSizeInt);
566 procedure Remove(const pItem:T);
567 procedure Exchange(const pIndex,pWithIndex:TPasGLTFSizeInt);
568 function GetEnumerator:TValueEnumerator;
569 property Count:TPasGLTFSizeInt read fCount write SetCount;
570 property Allocated:TPasGLTFSizeInt read fAllocated;
571 property Items[const pIndex:TPasGLTFSizeInt]:T read GetItem write SetItem; default;
572 property OwnsObjects:boolean read fOwnsObjects write fOwnsObjects;
573 end;
574
575 TPasGLTFHashMapEntityIndices=array of TPasGLTFInt32;
576
577 TPasGLTFHashMapUInt128=array[0..1] of TPasGLTFUInt64;
578
579 TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>=class
580 public
581 const CELL_EMPTY=-1;
582 CELL_DELETED=-2;
583 ENT_EMPTY=-1;
584 ENT_DELETED=-2;
585 type TPasGLTFHashMapKey=TPasGLTFUTF8String;
586 PPasGLTFHashMapEntity=^TPasGLTFHashMapEntity;
587 TPasGLTFHashMapEntity=record
588 Key:TPasGLTFHashMapKey;
589 Value:TPasGLTFHashMapValue;
590 end;
591 TPasGLTFHashMapEntities=array of TPasGLTFHashMapEntity;
592 private
593 type TPasGLTFHashMapEntityEnumerator=record
594 private
595 fHashMap:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>;
596 fIndex:TPasGLTFSizeInt;
597 function GetCurrent:TPasGLTFHashMapEntity; inline;
598 public
599 constructor Create(const aHashMap:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>);
600 function MoveNext:boolean; inline;
601 property Current:TPasGLTFHashMapEntity read GetCurrent;
602 end;
603 TPasGLTFHashMapKeyEnumerator=record
604 private
605 fHashMap:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>;
606 fIndex:TPasGLTFSizeInt;
607 function GetCurrent:TPasGLTFHashMapKey; inline;
608 public
609 constructor Create(const aHashMap:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>);
610 function MoveNext:boolean; inline;
611 property Current:TPasGLTFHashMapKey read GetCurrent;
612 end;
613 TPasGLTFHashMapValueEnumerator=record
614 private
615 fHashMap:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>;
616 fIndex:TPasGLTFSizeInt;
617 function GetCurrent:TPasGLTFHashMapValue; inline;
618 public
619 constructor Create(const aHashMap:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>);
620 function MoveNext:boolean; inline;
621 property Current:TPasGLTFHashMapValue read GetCurrent;
622 end;
623 TPasGLTFHashMapEntitiesObject=class
624 private
625 fOwner:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>;
626 public
627 constructor Create(const aOwner:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>);
628 function GetEnumerator:TPasGLTFHashMapEntityEnumerator;
629 end;
630 TPasGLTFHashMapKeysObject=class
631 private
632 fOwner:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>;
633 public
634 constructor Create(const aOwner:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>);
635 function GetEnumerator:TPasGLTFHashMapKeyEnumerator;
636 end;
637 TPasGLTFHashMapValuesObject=class
638 private
639 fOwner:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>;
640 function GetValue(const Key:TPasGLTFHashMapKey):TPasGLTFHashMapValue; inline;
641 procedure SetValue(const Key:TPasGLTFHashMapKey;const aValue:TPasGLTFHashMapValue); inline;
642 public
643 constructor Create(const aOwner:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>);
644 function GetEnumerator:TPasGLTFHashMapValueEnumerator;
645 property Values[const Key:TPasGLTFHashMapKey]:TPasGLTFHashMapValue read GetValue write SetValue; default;
646 end;
647 private
648 fRealSize:TPasGLTFInt32;
649 fLogSize:TPasGLTFInt32;
650 fSize:TPasGLTFInt32;
651 fEntities:TPasGLTFHashMapEntities;
652 fEntityToCellIndex:TPasGLTFHashMapEntityIndices;
653 fCellToEntityIndex:TPasGLTFHashMapEntityIndices;
654 fDefaultValue:TPasGLTFHashMapValue;
655 fCanShrink:boolean;
656 fEntitiesObject:TPasGLTFHashMapEntitiesObject;
657 fKeysObject:TPasGLTFHashMapKeysObject;
658 fValuesObject:TPasGLTFHashMapValuesObject;
659 function HashData(const Data:TPasGLTFPointer;const DataLength:TPasGLTFSizeUInt):TPasGLTFUInt32;
660 function HashKey(const Key:TPasGLTFHashMapKey):TPasGLTFUInt32;
661 function CompareKey(const KeyA,KeyB:TPasGLTFHashMapKey):boolean;
662 function FindCell(const Key:TPasGLTFHashMapKey):TPasGLTFUInt32;
663 procedure Resize;
664 protected
665 function GetValue(const Key:TPasGLTFHashMapKey):TPasGLTFHashMapValue;
666 procedure SetValue(const Key:TPasGLTFHashMapKey;const Value:TPasGLTFHashMapValue);
667 public
668 constructor Create(const DefaultValue:TPasGLTFHashMapValue);
669 destructor Destroy; override;
670 procedure Clear;
671 function Add(const Key:TPasGLTFHashMapKey;const Value:TPasGLTFHashMapValue):PPasGLTFHashMapEntity;
672 function Get(const Key:TPasGLTFHashMapKey;const CreateIfNotExist:boolean=false):PPasGLTFHashMapEntity;
673 function TryGet(const Key:TPasGLTFHashMapKey;out Value:TPasGLTFHashMapValue):boolean;
674 function ExistKey(const Key:TPasGLTFHashMapKey):boolean;
675 function Delete(const Key:TPasGLTFHashMapKey):boolean;
676 property EntityValues[const Key:TPasGLTFHashMapKey]:TPasGLTFHashMapValue read GetValue write SetValue; default;
677 property Entities:TPasGLTFHashMapEntitiesObject read fEntitiesObject;
678 property Keys:TPasGLTFHashMapKeysObject read fKeysObject;
679 property Values:TPasGLTFHashMapValuesObject read fValuesObject;
680 property CanShrink:boolean read fCanShrink write fCanShrink;
681 end;
682
683 TPasGLTF=class
684 public
685 type TBase64=class
686 public
687 const EncodingLookUpTable:array[0..63] of TPasGLTFRawByteChar=
688 (
689 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
690 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
691 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
692 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
693 );
694 DecodingLookUpTable:array[TPasGLTFRawByteChar] of TPasGLTFInt8=
695 (
696 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
697 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
698 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
699 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,
700 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
701 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
702 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
703 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,
704 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
705 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
706 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
707 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
708 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
709 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
710 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
711 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
712 );
713 public
714 class function Encode(const aData;const aDataLength:TPasGLTFSizeInt):TPasGLTFRawByteString; overload; static;
715 class function Encode(const aData:array of TPasGLTFUInt8):TPasGLTFRawByteString; overload; static;
716 class function Encode(const aData:TPasGLTFRawByteString):TPasGLTFRawByteString; overload; static;
717 class function Encode(const aData:TStream):TPasGLTFRawByteString; overload; static;
718 class function Decode(const aInput:TPasGLTFRawByteString;const aOutput:TStream):boolean; overload; static;
719 end;
720 TChunkHeader=packed record
721 ChunkLength:TPasGLTFUInt32;
722 ChunkType:TPasGLTFUInt32;
723 end;
724 PChunkHeader=^TChunkHeader;
725 TGLBHeader=packed record
726 Magic:TPasGLTFUInt32;
727 Version:TPasGLTFUInt32;
728 Length:TPasGLTFUInt32;
729 JSONChunkHeader:TChunkHeader;
730 end;
731 TVector2=array[0..1] of TPasGLTFFloat;
732 PVector2=^TVector2;
733 TVector2DynamicArray=array of TVector2;
734 TVector3=array[0..2] of TPasGLTFFloat;
735 PVector3=^TVector3;
736 TVector3DynamicArray=array of TVector3;
737 TVector4=array[0..3] of TPasGLTFFloat;
738 PVector4=^TVector4;
739 TVector4DynamicArray=array of TVector4;
740 TInt32Vector4=array[0..3] of TPasGLTFInt32;
741 PInt32Vector4=^TInt32Vector4;
742 TInt32Vector4DynamicArray=array of TInt32Vector4;
743 TUInt32Vector4=array[0..3] of TPasGLTFUInt32;
744 PUInt32Vector4=^TUInt32Vector4;
745 TUInt32Vector4DynamicArray=array of TUInt32Vector4;
746 TMatrix2x2=array[0..3] of TPasGLTFFloat;
747 PMatrix2x2=^TMatrix2x2;
748 TMatrix2x2DynamicArray=array of TMatrix2x2;
749 TMatrix3x3=array[0..9] of TPasGLTFFloat;
750 PMatrix3x3=^TMatrix3x3;
751 TMatrix3x3DynamicArray=array of TMatrix3x3;
752 TMatrix4x4=array[0..15] of TPasGLTFFloat;
753 PMatrix4x4=^TMatrix4x4;
754 TMatrix4x4DynamicArray=array of TMatrix4x4;
755 const ChunkHeaderSize=SizeOf(TChunkHeader);
756 GLBHeaderSize=SizeOf(TGLBHeader);
757 GLBHeaderMagicNativeEndianness=TPasGLTFUInt32($46546c67);
758 GLBHeaderMagicOtherEndianness=TPasGLTFUInt32($676c5446);
759 GLBChunkJSONNativeEndianness=TPasGLTFUInt32($4e4f534a);
760 GLBChunkJSONOtherEndianness=TPasGLTFUInt32($4a534f4e);
761 GLBChunkBinaryNativeEndianness=TPasGLTFUInt32($004e4942);
762 GLBChunkBinaryOtherEndianness=TPasGLTFUInt32($42494e00);
763 MimeTypeApplicationOctet='application/octet-stream';
764 MimeTypeImagePNG='image/png';
765 MimeTypeImageJPG='image/jpg';
766 type TDefaults=class
767 public
768 const AccessorNormalized=false;
769 MaterialAlphaCutoff=0.5;
770 MaterialDoubleSided=false;
771 IdentityScalar=1.0;
772 FloatSentinel=1e+27;
773 NullVector3:TVector3=(0.0,0.0,0.0);
774 IdentityVector3:TVector3=(1.0,1.0,1.0);
775 IdentityVector4:TVector4=(1.0,1.0,1.0,1.0);
776 IdentityQuaternion:TVector4=(0.0,0.0,0.0,1.0);
777 NullMatrix4x4:TMatrix4x4=(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0);
778 IdentityMatrix4x4:TMatrix4x4=(1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0);
779 end;
780 TDocument=class;
781 TBaseObject=class
782 private
783 fDocument:TDocument;
784 public
785 constructor Create(const aDocument:TDocument); reintroduce; virtual;
786 destructor Destroy; override;
787 end;
788 TBaseExtensionsExtrasObject=class(TBaseObject)
789 private
790 fExtensions:TPasJSONItemObject;
791 fExtras:TPasJSONItemObject;
792 public
793 constructor Create(const aDocument:TDocument); override;
794 destructor Destroy; override;
795 published
796 property Extensions:TPasJSONItemObject read fExtensions;
797 property Extras:TPasJSONItemObject read fExtras;
798 end;
799 TAttributes=TPasGLTFUTF8StringHashMap<TPasGLTFSizeInt>;
800 TAttributesList=TPasGLTFObjectList<TAttributes>;
801 TAccessor=class(TBaseExtensionsExtrasObject)
802 public
803 type TComponentType=
804 (
805 None=0,
806 SignedByte=5120,
807 UnsignedByte=5121,
808 SignedShort=5122,
809 UnsignedShort=5123,
810 UnsignedInt=5125,
811 Float=5126
812 );
813 PComponentType=^TComponentType;
814 TComponentTypeHelper=record helper for TComponentType
815 function GetSize:TPasGLTFSizeInt; inline;
816 end;
817 TRawComponentType=TPasGLTFUInt16;
818 PRawComponentType=^TRawComponentType;
819 TType=
820 (
821 None=0,
822 Scalar=1,
823 Vec2=2,
824 Vec3=3,
825 Vec4=4,
826 Mat2=5,
827 Mat3=6,
828 Mat4=7
829 );
830 PType=^TType;
831 TTypeHelper=record helper for TType
832 function GetComponentCount:TPasGLTFSizeInt; inline;
833 end;
834 TRawType=TPasGLTFUInt8;
835 PRawType=^TRawType;
836 TMinMaxDynamicArray=TPasGLTFDynamicArray<TPasGLTFFloat>;
837 TSparse=class(TBaseExtensionsExtrasObject)
838 public
839 type TIndices=class(TBaseExtensionsExtrasObject)
840 private
841 fComponentType:TComponentType;
842 fBufferView:TPasGLTFSizeInt;
843 fByteOffset:TPasGLTFSizeUInt;
844 fEmpty:boolean;
845 public
846 constructor Create(const aDocument:TDocument); override;
847 destructor Destroy; override;
848 published
849 property ComponentType:TComponentType read fComponentType write fComponentType default TComponentType.None;
850 property BufferView:TPasGLTFSizeInt read fBufferView write fBufferView default 0;
851 property ByteOffset:TPasGLTFSizeUInt read fByteOffset write fByteOffset default 0;
852 property Empty:boolean read fEmpty;
853 end;
854 TValues=class(TBaseExtensionsExtrasObject)
855 private
856 fBufferView:TPasGLTFSizeInt;
857 fByteOffset:TPasGLTFSizeUInt;
858 fEmpty:boolean;
859 public
860 constructor Create(const aDocument:TDocument); override;
861 destructor Destroy; override;
862 published
863 property BufferView:TPasGLTFSizeInt read fBufferView write fBufferView default 0;
864 property ByteOffset:TPasGLTFSizeUInt read fByteOffset write fByteOffset default 0;
865 property Empty:boolean read fEmpty;
866 end;
867 private
868 fCount:TPasGLTFSizeInt;
869 fIndices:TIndices;
870 fValues:TValues;
871 function GetEmpty:boolean;
872 public
873 constructor Create(const aDocument:TDocument); override;
874 destructor Destroy; override;
875 published
876 property Count:TPasGLTFSizeInt read fCount write fCount default 0;
877 property Indices:TIndices read fIndices;
878 property Values:TValues read fValues;
879 property Empty:boolean read GetEmpty;
880 end;
881 const TypeComponentCountTable:array[TType] of TPasGLTFSizeInt=(0,1,2,3,4,4,9,16);
882 private
883 fName:TPasGLTFUTF8String;
884 fComponentType:TComponentType;
885 fType:TType;
886 fBufferView:TPasGLTFSizeInt;
887 fByteOffset:TPasGLTFSizeUInt;
888 fCount:TPasGLTFSizeUInt;
889 fNormalized:boolean;
890 fMinArray:TMinMaxDynamicArray;
891 fMaxArray:TMinMaxDynamicArray;
892 fSparse:TSparse;
893 public
894 constructor Create(const aDocument:TDocument); override;
895 destructor Destroy; override;
896 function DecodeAsDoubleArray(const aForVertex:boolean=true):TPasGLTFDoubleDynamicArray;
897 function DecodeAsInt32Array(const aForVertex:boolean=true):TPasGLTFInt32DynamicArray;
898 function DecodeAsUInt32Array(const aForVertex:boolean=true):TPasGLTFUInt32DynamicArray;
899 function DecodeAsInt64Array(const aForVertex:boolean=true):TPasGLTFInt64DynamicArray;
900 function DecodeAsUInt64Array(const aForVertex:boolean=true):TPasGLTFUInt64DynamicArray;
901 function DecodeAsFloatArray(const aForVertex:boolean=true):TPasGLTFFloatDynamicArray;
902 function DecodeAsVector2Array(const aForVertex:boolean=true):TVector2DynamicArray;
903 function DecodeAsVector3Array(const aForVertex:boolean=true):TVector3DynamicArray;
904 function DecodeAsVector4Array(const aForVertex:boolean=true):TVector4DynamicArray;
905 function DecodeAsInt32Vector4Array(const aForVertex:boolean=true):TInt32Vector4DynamicArray;
906 function DecodeAsUInt32Vector4Array(const aForVertex:boolean=true):TUInt32Vector4DynamicArray;
907 function DecodeAsColorArray(const aForVertex:boolean=true):TVector4DynamicArray;
908 function DecodeAsMatrix2x2Array(const aForVertex:boolean=true):TMatrix2x2DynamicArray;
909 function DecodeAsMatrix3x3Array(const aForVertex:boolean=true):TMatrix3x3DynamicArray;
910 function DecodeAsMatrix4x4Array(const aForVertex:boolean=true):TMatrix4x4DynamicArray;
911 published
912 property ComponentType:TComponentType read fComponentType write fComponentType default TComponentType.None;
913 property Type_:TType read fType write fType default TType.None;
914 property BufferView:TPasGLTFSizeInt read fBufferView write fBufferView default -1;
915 property ByteOffset:TPasGLTFSizeUInt read fByteOffset write fByteOffset default 0;
916 property Count:TPasGLTFSizeUInt read fCount write fCount default 0;
917 property MinArray:TMinMaxDynamicArray read fMinArray;
918 property MaxArray:TMinMaxDynamicArray read fMaxArray;
919 property Normalized:boolean read fNormalized write fNormalized default false;
920 property Sparse:TSparse read fSparse;
921 end;
922 TAccessors=TPasGLTFObjectList<TAccessor>;
923 TAnimation=class(TBaseExtensionsExtrasObject)
924 public
925 type TChannel=class(TBaseExtensionsExtrasObject)
926 public
927 type TTarget=class(TBaseExtensionsExtrasObject)
928 private
929 fNode:TPasGLTFSizeInt;
930 fPath:TPasGLTFUTF8String;
931 fEmpty:boolean;
932 public
933 constructor Create(const aDocument:TDocument); override;
934 destructor Destroy; override;
935 published
936 property Node:TPasGLTFSizeInt read fNode write fNode default -1;
937 property Path:TPasGLTFUTF8String read fPath write fPath;
938 property Empty:boolean read fEmpty;
939 end;
940 private
941 fSampler:TPasGLTFSizeInt;
942 fTarget:TChannel.TTarget;
943 public
944 constructor Create(const aDocument:TDocument); override;
945 destructor Destroy; override;
946 published
947 property Sampler:TPasGLTFSizeInt read fSampler write fSampler default -1;
948 property Target:TChannel.TTarget read fTarget;
949 end;
950 TChannels=TPasGLTFObjectList<TChannel>;
951 TSampler=class(TBaseExtensionsExtrasObject)
952 public
953 // CGE: Workaround FPC problems (when generating debug info):
954 // PasGLTF.pas(5785,1) Error: Asm: Duplicate label DBG_$PASGLTF_$$_TTYPE
955 // PasGLTF.pas(5785,1) Error: Asm: Duplicate label DBGREF_$PASGLTF_$$_TTYPE
956 // You cannot define TType multiple times, so we rename to TCameraType, TSamplerType
957 //
958 // TODO: BeRo says fixed in FPC trunk, test.
959 // We need a workaround for FPC 3.0.4 anyway.
960 type TSamplerType=
961 (
962 Linear=0,
963 Step=1,
964 CubicSpline=2
965 );
966 PSamplerType=^TSamplerType;
967 private
968 fInput:TPasGLTFSizeInt;
969 fOutput:TPasGLTFSizeInt;
970 fInterpolation:TSamplerType;
971 public
972 constructor Create(const aDocument:TDocument); override;
973 destructor Destroy; override;
974 published
975 property Input:TPasGLTFSizeInt read fInput write fInput default -1;
976 property Output:TPasGLTFSizeInt read fOutput write fOutput default -1;
977 property Interpolation:TSamplerType read fInterpolation write fInterpolation default TSamplerType.Linear;
978 end;
979 TSamplers=TPasGLTFObjectList<TSampler>;
980 private
981 fName:TPasGLTFUTF8String;
982 fChannels:TChannels;
983 fSamplers:TSamplers;
984 public
985 constructor Create(const aDocument:TDocument); override;
986 destructor Destroy; override;
987 published
988 property Name:TPasGLTFUTF8String read fName write fName;
989 property Channels:TChannels read fChannels;
990 property Samplers:TSamplers read fSamplers;
991 end;
992 TAnimations=TPasGLTFObjectList<TAnimation>;
993 TAsset=class(TBaseExtensionsExtrasObject)
994 private
995 fCopyright:TPasGLTFUTF8String;
996 fGenerator:TPasGLTFUTF8String;
997 fMinVersion:TPasGLTFUTF8String;
998 fVersion:TPasGLTFUTF8String;
999 fEmpty:boolean;
1000 public
1001 constructor Create(const aDocument:TDocument); override;
1002 destructor Destroy; override;
1003 published
1004 property Copyright:TPasGLTFUTF8String read fCopyright write fCopyright;
1005 property Generator:TPasGLTFUTF8String read fGenerator write fGenerator;
1006 property MinVersion:TPasGLTFUTF8String read fMinVersion write fMinVersion;
1007 property Version:TPasGLTFUTF8String read fVersion write fVersion;
1008 property Empty:boolean read fEmpty;
1009 end;
1010 TBuffer=class(TBaseExtensionsExtrasObject)
1011 private
1012 fByteLength:TPasGLTFSizeUInt;
1013 fName:TPasGLTFUTF8String;
1014 fURI:TPasGLTFUTF8String;
1015 fData:TMemoryStream;
1016 public
1017 constructor Create(const aDocument:TDocument); override;
1018 destructor Destroy; override;
1019 procedure SetEmbeddedResourceData;
1020 published
1021 property ByteLength:TPasGLTFSizeUInt read fByteLength write fByteLength;
1022 property Name:TPasGLTFUTF8String read fName write fName;
1023 property URI:TPasGLTFUTF8String read fURI write fURI;
1024 property Data:TMemoryStream read fData write fData;
1025 end;
1026 TBuffers=TPasGLTFObjectList<TBuffer>;
1027 TBufferView=class(TBaseExtensionsExtrasObject)
1028 public
1029 type TTargetType=
1030 (
1031 None=0,
1032 ArrayBuffer=34962,
1033 ElementArrayBuffer=34963
1034 );
1035 PTargetType=^TTargetType;
1036 private
1037 fName:TPasGLTFUTF8String;
1038 fBuffer:TPasGLTFSizeInt;
1039 fByteOffset:TPasGLTFSizeUInt;
1040 fByteLength:TPasGLTFSizeUInt;
1041 fByteStride:TPasGLTFSizeUInt;
1042 fTarget:TTargetType;
1043 public
1044 constructor Create(const aDocument:TDocument); override;
1045 destructor Destroy; override;
1046 function Decode(const aSkipEvery:TPasGLTFSizeUInt;
1047 const aSkipBytes:TPasGLTFSizeUInt;
1048 const aElementSize:TPasGLTFSizeUInt;
1049 const aCount:TPasGLTFSizeUInt;
1050 const aType:TPasGLTF.TAccessor.TType;
1051 const aComponentCount:TPasGLTFSizeUInt;
1052 const aComponentType:TPasGLTF.TAccessor.TComponentType;
1053 const aComponentSize:TPasGLTFSizeUInt;
1054 const aByteOffset:TPasGLTFSizeUInt;
1055 const aNormalized:boolean;
1056 const aForVertex:boolean):TPasGLTFDoubleDynamicArray;
1057 published
1058 property Name:TPasGLTFUTF8String read fName write fName;
1059 property Buffer:TPasGLTFSizeInt read fBuffer write fBuffer;
1060 property ByteOffset:TPasGLTFSizeUInt read fByteOffset write fByteOffset;
1061 property ByteLength:TPasGLTFSizeUInt read fByteLength write fByteLength;
1062 property ByteStride:TPasGLTFSizeUInt read fByteStride write fByteStride;
1063 property Target:TTargetType read fTarget write fTarget default TTargetType.None;
1064 end;
1065 TBufferViews=TPasGLTFObjectList<TBufferView>;
1066 TCamera=class(TBaseExtensionsExtrasObject)
1067 public
1068 // CGE: Workaround FPC problems (when generating debug info):
1069 // PasGLTF.pas(5785,1) Error: Asm: Duplicate label DBG_$PASGLTF_$$_TTYPE
1070 // PasGLTF.pas(5785,1) Error: Asm: Duplicate label DBGREF_$PASGLTF_$$_TTYPE
1071 // You cannot define TType multiple times, so we rename to TCameraType, TSamplerType
1072 //
1073 // TODO: BeRo says fixed in FPC trunk, test.
1074 // We need a workaround for FPC 3.0.4 anyway.
1075 type TCameraType=
1076 (
1077 None=0,
1078 Orthographic=1,
1079 Perspective=2
1080 );
1081 TOrthographic=class(TBaseExtensionsExtrasObject)
1082 private
1083 fXMag:TPasGLTFFloat;
1084 fYMag:TPasGLTFFloat;
1085 fZNear:TPasGLTFFloat;
1086 fZFar:TPasGLTFFloat;
1087 fEmpty:boolean;
1088 public
1089 constructor Create(const aDocument:TDocument); override;
1090 destructor Destroy; override;
1091 published
1092 property XMag:TPasGLTFFloat read fXMag write fXMag;
1093 property YMag:TPasGLTFFloat read fYMag write fYMag;
1094 property ZNear:TPasGLTFFloat read fZNear write fZNear;
1095 property ZFar:TPasGLTFFloat read fZFar write fZFar;
1096 property Empty:boolean read fEmpty;
1097 end;
1098 TPerspective=class(TBaseExtensionsExtrasObject)
1099 private
1100 fAspectRatio:TPasGLTFFloat;
1101 fYFov:TPasGLTFFloat;
1102 fZNear:TPasGLTFFloat;
1103 fZFar:TPasGLTFFloat;
1104 fEmpty:boolean;
1105 public
1106 constructor Create(const aDocument:TDocument); override;
1107 destructor Destroy; override;
1108 published
1109 property AspectRatio:TPasGLTFFloat read fAspectRatio write fAspectRatio;
1110 property YFov:TPasGLTFFloat read fYFov write fYFov;
1111 property ZNear:TPasGLTFFloat read fZNear write fZNear;
1112 property ZFar:TPasGLTFFloat read fZFar write fZFar;
1113 property Empty:boolean read fEmpty;
1114 end;
1115 private
1116 fType:TCameraType;
1117 fOrthographic:TOrthographic;
1118 fPerspective:TPerspective;
1119 fName:TPasGLTFUTF8String;
1120 public
1121 constructor Create(const aDocument:TDocument); override;
1122 destructor Destroy; override;
1123 published
1124 property Type_:TCameraType read fType write fType;
1125 property Orthographic:TOrthographic read fOrthographic;
1126 property Perspective:TPerspective read fPerspective;
1127 property Name:TPasGLTFUTF8String read fName write fName;
1128 end;
1129 TCameras=TPasGLTFObjectList<TCamera>;
1130 TImage=class(TBaseExtensionsExtrasObject)
1131 private
1132 fBufferView:TPasGLTFSizeInt;
1133 fName:TPasGLTFUTF8String;
1134 fURI:TPasGLTFUTF8String;
1135 fMimeType:TPasGLTFUTF8String;
1136 public
1137 constructor Create(const aDocument:TDocument); override;
1138 destructor Destroy; override;
1139 procedure SetEmbeddedResourceData(const aStream:TStream);
1140 procedure GetResourceData(const aStream:TStream);
1141 function IsExternalResource:boolean;
1142 published
1143 property BufferView:TPasGLTFSizeInt read fBufferView write fBufferView;
1144 property Name:TPasGLTFUTF8String read fName write fName;
1145 property URI:TPasGLTFUTF8String read fURI write fURI;
1146 property MimeType:TPasGLTFUTF8String read fMimeType write fMimeType;
1147 end;
1148 TImages=TPasGLTFObjectList<TImage>;
1149 TMaterial=class(TBaseExtensionsExtrasObject)
1150 public
1151 type TAlphaMode=
1152 (
1153 Opaque=0,
1154 Mask=1,
1155 Blend=2
1156 );
1157 PAlphaMode=^TAlphaMode;
1158 TAlphaModes=set of TAlphaMode;
1159 TTexture=class(TBaseExtensionsExtrasObject)
1160 private
1161 fIndex:TPasGLTFSizeInt;
1162 fTexCoord:TPasGLTFSizeInt;
1163 function GetEmpty:boolean;
1164 public
1165 constructor Create(const aDocument:TDocument); override;
1166 destructor Destroy; override;
1167 published
1168 property Index:TPasGLTFSizeInt read fIndex write fIndex;
1169 property TexCoord:TPasGLTFSizeInt read fTexCoord write fTexCoord;
1170 property Empty:boolean read GetEmpty;
1171 end;
1172 TNormalTexture=class(TTexture)
1173 private
1174 fScale:TPasGLTFFloat;
1175 public
1176 constructor Create(const aDocument:TDocument); override;
1177 published
1178 property Scale:TPasGLTFFloat read fScale write fScale;
1179 end;
1180 TOcclusionTexture=class(TTexture)
1181 private
1182 fStrength:TPasGLTFFloat;
1183 public
1184 constructor Create(const aDocument:TDocument); override;
1185 published
1186 property Strength:TPasGLTFFloat read fStrength write fStrength;
1187 end;
1188 TPBRMetallicRoughness=class(TBaseExtensionsExtrasObject)
1189 private
1190 fBaseColorFactor:TVector4;
1191 fBaseColorTexture:TTexture;
1192 fRoughnessFactor:TPasGLTFFloat;
1193 fMetallicFactor:TPasGLTFFloat;
1194 fMetallicRoughnessTexture:TTexture;
1195 function GetEmpty:boolean;
1196 public
1197 constructor Create(const aDocument:TDocument); override;
1198 destructor Destroy; override;
1199 public
1200 property BaseColorFactor:TVector4 read fBaseColorFactor write fBaseColorFactor;
1201 published
1202 property BaseColorTexture:TTexture read fBaseColorTexture;
1203 property RoughnessFactor:TPasGLTFFloat read fRoughnessFactor write fRoughnessFactor;
1204 property MetallicFactor:TPasGLTFFloat read fMetallicFactor write fMetallicFactor;
1205 property MetallicRoughnessTexture:TTexture read fMetallicRoughnessTexture;
1206 property Empty:boolean read GetEmpty;
1207 end;
1208 private
1209 fName:TPasGLTFUTF8String;
1210 fAlphaCutOff:TPasGLTFFloat;
1211 fAlphaMode:TAlphaMode;
1212 fDoubleSided:boolean;
1213 fNormalTexture:TNormalTexture;
1214 fOcclusionTexture:TOcclusionTexture;
1215 fPBRMetallicRoughness:TPBRMetallicRoughness;
1216 fEmissiveTexture:TTexture;
1217 fEmissiveFactor:TVector3;
1218 public
1219 constructor Create(const aDocument:TDocument); override;
1220 destructor Destroy; override;
1221 public
1222 property EmissiveFactor:TVector3 read fEmissiveFactor write fEmissiveFactor;
1223 published
1224 property Name:TPasGLTFUTF8String read fName write fName;
1225 property AlphaCutOff:TPasGLTFFloat read fAlphaCutOff write fAlphaCutOff;
1226 property AlphaMode:TAlphaMode read fAlphaMode write fAlphaMode;
1227 property DoubleSided:boolean read fDoubleSided write fDoubleSided;
1228 property NormalTexture:TNormalTexture read fNormalTexture;
1229 property OcclusionTexture:TOcclusionTexture read fOcclusionTexture;
1230 property PBRMetallicRoughness:TPBRMetallicRoughness read fPBRMetallicRoughness;
1231 property EmissiveTexture:TTexture read fEmissiveTexture;
1232 end;
1233 TMaterials=TPasGLTFObjectList<TMaterial>;
1234 TMesh=class(TBaseExtensionsExtrasObject)
1235 public
1236 type TPrimitive=class(TBaseExtensionsExtrasObject)
1237 public
1238 type TMode=
1239 (
1240 Points=0,
1241 Lines=1,
1242 LineLoop=2,
1243 LineStrip=3,
1244 Triangles=4,
1245 TriangleStrip=5,
1246 TriangleFan=6
1247 );
1248 PMode=^TMode;
1249 private
1250 fMode:TMode;
1251 fIndices:TPasGLTFSizeInt;
1252 fMaterial:TPasGLTFSizeInt;
1253 fAttributes:TAttributes;
1254 fTargets:TAttributesList;
1255 public
1256 constructor Create(const aDocument:TDocument); override;
1257 destructor Destroy; override;
1258 published
1259 property Mode:TMode read fMode write fMode;
1260 property Indices:TPasGLTFSizeInt read fIndices write fIndices;
1261 property Material:TPasGLTFSizeInt read fMaterial write fMaterial;
1262 property Attributes:TAttributes read fAttributes;
1263 property Targets:TAttributesList read fTargets;
1264 end;
1265 TPrimitives=TPasGLTFObjectList<TPrimitive>;
1266 TWeights=TPasGLTFDynamicArray<TPasGLTFFloat>;
1267 private
1268 fName:TPasGLTFUTF8String;
1269 fWeights:TWeights;
1270 fPrimitives:TPrimitives;
1271 public
1272 constructor Create(const aDocument:TDocument); override;
1273 destructor Destroy; override;
1274 public
1275 published
1276 property Name:TPasGLTFUTF8String read fName write fName;
1277 property Weights:TWeights read fWeights;
1278 property Primitives:TPrimitives read fPrimitives;
1279 end;
1280 TMeshes=TPasGLTFObjectList<TMesh>;
1281 TNode=class(TBaseExtensionsExtrasObject)
1282 public
1283 type TChildren=TPasGLTFDynamicArray<TPasGLTFSizeInt>;
1284 TWeights=TPasGLTFDynamicArray<TPasGLTFFloat>;
1285 private
1286 fName:TPasGLTFUTF8String;
1287 fCamera:TPasGLTFSizeInt;
1288 fMesh:TPasGLTFSizeInt;
1289 fSkin:TPasGLTFSizeInt;
1290 fMatrix:TMatrix4x4;
1291 fRotation:TVector4;
1292 fScale:TVector3;
1293 fTranslation:TVector3;
1294 fChildren:TChildren;
1295 fWeights:TWeights;
1296 public
1297 constructor Create(const aDocument:TDocument); override;
1298 destructor Destroy; override;
1299 public
1300 property Matrix:TMatrix4x4 read fMatrix write fMatrix;
1301 property Rotation:TVector4 read fRotation write fRotation;
1302 property Scale:TVector3 read fScale write fScale;
1303 property Translation:TVector3 read fTranslation write fTranslation;
1304 published
1305 property Name:TPasGLTFUTF8String read fName write fName;
1306 property Camera:TPasGLTFSizeInt read fCamera write fCamera;
1307 property Mesh:TPasGLTFSizeInt read fMesh write fMesh;
1308 property Skin:TPasGLTFSizeInt read fSkin write fSkin;
1309 property Children:TChildren read fChildren;
1310 property Weights:TWeights read fWeights;
1311 end;
1312 TNodes=TPasGLTFObjectList<TNode>;
1313 TSampler=class(TBaseExtensionsExtrasObject)
1314 public
1315 type TMagFilter=
1316 (
1317 None=0,
1318 Nearest=9728,
1319 Linear=9729
1320 );
1321 PMagFilter=^TMagFilter;
1322 TMinFilter=
1323 (
1324 None=0,
1325 Nearest=9728,
1326 Linear=9729,
1327 NearestMipMapNearest=9984,
1328 LinearMipMapNearest=9985,
1329 NearestMipMapLinear=9986,
1330 LinearMipMapLinear=9987
1331 );
1332 PMinFilter=^TMinFilter;
1333 TWrappingMode=
1334 (
1335 Repeat_=10497,
1336 ClampToEdge=33071,
1337 MirroredRepeat=33648
1338 );
1339 PWrappingMode=^TWrappingMode;
1340 private
1341 fName:TPasGLTFUTF8String;
1342 fMagFilter:TMagFilter;
1343 fMinFilter:TMinFilter;
1344 fWrapS:TWrappingMode;
1345 fWrapT:TWrappingMode;
1346 function GetEmpty:boolean;
1347 public
1348 constructor Create(const aDocument:TDocument); override;
1349 destructor Destroy; override;
1350 published
1351 property Name:TPasGLTFUTF8String read fName write fName;
1352 property MagFilter:TMagFilter read fMagFilter write fMagFilter;
1353 property MinFilter:TMinFilter read fMinFilter write fMinFilter;
1354 property WrapS:TWrappingMode read fWrapS write fWrapS;
1355 property WrapT:TWrappingMode read fWrapT write fWrapT;
1356 property Empty:boolean read GetEmpty;
1357 end;
1358 TSamplers=TPasGLTFObjectList<TSampler>;
1359 TScene=class(TBaseExtensionsExtrasObject)
1360 public
1361 type TNodes=TPasGLTFDynamicArray<TPasGLTFSizeUInt>;
1362 private
1363 fName:TPasGLTFUTF8String;
1364 fNodes:TScene.TNodes;
1365 public
1366 constructor Create(const aDocument:TDocument); override;
1367 destructor Destroy; override;
1368 published
1369 property Name:TPasGLTFUTF8String read fName write fName;
1370 property Nodes:TScene.TNodes read fNodes;
1371 end;
1372 TScenes=TPasGLTFObjectList<TScene>;
1373 TSkin=class(TBaseExtensionsExtrasObject)
1374 public
1375 type TJoints=TPasGLTFDynamicArray<TPasGLTFSizeUInt>;
1376 private
1377 fName:TPasGLTFUTF8String;
1378 fInverseBindMatrices:TPasGLTFSizeInt;
1379 fSkeleton:TPasGLTFSizeInt;
1380 fJoints:TSkin.TJoints;
1381 public
1382 constructor Create(const aDocument:TDocument); override;
1383 destructor Destroy; override;
1384 published
1385 property Name:TPasGLTFUTF8String read fName write fName;
1386 property InverseBindMatrices:TPasGLTFSizeInt read fInverseBindMatrices write fInverseBindMatrices;
1387 property Skeleton:TPasGLTFSizeInt read fSkeleton write fSkeleton;
1388 property Joints:TSkin.TJoints read fJoints;
1389 end;
1390 TSkins=TPasGLTFObjectList<TSkin>;
1391 TTexture=class(TBaseExtensionsExtrasObject)
1392 private
1393 fName:TPasGLTFUTF8String;
1394 fSampler:TPasGLTFSizeInt;
1395 fSource:TPasGLTFSizeInt;
1396 public
1397 constructor Create(const aDocument:TDocument); override;
1398 destructor Destroy; override;
1399 published
1400 property Name:TPasGLTFUTF8String read fName write fName;
1401 property Sampler:TPasGLTFSizeInt read fSampler write fSampler;
1402 property Source:TPasGLTFSizeInt read fSource write fSource;
1403 end;
1404 TTextures=TPasGLTFObjectList<TTexture>;
1405 TDocument=class(TBaseExtensionsExtrasObject)
1406 public
1407 type TGetURI=function(const aURI:TPasGLTFUTF8String):TStream of object;
1408 private
1409 fAsset:TAsset;
1410 fAccessors:TAccessors;
1411 fAnimations:TAnimations;
1412 fBuffers:TBuffers;
1413 fBufferViews:TBufferViews;
1414 fCameras:TCameras;
1415 fImages:TImages;
1416 fMaterials:TMaterials;
1417 fMeshes:TMeshes;
1418 fNodes:TNodes;
1419 fSamplers:TSamplers;
1420 fScene:TPasGLTFSizeInt;
1421 fScenes:TScenes;
1422 fSkins:TSkins;
1423 fTextures:TTextures;
1424 fExtensionsUsed:TStringList;
1425 fExtensionsRequired:TStringList;
1426 fRootPath:TPasGLTFUTF8String;
1427 fGetURI:TGetURI;
1428 function DefaultGetURI(const aURI:TPasGLTFUTF8String):TStream;
1429 procedure LoadURISource(const aURI:TPasGLTFUTF8String;const aStream:TStream);
1430 procedure LoadURISources;
1431 public
1432 constructor Create(const aDocument:TDocument=nil); override;
1433 destructor Destroy; override;
1434 procedure LoadFromJSON(const aJSONRootItem:TPasJSONItem);
1435 procedure LoadFromBinary(const aStream:TStream);
1436 procedure LoadFromStream(const aStream:TStream);
1437 function SaveToJSON(const aFormatted:boolean=false):TPasJSONRawByteString;
1438 procedure SaveToBinary(const aStream:TStream);
1439 procedure SaveToStream(const aStream:TStream;const aBinary:boolean=false;const aFormatted:boolean=false);
1440 published
1441 property Asset:TAsset read fAsset;
1442 property Accessors:TAccessors read fAccessors;
1443 property Animations:TAnimations read fAnimations;
1444 property Buffers:TBuffers read fBuffers;
1445 property BufferViews:TBufferViews read fBufferViews;
1446 property Cameras:TCameras read fCameras;
1447 property Images:TImages read fImages;
1448 property Materials:TMaterials read fMaterials;
1449 property Meshes:TMeshes read fMeshes;
1450 property Nodes:TNodes read fNodes;
1451 property Samplers:TSamplers read fSamplers;
1452 property Scene:TPasGLTFSizeInt read fScene write fScene;
1453 property Scenes:TScenes read fScenes;
1454 property Skins:TSkins read fSkins;
1455 property Textures:TTextures read fTextures;
1456 property ExtensionsUsed:TStringList read fExtensionsUsed;
1457 property ExtensionsRequired:TStringList read fExtensionsRequired;
1458 property RootPath:TPasGLTFUTF8String read fRootPath write fRootPath;
1459 property GetURI:TGetURI read fGetURI write fGetURI;
1460 end;
1461 public
1462 class function ResolveURIToPath(const aURI:TPasGLTFUTF8String):TPasGLTFUTF8String; static;
1463 end;
1464
1465 implementation
1466
1467 {$ifndef fpc}
1468 class function TPasGLTFTypedSort<T>.BSRDWord(aValue:TPasGLTFUInt32):TPasGLTFInt32;
1469 const BSRDebruijn32Multiplicator=TPasGLTFUInt32($07c4acdd);
1470 BSRDebruijn32Shift=27;
1471 BSRDebruijn32Mask=31;
1472 BSRDebruijn32Table:array[0..31] of TPasGLTFInt32=(0,9,1,10,13,21,2,29,11,14,16,18,22,25,3,30,8,12,20,28,15,17,24,7,19,27,23,6,26,5,4,31);
1473 var Value:TPasGLTFUInt32;
1474 begin
1475 if aValue=0 then begin
1476 result:=255;
1477 end else begin
1478 Value:=aValue or (aValue shr 1);
1479 Value:=Value or (Value shr 2);
1480 Value:=Value or (Value shr 4);
1481 Value:=Value or (Value shr 8);
1482 Value:=Value or (Value shr 16);
1483 result:=BSRDebruijn32Table[((Value*BSRDebruijn32Multiplicator) shr BSRDebruijn32Shift) and BSRDebruijn32Mask];
1484 end;
1485 end;
1486 {$endif}
1487
1488 class procedure TPasGLTFTypedSort<T>.IntroSort(const pItems:TPasGLTFPointer;const pLeft,pRight:TPasGLTFSizeInt;const pCompareFunc:TPasGLTFTypedSortCompareFunction);
1489 type TItem=T;
1490 PItem=^TItem;
1491 TItemArray=array[0..65535] of TItem;
1492 PItemArray=^TItemArray;
1493 var Left,Right,i,j,Middle,Size,Parent,Child,Pivot,iA,iB,iC:TPasGLTFSizeInt;
1494 Depth:TPasGLTFInt32;
1495 StackItem:PStackItem;
1496 Stack:array[0..31] of TStackItem;
1497 Temp:T;
1498 begin
1499 if pLeft<pRight then begin
1500 StackItem:=@Stack[0];
1501 StackItem^.Left:=pLeft;
1502 StackItem^.Right:=pRight;
1503 if (TPasGLTFInt64(pRight)-TPasGLTFInt64(pLeft))<=TPasGLTFInt64(High(TPasGLTFUInt32)) then begin
1504 StackItem^.Depth:=BSRDWord((pRight-pLeft)+1) shl 1;
1505 if StackItem^.Depth>31 then begin
1506 StackItem^.Depth:=31;
1507 end;
1508 end else begin
1509 StackItem^.Depth:=31;
1510 end;
1511 inc(StackItem);
1512 while {%H-}TPasGLTFPtrUInt(TPasGLTFPointer(StackItem))>TPasGLTFPtrUInt(TPasGLTFPointer(@Stack[0])) do begin
1513 dec(StackItem);
1514 Left:=StackItem^.Left;
1515 Right:=StackItem^.Right;
1516 Depth:=StackItem^.Depth;
1517 Size:=(Right-Left)+1;
1518 if Size<16 then begin
1519 // Insertion sort
1520 iA:=Left;
1521 iB:=iA+1;
1522 while iB<=Right do begin
1523 iC:=iB;
1524 while (iA>=Left) and
1525 (iC>=Left) and
1526 (pCompareFunc(PItemArray(pItems)^[iA],PItemArray(pItems)^[iC])>0) do begin
1527 Temp:=PItemArray(pItems)^[iA];
1528 PItemArray(pItems)^[iA]:=PItemArray(pItems)^[iC];
1529 PItemArray(pItems)^[iC]:=Temp;
1530 dec(iA);
1531 dec(iC);
1532 end;
1533 iA:=iB;
1534 inc(iB);
1535 end;
1536 end else begin
1537 if (Depth=0) or ({%H-}TPasGLTFPtrUInt(TPasGLTFPointer(StackItem))>=TPasGLTFPtrUInt(TPasGLTFPointer(@Stack[high(Stack)-1]))) then begin
1538 // Heap sort
1539 i:=Size div 2;
1540 repeat
1541 if i>0 then begin
1542 dec(i);
1543 end else begin
1544 dec(Size);
1545 if Size>0 then begin
1546 Temp:=PItemArray(pItems)^[Left+Size];
1547 PItemArray(pItems)^[Left+Size]:=PItemArray(pItems)^[Left];
1548 PItemArray(pItems)^[Left]:=Temp;
1549 end else begin
1550 break;
1551 end;
1552 end;
1553 Parent:=i;
1554 repeat
1555 Child:=(Parent*2)+1;
1556 if Child<Size then begin
1557 if (Child<(Size-1)) and (pCompareFunc(PItemArray(pItems)^[Left+Child],PItemArray(pItems)^[Left+Child+1])<0) then begin
1558 inc(Child);
1559 end;
1560 if pCompareFunc(PItemArray(pItems)^[Left+Parent],PItemArray(pItems)^[Left+Child])<0 then begin
1561 Temp:=PItemArray(pItems)^[Left+Parent];
1562 PItemArray(pItems)^[Left+Parent]:=PItemArray(pItems)^[Left+Child];
1563 PItemArray(pItems)^[Left+Child]:=Temp;
1564 Parent:=Child;
1565 continue;
1566 end;
1567 end;
1568 break;
1569 until false;
1570 until false;
1571 end else begin
1572 // Quick sort width median-of-three optimization
1573 Middle:=Left+((Right-Left) shr 1);
1574 if (Right-Left)>3 then begin
1575 if pCompareFunc(PItemArray(pItems)^[Left],PItemArray(pItems)^[Middle])>0 then begin
1576 Temp:=PItemArray(pItems)^[Left];
1577 PItemArray(pItems)^[Left]:=PItemArray(pItems)^[Middle];
1578 PItemArray(pItems)^[Middle]:=Temp;
1579 end;
1580 if pCompareFunc(PItemArray(pItems)^[Left],PItemArray(pItems)^[Right])>0 then begin
1581 Temp:=PItemArray(pItems)^[Left];
1582 PItemArray(pItems)^[Left]:=PItemArray(pItems)^[Right];
1583 PItemArray(pItems)^[Right]:=Temp;
1584 end;
1585 if pCompareFunc(PItemArray(pItems)^[Middle],PItemArray(pItems)^[Right])>0 then begin
1586 Temp:=PItemArray(pItems)^[Middle];
1587 PItemArray(pItems)^[Middle]:=PItemArray(pItems)^[Right];
1588 PItemArray(pItems)^[Right]:=Temp;
1589 end;
1590 end;
1591 Pivot:=Middle;
1592 i:=Left;
1593 j:=Right;
1594 repeat
1595 while (i<Right) and (pCompareFunc(PItemArray(pItems)^[i],PItemArray(pItems)^[Pivot])<0) do begin
1596 inc(i);
1597 end;
1598 while (j>=i) and (pCompareFunc(PItemArray(pItems)^[j],PItemArray(pItems)^[Pivot])>0) do begin
1599 dec(j);
1600 end;
1601 if i>j then begin
1602 break;
1603 end else begin
1604 if i<>j then begin
1605 Temp:=PItemArray(pItems)^[i];
1606 PItemArray(pItems)^[i]:=PItemArray(pItems)^[j];
1607 PItemArray(pItems)^[j]:=Temp;
1608 if Pivot=i then begin
1609 Pivot:=j;
1610 end else if Pivot=j then begin
1611 Pivot:=i;
1612 end;
1613 end;
1614 inc(i);
1615 dec(j);
1616 end;
1617 until false;
1618 if i<Right then begin
1619 StackItem^.Left:=i;
1620 StackItem^.Right:=Right;
1621 StackItem^.Depth:=Depth-1;
1622 inc(StackItem);
1623 end;
1624 if Left<j then begin
1625 StackItem^.Left:=Left;
1626 StackItem^.Right:=j;
1627 StackItem^.Depth:=Depth-1;
1628 inc(StackItem);
1629 end;
1630 end;
1631 end;
1632 end;
1633 end;
1634 end;
1635
1636 constructor TPasGLTFDynamicArray<T>.TValueEnumerator.Create(const aDynamicArray:TPasGLTFDynamicArray<T>);
1637 begin
1638 fDynamicArray:=aDynamicArray;
1639 fIndex:=-1;
1640 end;
1641
1642 function TPasGLTFDynamicArray<T>.TValueEnumerator.MoveNext:boolean;
1643 begin
1644 inc(fIndex);
1645 result:=fIndex<fDynamicArray.fCount;
1646 end;
1647
1648 function TPasGLTFDynamicArray<T>.TValueEnumerator.GetCurrent:T;
1649 begin
1650 result:=fDynamicArray.fItems[fIndex];
1651 end;
1652
1653 constructor TPasGLTFDynamicArray<T>.Create;
1654 begin
1655 fItems:=nil;
1656 fCount:=0;
1657 fAllocated:=0;
1658 inherited Create;
1659 end;
1660
1661 destructor TPasGLTFDynamicArray<T>.Destroy;
1662 begin
1663 SetLength(fItems,0);
1664 fCount:=0;
1665 fAllocated:=0;
1666 inherited Destroy;
1667 end;
1668
1669 procedure TPasGLTFDynamicArray<T>.Clear;
1670 begin
1671 SetLength(fItems,0);
1672 fCount:=0;
1673 fAllocated:=0;
1674 end;
1675
1676 procedure TPasGLTFDynamicArray<T>.SetCount(const pNewCount:TPasGLTFSizeInt);
1677 begin
1678 if pNewCount<=0 then begin
1679 SetLength(fItems,0);
1680 fCount:=0;
1681 fAllocated:=0;
1682 end else begin
1683 if pNewCount<fCount then begin
1684 fCount:=pNewCount;
1685 if (fCount+fCount)<fAllocated then begin
1686 fAllocated:=fCount+fCount;
1687 SetLength(fItems,fAllocated);
1688 end;
1689 end else begin
1690 fCount:=pNewCount;
1691 if fAllocated<fCount then begin
1692 fAllocated:=fCount+fCount;
1693 SetLength(fItems,fAllocated);
1694 end;
1695 end;
1696 end;
1697 end;
1698
1699 function TPasGLTFDynamicArray<T>.GetItem(const pIndex:TPasGLTFSizeInt):T;
1700 begin
1701 result:=fItems[pIndex];
1702 end;
1703
1704 procedure TPasGLTFDynamicArray<T>.SetItem(const pIndex:TPasGLTFSizeInt;const pItem:T);
1705 begin
1706 fItems[pIndex]:=pItem;
1707 end;
1708
1709 function TPasGLTFDynamicArray<T>.Add(const pItem:T):TPasGLTFSizeInt;
1710 begin
1711 result:=fCount;
1712 inc(fCount);
1713 if fAllocated<fCount then begin
1714 fAllocated:=fCount+fCount;
1715 SetLength(fItems,fAllocated);
1716 end;
1717 fItems[result]:=pItem;
1718 end;
1719
1720 function TPasGLTFDynamicArray<T>.Add(const pItems:array of T):TPasGLTFSizeInt;
1721 var Index:TPasGLTFSizeInt;
1722 begin
1723 result:=fCount;
1724 if length(pItems)>0 then begin
1725 inc(fCount,length(pItems));
1726 if fAllocated<fCount then begin
1727 fAllocated:=fCount+fCount;
1728 SetLength(fItems,fAllocated);
1729 end;
1730 for Index:=0 to length(pItems)-1 do begin
1731 fItems[result+Index]:=pItems[Index];
1732 end;
1733 end;
1734 end;
1735
1736 procedure TPasGLTFDynamicArray<T>.Insert(const pIndex:TPasGLTFSizeInt;const pItem:T);
1737 begin
1738 if pIndex>=0 then begin
1739 if pIndex<fCount then begin
1740 inc(fCount);
1741 if fCount<fAllocated then begin
1742 fAllocated:=fCount shl 1;
1743 SetLength(fItems,fAllocated);
1744 end;
1745 Move(fItems[pIndex],fItems[pIndex+1],(fCount-pIndex)*SizeOf(T));
1746 FillChar(fItems[pIndex],SizeOf(T),#0);
1747 end else begin
1748 fCount:=pIndex+1;
1749 if fCount<fAllocated then begin
1750 fAllocated:=fCount shl 1;
1751 SetLength(fItems,fAllocated);
1752 end;
1753 end;
1754 fItems[pIndex]:=pItem;
1755 end;
1756 end;
1757
1758 procedure TPasGLTFDynamicArray<T>.Delete(const pIndex:TPasGLTFSizeInt);
1759 begin
1760 Finalize(fItems[pIndex]);
1761 Move(fItems[pIndex+1],fItems[pIndex],(fCount-pIndex)*SizeOf(T));
1762 dec(fCount);
1763 FillChar(fItems[fCount],SizeOf(T),#0);
1764 if fCount<(fAllocated shr 1) then begin
1765 fAllocated:=fAllocated shr 1;
1766 SetLength(fItems,fAllocated);
1767 end;
1768 end;
1769
1770 procedure TPasGLTFDynamicArray<T>.Exchange(const pIndex,pWithIndex:TPasGLTFSizeInt);
1771 var Temporary:T;
1772 begin
1773 Temporary:=fItems[pIndex];
1774 fItems[pIndex]:=fItems[pWithIndex];
1775 fItems[pWithIndex]:=Temporary;
1776 end;
1777
1778 function TPasGLTFDynamicArray<T>.Memory:TPasGLTFPointer;
1779 begin
1780 result:=@fItems[0];
1781 end;
1782
1783 function TPasGLTFDynamicArray<T>.GetEnumerator:TPasGLTFDynamicArray<T>.TValueEnumerator;
1784 begin
1785 result:=TValueEnumerator.Create(self);
1786 end;
1787
1788 constructor TPasGLTFObjectList<T>.TValueEnumerator.Create(const aObjectList:TPasGLTFObjectList<T>);
1789 begin
1790 fObjectList:=aObjectList;
1791 fIndex:=-1;
1792 end;
1793
1794 function TPasGLTFObjectList<T>.TValueEnumerator.MoveNext:boolean;
1795 begin
1796 inc(fIndex);
1797 result:=fIndex<fObjectList.fCount;
1798 end;
1799
1800 function TPasGLTFObjectList<T>.TValueEnumerator.GetCurrent:T;
1801 begin
1802 result:=fObjectList.fItems[fIndex];
1803 end;
1804
1805 constructor TPasGLTFObjectList<T>.Create;
1806 begin
1807 inherited Create;
1808 fItems:=nil;
1809 fCount:=0;
1810 fAllocated:=0;
1811 fOwnsObjects:=true;
1812 end;
1813
1814 destructor TPasGLTFObjectList<T>.Destroy;
1815 begin
1816 Clear;
1817 inherited Destroy;
1818 end;
1819
1820 function TPasGLTFObjectList<T>.RoundUpToPowerOfTwoSizeUInt(x:TPasGLTFSizeUInt):TPasGLTFSizeUInt;
1821 begin
1822 dec(x);
1823 x:=x or (x shr 1);
1824 x:=x or (x shr 2);
1825 x:=x or (x shr 4);
1826 x:=x or (x shr 8);
1827 x:=x or (x shr 16);
1828 {$ifdef CPU64}
1829 x:=x or (x shr 32);
1830 {$endif}
1831 result:=x+1;
1832 end;
1833
1834 procedure TPasGLTFObjectList<T>.Clear;
1835 var Index:TPasGLTFSizeInt;
1836 begin
1837 if fOwnsObjects then begin
1838 for Index:=fCount-1 downto 0 do begin
1839 FreeAndNil(fItems[Index]);
1840 end;
1841 end;
1842 fItems:=nil;
1843 fCount:=0;
1844 fAllocated:=0;
1845 end;
1846
1847 procedure TPasGLTFObjectList<T>.SetCount(const pNewCount:TPasGLTFSizeInt);
1848 var Index,NewAllocated:TPasGLTFSizeInt;
1849 begin
1850 if fCount<pNewCount then begin
1851 NewAllocated:=RoundUpToPowerOfTwoSizeUInt(pNewCount);
1852 if fAllocated<NewAllocated then begin
1853 SetLength(fItems,NewAllocated);
1854 FillChar(fItems[fAllocated],(NewAllocated-fAllocated)*SizeOf(T),#0);
1855 fAllocated:=NewAllocated;
1856 end;
1857 FillChar(fItems[fCount],(pNewCount-fCount)*SizeOf(T),#0);
1858 fCount:=pNewCount;
1859 end else if fCount>pNewCount then begin
1860 if fOwnsObjects then begin
1861 for Index:=fCount-1 downto pNewCount do begin
1862 FreeAndNil(fItems[Index]);
1863 end;
1864 end;
1865 fCount:=pNewCount;
1866 if pNewCount<(fAllocated shr 2) then begin
1867 if pNewCount=0 then begin
1868 fItems:=nil;
1869 fAllocated:=0;
1870 end else begin
1871 NewAllocated:=fAllocated shr 1;
1872 SetLength(fItems,NewAllocated);
1873 fAllocated:=NewAllocated;
1874 end;
1875 end;
1876 end;
1877 end;
1878
1879 function TPasGLTFObjectList<T>.GetItem(const pIndex:TPasGLTFSizeInt):T;
1880 begin
1881 if (pIndex<0) or (pIndex>=fCount) then begin
1882 raise ERangeError.Create('Out of index range');
1883 end;
1884 result:=fItems[pIndex];
1885 end;
1886
1887 procedure TPasGLTFObjectList<T>.SetItem(const pIndex:TPasGLTFSizeInt;const pItem:T);
1888 begin
1889 if (pIndex<0) or (pIndex>=fCount) then begin
1890 raise ERangeError.Create('Out of index range');
1891 end;
1892 fItems[pIndex]:=pItem;
1893 end;
1894
1895 function TPasGLTFObjectList<T>.IndexOf(const pItem:T):TPasGLTFSizeInt;
1896 var Index:TPasGLTFSizeInt;
1897 begin
1898 for Index:=0 to fCount-1 do begin
1899 if fItems[Index]=pItem then begin
1900 result:=Index;
1901 exit;
1902 end;
1903 end;
1904 result:=-1;
1905 end;
1906
1907 function TPasGLTFObjectList<T>.Add(const pItem:T):TPasGLTFSizeInt;
1908 begin
1909 result:=fCount;
1910 inc(fCount);
1911 if fAllocated<fCount then begin
1912 fAllocated:=fCount+fCount;
1913 SetLength(fItems,fAllocated);
1914 end;
1915 fItems[result]:=pItem;
1916 end;
1917
1918 procedure TPasGLTFObjectList<T>.Insert(const pIndex:TPasGLTFSizeInt;const pItem:T);
1919 var
1920 OldCount:TPasGLTFSizeInt;
1921 MoveSrc, MoveDst: Pointer;
1922 begin
1923 if pIndex>=0 then begin
1924 OldCount:=fCount;
1925 if fCount<pIndex then begin
1926 fCount:=pIndex+1;
1927 end else begin
1928 inc(fCount);
1929 end;
1930 if fAllocated<fCount then begin
1931 fAllocated:=fCount shl 1;
1932 SetLength(fItems,fAllocated);
1933 end;
1934 if OldCount<fCount then begin
1935 FillChar(fItems[OldCount],(fCount-OldCount)*SizeOf(T),#0);
1936 end;
1937 if pIndex<OldCount then begin
1938 // TODO: should be fixed in FPC ( https://bugs.freepascal.org/view.php?id=34893 ), retest.
1939 //
1940 // Use MoveSrc/Dst to workaround FPC 3.3.1 error:
1941 // Error: Incompatible type for arg no. 1: Got "$gendef124", expected "<Formal type>"
1942 // (FPC 3.3.1-r40366 [2018/11/24], on Linux/x86_64).
1943 MoveSrc := @(fItems[pIndex]);
1944 MoveDst := @(fItems[pIndex+1]);
1945 System.Move(MoveSrc^, MoveDst^, (OldCount-pIndex)*SizeOf(T));
1946 FillChar(fItems[pIndex],SizeOf(T),#0);
1947 end;
1948 fItems[pIndex]:=pItem;
1949 end;
1950 end;
1951
1952 procedure TPasGLTFObjectList<T>.Delete(const pIndex:TPasGLTFSizeInt);
1953 var
1954 Old:T;
1955 MoveSrc, MoveDst: Pointer;
1956 begin
1957 if (pIndex<0) or (pIndex>=fCount) then begin
1958 raise ERangeError.Create('Out of index range');
1959 end;
1960 Old:=fItems[pIndex];
1961 dec(fCount);
1962 FillChar(fItems[pIndex],SizeOf(T),#0);
1963 if pIndex<>fCount then begin
1964 // TODO: should be fixed in FPC ( https://bugs.freepascal.org/view.php?id=34893 ), retest.
1965 //
1966 // Use MoveSrc/Dst to workaround FPC 3.3.1 error:
1967 // Error: Incompatible type for arg no. 1: Got "$gendef124", expected "<Formal type>"
1968 // (FPC 3.3.1-r40366 [2018/11/24], on Linux/x86_64).
1969 MoveSrc := @(fItems[pIndex+1]);
1970 MoveDst := @(fItems[pIndex]);
1971 System.Move(MoveSrc, MoveDst, (fCount-pIndex)*SizeOf(T));
1972 FillChar(fItems[fCount],SizeOf(T),#0);
1973 end;
1974 if fCount<(fAllocated shr 1) then begin
1975 fAllocated:=fAllocated shr 1;
1976 SetLength(fItems,fAllocated);
1977 end;
1978 if fOwnsObjects then begin
1979 FreeAndNil(Old);
1980 end;
1981 end;
1982
1983 procedure TPasGLTFObjectList<T>.Remove(const pItem:T);
1984 var Index:TPasGLTFSizeInt;
1985 begin
1986 Index:=IndexOf(pItem);
1987 if Index>=0 then begin
1988 Delete(Index);
1989 end;
1990 end;
1991
1992 procedure TPasGLTFObjectList<T>.Exchange(const pIndex,pWithIndex:TPasGLTFSizeInt);
1993 var Temporary:T;
1994 begin
1995 if ((pIndex<0) or (pIndex>=fCount)) or ((pWithIndex<0) or (pWithIndex>=fCount)) then begin
1996 raise ERangeError.Create('Out of index range');
1997 end;
1998 Temporary:=fItems[pIndex];
1999 fItems[pIndex]:=fItems[pWithIndex];
2000 fItems[pWithIndex]:=Temporary;
2001 end;
2002
2003 function TPasGLTFObjectList<T>.GetEnumerator:TPasGLTFObjectList<T>.TValueEnumerator;
2004 begin
2005 result:=TValueEnumerator.Create(self);
2006 end;
2007
2008 constructor TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapEntityEnumerator.Create(const aHashMap:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>);
2009 begin
2010 fHashMap:=aHashMap;
2011 fIndex:=-1;
2012 end;
2013
2014 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapEntityEnumerator.GetCurrent:TPasGLTFHashMapEntity;
2015 begin
2016 result:=fHashMap.fEntities[fIndex];
2017 end;
2018
2019 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapEntityEnumerator.MoveNext:boolean;
2020 begin
2021 repeat
2022 inc(fIndex);
2023 if fIndex<fHashMap.fSize then begin
2024 if fHashMap.fEntityToCellIndex[fIndex]>=0 then begin
2025 result:=true;
2026 exit;
2027 end;
2028 end else begin
2029 break;
2030 end;
2031 until false;
2032 result:=false;
2033 end;
2034
2035 constructor TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapKeyEnumerator.Create(const aHashMap:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>);
2036 begin
2037 fHashMap:=aHashMap;
2038 fIndex:=-1;
2039 end;
2040
2041 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapKeyEnumerator.GetCurrent:TPasGLTFHashMapKey;
2042 begin
2043 result:=fHashMap.fEntities[fIndex].Key;
2044 end;
2045
2046 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapKeyEnumerator.MoveNext:boolean;
2047 begin
2048 repeat
2049 inc(fIndex);
2050 if fIndex<fHashMap.fSize then begin
2051 if fHashMap.fEntityToCellIndex[fIndex]>=0 then begin
2052 result:=true;
2053 exit;
2054 end;
2055 end else begin
2056 break;
2057 end;
2058 until false;
2059 result:=false;
2060 end;
2061
2062 constructor TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapValueEnumerator.Create(const aHashMap:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>);
2063 begin
2064 fHashMap:=aHashMap;
2065 fIndex:=-1;
2066 end;
2067
2068 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapValueEnumerator.GetCurrent:TPasGLTFHashMapValue;
2069 begin
2070 result:=fHashMap.fEntities[fIndex].Value;
2071 end;
2072
2073 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapValueEnumerator.MoveNext:boolean;
2074 begin
2075 repeat
2076 inc(fIndex);
2077 if fIndex<fHashMap.fSize then begin
2078 if fHashMap.fEntityToCellIndex[fIndex]>=0 then begin
2079 result:=true;
2080 exit;
2081 end;
2082 end else begin
2083 break;
2084 end;
2085 until false;
2086 result:=false;
2087 end;
2088
2089 constructor TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapEntitiesObject.Create(const aOwner:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>);
2090 begin
2091 inherited Create;
2092 fOwner:=aOwner;
2093 end;
2094
2095 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapEntitiesObject.GetEnumerator:TPasGLTFHashMapEntityEnumerator;
2096 begin
2097 result:=TPasGLTFHashMapEntityEnumerator.Create(fOwner);
2098 end;
2099
2100 constructor TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapKeysObject.Create(const aOwner:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>);
2101 begin
2102 inherited Create;
2103 fOwner:=aOwner;
2104 end;
2105
2106 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapKeysObject.GetEnumerator:TPasGLTFHashMapKeyEnumerator;
2107 begin
2108 result:=TPasGLTFHashMapKeyEnumerator.Create(fOwner);
2109 end;
2110
2111 constructor TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapValuesObject.Create(const aOwner:TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>);
2112 begin
2113 inherited Create;
2114 fOwner:=aOwner;
2115 end;
2116
2117 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapValuesObject.GetEnumerator:TPasGLTFHashMapValueEnumerator;
2118 begin
2119 result:=TPasGLTFHashMapValueEnumerator.Create(fOwner);
2120 end;
2121
2122 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapValuesObject.GetValue(const Key:TPasGLTFHashMapKey):TPasGLTFHashMapValue;
2123 begin
2124 result:=fOwner.GetValue(Key);
2125 end;
2126
2127 procedure TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TPasGLTFHashMapValuesObject.SetValue(const Key:TPasGLTFHashMapKey;const aValue:TPasGLTFHashMapValue);
2128 begin
2129 fOwner.SetValue(Key,aValue);
2130 end;
2131
2132 constructor TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.Create(const DefaultValue:TPasGLTFHashMapValue);
2133 begin
2134 inherited Create;
2135 fRealSize:=0;
2136 fLogSize:=0;
2137 fSize:=0;
2138 fEntities:=nil;
2139 fEntityToCellIndex:=nil;
2140 fCellToEntityIndex:=nil;
2141 fDefaultValue:=DefaultValue;
2142 fCanShrink:=true;
2143 fEntitiesObject:=TPasGLTFHashMapEntitiesObject.Create(self);
2144 fKeysObject:=TPasGLTFHashMapKeysObject.Create(self);
2145 fValuesObject:=TPasGLTFHashMapValuesObject.Create(self);
2146 Resize;
2147 end;
2148
2149 destructor TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.Destroy;
2150 var Counter:TPasGLTFSizeInt;
2151 begin
2152 Clear;
2153 for Counter:=0 to length(fEntities)-1 do begin
2154 Finalize(fEntities[Counter].Key);
2155 Finalize(fEntities[Counter].Value);
2156 end;
2157 SetLength(fEntities,0);
2158 SetLength(fEntityToCellIndex,0);
2159 SetLength(fCellToEntityIndex,0);
2160 FreeAndNil(fEntitiesObject);
2161 FreeAndNil(fKeysObject);
2162 FreeAndNil(fValuesObject);
2163 inherited Destroy;
2164 end;
2165
2166 procedure TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.Clear;
2167 var Counter:TPasGLTFSizeInt;
2168 begin
2169 for Counter:=0 to length(fEntities)-1 do begin
2170 Finalize(fEntities[Counter].Key);
2171 Finalize(fEntities[Counter].Value);
2172 end;
2173 if fCanShrink then begin
2174 fRealSize:=0;
2175 fLogSize:=0;
2176 fSize:=0;
2177 SetLength(fEntities,0);
2178 SetLength(fEntityToCellIndex,0);
2179 SetLength(fCellToEntityIndex,0);
2180 Resize;
2181 end else begin
2182 for Counter:=0 to length(fCellToEntityIndex)-1 do begin
2183 fCellToEntityIndex[Counter]:=ENT_EMPTY;
2184 end;
2185 for Counter:=0 to length(fEntityToCellIndex)-1 do begin
2186 fEntityToCellIndex[Counter]:=CELL_EMPTY;
2187 end;
2188 end;
2189 end;
2190
2191 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.HashData(const Data:TPasGLTFPointer;const DataLength:TPasGLTFSizeUInt):TPasGLTFUInt32;
2192 // xxHash32
2193 const PRIME32_1=TPasGLTFUInt32(2654435761);
2194 PRIME32_2=TPasGLTFUInt32(2246822519);
2195 PRIME32_3=TPasGLTFUInt32(3266489917);
2196 PRIME32_4=TPasGLTFUInt32(668265263);
2197 PRIME32_5=TPasGLTFUInt32(374761393);
2198 Seed=TPasGLTFUInt32($1337c0d3);
2199 v1Initialization=TPasGLTFUInt32(TPasGLTFUInt64(TPasGLTFUInt64(Seed)+TPasGLTFUInt64(PRIME32_1)+TPasGLTFUInt64(PRIME32_2)));
2200 v2Initialization=TPasGLTFUInt32(TPasGLTFUInt64(TPasGLTFUInt64(Seed)+TPasGLTFUInt64(PRIME32_2)));
2201 v3Initialization=TPasGLTFUInt32(TPasGLTFUInt64(TPasGLTFUInt64(Seed)+TPasGLTFUInt64(0)));
2202 v4Initialization=TPasGLTFUInt32(TPasGLTFUInt64(TPasGLTFInt64(TPasGLTFInt64(Seed)-TPasGLTFInt64(PRIME32_1))));
2203 HashInitialization=TPasGLTFUInt32(TPasGLTFUInt64(TPasGLTFUInt64(Seed)+TPasGLTFUInt64(PRIME32_5)));
2204 var v1,v2,v3,v4:TPasGLTFUInt32;
2205 p,e:PPasGLTFUInt8;
2206 begin
2207 p:=Data;
2208 if DataLength>=16 then begin
2209 v1:=v1Initialization;
2210 v2:=v2Initialization;
2211 v3:=v3Initialization;
2212 v4:=v4Initialization;
2213 e:=@PPasGLTFUInt8Array(Data)^[DataLength-16];
2214 repeat
2215 {$if defined(fpc) or declared(ROLDWord)}
2216 v1:=ROLDWord(v1+(TPasGLTFUInt32(TPasGLTFPointer(p)^)*TPasGLTFUInt32(PRIME32_2)),13)*TPasGLTFUInt32(PRIME32_1);
2217 {$else}
2218 inc(v1,TPasGLTFUInt32(TPasGLTFPointer(p)^)*TPasGLTFUInt32(PRIME32_2));
2219 v1:=((v1 shl 13) or (v1 shr 19))*TPasGLTFUInt32(PRIME32_1);
2220 {$ifend}
2221 inc(p,SizeOf(TPasGLTFUInt32));
2222 {$if defined(fpc) or declared(ROLDWord)}
2223 v2:=ROLDWord(v2+(TPasGLTFUInt32(TPasGLTFPointer(p)^)*TPasGLTFUInt32(PRIME32_2)),13)*TPasGLTFUInt32(PRIME32_1);
2224 {$else}
2225 inc(v2,TPasGLTFUInt32(TPasGLTFPointer(p)^)*TPasGLTFUInt32(PRIME32_2));
2226 v2:=((v2 shl 13) or (v2 shr 19))*TPasGLTFUInt32(PRIME32_1);
2227 {$ifend}
2228 inc(p,SizeOf(TPasGLTFUInt32));
2229 {$if defined(fpc) or declared(ROLDWord)}
2230 v3:=ROLDWord(v3+(TPasGLTFUInt32(TPasGLTFPointer(p)^)*TPasGLTFUInt32(PRIME32_2)),13)*TPasGLTFUInt32(PRIME32_1);
2231 {$else}
2232 inc(v3,TPasGLTFUInt32(TPasGLTFPointer(p)^)*TPasGLTFUInt32(PRIME32_2));
2233 v3:=((v3 shl 13) or (v3 shr 19))*TPasGLTFUInt32(PRIME32_1);
2234 {$ifend}
2235 inc(p,SizeOf(TPasGLTFUInt32));
2236 {$if defined(fpc) or declared(ROLDWord)}
2237 v4:=ROLDWord(v4+(TPasGLTFUInt32(TPasGLTFPointer(p)^)*TPasGLTFUInt32(PRIME32_2)),13)*TPasGLTFUInt32(PRIME32_1);
2238 {$else}
2239 inc(v4,TPasGLTFUInt32(TPasGLTFPointer(p)^)*TPasGLTFUInt32(PRIME32_2));
2240 v4:=((v4 shl 13) or (v4 shr 19))*TPasGLTFUInt32(PRIME32_1);
2241 {$ifend}
2242 inc(p,SizeOf(TPasGLTFUInt32));
2243 until {%H-}TPasGLTFPtrUInt(p)>{%H-}TPasGLTFPtrUInt(e);
2244 {$if defined(fpc) or declared(ROLDWord)}
2245 result:=ROLDWord(v1,1)+ROLDWord(v2,7)+ROLDWord(v3,12)+ROLDWord(v4,18);
2246 {$else}
2247 result:=((v1 shl 1) or (v1 shr 31))+
2248 ((v2 shl 7) or (v2 shr 25))+
2249 ((v3 shl 12) or (v3 shr 20))+
2250 ((v4 shl 18) or (v4 shr 14));
2251 {$ifend}
2252 end else begin
2253 result:=HashInitialization;
2254 end;
2255 inc(result,DataLength);
2256 e:=@PPasGLTFUInt8Array(Data)^[DataLength];
2257 while ({%H-}TPasGLTFPtrUInt(p)+SizeOf(TPasGLTFUInt32))<={%H-}TPasGLTFPtrUInt(e) do begin
2258 {$if defined(fpc) or declared(ROLDWord)}
2259 result:=ROLDWord(result+(TPasGLTFUInt32(TPasGLTFPointer(p)^)*TPasGLTFUInt32(PRIME32_3)),17)*TPasGLTFUInt32(PRIME32_4);
2260 {$else}
2261 inc(result,TPasGLTFUInt32(TPasGLTFPointer(p)^)*TPasGLTFUInt32(PRIME32_3));
2262 result:=((result shl 17) or (result shr 15))*TPasGLTFUInt32(PRIME32_4);
2263 {$ifend}
2264 inc(p,SizeOf(TPasGLTFUInt32));
2265 end;
2266 while {%H-}TPasGLTFPtrUInt(p)<{%H-}TPasGLTFPtrUInt(e) do begin
2267 {$if defined(fpc) or declared(ROLDWord)}
2268 result:=ROLDWord(result+(TPasGLTFUInt8(TPasGLTFPointer(p)^)*TPasGLTFUInt32(PRIME32_5)),11)*TPasGLTFUInt32(PRIME32_1);
2269 {$else}
2270 inc(result,TPasGLTFUInt8(TPasGLTFPointer(p)^)*TPasGLTFUInt32(PRIME32_5));
2271 result:=((result shl 11) or (result shr 21))*TPasGLTFUInt32(PRIME32_1);
2272 {$ifend}
2273 inc(p,SizeOf(TPasGLTFUInt8));
2274 end;
2275 result:=(result xor (result shr 15))*TPasGLTFUInt32(PRIME32_2);
2276 result:=(result xor (result shr 13))*TPasGLTFUInt32(PRIME32_3);
2277 result:=result xor (result shr 16);
2278 end;
2279
2280 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.HashKey(const Key:TPasGLTFHashMapKey):TPasGLTFUInt32;
2281 begin
2282 result:=HashData(PPasGLTFUInt8(@Key[1]),length(Key)*SizeOf(TPasGLTFRawByteChar));
2283 {$if defined(CPU386) or defined(CPUAMD64)}
2284 // Special case: The hash value may be never zero
2285 result:=result or (-TPasGLTFUInt32(ord(result=0) and 1));
2286 {$else}
2287 if result=0 then begin
2288 // Special case: The hash value may be never zero
2289 result:=$ffffffff;
2290 end;
2291 {$ifend}
2292 end;
2293
2294 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.CompareKey(const KeyA,KeyB:TPasGLTFHashMapKey):boolean;
2295 begin
2296 result:=KeyA=KeyB;
2297 end;
2298
2299 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.FindCell(const Key:TPasGLTFHashMapKey):TPasGLTFUInt32;
2300 var HashCode,Mask,Step:TPasGLTFUInt32;
2301 Entity:TPasGLTFInt32;
2302 begin
2303 HashCode:=HashKey(Key);
2304 Mask:=(2 shl fLogSize)-1;
2305 Step:=((HashCode shl 1)+1) and Mask;
2306 if fLogSize<>0 then begin
2307 result:=HashCode shr (32-fLogSize);
2308 end else begin
2309 result:=0;
2310 end;
2311 repeat
2312 Entity:=fCellToEntityIndex[result];
2313 if (Entity=ENT_EMPTY) or ((Entity<>ENT_DELETED) and CompareKey(fEntities[Entity].Key,Key)) then begin
2314 exit;
2315 end;
2316 result:=(result+Step) and Mask;
2317 until false;
2318 end;
2319
2320 procedure TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.Resize;
2321 var NewLogSize,NewSize,Cell,Entity:TPasGLTFInt32;
2322 Counter:TPasGLTFSizeInt;
2323 OldEntities:TPasGLTFHashMapEntities;
2324 OldCellToEntityIndex:TPasGLTFHashMapEntityIndices;
2325 OldEntityToCellIndex:TPasGLTFHashMapEntityIndices;
2326 begin
2327 NewLogSize:=0;
2328 NewSize:=fRealSize;
2329 while NewSize<>0 do begin
2330 NewSize:=NewSize shr 1;
2331 inc(NewLogSize);
2332 end;
2333 if NewLogSize<1 then begin
2334 NewLogSize:=1;
2335 end;
2336 fSize:=0;
2337 fRealSize:=0;
2338 fLogSize:=NewLogSize;
2339 OldEntities:=fEntities;
2340 OldCellToEntityIndex:=fCellToEntityIndex;
2341 OldEntityToCellIndex:=fEntityToCellIndex;
2342 fEntities:=nil;
2343 fCellToEntityIndex:=nil;
2344 fEntityToCellIndex:=nil;
2345 SetLength(fEntities,2 shl fLogSize);
2346 SetLength(fCellToEntityIndex,2 shl fLogSize);
2347 SetLength(fEntityToCellIndex,2 shl fLogSize);
2348 for Counter:=0 to length(fCellToEntityIndex)-1 do begin
2349 fCellToEntityIndex[Counter]:=ENT_EMPTY;
2350 end;
2351 for Counter:=0 to length(fEntityToCellIndex)-1 do begin
2352 fEntityToCellIndex[Counter]:=CELL_EMPTY;
2353 end;
2354 for Counter:=0 to length(OldEntityToCellIndex)-1 do begin
2355 Cell:=OldEntityToCellIndex[Counter];
2356 if Cell>=0 then begin
2357 Entity:=OldCellToEntityIndex[Cell];
2358 if Entity>=0 then begin
2359 Add(OldEntities[Counter].Key,OldEntities[Counter].Value);
2360 end;
2361 end;
2362 end;
2363 for Counter:=0 to length(OldEntities)-1 do begin
2364 Finalize(OldEntities[Counter].Key);
2365 Finalize(OldEntities[Counter].Value);
2366 end;
2367 SetLength(OldEntities,0);
2368 SetLength(OldCellToEntityIndex,0);
2369 SetLength(OldEntityToCellIndex,0);
2370 end;
2371
2372 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.Add(const Key:TPasGLTFHashMapKey;const Value:TPasGLTFHashMapValue):PPasGLTFHashMapEntity;
2373 var Entity:TPasGLTFInt32;
2374 Cell:TPasGLTFUInt32;
2375 begin
2376 result:=nil;
2377 while fRealSize>=(1 shl fLogSize) do begin
2378 Resize;
2379 end;
2380 Cell:=FindCell(Key);
2381 Entity:=fCellToEntityIndex[Cell];
2382 if Entity>=0 then begin
2383 result:=@fEntities[Entity];
2384 result^.Key:=Key;
2385 result^.Value:=Value;
2386 exit;
2387 end;
2388 Entity:=fSize;
2389 inc(fSize);
2390 if Entity<(2 shl fLogSize) then begin
2391 fCellToEntityIndex[Cell]:=Entity;
2392 fEntityToCellIndex[Entity]:=Cell;
2393 inc(fRealSize);
2394 result:=@fEntities[Entity];
2395 result^.Key:=Key;
2396 result^.Value:=Value;
2397 end;
2398 end;
2399
2400 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.Get(const Key:TPasGLTFHashMapKey;const CreateIfNotExist:boolean=false):PPasGLTFHashMapEntity;
2401 var Entity:TPasGLTFInt32;
2402 Cell:TPasGLTFUInt32;
2403 Value:TPasGLTFHashMapValue;
2404 begin
2405 result:=nil;
2406 Cell:=FindCell(Key);
2407 Entity:=fCellToEntityIndex[Cell];
2408 if Entity>=0 then begin
2409 result:=@fEntities[Entity];
2410 end else if CreateIfNotExist then begin
2411 Initialize(Value);
2412 result:=Add(Key,Value);
2413 end;
2414 end;
2415
2416 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.TryGet(const Key:TPasGLTFHashMapKey;out Value:TPasGLTFHashMapValue):boolean;
2417 var Entity:TPasGLTFInt32;
2418 begin
2419 Entity:=fCellToEntityIndex[FindCell(Key)];
2420 result:=Entity>=0;
2421 if result then begin
2422 Value:=fEntities[Entity].Value;
2423 end else begin
2424 Initialize(Value);
2425 end;
2426 end;
2427
2428 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.ExistKey(const Key:TPasGLTFHashMapKey):boolean;
2429 begin
2430 result:=fCellToEntityIndex[FindCell(Key)]>=0;
2431 end;
2432
2433 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.Delete(const Key:TPasGLTFHashMapKey):boolean;
2434 var Entity:TPasGLTFInt32;
2435 Cell:TPasGLTFUInt32;
2436 begin
2437 result:=false;
2438 Cell:=FindCell(Key);
2439 Entity:=fCellToEntityIndex[Cell];
2440 if Entity>=0 then begin
2441 Finalize(fEntities[Entity].Key);
2442 Finalize(fEntities[Entity].Value);
2443 fEntityToCellIndex[Entity]:=CELL_DELETED;
2444 fCellToEntityIndex[Cell]:=ENT_DELETED;
2445 result:=true;
2446 end;
2447 end;
2448
2449 function TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.GetValue(const Key:TPasGLTFHashMapKey):TPasGLTFHashMapValue;
2450 var Entity:TPasGLTFInt32;
2451 Cell:TPasGLTFUInt32;
2452 begin
2453 Cell:=FindCell(Key);
2454 Entity:=fCellToEntityIndex[Cell];
2455 if Entity>=0 then begin
2456 result:=fEntities[Entity].Value;
2457 end else begin
2458 result:=fDefaultValue;
2459 end;
2460 end;
2461
2462 procedure TPasGLTFUTF8StringHashMap<TPasGLTFHashMapValue>.SetValue(const Key:TPasGLTFHashMapKey;const Value:TPasGLTFHashMapValue);
2463 begin
2464 Add(Key,Value);
2465 end;
2466
2467 { TPasGLTF }
2468
2469 class function TPasGLTF.ResolveURIToPath(const aURI:TPasGLTFUTF8String):TPasGLTFUTF8String;
2470 begin
2471 result:=TPasGLTFUTF8String(StringReplace(String(aURI),{$ifdef Windows}'/','\'{$else}'\','/'{$endif},[rfReplaceAll]));
2472 end;
2473
2474 { TPasGLTF.TBase64 }
2475
2476 class function TPasGLTF.TBase64.Encode(const aData;const aDataLength:TPasGLTFSizeInt):TPasGLTFRawByteString;
2477 var Index,BitCount,OutputIndex:TPasGLTFSizeInt;
2478 Value:TPasGLTFUInt32;
2479 begin
2480 result:='';
2481 if aDataLength>0 then begin
2482 SetLength(result,(((aDataLength*4) div 3)+3) and not 3);
2483 OutputIndex:=0;
2484 Value:=0;
2485 BitCount:=-6;
2486 for Index:=0 to aDataLength-1 do begin
2487 Value:=(Value shl 8) or PPasGLTFUInt8Array(@aData)^[Index];
2488 inc(BitCount,8);
2489 while BitCount>=0 do begin
2490 result[Low(result)+OutputIndex]:=EncodingLookUpTable[(Value shr BitCount) and 63];
2491 inc(OutputIndex);
2492 dec(BitCount,6);
2493 end;
2494 end;
2495 if BitCount>-6 then begin
2496 result[Low(result)+OutputIndex]:=EncodingLookUpTable[((Value shl 8) shr (BitCount+8)) and 63];
2497 inc(OutputIndex);
2498 end;
2499 while (OutputIndex and 3)<>0 do begin
2500 result[Low(result)+OutputIndex]:='=';
2501 inc(OutputIndex);
2502 end;
2503 SetLength(result,OutputIndex);
2504 end;
2505 end;
2506
2507 class function TPasGLTF.TBase64.Encode(const aData:array of TPasGLTFUInt8):TPasGLTFRawByteString;
2508 begin
2509 result:=Encode(aData[0],length(aData));
2510 end;
2511
2512 class function TPasGLTF.TBase64.Encode(const aData:TPasGLTFRawByteString):TPasGLTFRawByteString;
2513 begin
2514 result:=Encode(aData[Low(aData)],length(aData));
2515 end;
2516
2517 class function TPasGLTF.TBase64.Encode(const aData:TStream):TPasGLTFRawByteString;
2518 var Bytes:TPasGLTFUInt8DynamicArray;
2519 begin
2520 Bytes:=nil;
2521 try
2522 SetLength(Bytes,aData.Size);
2523 aData.Seek(0,soBeginning);
2524 aData.ReadBuffer(Bytes[0],aData.Size);
2525 result:=Encode(Bytes[0],length(Bytes));
2526 finally
2527 Bytes:=nil;
2528 end;
2529 end;
2530
2531 class function TPasGLTF.TBase64.Decode(const aInput:TPasGLTFRawByteString;const aOutput:TStream):boolean;
2532 var Index,Size,BitCount,OutputIndex,
2533 LookUpTableValue,Remaining:TPasGLTFSizeInt;
2534 Value:TPasGLTFUInt32;
2535 Buffer:TPasGLTFUInt8DynamicArray;
2536 begin
2537 result:=false;
2538 Buffer:=nil;
2539 try
2540 Size:=length(aInput);
2541 if Size>0 then begin
2542 if (Size and 3)=0 then begin
2543 result:=true;
2544 SetLength(Buffer,(Size*3) shr 2);
2545 Value:=0;
2546 BitCount:=-8;
2547 OutputIndex:=0;
2548 try
2549 for Index:=1 to Size do begin
2550 LookUpTableValue:=DecodingLookUpTable[aInput[Index]];
2551 if LookUpTableValue>=0 then begin
2552 Value:=(Value shl 6) or LookUpTableValue;
2553 inc(BitCount,6);
2554 while BitCount>=0 do begin
2555 Buffer[OutputIndex]:=(Value shr BitCount) and $ff;
2556 inc(OutputIndex);
2557 dec(BitCount,8);
2558 end;
2559 end else begin
2560 case aInput[Index] of
2561 '=':begin
2562 Remaining:=Size-Index;
2563 if (Remaining>1) or ((Remaining=1) and (aInput[Index+1]<>'=')) then begin
2564 result:=false;
2565 end;
2566 end;
2567 else begin
2568 result:=false;
2569 end;
2570 end;
2571 break;
2572 end;
2573 end;
2574 finally
2575 SetLength(Buffer,OutputIndex);
2576 end;
2577 if result then begin
2578 aOutput.WriteBuffer(Buffer[0],OutputIndex);
2579 end;
2580 end;
2581 end else begin
2582 result:=true;
2583 end;
2584 finally
2585 Buffer:=nil;
2586 end;
2587 end;
2588
2589 { TPasGLTF.TBaseObject }
2590
2591 constructor TPasGLTF.TBaseObject.Create(const aDocument:TDocument);
2592 begin
2593 inherited Create;
2594 fDocument:=aDocument;
2595 end;
2596
2597 destructor TPasGLTF.TBaseObject.Destroy;
2598 begin
2599 inherited Destroy;
2600 end;
2601
2602 { TPasGLTF.TBaseExtensionsExtrasObject }
2603
2604 constructor TPasGLTF.TBaseExtensionsExtrasObject.Create(const aDocument:TDocument);
2605 begin
2606 inherited Create(aDocument);
2607 fExtensions:=TPasJSONItemObject.Create;
2608 fExtras:=TPasJSONItemObject.Create;
2609 end;
2610
2611 destructor TPasGLTF.TBaseExtensionsExtrasObject.Destroy;
2612 begin
2613 FreeAndNil(fExtensions);
2614 FreeAndNil(fExtras);
2615 inherited Destroy;
2616 end;
2617
2618 { TPasGLTF.TAccessor.TComponentTypeHelper }
2619
2620 function TPasGLTF.TAccessor.TComponentTypeHelper.GetSize:TPasGLTFSizeInt;
2621 begin
2622 case self of
2623 TPasGLTF.TAccessor.TComponentType.SignedByte:begin
2624 result:=SizeOf(TPasGLTFInt8);
2625 end;
2626 TPasGLTF.TAccessor.TComponentType.UnsignedByte:begin
2627 result:=SizeOf(TPasGLTFUInt8);
2628 end;
2629 TPasGLTF.TAccessor.TComponentType.SignedShort:begin
2630 result:=SizeOf(TPasGLTFInt16);
2631 end;
2632 TPasGLTF.TAccessor.TComponentType.UnsignedShort:begin
2633 result:=SizeOf(TPasGLTFUInt16);
2634 end;
2635 TPasGLTF.TAccessor.TComponentType.UnsignedInt:begin
2636 result:=SizeOf(TPasGLTFUInt32);
2637 end;
2638 TPasGLTF.TAccessor.TComponentType.Float:begin
2639 result:=SizeOf(TPasGLTFFloat);
2640 end;
2641 else {TPasGLTF.TAccessor.TComponentType.None:}begin
2642 result:=0;
2643 Assert(false);
2644 end;
2645 end;
2646 end;
2647
2648 { TPasGLTF.TAccessor.TTypeHelper }
2649
2650 function TPasGLTF.TAccessor.TTypeHelper.GetComponentCount:TPasGLTFSizeInt;
2651 begin
2652 result:=TPasGLTF.TAccessor.TypeComponentCountTable[self];
2653 end;
2654
2655 { TPasGLTF.TAccessor.TSparse.TIndices }
2656
2657 constructor TPasGLTF.TAccessor.TSparse.TIndices.Create(const aDocument:TDocument);
2658 begin
2659 inherited Create(aDocument);
2660 fComponentType:=TComponentType.None;
2661 fBufferView:=0;
2662 fByteOffset:=0;
2663 fEmpty:=false;
2664 end;
2665
2666 destructor TPasGLTF.TAccessor.TSparse.TIndices.Destroy;
2667 begin
2668 inherited Destroy;
2669 end;
2670
2671 { TPasGLTF.TAccessor.TSparse.TValues }
2672
2673 constructor TPasGLTF.TAccessor.TSparse.TValues.Create(const aDocument:TDocument);
2674 begin
2675 inherited Create(aDocument);
2676 fBufferView:=0;
2677 fByteOffset:=0;
2678 fEmpty:=false;
2679 end;
2680
2681 destructor TPasGLTF.TAccessor.TSparse.TValues.Destroy;
2682 begin
2683 inherited Destroy;
2684 end;
2685
2686 { TPasGLTF.TAccessor.TSparse }
2687
2688 constructor TPasGLTF.TAccessor.TSparse.Create(const aDocument:TDocument);
2689 begin
2690 inherited Create(aDocument);
2691 fCount:=0;
2692 fIndices:=TIndices.Create(fDocument);
2693 fValues:=TValues.Create(fDocument);
2694 end;
2695
2696 destructor TPasGLTF.TAccessor.TSparse.Destroy;
2697 begin
2698 FreeAndNil(fIndices);
2699 FreeAndNil(fValues);
2700 inherited Destroy;
2701 end;
2702
2703 function TPasGLTF.TAccessor.TSparse.GetEmpty:boolean;
2704 begin
2705 result:=fCount=0;
2706 end;
2707
2708 { TPasGLTF.TAccessor }
2709
2710 constructor TPasGLTF.TAccessor.Create(const aDocument:TDocument);
2711 begin
2712 inherited Create(aDocument);
2713 fComponentType:=TComponentType.None;
2714 fType:=TType.None;
2715 fBufferView:=-1;
2716 fByteOffset:=0;
2717 fCount:=0;
2718 fNormalized:=TDefaults.AccessorNormalized;
2719 fMinArray:=TMinMaxDynamicArray.Create;
2720 fMaxArray:=TMinMaxDynamicArray.Create;
2721 fSparse:=TSparse.Create(fDocument);
2722 end;
2723
2724 destructor TPasGLTF.TAccessor.Destroy;
2725 begin
2726 FreeAndNil(fMinArray);
2727 FreeAndNil(fMaxArray);
2728 FreeAndNil(fSparse);
2729 inherited Destroy;
2730 end;
2731
2732 function TPasGLTF.TAccessor.DecodeAsDoubleArray(const aForVertex:boolean=true):TPasGLTFDoubleDynamicArray;
2733 var Index,
2734 ComponentIndex,
2735 ComponentCount,
2736 ComponentSize,
2737 ElementSize,
2738 SkipEvery,
2739 SkipBytes,
2740 IndicesComponentSize,
2741 Base:TPasGLTFSizeInt;
2742 Indices,
2743 Values:TPasGLTFDoubleDynamicArray;
2744 begin
2745 ComponentCount:=fType.GetComponentCount;
2746 ComponentSize:=fComponentType.GetSize;
2747 ElementSize:=ComponentSize*ComponentCount;
2748 SkipEvery:=0;
2749 SkipBytes:=0;
2750 case fComponentType of
2751 TPasGLTF.TAccessor.TComponentType.SignedByte,
2752 TPasGLTF.TAccessor.TComponentType.UnsignedByte:begin
2753 case fType of
2754 TPasGLTF.TAccessor.TType.Mat2:begin
2755 SkipEvery:=2;
2756 SkipBytes:=2;
2757 ElementSize:=8;
2758 end;
2759 TPasGLTF.TAccessor.TType.Mat3:begin
2760 SkipEvery:=3;
2761 SkipBytes:=1;
2762 ElementSize:=12;
2763 end;
2764 end;
2765 end;
2766 TPasGLTF.TAccessor.TComponentType.SignedShort,
2767 TPasGLTF.TAccessor.TComponentType.UnsignedShort:begin
2768 case fType of
2769 TPasGLTF.TAccessor.TType.Mat3:begin
2770 SkipEvery:=6;
2771 SkipBytes:=4;
2772 ElementSize:=16;
2773 end;
2774 end;
2775 end;
2776 end;
2777 result:=nil;
2778 if fBufferView>=0 then begin
2779 if fBufferView<fDocument.fBufferViews.Count then begin
2780 result:=fDocument.fBufferViews[fBufferView].Decode(SkipEvery,
2781 SkipBytes,
2782 ElementSize,
2783 fCount,
2784 fType,
2785 ComponentCount,
2786 fComponentType,
2787 ComponentSize,
2788 fByteOffset,
2789 fNormalized,
2790 aForVertex);
2791 end else begin
2792 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
2793 end;
2794 end else begin
2795 SetLength(result,ComponentCount*fCount);
2796 for Index:=0 to length(result)-1 do begin
2797 result[Index]:=0;
2798 end;
2799 end;
2800 if fSparse.fCount>0 then begin
2801 if (fSparse.fIndices.fBufferView>=0) and (fSparse.fIndices.fBufferView<fDocument.fBufferViews.Count) then begin
2802 IndicesComponentSize:=fSparse.fIndices.fComponentType.GetSize;
2803 Indices:=fDocument.fBufferViews[fSparse.fIndices.fBufferView].Decode(0,
2804 0,
2805 IndicesComponentSize,
2806 fSparse.fCount,
2807 TType.Scalar,
2808 1,
2809 fSparse.fIndices.fComponentType,
2810 IndicesComponentSize,
2811 fSparse.fIndices.fByteOffset,
2812 false,
2813 false);
2814 if (fSparse.fValues.fBufferView>=0) and (fSparse.fValues.fBufferView<fDocument.fBufferViews.Count) then begin
2815 Values:=fDocument.fBufferViews[fSparse.fValues.fBufferView].Decode(SkipEvery,
2816 SkipBytes,
2817 ElementSize,
2818 fSparse.fCount,
2819 fType,
2820 ComponentCount,
2821 fComponentType,
2822 ComponentSize,
2823 fSparse.fValues.fByteOffset,
2824 fNormalized,
2825 aForVertex);
2826 for Index:=0 to length(Indices)-1 do begin
2827 Base:=trunc(Indices[Index])*ComponentCount;
2828 for ComponentIndex:=0 to ComponentCount-1 do begin
2829 result[Base+ComponentIndex]:=Values[(Index*ComponentCount)+ComponentIndex];
2830 end;
2831 end;
2832 end else begin
2833 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
2834 end;
2835 end else begin
2836 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
2837 end;
2838 end;
2839 end;
2840
2841 function TPasGLTF.TAccessor.DecodeAsInt32Array(const aForVertex:boolean):TPasGLTFInt32DynamicArray;
2842 var Index:TPasGLTFSizeInt;
2843 DoubleArray:TPasGLTFDoubleDynamicArray;
2844 begin
2845 result:=nil;
2846 DoubleArray:=DecodeAsDoubleArray(aForVertex);
2847 SetLength(result,length(DoubleArray));
2848 for Index:=0 to length(result)-1 do begin
2849 result[Index]:=trunc(DoubleArray[Index]);
2850 end;
2851 end;
2852
2853 function TPasGLTF.TAccessor.DecodeAsUInt32Array(const aForVertex:boolean):TPasGLTFUInt32DynamicArray;
2854 var Index:TPasGLTFSizeInt;
2855 DoubleArray:TPasGLTFDoubleDynamicArray;
2856 begin
2857 result:=nil;
2858 DoubleArray:=DecodeAsDoubleArray(aForVertex);
2859 SetLength(result,length(DoubleArray));
2860 for Index:=0 to length(result)-1 do begin
2861 result[Index]:=trunc(DoubleArray[Index]);
2862 end;
2863 end;
2864
2865 function TPasGLTF.TAccessor.DecodeAsInt64Array(const aForVertex:boolean):TPasGLTFInt64DynamicArray;
2866 var Index:TPasGLTFSizeInt;
2867 DoubleArray:TPasGLTFDoubleDynamicArray;
2868 begin
2869 result:=nil;
2870 DoubleArray:=DecodeAsDoubleArray(aForVertex);
2871 SetLength(result,length(DoubleArray));
2872 for Index:=0 to length(result)-1 do begin
2873 result[Index]:=trunc(DoubleArray[Index]);
2874 end;
2875 end;
2876
2877 function TPasGLTF.TAccessor.DecodeAsUInt64Array(const aForVertex:boolean):TPasGLTFUInt64DynamicArray;
2878 var Index:TPasGLTFSizeInt;
2879 DoubleArray:TPasGLTFDoubleDynamicArray;
2880 begin
2881 result:=nil;
2882 DoubleArray:=DecodeAsDoubleArray(aForVertex);
2883 SetLength(result,length(DoubleArray));
2884 for Index:=0 to length(result)-1 do begin
2885 result[Index]:=trunc(DoubleArray[Index]);
2886 end;
2887 end;
2888
2889 function TPasGLTF.TAccessor.DecodeAsFloatArray(const aForVertex:boolean):TPasGLTFFloatDynamicArray;
2890 var Index:TPasGLTFSizeInt;
2891 DoubleArray:TPasGLTFDoubleDynamicArray;
2892 begin
2893 result:=nil;
2894 DoubleArray:=DecodeAsDoubleArray(aForVertex);
2895 SetLength(result,length(DoubleArray));
2896 for Index:=0 to length(result)-1 do begin
2897 result[Index]:=DoubleArray[Index];
2898 end;
2899 end;
2900
2901 function TPasGLTF.TAccessor.DecodeAsVector2Array(const aForVertex:boolean):TVector2DynamicArray;
2902 var Index:TPasGLTFSizeInt;
2903 DoubleArray:TPasGLTFDoubleDynamicArray;
2904 begin
2905 result:=nil;
2906 DoubleArray:=DecodeAsDoubleArray(aForVertex);
2907 Assert((length(DoubleArray) and 1)=0);
2908 SetLength(result,length(DoubleArray) shr 1);
2909 for Index:=0 to length(result)-1 do begin
2910 result[Index,0]:=DoubleArray[(Index shl 1) or 0];
2911 result[Index,1]:=DoubleArray[(Index shl 1) or 1];
2912 end;
2913 end;
2914
2915 function TPasGLTF.TAccessor.DecodeAsVector3Array(const aForVertex:boolean):TVector3DynamicArray;
2916 var Index:TPasGLTFSizeInt;
2917 DoubleArray:TPasGLTFDoubleDynamicArray;
2918 begin
2919 result:=nil;
2920 DoubleArray:=DecodeAsDoubleArray(aForVertex);
2921 Assert((length(DoubleArray) mod 3)=0);
2922 SetLength(result,length(DoubleArray) div 3);
2923 for Index:=0 to length(result)-1 do begin
2924 result[Index,0]:=DoubleArray[(Index*3)+0];
2925 result[Index,1]:=DoubleArray[(Index*3)+1];
2926 result[Index,2]:=DoubleArray[(Index*3)+2];
2927 end;
2928 end;
2929
2930 function TPasGLTF.TAccessor.DecodeAsVector4Array(const aForVertex:boolean):TVector4DynamicArray;
2931 var Index:TPasGLTFSizeInt;
2932 DoubleArray:TPasGLTFDoubleDynamicArray;
2933 begin
2934 result:=nil;
2935 DoubleArray:=DecodeAsDoubleArray(aForVertex);
2936 Assert((length(DoubleArray) and 3)=0);
2937 SetLength(result,length(DoubleArray) shr 2);
2938 for Index:=0 to length(result)-1 do begin
2939 result[Index,0]:=DoubleArray[(Index shl 2) or 0];
2940 result[Index,1]:=DoubleArray[(Index shl 2) or 1];
2941 result[Index,2]:=DoubleArray[(Index shl 2) or 2];
2942 result[Index,3]:=DoubleArray[(Index shl 2) or 3];
2943 end;
2944 end;
2945
2946 function TPasGLTF.TAccessor.DecodeAsInt32Vector4Array(const aForVertex:boolean):TInt32Vector4DynamicArray;
2947 var Index:TPasGLTFSizeInt;
2948 DoubleArray:TPasGLTFDoubleDynamicArray;
2949 begin
2950 result:=nil;
2951 DoubleArray:=DecodeAsDoubleArray(aForVertex);
2952 Assert((length(DoubleArray) and 3)=0);
2953 SetLength(result,length(DoubleArray) shr 2);
2954 for Index:=0 to length(result)-1 do begin
2955 result[Index,0]:=trunc(DoubleArray[(Index shl 2) or 0]);
2956 result[Index,1]:=trunc(DoubleArray[(Index shl 2) or 1]);
2957 result[Index,2]:=trunc(DoubleArray[(Index shl 2) or 2]);
2958 result[Index,3]:=trunc(DoubleArray[(Index shl 2) or 3]);
2959 end;
2960 end;
2961
2962 function TPasGLTF.TAccessor.DecodeAsUInt32Vector4Array(const aForVertex:boolean):TUInt32Vector4DynamicArray;
2963 var Index:TPasGLTFSizeInt;
2964 DoubleArray:TPasGLTFDoubleDynamicArray;
2965 begin
2966 result:=nil;
2967 DoubleArray:=DecodeAsDoubleArray(aForVertex);
2968 Assert((length(DoubleArray) and 3)=0);
2969 SetLength(result,length(DoubleArray) shr 2);
2970 for Index:=0 to length(result)-1 do begin
2971 result[Index,0]:=trunc(DoubleArray[(Index shl 2) or 0]);
2972 result[Index,1]:=trunc(DoubleArray[(Index shl 2) or 1]);
2973 result[Index,2]:=trunc(DoubleArray[(Index shl 2) or 2]);
2974 result[Index,3]:=trunc(DoubleArray[(Index shl 2) or 3]);
2975 end;
2976 end;
2977
2978 function TPasGLTF.TAccessor.DecodeAsColorArray(const aForVertex:boolean=true):TVector4DynamicArray;
2979 var Index:TPasGLTFSizeInt;
2980 DoubleArray:TPasGLTFDoubleDynamicArray;
2981 begin
2982 result:=nil;
2983 DoubleArray:=DecodeAsDoubleArray(aForVertex);
2984 if fType=TType.Vec3 then begin
2985 Assert((length(DoubleArray) mod 3)=0);
2986 SetLength(result,length(DoubleArray) div 3);
2987 for Index:=0 to length(result)-1 do begin
2988 result[Index,0]:=DoubleArray[(Index*3)+0];
2989 result[Index,1]:=DoubleArray[(Index*3)+1];
2990 result[Index,2]:=DoubleArray[(Index*3)+2];
2991 result[Index,3]:=1.0;
2992 end;
2993 end else begin
2994 Assert((length(DoubleArray) and 3)=0);
2995 SetLength(result,length(DoubleArray) shr 2);
2996 for Index:=0 to length(result)-1 do begin
2997 result[Index,0]:=DoubleArray[(Index shl 2) or 0];
2998 result[Index,1]:=DoubleArray[(Index shl 2) or 1];
2999 result[Index,2]:=DoubleArray[(Index shl 2) or 2];
3000 result[Index,3]:=DoubleArray[(Index shl 2) or 3];
3001 end;
3002 end;
3003 end;
3004
3005 function TPasGLTF.TAccessor.DecodeAsMatrix2x2Array(const aForVertex:boolean=true):TMatrix2x2DynamicArray;
3006 var Index:TPasGLTFSizeInt;
3007 DoubleArray:TPasGLTFDoubleDynamicArray;
3008 begin
3009 result:=nil;
3010 DoubleArray:=DecodeAsDoubleArray(aForVertex);
3011 Assert((length(DoubleArray) and 3)=0);
3012 SetLength(result,length(DoubleArray) shr 2);
3013 for Index:=0 to length(result)-1 do begin
3014 result[Index,0]:=DoubleArray[(Index shl 2) or 0];
3015 result[Index,1]:=DoubleArray[(Index shl 2) or 1];
3016 result[Index,2]:=DoubleArray[(Index shl 2) or 2];
3017 result[Index,3]:=DoubleArray[(Index shl 2) or 3];
3018 end;
3019 end;
3020
3021 function TPasGLTF.TAccessor.DecodeAsMatrix3x3Array(const aForVertex:boolean=true):TMatrix3x3DynamicArray;
3022 var Index:TPasGLTFSizeInt;
3023 DoubleArray:TPasGLTFDoubleDynamicArray;
3024 begin
3025 result:=nil;
3026 DoubleArray:=DecodeAsDoubleArray(aForVertex);
3027 Assert((length(DoubleArray) mod 9)=0);
3028 SetLength(result,length(DoubleArray) div 9);
3029 for Index:=0 to length(result)-1 do begin
3030 result[Index,0]:=DoubleArray[(Index*9)+0];
3031 result[Index,1]:=DoubleArray[(Index*9)+1];
3032 result[Index,2]:=DoubleArray[(Index*9)+2];
3033 result[Index,3]:=DoubleArray[(Index*9)+3];
3034 result[Index,4]:=DoubleArray[(Index*9)+4];
3035 result[Index,5]:=DoubleArray[(Index*9)+5];
3036 result[Index,6]:=DoubleArray[(Index*9)+6];
3037 result[Index,7]:=DoubleArray[(Index*9)+7];
3038 result[Index,8]:=DoubleArray[(Index*9)+8];
3039 end;
3040 end;
3041
3042 function TPasGLTF.TAccessor.DecodeAsMatrix4x4Array(const aForVertex:boolean=true):TMatrix4x4DynamicArray;
3043 var Index:TPasGLTFSizeInt;
3044 DoubleArray:TPasGLTFDoubleDynamicArray;
3045 begin
3046 result:=nil;
3047 DoubleArray:=DecodeAsDoubleArray(aForVertex);
3048 Assert((length(DoubleArray) and 15)=0);
3049 SetLength(result,length(DoubleArray) shr 4);
3050 for Index:=0 to length(result)-1 do begin
3051 result[Index,0]:=DoubleArray[(Index shl 4) or 0];
3052 result[Index,1]:=DoubleArray[(Index shl 4) or 1];
3053 result[Index,2]:=DoubleArray[(Index shl 4) or 2];
3054 result[Index,3]:=DoubleArray[(Index shl 4) or 3];
3055 result[Index,4]:=DoubleArray[(Index shl 4) or 4];
3056 result[Index,5]:=DoubleArray[(Index shl 4) or 5];
3057 result[Index,6]:=DoubleArray[(Index shl 4) or 6];
3058 result[Index,7]:=DoubleArray[(Index shl 4) or 7];
3059 result[Index,8]:=DoubleArray[(Index shl 4) or 8];
3060 result[Index,9]:=DoubleArray[(Index shl 4) or 9];
3061 result[Index,10]:=DoubleArray[(Index shl 4) or 10];
3062 result[Index,11]:=DoubleArray[(Index shl 4) or 11];
3063 result[Index,12]:=DoubleArray[(Index shl 4) or 12];
3064 result[Index,13]:=DoubleArray[(Index shl 4) or 13];
3065 result[Index,14]:=DoubleArray[(Index shl 4) or 14];
3066 result[Index,15]:=DoubleArray[(Index shl 4) or 15];
3067 end;
3068 end;
3069
3070 { TPasGLTF.TAnimation.TChannel.TTarget }
3071
3072 constructor TPasGLTF.TAnimation.TChannel.TTarget.Create(const aDocument:TDocument);
3073 begin
3074 inherited Create(aDocument);
3075 fNode:=-1;
3076 fPath:='';
3077 fEmpty:=false;
3078 end;
3079
3080 destructor TPasGLTF.TAnimation.TChannel.TTarget.Destroy;
3081 begin
3082 inherited Destroy;
3083 end;
3084
3085 { TPasGLTF.TAnimation.TChannel }
3086
3087 constructor TPasGLTF.TAnimation.TChannel.Create(const aDocument:TDocument);
3088 begin
3089 inherited Create(aDocument);
3090 fSampler:=-1;
3091 fTarget:=TTarget.Create(aDocument);
3092 end;
3093
3094 destructor TPasGLTF.TAnimation.TChannel.Destroy;
3095 begin
3096 FreeAndNil(fTarget);
3097 inherited Destroy;
3098 end;
3099
3100 { TPasGLTF.TAnimation.TSampler }
3101
3102 constructor TPasGLTF.TAnimation.TSampler.Create(const aDocument:TDocument);
3103 begin
3104 inherited Create(aDocument);
3105 fInput:=-1;
3106 fOutput:=-1;
3107 fInterpolation:=TSamplerType.Linear;
3108 end;
3109
3110 destructor TPasGLTF.TAnimation.TSampler.Destroy;
3111 begin
3112 inherited Destroy;
3113 end;
3114
3115 { TPasGLTF.TAnimation }
3116
3117 constructor TPasGLTF.TAnimation.Create(const aDocument:TDocument);
3118 begin
3119 inherited Create(aDocument);
3120 fName:='';
3121 fChannels:=TChannels.Create;
3122 fSamplers:=TSamplers.Create;
3123 end;
3124
3125 destructor TPasGLTF.TAnimation.Destroy;
3126 begin
3127 FreeAndNil(fChannels);
3128 FreeAndNil(fSamplers);
3129 inherited Destroy;
3130 end;
3131
3132 { TPasGLTF.TAsset }
3133
3134 constructor TPasGLTF.TAsset.Create(const aDocument:TDocument);
3135 begin
3136 inherited Create(aDocument);
3137 fCopyright:='';
3138 fGenerator:='';
3139 fMinVersion:='';
3140 fVersion:='2.0';
3141 fEmpty:=false;
3142 end;
3143
3144 destructor TPasGLTF.TAsset.Destroy;
3145 begin
3146 inherited Destroy;
3147 end;
3148
3149 { TPasGLTF.TBuffer }
3150
3151 constructor TPasGLTF.TBuffer.Create(const aDocument:TDocument);
3152 begin
3153 inherited Create(aDocument);
3154 fByteLength:=0;
3155 fName:='';
3156 fURI:='';
3157 fData:=TMemoryStream.Create;
3158 end;
3159
3160 destructor TPasGLTF.TBuffer.Destroy;
3161 begin
3162 FreeAndNil(fData);
3163 inherited Destroy;
3164 end;
3165
3166 procedure TPasGLTF.TBuffer.SetEmbeddedResourceData;
3167 begin
3168 fURI:='data:'+MimeTypeApplicationOctet+';base64,'+TBase64.Encode(fData);
3169 end;
3170
3171 { TPasGLTF.TBufferView }
3172
3173 constructor TPasGLTF.TBufferView.Create(const aDocument:TDocument);
3174 begin
3175 inherited Create(aDocument);
3176 fName:='';
3177 fBuffer:=-1;
3178 fByteOffset:=0;
3179 fByteLength:=0;
3180 fByteStride:=0;
3181 fTarget:=TTargetType.None;
3182 end;
3183
3184 destructor TPasGLTF.TBufferView.Destroy;
3185 begin
3186 inherited Destroy;
3187 end;
3188
3189 function TPasGLTF.TBufferView.Decode(const aSkipEvery:TPasGLTFSizeUInt;
3190 const aSkipBytes:TPasGLTFSizeUInt;
3191 const aElementSize:TPasGLTFSizeUInt;
3192 const aCount:TPasGLTFSizeUInt;
3193 const aType:TPasGLTF.TAccessor.TType;
3194 const aComponentCount:TPasGLTFSizeUInt;
3195 const aComponentType:TPasGLTF.TAccessor.TComponentType;
3196 const aComponentSize:TPasGLTFSizeUInt;
3197 const aByteOffset:TPasGLTFSizeUInt;
3198 const aNormalized:boolean;
3199 const aForVertex:boolean):TPasGLTFDoubleDynamicArray;
3200 var Stride,Offset,Index,
3201 ComponentIndex,
3202 OutputIndex:TPasGLTFSizeUInt;
3203 Buffer:TPasGLTF.TBuffer;
3204 BufferData,Source:PPasGLTFUInt8Array;
3205 Value:TPasGLTFDouble;
3206 begin
3207
3208 result:=nil;
3209
3210 Buffer:=fDocument.fBuffers[fBuffer];
3211
3212 if fByteStride<>0 then begin
3213 Stride:=fByteStride;
3214 end else begin
3215 Stride:=aElementSize;
3216 end;
3217
3218 if aForVertex and ((Stride and 3)<>0) then begin
3219 inc(Stride,4-(Stride and 3));
3220 end;
3221
3222 SetLength(result,aCount*aComponentCount);
3223
3224 Offset:=fByteOffset+aByteOffset;
3225
3226 BufferData:=Buffer.fData.Memory;
3227
3228 if (((Stride*(aCount-1))+aElementSize)>fByteLength) or
3229 ((Offset+((Stride*(aCount-1))+aElementSize))>TPasGLTFSizeUInt(Buffer.fData.Size)) then begin
3230 raise EPasGLTFInvalidDocument.Create('Invalid document');
3231 end;
3232
3233 OutputIndex:=0;
3234
3235 for Index:=1 to aCount do begin
3236
3237 Source:=@BufferData^[Offset+((Index-1)*Stride)];
3238
3239 for ComponentIndex:=1 to aComponentCount do begin
3240
3241 if (aSkipEvery>0) and (ComponentIndex>1) and (((ComponentIndex-1) mod aSkipEvery)=0) then begin
3242 Source:=@Source^[aSkipBytes];
3243 end;
3244
3245 Value:=0.0;
3246
3247 case aComponentType of
3248 TPasGLTF.TAccessor.TComponentType.SignedByte:begin
3249 if aNormalized then begin
3250 Value:=TPasGLTFInt8(TPasGLTFPointer(@Source^[0])^)/128.0;
3251 end else begin
3252 Value:=TPasGLTFInt8(TPasGLTFPointer(@Source^[0])^);
3253 end;
3254 end;
3255 TPasGLTF.TAccessor.TComponentType.UnsignedByte:begin
3256 if aNormalized then begin
3257 Value:=TPasGLTFUInt8(TPasGLTFPointer(@Source^[0])^)/255.0;
3258 end else begin
3259 Value:=TPasGLTFUInt8(TPasGLTFPointer(@Source^[0])^);
3260 end;
3261 end;
3262 TPasGLTF.TAccessor.TComponentType.SignedShort:begin
3263 if aNormalized then begin
3264 Value:=TPasGLTFInt16(TPasGLTFPointer(@Source^[0])^)/32768.0;
3265 end else begin
3266 Value:=TPasGLTFInt16(TPasGLTFPointer(@Source^[0])^);
3267 end;
3268 end;
3269 TPasGLTF.TAccessor.TComponentType.UnsignedShort:begin
3270 if aNormalized then begin
3271 Value:=TPasGLTFUInt16(TPasGLTFPointer(@Source^[0])^)/65535.0;
3272 end else begin
3273 Value:=TPasGLTFUInt16(TPasGLTFPointer(@Source^[0])^);
3274 end;
3275 end;
3276 TPasGLTF.TAccessor.TComponentType.UnsignedInt:begin
3277 if aNormalized then begin
3278 Value:=TPasGLTFUInt32(TPasGLTFPointer(@Source^[0])^)/4294967295.0;
3279 end else begin
3280 Value:=TPasGLTFUInt32(TPasGLTFPointer(@Source^[0])^);
3281 end;
3282 end;
3283 TPasGLTF.TAccessor.TComponentType.Float:begin
3284 Value:=TPasGLTFFloat(TPasGLTFPointer(@Source^[0])^);
3285 end;
3286 else {TPasGLTF.TAccessor.TComponentType.None:}begin
3287 raise EPasGLTFInvalidDocument.Create('Invalid document');
3288 end;
3289 end;
3290
3291 result[OutputIndex]:=Value;
3292 inc(OutputIndex);
3293
3294 Source:=@Source^[aComponentSize];
3295
3296 end;
3297
3298 end;
3299
3300 end;
3301
3302 { TPasGLTF.TCamera.TOrthographic }
3303
3304 constructor TPasGLTF.TCamera.TOrthographic.Create(const aDocument:TDocument);
3305 begin
3306 inherited Create(aDocument);
3307 fXMag:=TDefaults.FloatSentinel;
3308 fYMag:=TDefaults.FloatSentinel;
3309 fZNear:=-TDefaults.FloatSentinel;
3310 fZFar:=-TDefaults.FloatSentinel;
3311 fEmpty:=false;
3312 end;
3313
3314 destructor TPasGLTF.TCamera.TOrthographic.Destroy;
3315 begin
3316 inherited Destroy;
3317 end;
3318
3319 { TPasGLTF.TCamera.TPerspective }
3320
3321 constructor TPasGLTF.TCamera.TPerspective.Create(const aDocument:TDocument);
3322 begin
3323 inherited Create(aDocument);
3324 fAspectRatio:=1.778;
3325 fYFov:=0.252;
3326 fZNear:=0.1;
3327 fZFar:=1000.0;
3328 fEmpty:=false;
3329 end;
3330
3331 destructor TPasGLTF.TCamera.TPerspective.Destroy;
3332 begin
3333 inherited Destroy;
3334 end;
3335
3336 { TPasGLTF.TCamera }
3337
3338 constructor TPasGLTF.TCamera.Create(const aDocument:TDocument);
3339 begin
3340 inherited Create(aDocument);
3341 fType:=TCameraType.None;
3342 fOrthographic:=TOrthographic.Create(fDocument);
3343 fPerspective:=TPerspective.Create(fDocument);
3344 end;
3345
3346 destructor TPasGLTF.TCamera.Destroy;
3347 begin
3348 FreeAndNil(fOrthographic);
3349 FreeAndNil(fPerspective);
3350 inherited Destroy;
3351 end;
3352
3353 { TPasGLTF.TImage }
3354
3355 constructor TPasGLTF.TImage.Create(const aDocument:TDocument);
3356 begin
3357 inherited Create(aDocument);
3358 fBufferView:=-1;
3359 fName:='';
3360 fURI:='';
3361 fMimeType:='';
3362 end;
3363
3364 destructor TPasGLTF.TImage.Destroy;
3365 begin
3366 inherited Destroy;
3367 end;
3368
3369 procedure TPasGLTF.TImage.SetEmbeddedResourceData(const aStream:TStream);
3370 begin
3371 fURI:='data:'+fMimeType+';base64,'+TBase64.Encode(aStream);
3372 end;
3373
3374 procedure TPasGLTF.TImage.GetResourceData(const aStream:TStream);
3375 var BufferView:TBufferView;
3376 Buffer:TBuffer;
3377 begin
3378 if fBufferView>=0 then begin
3379 if fBufferView<fDocument.fBufferViews.Count then begin
3380 BufferView:=fDocument.fBufferViews[fBufferView];
3381 if (BufferView.fBuffer>=0) and (BufferView.fBuffer<fDocument.fBuffers.Count) then begin
3382 Buffer:=fDocument.fBuffers[BufferView.fBuffer];
3383 if (BufferView.fByteOffset+BufferView.fByteLength)<=Buffer.fData.Size then begin
3384 aStream.WriteBuffer(PPasGLTFUInt8Array(Buffer.fData.Memory)^[BufferView.fByteOffset],BufferView.fByteLength);
3385 aStream.Seek(-BufferView.fByteLength,soCurrent);
3386 end else begin
3387 raise EInOutError.Create('I/O error');
3388 end;
3389 end else begin
3390 raise EInOutError.Create('I/O error');
3391 end;
3392 end else begin
3393 raise EInOutError.Create('I/O error');
3394 end;
3395 end else begin
3396 fDocument.LoadURISource(fURI,aStream);
3397 end;
3398 end;
3399
3400 function TPasGLTF.TImage.IsExternalResource:boolean;
3401 begin
3402 result:=not ((fBufferView>=0) or (pos('data:',fURI)=1));
3403 end;
3404
3405 { TPasGLTF.TMaterial.TTexture }
3406
3407 constructor TPasGLTF.TMaterial.TTexture.Create(const aDocument:TDocument);
3408 begin
3409 inherited Create(aDocument);
3410 fIndex:=-1;
3411 fTexCoord:=0;
3412 end;
3413
3414 destructor TPasGLTF.TMaterial.TTexture.Destroy;
3415 begin
3416 inherited Destroy;
3417 end;
3418
3419 function TPasGLTF.TMaterial.TTexture.GetEmpty:boolean;
3420 begin
3421 result:=fIndex<0;
3422 end;
3423
3424 { TPasGLTF.TMaterial.TNormalTexture }
3425
3426 constructor TPasGLTF.TMaterial.TNormalTexture.Create(const aDocument:TDocument);
3427 begin
3428 inherited Create(aDocument);
3429 fScale:=TDefaults.IdentityScalar;
3430 end;
3431
3432 { TPasGLTF.TMaterial.TOcclusionTexture }
3433
3434 constructor TPasGLTF.TMaterial.TOcclusionTexture.Create(const aDocument:TDocument);
3435 begin
3436 inherited Create(aDocument);
3437 fStrength:=TDefaults.IdentityScalar;
3438 end;
3439
3440 { TPasGLTF.TMaterial.TPBRMetallicRoughness }
3441
3442 constructor TPasGLTF.TMaterial.TPBRMetallicRoughness.Create(const aDocument:TDocument);
3443 begin
3444 inherited Create(aDocument);
3445 fBaseColorFactor:=TDefaults.IdentityVector4;
3446 fBaseColorTexture:=TTexture.Create(fDocument);
3447 fRoughnessFactor:=TDefaults.IdentityScalar;
3448 fMetallicFactor:=TDefaults.IdentityScalar;
3449 fMetallicRoughnessTexture:=TTexture.Create(fDocument);
3450 end;
3451
3452 destructor TPasGLTF.TMaterial.TPBRMetallicRoughness.Destroy;
3453 begin
3454 FreeAndNil(fBaseColorTexture);
3455 FreeAndNil(fMetallicRoughnessTexture);
3456 inherited Destroy;
3457 end;
3458
3459 function TPasGLTF.TMaterial.TPBRMetallicRoughness.GetEmpty:boolean;
3460 begin
3461 result:=fBaseColorTexture.Empty and
3462 fMetallicRoughnessTexture.Empty and
3463 SameValue(fRoughnessFactor,TDefaults.IdentityScalar) and
3464 SameValue(fMetallicFactor,TDefaults.IdentityScalar);
3465 end;
3466
3467 { TPasGLTF.TMaterial }
3468
3469 constructor TPasGLTF.TMaterial.Create(const aDocument:TDocument);
3470 begin
3471 inherited Create(aDocument);
3472 fName:='';
3473 fAlphaCutOff:=TDefaults.MaterialAlphaCutoff;
3474 fAlphaMode:=TAlphaMode.Opaque;
3475 fDoubleSided:=TDefaults.MaterialDoubleSided;
3476 fNormalTexture:=TNormalTexture.Create(fDocument);
3477 fOcclusionTexture:=TOcclusionTexture.Create(fDocument);
3478 fPBRMetallicRoughness:=TPBRMetallicRoughness.Create(fDocument);
3479 fEmissiveTexture:=TTexture.Create(fDocument);
3480 fEmissiveFactor:=TDefaults.NullVector3;
3481 end;
3482
3483 destructor TPasGLTF.TMaterial.Destroy;
3484 begin
3485 FreeAndNil(fNormalTexture);
3486 FreeAndNil(fOcclusionTexture);
3487 FreeAndNil(fPBRMetallicRoughness);
3488 FreeAndNil(fEmissiveTexture);
3489 inherited Destroy;
3490 end;
3491
3492 { TPasGLTF.TMesh.TPrimitive }
3493
3494 constructor TPasGLTF.TMesh.TPrimitive.Create(const aDocument:TDocument);
3495 begin
3496 inherited Create(aDocument);
3497 fMode:=TMode.Triangles;
3498 fIndices:=-1;
3499 fMaterial:=-1;
3500 fAttributes:=TAttributes.Create(-1);
3501 fTargets:=TAttributesList.Create;
3502 end;
3503
3504 destructor TPasGLTF.TMesh.TPrimitive.Destroy;
3505 begin
3506 FreeAndNil(fAttributes);
3507 FreeAndNil(fTargets);
3508 inherited Destroy;
3509 end;
3510
3511 { TPasGLTF.TMesh }
3512
3513 constructor TPasGLTF.TMesh.Create(const aDocument:TDocument);
3514 begin
3515 inherited Create(aDocument);
3516 fName:='';
3517 fWeights:=TWeights.Create;
3518 fPrimitives:=TPrimitives.Create;
3519 end;
3520
3521 destructor TPasGLTF.TMesh.Destroy;
3522 begin
3523 FreeAndNil(fWeights);
3524 FreeAndNil(fPrimitives);
3525 inherited Destroy;
3526 end;
3527
3528 { TPasGLTF.TNode }
3529
3530 constructor TPasGLTF.TNode.Create(const aDocument:TDocument);
3531 begin
3532 inherited Create(aDocument);
3533 fName:='';
3534 fCamera:=-1;
3535 fMesh:=-1;
3536 fSkin:=-1;
3537 fMatrix:=TDefaults.IdentityMatrix4x4;
3538 fRotation:=TDefaults.IdentityQuaternion;
3539 fScale:=TDefaults.IdentityVector3;
3540 fTranslation:=TDefaults.NullVector3;
3541 fChildren:=TChildren.Create;
3542 fWeights:=TWeights.Create;
3543 end;
3544
3545 destructor TPasGLTF.TNode.Destroy;
3546 begin
3547 FreeAndNil(fChildren);
3548 FreeAndNil(fWeights);
3549 inherited Destroy;
3550 end;
3551
3552 { TPasGLTF.TSampler }
3553
3554 constructor TPasGLTF.TSampler.Create(const aDocument:TDocument);
3555 begin
3556 inherited Create(aDocument);
3557 fName:='';
3558 fMagFilter:=TMagFilter.None;
3559 fMinFilter:=TMinFilter.None;
3560 fWrapS:=TWrappingMode.Repeat_;
3561 fWrapT:=TWrappingMode.Repeat_;
3562 end;
3563
3564 destructor TPasGLTF.TSampler.Destroy;
3565 begin
3566 inherited Destroy;
3567 end;
3568
3569 function TPasGLTF.TSampler.GetEmpty:boolean;
3570 begin
3571 result:=(length(fName)=0) and
3572 (fMagFilter=TMagFilter.None) and
3573 (fMinFilter=TMinFilter.None) and
3574 (fWrapS=TWrappingMode.Repeat_) and
3575 (fWrapT=TWrappingMode.Repeat_);
3576 end;
3577
3578 { TPasGLTF.TScene }
3579
3580 constructor TPasGLTF.TScene.Create(const aDocument:TDocument);
3581 begin
3582 inherited Create(aDocument);
3583 fName:='';
3584 fNodes:=TPasGLTF.TScene.TNodes.Create;
3585 end;
3586
3587 destructor TPasGLTF.TScene.Destroy;
3588 begin
3589 FreeAndNil(fNodes);
3590 inherited Destroy;
3591 end;
3592
3593 { TPasGLTF.TSkin }
3594
3595 constructor TPasGLTF.TSkin.Create(const aDocument:TDocument);
3596 begin
3597 inherited Create(aDocument);
3598 fName:='';
3599 fInverseBindMatrices:=-1;
3600 fSkeleton:=-1;
3601 fJoints:=TPasGLTF.TSkin.TJoints.Create;
3602 end;
3603
3604 destructor TPasGLTF.TSkin.Destroy;
3605 begin
3606 FreeAndNil(fJoints);
3607 inherited Destroy;
3608 end;
3609
3610 { TPasGLTF.TTexture }
3611
3612 constructor TPasGLTF.TTexture.Create(const aDocument:TDocument);
3613 begin
3614 inherited Create(aDocument);
3615 fName:='';
3616 fSampler:=-1;
3617 fSource:=-1;
3618 end;
3619
3620 destructor TPasGLTF.TTexture.Destroy;
3621 begin
3622 inherited Destroy;
3623 end;
3624
3625 { TPasGLTF.TDocument }
3626
3627 constructor TPasGLTF.TDocument.Create(const aDocument:TDocument=nil);
3628 begin
3629 inherited Create(aDocument);
3630 fAsset:=TAsset.Create(fDocument);
3631 fAccessors:=TAccessors.Create;
3632 fAnimations:=TAnimations.Create;
3633 fBuffers:=TBuffers.Create;
3634 fBufferViews:=TBufferViews.Create;
3635 fCameras:=TCameras.Create;
3636 fImages:=TImages.Create;
3637 fMaterials:=TMaterials.Create;
3638 fMeshes:=TMeshes.Create;
3639 fNodes:=TNodes.Create;
3640 fSamplers:=TSamplers.Create;
3641 fScene:=-1;
3642 fScenes:=TScenes.Create;
3643 fSkins:=TSkins.Create;
3644 fTextures:=TTextures.Create;
3645 fExtensionsUsed:=TStringList.Create;
3646 fExtensionsRequired:=TStringList.Create;
3647 fRootPath:='';
3648 fGetURI:=DefaultGetURI;
3649 end;
3650
3651 destructor TPasGLTF.TDocument.Destroy;
3652 begin
3653 FreeAndNil(fAsset);
3654 FreeAndNil(fAccessors);
3655 FreeAndNil(fAnimations);
3656 FreeAndNil(fBuffers);
3657 FreeAndNil(fBufferViews);
3658 FreeAndNil(fCameras);
3659 FreeAndNil(fImages);
3660 FreeAndNil(fMaterials);
3661 FreeAndNil(fMeshes);
3662 FreeAndNil(fNodes);
3663 FreeAndNil(fSamplers);
3664 FreeAndNil(fScenes);
3665 FreeAndNil(fSkins);
3666 FreeAndNil(fTextures);
3667 FreeAndNil(fExtensionsUsed);
3668 FreeAndNil(fExtensionsRequired);
3669 inherited Destroy;
3670 end;
3671
3672 function TPasGLTF.TDocument.DefaultGetURI(const aURI:TPasGLTFUTF8String):TStream;
3673 var FileName:String;
3674 begin
3675 FileName:=ExpandFileName(IncludeTrailingPathDelimiter(fRootPath)+String(TPasGLTF.ResolveURIToPath(aURI)));
3676 result:=TFileStream.Create(FileName,fmOpenRead or fmShareDenyWrite);
3677 end;
3678
3679 procedure TPasGLTF.TDocument.LoadURISource(const aURI:TPasGLTFUTF8String;const aStream:TStream);
3680 const Base64Signature=';base64,';
3681 var Stream:TStream;
3682 Base64Position:TPasGLTFSizeInt;
3683 begin
3684 if length(trim(aURI))>0 then begin
3685 if (length(aURI)>5) and
3686 (aURI[1]='d') and
3687 (aURI[2]='a') and
3688 (aURI[3]='t') and
3689 (aURI[4]='a') and
3690 (aURI[5]=':') then begin
3691 Base64Position:=pos(Base64Signature,aURI);
3692 if Base64Position>0 then begin
3693 TBase64.Decode(copy(aURI,Base64Position+length(Base64Signature),(length(aURI)-(Base64Position+length(Base64Signature)))+1),aStream);
3694 end;
3695 end else if assigned(fGetURI) then begin
3696 Stream:=fGetURI(aURI);
3697 if assigned(Stream) then begin
3698 try
3699 Stream.Seek(0,soBeginning);
3700 if aStream.CopyFrom(Stream,Stream.Size)<>Stream.Size then begin
3701 raise EInOutError.Create('I/O error');
3702 end;
3703 finally
3704 FreeAndNil(Stream);
3705 end;
3706 end;
3707 end;
3708 aStream.Seek(0,soBeginning);
3709 end;
3710 end;
3711
3712 procedure TPasGLTF.TDocument.LoadURISources;
3713 var Buffer:TBuffer;
3714 begin
3715 for Buffer in fBuffers do begin
3716 if length(trim(Buffer.fURI))>0 then begin
3717 LoadURISource(Buffer.fURI,Buffer.fData);
3718 end;
3719 end;
3720 end;
3721
3722 procedure TPasGLTF.TDocument.LoadFromJSON(const aJSONRootItem:TPasJSONItem);
3723 function Required(const aJSONItem:TPasJSONItem;const aName:TPasGLTFUTF8String=''):TPasJSONItem;
3724 begin
3725 result:=aJSONItem;
3726 if not assigned(result) then begin
3727 if length(aName)>0 then begin
3728 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document, missing "'+String(aName)+'" field');
3729 end else begin
3730 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3731 end;
3732 end;
3733 end;
3734 procedure ProcessExtensionsAndExtras(const aJSONItem:TPasJSONItem;const aBaseExtensionsExtrasObject:TBaseExtensionsExtrasObject);
3735 var JSONObject:TPasJSONItemObject;
3736 JSONObjectItem:TPasJSONItem;
3737 begin
3738 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
3739 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3740 end;
3741 JSONObject:=TPasJSONItemObject(aJSONItem);
3742 begin
3743 JSONObjectItem:=JSONObject.Properties['extensions'];
3744 if assigned(JSONObjectItem) and (JSONObjectItem is TPasJSONItemObject) then begin
3745 aBaseExtensionsExtrasObject.fExtensions.Merge(JSONObjectItem);
3746 end;
3747 end;
3748 begin
3749 JSONObjectItem:=JSONObject.Properties['extras'];
3750 if assigned(JSONObjectItem) and (JSONObjectItem is TPasJSONItemObject) then begin
3751 aBaseExtensionsExtrasObject.fExtras.Merge(JSONObjectItem);
3752 end;
3753 end;
3754 end;
3755 procedure ProcessAccessors(const aJSONItem:TPasJSONItem);
3756 function ProcessAccessor(const aJSONItem:TPasJSONItem):TAccessor;
3757 procedure ProcessSparse(const aJSONItem:TPasJSONItem;const aSparse:TAccessor.TSparse);
3758 var JSONObject:TPasJSONItemObject;
3759 JSONItem:TPasJSONItem;
3760 begin
3761 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
3762 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3763 end;
3764 JSONObject:=TPasJSONItemObject(aJSONItem);
3765 ProcessExtensionsAndExtras(JSONObject,aSparse);
3766 aSparse.fCount:=TPasJSON.GetInt64(Required(JSONObject.Properties['count'],'count'),aSparse.fCount);
3767 begin
3768 JSONItem:=JSONObject.Properties['indices'];
3769 if not (assigned(JSONItem) and (JSONItem is TPasJSONItemObject)) then begin
3770 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3771 end;
3772 ProcessExtensionsAndExtras(TPasJSONItemObject(JSONItem),aSparse.fIndices);
3773 aSparse.fIndices.fBufferView:=TPasJSON.GetInt64(Required(TPasJSONItemObject(JSONItem).Properties['bufferView'],'bufferView'),aSparse.fIndices.fBufferView);
3774 aSparse.fIndices.fComponentType:=TAccessor.TComponentType(TPasJSON.GetInt64(Required(TPasJSONItemObject(JSONItem).Properties['componentType'],'componentType'),TPasGLTFInt64(TAccessor.TComponentType.None)));
3775 aSparse.fIndices.fByteOffset:=TPasJSON.GetInt64(TPasJSONItemObject(JSONItem).Properties['byteOffset'],aSparse.fIndices.fByteOffset);
3776 end;
3777 begin
3778 JSONItem:=JSONObject.Properties['values'];
3779 if not (assigned(JSONItem) and (JSONItem is TPasJSONItemObject)) then begin
3780 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3781 end;
3782 ProcessExtensionsAndExtras(TPasJSONItemObject(JSONItem),aSparse.fValues);
3783 aSparse.fValues.fBufferView:=TPasJSON.GetInt64(Required(TPasJSONItemObject(JSONItem).Properties['bufferView'],'bufferView'),aSparse.fValues.fBufferView);
3784 aSparse.fValues.fByteOffset:=TPasJSON.GetInt64(TPasJSONItemObject(JSONItem).Properties['byteOffset'],aSparse.fValues.fByteOffset);
3785 end;
3786 end;
3787 var JSONObject:TPasJSONItemObject;
3788 JSONItem,JSONArrayItem:TPasJSONItem;
3789 Type_:TPasGLTFUTF8String;
3790 begin
3791 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
3792 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3793 end;
3794 JSONObject:=TPasJSONItemObject(aJSONItem);
3795 result:=TAccessor.Create(self);
3796 try
3797 ProcessExtensionsAndExtras(JSONObject,result);
3798 result.fComponentType:=TAccessor.TComponentType(TPasJSON.GetInt64(Required(JSONObject.Properties['componentType'],'componentType'),TPasGLTFInt64(TAccessor.TComponentType.None)));
3799 result.fCount:=TPasJSON.GetInt64(Required(JSONObject.Properties['count'],'count'),result.fCount);
3800 begin
3801 Type_:=TPasJSON.GetString(Required(JSONObject.Properties['type'],'type'),'NONE');
3802 if Type_='SCALAR' then begin
3803 result.fType:=TAccessor.TType.Scalar;
3804 end else if Type_='VEC2' then begin
3805 result.fType:=TAccessor.TType.Vec2;
3806 end else if Type_='VEC3' then begin
3807 result.fType:=TAccessor.TType.Vec3;
3808 end else if Type_='VEC4' then begin
3809 result.fType:=TAccessor.TType.Vec4;
3810 end else if Type_='MAT2' then begin
3811 result.fType:=TAccessor.TType.Mat2;
3812 end else if Type_='MAT3' then begin
3813 result.fType:=TAccessor.TType.Mat3;
3814 end else if Type_='MAT4' then begin
3815 result.fType:=TAccessor.TType.Mat4;
3816 end else begin
3817 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3818 end;
3819 end;
3820 result.fBufferView:=TPasJSON.GetInt64(JSONObject.Properties['bufferView'],result.fBufferView);
3821 result.fByteOffset:=TPasJSON.GetInt64(JSONObject.Properties['byteOffset'],result.fByteOffset);
3822 begin
3823 JSONItem:=JSONObject.Properties['min'];
3824 if assigned(JSONItem) then begin
3825 if not (JSONItem is TPasJSONItemArray) then begin
3826 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3827 end;
3828 for JSONArrayItem in TPasJSONItemArray(JSONItem) do begin
3829 if not (assigned(JSONArrayItem) and (JSONArrayItem is TPasJSONItemNumber)) then begin
3830 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3831 end;
3832 result.fMinArray.Add(TPasJSON.GetNumber(JSONArrayItem,0.0));
3833 end;
3834 end;
3835 end;
3836 begin
3837 JSONItem:=JSONObject.Properties['max'];
3838 if assigned(JSONItem) then begin
3839 if not (JSONItem is TPasJSONItemArray) then begin
3840 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3841 end;
3842 for JSONArrayItem in TPasJSONItemArray(JSONItem) do begin
3843 if not (assigned(JSONArrayItem) and (JSONArrayItem is TPasJSONItemNumber)) then begin
3844 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3845 end;
3846 result.fMaxArray.Add(TPasJSON.GetNumber(JSONArrayItem,0.0));
3847 end;
3848 end;
3849 end;
3850 result.fName:=TPasJSON.GetString(JSONObject.Properties['name'],result.fName);
3851 result.fNormalized:=TPasJSON.GetBoolean(JSONObject.Properties['normalized'],result.fNormalized);
3852 begin
3853 JSONItem:=JSONObject.Properties['sparse'];
3854 if assigned(JSONItem) then begin
3855 ProcessSparse(JSONItem,result.fSparse);
3856 end;
3857 end;
3858 except
3859 FreeAndNil(result);
3860 raise;
3861 end;
3862 end;
3863 var JSONArray:TPasJSONItemArray;
3864 JSONItem:TPasJSONItem;
3865 begin
3866 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemArray)) then begin
3867 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3868 end;
3869 JSONArray:=TPasJSONItemArray(aJSONItem);
3870 for JSONItem in JSONArray do begin
3871 fAccessors.Add(ProcessAccessor(JSONItem));
3872 end;
3873 end;
3874 procedure ProcessAnimations(const aJSONItem:TPasJSONItem);
3875 function ProcessAnimation(const aJSONItem:TPasJSONItem):TAnimation;
3876 var JSONObject:TPasJSONItemObject;
3877 JSONItem,JSONArrayItem,TargetItem,InterpolationItem:TPasJSONItem;
3878 Interpolation:TPasGLTFUTF8String;
3879 Channel:TAnimation.TChannel;
3880 Sampler:TAnimation.TSampler;
3881 begin
3882 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
3883 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3884 end;
3885 JSONObject:=TPasJSONItemObject(aJSONItem);
3886 result:=TAnimation.Create(self);
3887 try
3888 ProcessExtensionsAndExtras(JSONObject,result);
3889 result.fName:=TPasJSON.GetString(JSONObject.Properties['name'],result.fName);
3890 begin
3891 JSONItem:=JSONObject.Properties['channels'];
3892 if not (assigned(JSONItem) and (JSONItem is TPasJSONItemArray)) then begin
3893 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3894 end;
3895 for JSONArrayItem in TPasJSONItemArray(JSONItem) do begin
3896 if not (assigned(JSONArrayItem) and (JSONArrayItem is TPasJSONItemObject)) then begin
3897 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3898 end;
3899 Channel:=TAnimation.TChannel.Create(self);
3900 try
3901 ProcessExtensionsAndExtras(TPasJSONItemObject(JSONArrayItem),Channel);
3902 Channel.fSampler:=TPasJSON.GetInt64(Required(TPasJSONItemObject(JSONArrayItem).Properties['sampler'],'sampler'),Channel.fSampler);
3903 begin
3904 TargetItem:=Required(TPasJSONItemObject(JSONArrayItem).Properties['target'],'target');
3905 if not (assigned(TargetItem) and (TargetItem is TPasJSONItemObject)) then begin
3906 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3907 end;
3908 ProcessExtensionsAndExtras(TPasJSONItemObject(TargetItem),Channel.fTarget);
3909 Channel.fTarget.fPath:=TPasJSON.GetString(Required(TPasJSONItemObject(TargetItem).Properties['path'],'path'),Channel.fTarget.fPath);
3910 Channel.fTarget.fNode:=TPasJSON.GetInt64(TPasJSONItemObject(TargetItem).Properties['node'],Channel.fTarget.fNode);
3911 end;
3912 finally
3913 result.fChannels.Add(Channel);
3914 end;
3915 end;
3916 end;
3917 begin
3918 JSONItem:=JSONObject.Properties['samplers'];
3919 if not (assigned(JSONItem) and (JSONItem is TPasJSONItemArray)) then begin
3920 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3921 end;
3922 for JSONArrayItem in TPasJSONItemArray(JSONItem) do begin
3923 if not (assigned(JSONArrayItem) and (JSONArrayItem is TPasJSONItemObject)) then begin
3924 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3925 end;
3926 Sampler:=TAnimation.TSampler.Create(self);
3927 try
3928 ProcessExtensionsAndExtras(TPasJSONItemObject(JSONArrayItem),Sampler);
3929 Sampler.fInput:=TPasJSON.GetInt64(Required(TPasJSONItemObject(JSONArrayItem).Properties['input'],'input'),Sampler.fInput);
3930 Sampler.fOutput:=TPasJSON.GetInt64(Required(TPasJSONItemObject(JSONArrayItem).Properties['output'],'output'),Sampler.fOutput);
3931 begin
3932 InterpolationItem:=TPasJSONItemObject(JSONArrayItem).Properties['interpolation'];
3933 if assigned(InterpolationItem) then begin
3934 if not (InterpolationItem is TPasJSONItemString) then begin
3935 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3936 end;
3937 Interpolation:=TPasJSON.GetString(InterpolationItem,'NONE');
3938 if Interpolation='LINEAR' then begin
3939 Sampler.fInterpolation:=TAnimation.TSampler.TSamplerType.Linear;
3940 end else if Interpolation='STEP' then begin
3941 Sampler.fInterpolation:=TAnimation.TSampler.TSamplerType.Step;
3942 end else if Interpolation='CUBICSPLINE' then begin
3943 Sampler.fInterpolation:=TAnimation.TSampler.TSamplerType.CubicSpline;
3944 end else begin
3945 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3946 end;
3947 end;
3948 end;
3949 finally
3950 result.fSamplers.Add(Sampler);
3951 end;
3952 end;
3953 end;
3954 except
3955 FreeAndNil(result);
3956 raise;
3957 end;
3958 end;
3959 var JSONArray:TPasJSONItemArray;
3960 JSONItem:TPasJSONItem;
3961 begin
3962 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemArray)) then begin
3963 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3964 end;
3965 JSONArray:=TPasJSONItemArray(aJSONItem);
3966 for JSONItem in JSONArray do begin
3967 fAnimations.Add(ProcessAnimation(JSONItem));
3968 end;
3969 end;
3970 procedure ProcessAsset(const aJSONItem:TPasJSONItem);
3971 var JSONObject:TPasJSONItemObject;
3972 begin
3973 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
3974 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3975 end;
3976 JSONObject:=TPasJSONItemObject(aJSONItem);
3977 ProcessExtensionsAndExtras(JSONObject,fAsset);
3978 fAsset.fCopyright:=TPasJSON.GetString(JSONObject.Properties['copyright'],fAsset.fCopyright);
3979 fAsset.fGenerator:=TPasJSON.GetString(JSONObject.Properties['generator'],fAsset.fGenerator);
3980 fAsset.fMinVersion:=TPasJSON.GetString(JSONObject.Properties['minVersion'],fAsset.fMinVersion);
3981 fAsset.fVersion:=TPasJSON.GetString(Required(JSONObject.Properties['version'],'version'),fAsset.fVersion);
3982 end;
3983 procedure ProcessBuffers(const aJSONItem:TPasJSONItem);
3984 function ProcessBuffer(const aJSONItem:TPasJSONItem):TBuffer;
3985 var JSONObject:TPasJSONItemObject;
3986 begin
3987 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
3988 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
3989 end;
3990 JSONObject:=TPasJSONItemObject(aJSONItem);
3991 result:=TBuffer.Create(self);
3992 try
3993 ProcessExtensionsAndExtras(JSONObject,result);
3994 result.fName:=TPasJSON.GetString(JSONObject.Properties['name'],result.fName);
3995 result.fURI:=TPasJSON.GetString(JSONObject.Properties['uri'],result.fURI);
3996 result.fByteLength:=TPasJSON.GetInt64(Required(JSONObject.Properties['byteLength'],'byteLength'),result.fByteLength);
3997 except
3998 FreeAndNil(result);
3999 raise;
4000 end;
4001 end;
4002 var JSONArray:TPasJSONItemArray;
4003 JSONItem:TPasJSONItem;
4004 begin
4005 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemArray)) then begin
4006 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4007 end;
4008 JSONArray:=TPasJSONItemArray(aJSONItem);
4009 for JSONItem in JSONArray do begin
4010 fBuffers.Add(ProcessBuffer(JSONItem));
4011 end;
4012 end;
4013 procedure ProcessBufferViews(const aJSONItem:TPasJSONItem);
4014 function ProcessBufferView(const aJSONItem:TPasJSONItem):TBufferView;
4015 var JSONObject:TPasJSONItemObject;
4016 begin
4017 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
4018 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4019 end;
4020 JSONObject:=TPasJSONItemObject(aJSONItem);
4021 result:=TBufferView.Create(self);
4022 try
4023 ProcessExtensionsAndExtras(JSONObject,result);
4024 result.fBuffer:=TPasJSON.GetInt64(Required(JSONObject.Properties['buffer'],'buffer'),result.fBuffer);
4025 result.fByteLength:=TPasJSON.GetInt64(Required(JSONObject.Properties['byteLength'],'byteLength'),result.fByteLength);
4026 result.fByteOffset:=TPasJSON.GetInt64(JSONObject.Properties['byteOffset'],result.fByteOffset);
4027 result.fByteStride:=TPasJSON.GetInt64(JSONObject.Properties['byteStride'],result.fByteStride);
4028 result.fName:=TPasJSON.GetString(JSONObject.Properties['name'],result.fName);
4029 result.fTarget:=TBufferView.TTargetType(TPasJSON.GetInt64(JSONObject.Properties['target'],TPasGLTFInt64(result.fTarget)));
4030 except
4031 FreeAndNil(result);
4032 raise;
4033 end;
4034 end;
4035 var JSONArray:TPasJSONItemArray;
4036 JSONItem:TPasJSONItem;
4037 begin
4038 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemArray)) then begin
4039 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4040 end;
4041 JSONArray:=TPasJSONItemArray(aJSONItem);
4042 for JSONItem in JSONArray do begin
4043 fBufferViews.Add(ProcessBufferView(JSONItem));
4044 end;
4045 end;
4046 procedure ProcessCameras(const aJSONItem:TPasJSONItem);
4047 function ProcessCamera(const aJSONItem:TPasJSONItem):TCamera;
4048 var JSONObject:TPasJSONItemObject;
4049 JSONItem:TPasJSONItem;
4050 Type_:TPasGLTFUTF8String;
4051 begin
4052 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
4053 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4054 end;
4055 JSONObject:=TPasJSONItemObject(aJSONItem);
4056 result:=TCamera.Create(self);
4057 try
4058 ProcessExtensionsAndExtras(JSONObject,result);
4059 result.fName:=TPasJSON.GetString(JSONObject.Properties['name'],result.fName);
4060 begin
4061 Type_:=TPasJSON.GetString(Required(JSONObject.Properties['type'],'type'),'none');
4062 if Type_='orthographic' then begin
4063 result.fType:=TCamera.TCameraType.Orthographic;
4064 end else if Type_='perspective' then begin
4065 result.fType:=TCamera.TCameraType.Perspective;
4066 end else begin
4067 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4068 end;
4069 end;
4070 case result.fType of
4071 TCamera.TCameraType.Orthographic:begin
4072 JSONItem:=JSONObject.Properties['orthographic'];
4073 if not (assigned(JSONItem) and (JSONItem is TPasJSONItemObject)) then begin
4074 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4075 end;
4076 ProcessExtensionsAndExtras(TPasJSONItemObject(JSONItem),result.fOrthographic);
4077 result.fOrthographic.fXMag:=TPasJSON.GetNumber(Required(TPasJSONItemObject(JSONItem).Properties['xmag'],'xmag'),result.fOrthographic.fXMag);
4078 result.fOrthographic.fYMag:=TPasJSON.GetNumber(Required(TPasJSONItemObject(JSONItem).Properties['ymag'],'ymag'),result.fOrthographic.fYMag);
4079 result.fOrthographic.fZNear:=TPasJSON.GetNumber(Required(TPasJSONItemObject(JSONItem).Properties['znear'],'znear'),result.fOrthographic.fZNear);
4080 result.fOrthographic.fZFar:=TPasJSON.GetNumber(Required(TPasJSONItemObject(JSONItem).Properties['zfar'],'zfar'),result.fOrthographic.fZFar);
4081 end;
4082 TCamera.TCameraType.Perspective:begin
4083 JSONItem:=JSONObject.Properties['perspective'];
4084 if not (assigned(JSONItem) and (JSONItem is TPasJSONItemObject)) then begin
4085 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4086 end;
4087 ProcessExtensionsAndExtras(TPasJSONItemObject(JSONItem),result.fPerspective);
4088 result.fPerspective.fAspectRatio:=TPasJSON.GetNumber(TPasJSONItemObject(JSONItem).Properties['aspectRatio'],result.fPerspective.fAspectRatio);
4089 result.fPerspective.fYFov:=TPasJSON.GetNumber(Required(TPasJSONItemObject(JSONItem).Properties['yfov'],'yfov'),result.fPerspective.fYFov);
4090 result.fPerspective.fZNear:=TPasJSON.GetNumber(Required(TPasJSONItemObject(JSONItem).Properties['znear'],'znear'),result.fPerspective.fZNear);
4091 result.fPerspective.fZFar:=TPasJSON.GetNumber(Required(TPasJSONItemObject(JSONItem).Properties['zfar'],'zfar'),result.fPerspective.fZFar);
4092 end;
4093 else begin
4094 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4095 end;
4096 end;
4097 except
4098 FreeAndNil(result);
4099 raise;
4100 end;
4101 end;
4102 var JSONArray:TPasJSONItemArray;
4103 JSONItem:TPasJSONItem;
4104 begin
4105 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemArray)) then begin
4106 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4107 end;
4108 JSONArray:=TPasJSONItemArray(aJSONItem);
4109 for JSONItem in JSONArray do begin
4110 fCameras.Add(ProcessCamera(JSONItem));
4111 end;
4112 end;
4113 procedure ProcessImages(const aJSONItem:TPasJSONItem);
4114 function ProcessImage(const aJSONItem:TPasJSONItem):TImage;
4115 var JSONObject:TPasJSONItemObject;
4116 begin
4117 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
4118 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4119 end;
4120 JSONObject:=TPasJSONItemObject(aJSONItem);
4121 result:=TImage.Create(self);
4122 try
4123 ProcessExtensionsAndExtras(JSONObject,result);
4124 result.fBufferView:=TPasJSON.GetInt64(JSONObject.Properties['bufferView'],result.fBufferView);
4125 result.fMimeType:=TPasJSON.GetString(JSONObject.Properties['mimeType'],result.fMimeType);
4126 result.fName:=TPasJSON.GetString(JSONObject.Properties['name'],result.fName);
4127 result.fURI:=TPasJSON.GetString(JSONObject.Properties['uri'],result.fURI);
4128 except
4129 FreeAndNil(result);
4130 raise;
4131 end;
4132 end;
4133 var JSONArray:TPasJSONItemArray;
4134 JSONItem:TPasJSONItem;
4135 begin
4136 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemArray)) then begin
4137 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4138 end;
4139 JSONArray:=TPasJSONItemArray(aJSONItem);
4140 for JSONItem in JSONArray do begin
4141 fImages.Add(ProcessImage(JSONItem));
4142 end;
4143 end;
4144 procedure ProcessMaterials(const aJSONItem:TPasJSONItem);
4145 function ProcessMaterial(const aJSONItem:TPasJSONItem):TMaterial;
4146 var JSONObject:TPasJSONItemObject;
4147 JSONItem,JSONSubItem:TPasJSONItem;
4148 Mode:TPasGLTFUTF8String;
4149 Index:TPasGLTFSizeInt;
4150 begin
4151 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
4152 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4153 end;
4154 JSONObject:=TPasJSONItemObject(aJSONItem);
4155 result:=TMaterial.Create(self);
4156 try
4157 ProcessExtensionsAndExtras(JSONObject,result);
4158 result.fAlphaCutOff:=TPasJSON.GetNumber(JSONObject.Properties['alphaCutoff'],result.fAlphaCutOff);
4159 begin
4160 JSONItem:=JSONObject.Properties['alphaMode'];
4161 if assigned(JSONItem) then begin
4162 Mode:=TPasJSON.GetString(JSONItem,'NONE');
4163 if Mode='OPAQUE' then begin
4164 result.fAlphaMode:=TMaterial.TAlphaMode.Opaque;
4165 end else if Mode='MASK' then begin
4166 result.fAlphaMode:=TMaterial.TAlphaMode.Mask;
4167 end else if Mode='BLEND' then begin
4168 result.fAlphaMode:=TMaterial.TAlphaMode.Blend;
4169 end else begin
4170 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4171 end;
4172 end;
4173 end;
4174 result.fDoubleSided:=TPasJSON.GetBoolean(JSONObject.Properties['doubleSided'],result.fDoubleSided);
4175 begin
4176 JSONItem:=JSONObject.Properties['emissiveFactor'];
4177 if assigned(JSONItem) then begin
4178 if not ((JSONItem is TPasJSONItemArray) and (TPasJSONItemArray(JSONItem).Count=3)) then begin
4179 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4180 end;
4181 for Index:=0 to 2 do begin
4182 result.fEmissiveFactor[Index]:=TPasJSON.GetNumber(TPasJSONItemArray(JSONItem).Items[Index],result.fEmissiveFactor[Index]);
4183 end;
4184 end;
4185 end;
4186 begin
4187 JSONItem:=JSONObject.Properties['emissiveTexture'];
4188 if assigned(JSONItem) then begin
4189 if not (JSONItem is TPasJSONItemObject) then begin
4190 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4191 end;
4192 ProcessExtensionsAndExtras(TPasJSONItemObject(JSONItem),result.fEmissiveTexture);
4193 result.fEmissiveTexture.fIndex:=TPasJSON.GetInt64(Required(TPasJSONItemObject(JSONItem).Properties['index'],'index'),result.fEmissiveTexture.fIndex);
4194 result.fEmissiveTexture.fTexCoord:=TPasJSON.GetInt64(TPasJSONItemObject(JSONItem).Properties['texCoord'],result.fEmissiveTexture.fTexCoord);
4195 end;
4196 end;
4197 result.fName:=TPasJSON.GetString(JSONObject.Properties['name'],result.fName);
4198 begin
4199 JSONItem:=JSONObject.Properties['normalTexture'];
4200 if assigned(JSONItem) then begin
4201 if not (JSONItem is TPasJSONItemObject) then begin
4202 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4203 end;
4204 ProcessExtensionsAndExtras(TPasJSONItemObject(JSONItem),result.fNormalTexture);
4205 result.fNormalTexture.fIndex:=TPasJSON.GetInt64(Required(TPasJSONItemObject(JSONItem).Properties['index'],'index'),result.fNormalTexture.fIndex);
4206 result.fNormalTexture.fTexCoord:=TPasJSON.GetInt64(TPasJSONItemObject(JSONItem).Properties['texCoord'],result.fNormalTexture.fTexCoord);
4207 result.fNormalTexture.fScale:=TPasJSON.GetNumber(TPasJSONItemObject(JSONItem).Properties['scale'],result.fNormalTexture.fScale);
4208 end;
4209 end;
4210 begin
4211 JSONItem:=JSONObject.Properties['occlusionTexture'];
4212 if assigned(JSONItem) then begin
4213 if not (JSONItem is TPasJSONItemObject) then begin
4214 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4215 end;
4216 ProcessExtensionsAndExtras(TPasJSONItemObject(JSONItem),result.fOcclusionTexture);
4217 result.fOcclusionTexture.fIndex:=TPasJSON.GetInt64(Required(TPasJSONItemObject(JSONItem).Properties['index'],'index'),result.fOcclusionTexture.fIndex);
4218 result.fOcclusionTexture.fTexCoord:=TPasJSON.GetInt64(TPasJSONItemObject(JSONItem).Properties['texCoord'],result.fOcclusionTexture.fTexCoord);
4219 result.fOcclusionTexture.fStrength:=TPasJSON.GetNumber(TPasJSONItemObject(JSONItem).Properties['scale'],result.fOcclusionTexture.fStrength);
4220 end;
4221 end;
4222 begin
4223 JSONItem:=JSONObject.Properties['pbrMetallicRoughness'];
4224 if assigned(JSONItem) then begin
4225 if not (JSONItem is TPasJSONItemObject) then begin
4226 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4227 end;
4228 ProcessExtensionsAndExtras(TPasJSONItemObject(JSONItem),result.fPBRMetallicRoughness);
4229 begin
4230 JSONSubItem:=TPasJSONItemObject(JSONItem).Properties['baseColorFactor'];
4231 if assigned(JSONSubItem) then begin
4232 if not ((JSONSubItem is TPasJSONItemArray) and (TPasJSONItemArray(JSONSubItem).Count=4)) then begin
4233 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4234 end;
4235 for Index:=0 to 3 do begin
4236 result.fPBRMetallicRoughness.fBaseColorFactor[Index]:=TPasJSON.GetNumber(TPasJSONItemArray(JSONSubItem).Items[Index],result.fPBRMetallicRoughness.fBaseColorFactor[Index]);
4237 end;
4238 end;
4239 end;
4240 begin
4241 JSONSubItem:=TPasJSONItemObject(JSONItem).Properties['baseColorTexture'];
4242 if assigned(JSONSubItem) then begin
4243 if not (JSONSubItem is TPasJSONItemObject) then begin
4244 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4245 end;
4246 ProcessExtensionsAndExtras(TPasJSONItemObject(JSONSubItem),result.fPBRMetallicRoughness.fBaseColorTexture);
4247 result.fPBRMetallicRoughness.fBaseColorTexture.fIndex:=TPasJSON.GetInt64(Required(TPasJSONItemObject(JSONSubItem).Properties['index'],'index'),result.fPBRMetallicRoughness.fBaseColorTexture.fIndex);
4248 result.fPBRMetallicRoughness.fBaseColorTexture.fTexCoord:=TPasJSON.GetInt64(TPasJSONItemObject(JSONSubItem).Properties['texCoord'],result.fPBRMetallicRoughness.fBaseColorTexture.fTexCoord);
4249 end;
4250 end;
4251 result.fPBRMetallicRoughness.fMetallicFactor:=TPasJSON.GetNumber(TPasJSONItemObject(JSONItem).Properties['metallicFactor'],result.fPBRMetallicRoughness.fMetallicFactor);
4252 begin
4253 JSONSubItem:=TPasJSONItemObject(JSONItem).Properties['metallicRoughnessTexture'];
4254 if assigned(JSONSubItem) then begin
4255 if not (JSONSubItem is TPasJSONItemObject) then begin
4256 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4257 end;
4258 ProcessExtensionsAndExtras(TPasJSONItemObject(JSONSubItem),result.fPBRMetallicRoughness.fMetallicRoughnessTexture);
4259 result.fPBRMetallicRoughness.fMetallicRoughnessTexture.fIndex:=TPasJSON.GetInt64(Required(TPasJSONItemObject(JSONSubItem).Properties['index'],'index'),result.fPBRMetallicRoughness.fMetallicRoughnessTexture.fIndex);
4260 result.fPBRMetallicRoughness.fMetallicRoughnessTexture.fTexCoord:=TPasJSON.GetInt64(TPasJSONItemObject(JSONSubItem).Properties['texCoord'],result.fPBRMetallicRoughness.fMetallicRoughnessTexture.fTexCoord);
4261 end;
4262 end;
4263 result.fPBRMetallicRoughness.fRoughnessFactor:=TPasJSON.GetNumber(TPasJSONItemObject(JSONItem).Properties['roughnessFactor'],result.fPBRMetallicRoughness.fRoughnessFactor);
4264 end;
4265 end;
4266 except
4267 FreeAndNil(result);
4268 raise;
4269 end;
4270 end;
4271 var JSONArray:TPasJSONItemArray;
4272 JSONItem:TPasJSONItem;
4273 begin
4274 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemArray)) then begin
4275 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4276 end;
4277 JSONArray:=TPasJSONItemArray(aJSONItem);
4278 for JSONItem in JSONArray do begin
4279 fMaterials.Add(ProcessMaterial(JSONItem));
4280 end;
4281 end;
4282 procedure ProcessMeshes(const aJSONItem:TPasJSONItem);
4283 function ProcessMesh(const aJSONItem:TPasJSONItem):TMesh;
4284 function ProcessPrimitive(const aJSONItem:TPasJSONItem):TMesh.TPrimitive;
4285 var JSONObject:TPasJSONItemObject;
4286 JSONItem,JSONArrayItem:TPasJSONItem;
4287 JSONObjectProperty:TPasJSONItemObjectProperty;
4288 Attributes:TAttributes;
4289 begin
4290 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
4291 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4292 end;
4293 JSONObject:=TPasJSONItemObject(aJSONItem);
4294 result:=TMesh.TPrimitive.Create(self);
4295 try
4296 ProcessExtensionsAndExtras(JSONObject,result);
4297 begin
4298 JSONItem:=Required(JSONObject.Properties['attributes'],'attributes');
4299 if not (assigned(JSONItem) and (JSONItem is TPasJSONItemObject)) then begin
4300 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4301 end;
4302 for JSONObjectProperty in TPasJSONItemObject(JSONItem) do begin
4303 result.fAttributes.Add(JSONObjectProperty.Key,TPasJSON.GetInt64(JSONObjectProperty.Value,0));
4304 end;
4305 end;
4306 result.fIndices:=TPasJSON.GetInt64(JSONObject.Properties['indices'],result.fIndices);
4307 result.fMaterial:=TPasJSON.GetInt64(JSONObject.Properties['material'],result.fMaterial);
4308 result.fMode:=TMesh.TPrimitive.TMode(TPasJSON.GetInt64(JSONObject.Properties['mode'],TPasGLTFInt64(result.fMode)));
4309 begin
4310 JSONItem:=JSONObject.Properties['targets'];
4311 if assigned(JSONItem) then begin
4312 if not (JSONItem is TPasJSONItemArray) then begin
4313 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4314 end;
4315 for JSONArrayItem in TPasJSONItemArray(JSONItem) do begin
4316 if not (assigned(JSONArrayItem) and (JSONArrayItem is TPasJSONItemObject)) then begin
4317 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4318 end;
4319 Attributes:=TAttributes.Create(-1);
4320 try
4321 for JSONObjectProperty in TPasJSONItemObject(JSONArrayItem) do begin
4322 Attributes.Add(JSONObjectProperty.Key,TPasJSON.GetInt64(JSONObjectProperty.Value,0));
4323 end;
4324 finally
4325 result.fTargets.Add(Attributes);
4326 end;
4327 end;
4328 end;
4329 end;
4330 except
4331 FreeAndNil(result);
4332 raise;
4333 end;
4334 end;
4335 var JSONObject:TPasJSONItemObject;
4336 JSONItem,JSONArrayItem:TPasJSONItem;
4337 begin
4338 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
4339 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4340 end;
4341 JSONObject:=TPasJSONItemObject(aJSONItem);
4342 result:=TMesh.Create(self);
4343 try
4344 ProcessExtensionsAndExtras(JSONObject,result);
4345 result.fName:=TPasJSON.GetString(JSONObject.Properties['name'],result.fName);
4346 begin
4347 JSONItem:=Required(JSONObject.Properties['primitives'],'primitives');
4348 if not (assigned(JSONItem) and (JSONItem is TPasJSONItemArray)) then begin
4349 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4350 end;
4351 for JSONArrayItem in TPasJSONItemArray(JSONItem) do begin
4352 result.fPrimitives.Add(ProcessPrimitive(JSONArrayItem));
4353 end;
4354 end;
4355 begin
4356 JSONItem:=JSONObject.Properties['weights'];
4357 if assigned(JSONItem) then begin
4358 if not (JSONItem is TPasJSONItemArray) then begin
4359 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4360 end;
4361 for JSONArrayItem in TPasJSONItemArray(JSONItem) do begin
4362 result.fWeights.Add(TPasJSON.GetNumber(JSONArrayItem,0.0));
4363 end;
4364 end;
4365 end;
4366 except
4367 FreeAndNil(result);
4368 raise;
4369 end;
4370 end;
4371 var JSONArray:TPasJSONItemArray;
4372 JSONItem:TPasJSONItem;
4373 begin
4374 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemArray)) then begin
4375 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4376 end;
4377 JSONArray:=TPasJSONItemArray(aJSONItem);
4378 for JSONItem in JSONArray do begin
4379 fMeshes.Add(ProcessMesh(JSONItem));
4380 end;
4381 end;
4382 procedure ProcessNodes(const aJSONItem:TPasJSONItem);
4383 function ProcessNode(const aJSONItem:TPasJSONItem):TNode;
4384 var JSONObject:TPasJSONItemObject;
4385 JSONItem,JSONArrayItem:TPasJSONItem;
4386 Index:TPasGLTFSizeInt;
4387 begin
4388 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
4389 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4390 end;
4391 JSONObject:=TPasJSONItemObject(aJSONItem);
4392 result:=TNode.Create(self);
4393 try
4394 ProcessExtensionsAndExtras(JSONObject,result);
4395 result.fCamera:=TPasJSON.GetInt64(JSONObject.Properties['camera'],result.fCamera);
4396 begin
4397 JSONItem:=JSONObject.Properties['children'];
4398 if assigned(JSONItem) then begin
4399 if not (JSONItem is TPasJSONItemArray) then begin
4400 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4401 end;
4402 for JSONArrayItem in TPasJSONItemArray(JSONItem) do begin
4403 result.fChildren.Add(TPasJSON.GetInt64(JSONArrayItem));
4404 end;
4405 end;
4406 end;
4407 begin
4408 JSONItem:=JSONObject.Properties['matrix'];
4409 if assigned(JSONItem) then begin
4410 if not ((JSONItem is TPasJSONItemArray) and (TPasJSONItemArray(JSONItem).Count=16)) then begin
4411 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4412 end;
4413 for Index:=0 to 15 do begin
4414 result.fMatrix[Index]:=TPasJSON.GetNumber(TPasJSONItemArray(JSONItem).Items[Index],result.fMatrix[Index]);
4415 end;
4416 end;
4417 end;
4418 result.fMesh:=TPasJSON.GetInt64(JSONObject.Properties['mesh'],result.fMesh);
4419 result.fName:=TPasJSON.GetString(JSONObject.Properties['name'],result.fName);
4420 begin
4421 JSONItem:=JSONObject.Properties['rotation'];
4422 if assigned(JSONItem) then begin
4423 if not ((JSONItem is TPasJSONItemArray) and (TPasJSONItemArray(JSONItem).Count=4)) then begin
4424 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4425 end;
4426 for Index:=0 to 3 do begin
4427 result.fRotation[Index]:=TPasJSON.GetNumber(TPasJSONItemArray(JSONItem).Items[Index],result.fRotation[Index]);
4428 end;
4429 end;
4430 end;
4431 begin
4432 JSONItem:=JSONObject.Properties['scale'];
4433 if assigned(JSONItem) then begin
4434 if not ((JSONItem is TPasJSONItemArray) and (TPasJSONItemArray(JSONItem).Count=3)) then begin
4435 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4436 end;
4437 for Index:=0 to 2 do begin
4438 result.fScale[Index]:=TPasJSON.GetNumber(TPasJSONItemArray(JSONItem).Items[Index],result.fScale[Index]);
4439 end;
4440 end;
4441 end;
4442 result.fSkin:=TPasJSON.GetInt64(JSONObject.Properties['skin'],result.fSkin);
4443 begin
4444 JSONItem:=JSONObject.Properties['translation'];
4445 if assigned(JSONItem) then begin
4446 if not ((JSONItem is TPasJSONItemArray) and (TPasJSONItemArray(JSONItem).Count=3)) then begin
4447 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4448 end;
4449 for Index:=0 to 2 do begin
4450 result.fTranslation[Index]:=TPasJSON.GetNumber(TPasJSONItemArray(JSONItem).Items[Index],result.fTranslation[Index]);
4451 end;
4452 end;
4453 end;
4454 except
4455 FreeAndNil(result);
4456 raise;
4457 end;
4458 end;
4459 var JSONArray:TPasJSONItemArray;
4460 JSONItem:TPasJSONItem;
4461 begin
4462 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemArray)) then begin
4463 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4464 end;
4465 JSONArray:=TPasJSONItemArray(aJSONItem);
4466 for JSONItem in JSONArray do begin
4467 fNodes.Add(ProcessNode(JSONItem));
4468 end;
4469 end;
4470 procedure ProcessSamplers(const aJSONItem:TPasJSONItem);
4471 function ProcessSampler(const aJSONItem:TPasJSONItem):TSampler;
4472 var JSONObject:TPasJSONItemObject;
4473 begin
4474 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
4475 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4476 end;
4477 JSONObject:=TPasJSONItemObject(aJSONItem);
4478 result:=TSampler.Create(self);
4479 try
4480 ProcessExtensionsAndExtras(JSONObject,result);
4481 result.fMagFilter:=TSampler.TMagFilter(TPasJSON.GetInt64(JSONObject.Properties['magFilter'],TPasGLTFInt64(result.fMagFilter)));
4482 result.fMinFilter:=TSampler.TMinFilter(TPasJSON.GetInt64(JSONObject.Properties['minFilter'],TPasGLTFInt64(result.fMinFilter)));
4483 result.fName:=TPasJSON.GetString(JSONObject.Properties['name'],result.fName);
4484 result.fWrapS:=TSampler.TWrappingMode(TPasJSON.GetInt64(JSONObject.Properties['wrapS'],TPasGLTFInt64(result.fWrapS)));
4485 result.fWrapT:=TSampler.TWrappingMode(TPasJSON.GetInt64(JSONObject.Properties['wrapT'],TPasGLTFInt64(result.fWrapT)));
4486 except
4487 FreeAndNil(result);
4488 raise;
4489 end;
4490 end;
4491 var JSONArray:TPasJSONItemArray;
4492 JSONItem:TPasJSONItem;
4493 begin
4494 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemArray)) then begin
4495 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4496 end;
4497 JSONArray:=TPasJSONItemArray(aJSONItem);
4498 for JSONItem in JSONArray do begin
4499 fSamplers.Add(ProcessSampler(JSONItem));
4500 end;
4501 end;
4502 procedure ProcessScenes(const aJSONItem:TPasJSONItem);
4503 function ProcessScene(const aJSONItem:TPasJSONItem):TScene;
4504 var JSONObject:TPasJSONItemObject;
4505 JSONItem,JSONArrayItem:TPasJSONItem;
4506 begin
4507 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
4508 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4509 end;
4510 JSONObject:=TPasJSONItemObject(aJSONItem);
4511 result:=TScene.Create(self);
4512 try
4513 ProcessExtensionsAndExtras(JSONObject,result);
4514 result.fName:=TPasJSON.GetString(JSONObject.Properties['name'],result.fName);
4515 begin
4516 JSONItem:=JSONObject.Properties['nodes'];
4517 if assigned(JSONItem) then begin
4518 if not (JSONItem is TPasJSONItemArray) then begin
4519 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4520 end;
4521 for JSONArrayItem in TPasJSONItemArray(JSONItem) do begin
4522 result.fNodes.Add(TPasJSON.GetInt64(JSONArrayItem));
4523 end;
4524 end;
4525 end;
4526 except
4527 FreeAndNil(result);
4528 raise;
4529 end;
4530 end;
4531 var JSONArray:TPasJSONItemArray;
4532 JSONItem:TPasJSONItem;
4533 begin
4534 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemArray)) then begin
4535 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4536 end;
4537 JSONArray:=TPasJSONItemArray(aJSONItem);
4538 for JSONItem in JSONArray do begin
4539 fScenes.Add(ProcessScene(JSONItem));
4540 end;
4541 end;
4542 procedure ProcessSkins(const aJSONItem:TPasJSONItem);
4543 function ProcessSkin(const aJSONItem:TPasJSONItem):TSkin;
4544 var JSONObject:TPasJSONItemObject;
4545 JSONItem,JSONArrayItem:TPasJSONItem;
4546 begin
4547 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
4548 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4549 end;
4550 JSONObject:=TPasJSONItemObject(aJSONItem);
4551 result:=TSkin.Create(self);
4552 try
4553 ProcessExtensionsAndExtras(JSONObject,result);
4554 result.fName:=TPasJSON.GetString(JSONObject.Properties['name'],result.fName);
4555 begin
4556 JSONItem:=Required(JSONObject.Properties['joints']);
4557 if assigned(JSONItem) then begin
4558 if not (JSONItem is TPasJSONItemArray) then begin
4559 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4560 end;
4561 for JSONArrayItem in TPasJSONItemArray(JSONItem) do begin
4562 result.fJoints.Add(TPasJSON.GetInt64(JSONArrayItem));
4563 end;
4564 end;
4565 end;
4566 result.fInverseBindMatrices:=TPasJSON.GetInt64(JSONObject.Properties['inverseBindMatrices'],result.fInverseBindMatrices);
4567 result.fSkeleton:=TPasJSON.GetInt64(JSONObject.Properties['skeleton'],result.fSkeleton);
4568 except
4569 FreeAndNil(result);
4570 raise;
4571 end;
4572 end;
4573 var JSONArray:TPasJSONItemArray;
4574 JSONItem:TPasJSONItem;
4575 begin
4576 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemArray)) then begin
4577 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4578 end;
4579 JSONArray:=TPasJSONItemArray(aJSONItem);
4580 for JSONItem in JSONArray do begin
4581 fSkins.Add(ProcessSkin(JSONItem));
4582 end;
4583 end;
4584 procedure ProcessTextures(const aJSONItem:TPasJSONItem);
4585 function ProcessTexture(const aJSONItem:TPasJSONItem):TTexture;
4586 var JSONObject:TPasJSONItemObject;
4587 begin
4588 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemObject)) then begin
4589 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4590 end;
4591 JSONObject:=TPasJSONItemObject(aJSONItem);
4592 result:=TTexture.Create(self);
4593 try
4594 ProcessExtensionsAndExtras(JSONObject,result);
4595 result.fName:=TPasJSON.GetString(JSONObject.Properties['name'],result.fName);
4596 result.fSampler:=TPasJSON.GetInt64(JSONObject.Properties['sampler'],result.fSampler);
4597 result.fSource:=TPasJSON.GetInt64(JSONObject.Properties['source'],result.fSource);
4598 except
4599 FreeAndNil(result);
4600 raise;
4601 end;
4602 end;
4603 var JSONArray:TPasJSONItemArray;
4604 JSONItem:TPasJSONItem;
4605 begin
4606 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemArray)) then begin
4607 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4608 end;
4609 JSONArray:=TPasJSONItemArray(aJSONItem);
4610 for JSONItem in JSONArray do begin
4611 fTextures.Add(ProcessTexture(JSONItem));
4612 end;
4613 end;
4614 procedure ProcessStringList(const aJSONItem:TPasJSONItem;const aStrings:TStrings);
4615 var JSONArray:TPasJSONItemArray;
4616 JSONItem:TPasJSONItem;
4617 begin
4618 if not (assigned(aJSONItem) and (aJSONItem is TPasJSONItemArray)) then begin
4619 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4620 end;
4621 JSONArray:=TPasJSONItemArray(aJSONItem);
4622 for JSONItem in JSONArray do begin
4623 aStrings.Add(TPasJSON.GetString(JSONItem,''));
4624 end;
4625 end;
4626 var JSONObject:TPasJSONItemObject;
4627 JSONObjectProperty:TPasJSONItemObjectProperty;
4628 HasAsset:boolean;
4629 begin
4630 if not (assigned(aJSONRootItem) and (aJSONRootItem is TPasJSONItemObject)) then begin
4631 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4632 end;
4633 JSONObject:=TPasJSONItemObject(aJSONRootItem);
4634 ProcessExtensionsAndExtras(JSONObject,self);
4635 HasAsset:=false;
4636 for JSONObjectProperty in JSONObject do begin
4637 if JSONObjectProperty.Key='accessors' then begin
4638 ProcessAccessors(JSONObjectProperty.Value);
4639 end else if JSONObjectProperty.Key='animations' then begin
4640 ProcessAnimations(JSONObjectProperty.Value);
4641 end else if JSONObjectProperty.Key='asset' then begin
4642 HasAsset:=true;
4643 ProcessAsset(JSONObjectProperty.Value);
4644 end else if JSONObjectProperty.Key='buffers' then begin
4645 ProcessBuffers(JSONObjectProperty.Value);
4646 end else if JSONObjectProperty.Key='bufferViews' then begin
4647 ProcessBufferViews(JSONObjectProperty.Value);
4648 end else if JSONObjectProperty.Key='cameras' then begin
4649 ProcessCameras(JSONObjectProperty.Value);
4650 end else if JSONObjectProperty.Key='images' then begin
4651 ProcessImages(JSONObjectProperty.Value);
4652 end else if JSONObjectProperty.Key='materials' then begin
4653 ProcessMaterials(JSONObjectProperty.Value);
4654 end else if JSONObjectProperty.Key='meshes' then begin
4655 ProcessMeshes(JSONObjectProperty.Value);
4656 end else if JSONObjectProperty.Key='nodes' then begin
4657 ProcessNodes(JSONObjectProperty.Value);
4658 end else if JSONObjectProperty.Key='samplers' then begin
4659 ProcessSamplers(JSONObjectProperty.Value);
4660 end else if JSONObjectProperty.Key='scene' then begin
4661 fScene:=TPasJSON.GetInt64(JSONObjectProperty.Value,fScene);
4662 end else if JSONObjectProperty.Key='scenes' then begin
4663 ProcessScenes(JSONObjectProperty.Value);
4664 end else if JSONObjectProperty.Key='skins' then begin
4665 ProcessSkins(JSONObjectProperty.Value);
4666 end else if JSONObjectProperty.Key='textures' then begin
4667 ProcessTextures(JSONObjectProperty.Value);
4668 end else if JSONObjectProperty.Key='extensionsUsed' then begin
4669 ProcessStringList(JSONObjectProperty.Value,fExtensionsUsed);
4670 end else if JSONObjectProperty.Key='extensionsRequired' then begin
4671 ProcessStringList(JSONObjectProperty.Value,fExtensionsRequired);
4672 end;
4673 end;
4674 if not HasAsset then begin
4675 raise EPasGLTFInvalidDocument.Create('Invalid GLTF document');
4676 end;
4677 LoadURISources;
4678 end;
4679
4680 procedure TPasGLTF.TDocument.LoadFromBinary(const aStream:TStream);
4681 var GLBHeader:TGLBHeader;
4682 OtherEndianness:boolean;
4683 function SwapEndianness32(const aValue:TPasGLTFUInt32):TPasGLTFUInt32;
4684 begin
4685 if OtherEndianness then begin
4686 result:=((aValue and $000000ff) shl 24) or
4687 ((aValue and $0000ff00) shl 8) or
4688 ((aValue and $00ff0000) shr 8) or
4689 ((aValue and $ff000000) shr 24);
4690 end else begin
4691 result:=aValue;
4692 end;
4693 end;
4694 var RawJSONRawByteString:TPasJSONRawByteString;
4695 ChunkHeader:TChunkHeader;
4696 Stream:TMemoryStream;
4697 JSONItem:TPasJSONItem;
4698 begin
4699 if not (assigned(aStream) and (aStream.Size>=GLBHeaderSize)) then begin
4700 raise EPasGLTFInvalidDocument.Create('Invalid GLB document');
4701 end;
4702 if aStream.Read(GLBHeader,SizeOf(TGLBHeader))<>SizeOf(TGLBHeader) then begin
4703 raise EPasGLTFInvalidDocument.Create('Invalid GLB document');
4704 end;
4705 if (GLBHeader.Magic<>GLBHeaderMagicNativeEndianness) and
4706 (GLBHeader.Magic<>GLBHeaderMagicOtherEndianness) then begin
4707 raise EPasGLTFInvalidDocument.Create('Invalid GLB document');
4708 end;
4709 OtherEndianness:=GLBHeader.Magic=GLBHeaderMagicOtherEndianness;
4710 if not ((not OtherEndianness) and (GLBHeader.JSONChunkHeader.ChunkType=GLBChunkJSONNativeEndianness)) or
4711 (OtherEndianness and (GLBHeader.JSONChunkHeader.ChunkType=GLBChunkJSONOtherEndianness)) then begin
4712 raise EPasGLTFInvalidDocument.Create('Invalid GLB document');
4713 end;
4714 GLBHeader.Magic:=SwapEndianness32(GLBHeader.Magic);
4715 GLBHeader.Version:=SwapEndianness32(GLBHeader.Version);
4716 GLBHeader.Length:=SwapEndianness32(GLBHeader.Length);
4717 GLBHeader.JSONChunkHeader.ChunkLength:=SwapEndianness32(GLBHeader.JSONChunkHeader.ChunkLength);
4718 GLBHeader.JSONChunkHeader.ChunkType:=SwapEndianness32(GLBHeader.JSONChunkHeader.ChunkType);
4719 if ((GLBHeader.JSONChunkHeader.ChunkLength+GLBHeaderSize)>GLBHeader.Length) or
4720 (GLBHeader.JSONChunkHeader.ChunkLength<2) then begin
4721 raise EPasGLTFInvalidDocument.Create('Invalid GLB document');
4722 end;
4723 RawJSONRawByteString:='';
4724 SetLength(RawJSONRawByteString,GLBHeader.JSONChunkHeader.ChunkLength);
4725 aStream.ReadBuffer(RawJSONRawByteString[1],length(RawJSONRawByteString));
4726 JSONItem:=TPasJSON.Parse(RawJSONRawByteString,[],TPasJSONEncoding.UTF8);
4727 if assigned(JSONItem) then begin
4728 try
4729 LoadFromJSON(JSONItem);
4730 finally
4731 FreeAndNil(JSONItem);
4732 end;
4733 end;
4734 if aStream.Position<aStream.Size then begin
4735 if aStream.Read(ChunkHeader,SizeOf(TChunkHeader))<>SizeOf(ChunkHeader) then begin
4736 raise EPasGLTFInvalidDocument.Create('Invalid GLB document');
4737 end;
4738 ChunkHeader.ChunkLength:=SwapEndianness32(ChunkHeader.ChunkLength);
4739 ChunkHeader.ChunkType:=SwapEndianness32(ChunkHeader.ChunkType);
4740 if (ChunkHeader.ChunkType<>GLBChunkBinaryNativeEndianness) or
4741 ((ChunkHeader.ChunkLength+aStream.Position)>GLBHeader.Length) then begin
4742 raise EPasGLTFInvalidDocument.Create('Invalid GLB document');
4743 end;
4744 if fBuffers.Count<1 then begin
4745 fBuffers.Add(TBuffer.Create(self));
4746 end;
4747 Stream:=fBuffers[0].fData;
4748 Stream.Clear;
4749 Stream.CopyFrom(aStream,ChunkHeader.ChunkLength);
4750 end;
4751 end;
4752
4753 procedure TPasGLTF.TDocument.LoadFromStream(const aStream:TStream);
4754 var FirstFourBytes:array[0..3] of TPasGLTFUInt8;
4755 JSONItem:TPasJSONItem;
4756 begin
4757 aStream.ReadBuffer(FirstFourBytes,SizeOf(FirstFourBytes));
4758 aStream.Seek(-SizeOf(FirstFourBytes),soCurrent);
4759 if (FirstFourBytes[0]=ord('g')) and
4760 (FirstFourBytes[1]=ord('l')) and
4761 (FirstFourBytes[2]=ord('T')) and
4762 (FirstFourBytes[3]=ord('F')) then begin
4763 LoadFromBinary(aStream);
4764 end else begin
4765 JSONItem:=TPasJSON.Parse(aStream,[],TPasJSONEncoding.AutomaticDetection);
4766 if assigned(JSONItem) then begin
4767 try
4768 LoadFromJSON(JSONItem);
4769 finally
4770 FreeAndNil(JSONItem);
4771 end;
4772 end;
4773 end;
4774 end;
4775
4776 function TPasGLTF.TDocument.SaveToJSON(const aFormatted:boolean=false):TPasJSONRawByteString;
4777 procedure ProcessExtensionsAndExtras(const aJSONObject:TPasJSONItemObject;const aBaseExtensionsExtrasObject:TBaseExtensionsExtrasObject);
4778 var TemporaryObject,TemporarySubObject:TPasJSONItemObject;
4779 begin
4780 TemporaryObject:=TPasJSONItemObject.Create;
4781 try
4782 if aBaseExtensionsExtrasObject.fExtensions.Count>0 then begin
4783 TemporarySubObject:=TPasJSONItemObject.Create;
4784 try
4785 TemporarySubObject.Merge(aBaseExtensionsExtrasObject.fExtensions);
4786 finally
4787 TemporaryObject.Add('extensions',TemporarySubObject);
4788 end;
4789 end;
4790 if aBaseExtensionsExtrasObject.fExtras.Count>0 then begin
4791 TemporarySubObject:=TPasJSONItemObject.Create;
4792 try
4793 TemporarySubObject.Merge(aBaseExtensionsExtrasObject.fExtras);
4794 finally
4795 TemporaryObject.Add('extras',TemporarySubObject);
4796 end;
4797 end;
4798 aJSONObject.Merge(TemporaryObject);
4799 finally
4800 FreeAndNil(TemporaryObject);
4801 end;
4802 end;
4803 function ProcessAccessors:TPasJSONItemArray;
4804 function ProcessAccessor(const aObject:TAccessor):TPasJSONItemObject;
4805 var Index:TPasJSONSizeInt;
4806 JSONArray:TPasJSONItemArray;
4807 JSONObject,JSONSubObject:TPasJSONItemObject;
4808 begin
4809 result:=TPasJSONItemObject.Create;
4810 try
4811 if aObject.fBufferView>=0 then begin
4812 result.Add('bufferView',TPasJSONItemNumber.Create(aObject.fBufferView));
4813 end;
4814 result.Add('byteOffset',TPasJSONItemNumber.Create(aObject.fByteOffset));
4815 if aObject.fComponentType<>TAccessor.TComponentType.None then begin
4816 result.Add('componentType',TPasJSONItemNumber.Create(TPasGLTFInt64(aObject.fComponentType)));
4817 end;
4818 result.Add('count',TPasJSONItemNumber.Create(aObject.fCount));
4819 if aObject.fMinArray.Count>0 then begin
4820 JSONArray:=TPasJSONItemArray.Create;
4821 try
4822 for Index:=0 to aObject.fMinArray.Count-1 do begin
4823 JSONArray.Add(TPasJSONItemNumber.Create(aObject.fMinArray.Items[Index]));
4824 end;
4825 finally
4826 result.Add('min',JSONArray);
4827 end;
4828 end;
4829 if aObject.fMaxArray.Count>0 then begin
4830 JSONArray:=TPasJSONItemArray.Create;
4831 try
4832 for Index:=0 to aObject.fMaxArray.Count-1 do begin
4833 JSONArray.Add(TPasJSONItemNumber.Create(aObject.fMaxArray.Items[Index]));
4834 end;
4835 finally
4836 result.Add('max',JSONArray);
4837 end;
4838 end;
4839 if length(aObject.fName)>0 then begin
4840 result.Add('name',TPasJSONItemString.Create(aObject.fName));
4841 end;
4842 if aObject.Normalized then begin
4843 result.Add('normalized',TPasJSONItemBoolean.Create(aObject.Normalized));
4844 end;
4845 if not aObject.fSparse.Empty then begin
4846 JSONObject:=TPasJSONItemObject.Create;
4847 try
4848 if aObject.fSparse.fCount>=0 then begin
4849 JSONObject.Add('count',TPasJSONItemNumber.Create(aObject.fSparse.fCount));
4850 end;
4851 if not aObject.fSparse.fIndices.Empty then begin
4852 JSONSubObject:=TPasJSONItemObject.Create;
4853 try
4854 if aObject.fSparse.fIndices.fComponentType<>TAccessor.TComponentType.None then begin
4855 JSONSubObject.Add('componentType',TPasJSONItemNumber.Create(TPasGLTFInt64(aObject.fSparse.fIndices.fComponentType)));
4856 end;
4857 if aObject.fSparse.fIndices.fBufferView>=0 then begin
4858 JSONSubObject.Add('bufferView',TPasJSONItemNumber.Create(aObject.fSparse.fIndices.fBufferView));
4859 end;
4860 JSONSubObject.Add('byteOffset',TPasJSONItemNumber.Create(aObject.fSparse.fIndices.fByteOffset));
4861 finally
4862 JSONObject.Add('indices',JSONSubObject);
4863 end;
4864 end;
4865 if not aObject.fSparse.fValues.Empty then begin
4866 JSONSubObject:=TPasJSONItemObject.Create;
4867 try
4868 if aObject.fSparse.fValues.fBufferView>=0 then begin
4869 JSONSubObject.Add('bufferView',TPasJSONItemNumber.Create(aObject.fSparse.fValues.fBufferView));
4870 end;
4871 JSONSubObject.Add('byteOffset',TPasJSONItemNumber.Create(aObject.fSparse.fValues.fByteOffset));
4872 finally
4873 JSONObject.Add('values',JSONSubObject);
4874 end;
4875 end;
4876 finally
4877 result.Add('sparse',JSONObject);
4878 end;
4879 end;
4880 case aObject.fType of
4881 TPasGLTF.TAccessor.TType.Scalar:begin
4882 result.Add('type',TPasJSONItemString.Create('SCALAR'));
4883 end;
4884 TPasGLTF.TAccessor.TType.Vec2:begin
4885 result.Add('type',TPasJSONItemString.Create('VEC2'));
4886 end;
4887 TPasGLTF.TAccessor.TType.Vec3:begin
4888 result.Add('type',TPasJSONItemString.Create('VEC3'));
4889 end;
4890 TPasGLTF.TAccessor.TType.Vec4:begin
4891 result.Add('type',TPasJSONItemString.Create('VEC4'));
4892 end;
4893 TPasGLTF.TAccessor.TType.Mat2:begin
4894 result.Add('type',TPasJSONItemString.Create('MAT2'));
4895 end;
4896 TPasGLTF.TAccessor.TType.Mat3:begin
4897 result.Add('type',TPasJSONItemString.Create('MAT3'));
4898 end;
4899 TPasGLTF.TAccessor.TType.Mat4:begin
4900 result.Add('type',TPasJSONItemString.Create('MAT4'));
4901 end;
4902 end;
4903 ProcessExtensionsAndExtras(result,aObject);
4904 except
4905 FreeAndNil(result);
4906 raise;
4907 end;
4908 end;
4909 var Accessor:TAccessor;
4910 begin
4911 result:=TPasJSONItemArray.Create;
4912 try
4913 for Accessor in fAccessors do begin
4914 result.Add(ProcessAccessor(Accessor));
4915 end;
4916 except
4917 FreeAndNil(result);
4918 raise;
4919 end;
4920 end;
4921 function ProcessAnimations:TPasJSONItemArray;
4922 function ProcessAnimation(const aObject:TAnimation):TPasJSONItemObject;
4923 var JSONArray:TPasJSONItemArray;
4924 JSONObject,JSONSubObject:TPasJSONItemObject;
4925 Channel:TAnimation.TChannel;
4926 Sampler:TAnimation.TSampler;
4927 begin
4928 result:=TPasJSONItemObject.Create;
4929 try
4930 if length(aObject.fName)>0 then begin
4931 result.Add('name',TPasJSONItemString.Create(aObject.fName));
4932 end;
4933 if aObject.fChannels.Count>0 then begin
4934 JSONArray:=TPasJSONItemArray.Create;
4935 try
4936 for Channel in aObject.fChannels do begin
4937 JSONObject:=TPasJSONItemObject.Create;
4938 try
4939 if Channel.fSampler>=0 then begin
4940 JSONObject.Add('sampler',TPasJSONItemNumber.Create(Channel.fSampler));
4941 end;
4942 if not Channel.fTarget.Empty then begin
4943 JSONSubObject:=TPasJSONItemObject.Create;
4944 try
4945 if Channel.fTarget.fNode>=0 then begin
4946 JSONSubObject.Add('node',TPasJSONItemNumber.Create(Channel.fTarget.fNode));
4947 end;
4948 if length(Channel.fTarget.fPath)>0 then begin
4949 JSONSubObject.Add('path',TPasJSONItemString.Create(Channel.fTarget.fPath));
4950 end;
4951 ProcessExtensionsAndExtras(JSONSubObject,Channel.fTarget);
4952 finally
4953 JSONObject.Add('target',JSONSubObject);
4954 end;
4955 end;
4956 ProcessExtensionsAndExtras(JSONObject,Channel);
4957 finally
4958 JSONArray.Add(JSONObject);
4959 end;
4960 end;
4961 finally
4962 result.Add('channels',JSONArray);
4963 end;
4964 end;
4965 if aObject.fSamplers.Count>0 then begin
4966 JSONArray:=TPasJSONItemArray.Create;
4967 try
4968 for Sampler in aObject.fSamplers do begin
4969 JSONObject:=TPasJSONItemObject.Create;
4970 try
4971 if Sampler.fInput>=0 then begin
4972 JSONObject.Add('input',TPasJSONItemNumber.Create(Sampler.fInput));
4973 end;
4974 if Sampler.fOutput>=0 then begin
4975 JSONObject.Add('output',TPasJSONItemNumber.Create(Sampler.fOutput));
4976 end;
4977 case Sampler.fInterpolation of
4978 TPasGLTF.TAnimation.TSampler.TSamplerType.Linear:begin
4979 JSONObject.Add('interpolation',TPasJSONItemString.Create('LINEAR'));
4980 end;
4981 TPasGLTF.TAnimation.TSampler.TSamplerType.Step:begin
4982 JSONObject.Add('interpolation',TPasJSONItemString.Create('STEP'));
4983 end;
4984 TPasGLTF.TAnimation.TSampler.TSamplerType.CubicSpline:begin
4985 JSONObject.Add('interpolation',TPasJSONItemString.Create('CUBICSPLINE'));
4986 end;
4987 else begin
4988 Assert(false);
4989 end;
4990 end;
4991 finally
4992 JSONArray.Add(JSONObject);
4993 end;
4994 end;
4995 finally
4996 result.Add('samplers',JSONArray);
4997 end;
4998 end;
4999 ProcessExtensionsAndExtras(result,aObject);
5000 except
5001 FreeAndNil(result);
5002 raise;
5003 end;
5004 end;
5005 var Animation:TAnimation;
5006 begin
5007 result:=TPasJSONItemArray.Create;
5008 try
5009 for Animation in fAnimations do begin
5010 result.Add(ProcessAnimation(Animation));
5011 end;
5012 except
5013 FreeAndNil(result);
5014 raise;
5015 end;
5016 end;
5017 function ProcessAsset:TPasJSONItemObject;
5018 begin
5019 result:=TPasJSONItemObject.Create;
5020 try
5021 if length(fAsset.fCopyright)>0 then begin
5022 result.Add('copyright',TPasJSONItemString.Create(fAsset.fCopyright));
5023 end;
5024 if length(fAsset.fGenerator)>0 then begin
5025 result.Add('generator',TPasJSONItemString.Create(fAsset.fGenerator));
5026 end;
5027 if length(fAsset.fMinVersion)>0 then begin
5028 result.Add('minVersion',TPasJSONItemString.Create(fAsset.fMinVersion));
5029 end;
5030 result.Add('version',TPasJSONItemString.Create(fAsset.fVersion));
5031 ProcessExtensionsAndExtras(result,fAsset);
5032 except
5033 FreeAndNil(result);
5034 raise;
5035 end;
5036 end;
5037 function ProcessBuffers:TPasJSONItemArray;
5038 function ProcessBuffer(const aObject:TBuffer):TPasJSONItemObject;
5039 begin
5040 result:=TPasJSONItemObject.Create;
5041 try
5042 if length(aObject.fName)>0 then begin
5043 result.Add('name',TPasJSONItemString.Create(aObject.fName));
5044 end;
5045 if length(aObject.fURI)>0 then begin
5046 result.Add('uri',TPasJSONItemString.Create(aObject.fURI));
5047 end;
5048 result.Add('byteLength',TPasJSONItemNumber.Create(aObject.fByteLength));
5049 ProcessExtensionsAndExtras(result,aObject);
5050 except
5051 FreeAndNil(result);
5052 raise;
5053 end;
5054 end;
5055 var Buffer:TBuffer;
5056 begin
5057 result:=TPasJSONItemArray.Create;
5058 try
5059 for Buffer in fBuffers do begin
5060 result.Add(ProcessBuffer(Buffer));
5061 end;
5062 except
5063 FreeAndNil(result);
5064 raise;
5065 end;
5066 end;
5067 function ProcessBufferViews:TPasJSONItemArray;
5068 function ProcessBufferView(const aObject:TBufferView):TPasJSONItemObject;
5069 begin
5070 result:=TPasJSONItemObject.Create;
5071 try
5072 if length(aObject.fName)>0 then begin
5073 result.Add('name',TPasJSONItemString.Create(aObject.fName));
5074 end;
5075 if aObject.fBuffer>=0 then begin
5076 result.Add('buffer',TPasJSONItemNumber.Create(aObject.fBuffer));
5077 end;
5078 result.Add('byteLength',TPasJSONItemNumber.Create(aObject.fByteLength));
5079 result.Add('byteOffset',TPasJSONItemNumber.Create(aObject.fByteOffset));
5080 if aObject.fByteStride>0 then begin
5081 result.Add('byteStride',TPasJSONItemNumber.Create(aObject.fByteStride));
5082 end;
5083 if aObject.fTarget<>TBufferView.TTargetType.None then begin
5084 result.Add('target',TPasJSONItemNumber.Create(TPasGLTFInt64(aObject.fTarget)));
5085 end;
5086 ProcessExtensionsAndExtras(result,aObject);
5087 except
5088 FreeAndNil(result);
5089 raise;
5090 end;
5091 end;
5092 var BufferView:TBufferView;
5093 begin
5094 result:=TPasJSONItemArray.Create;
5095 try
5096 for BufferView in fBufferViews do begin
5097 result.Add(ProcessBufferView(BufferView));
5098 end;
5099 except
5100 FreeAndNil(result);
5101 raise;
5102 end;
5103 end;
5104 function ProcessCameras:TPasJSONItemArray;
5105 function ProcessCamera(const aObject:TCamera):TPasJSONItemObject;
5106 var JSONObject:TPasJSONItemObject;
5107 begin
5108 result:=TPasJSONItemObject.Create;
5109 try
5110 if length(aObject.fName)>0 then begin
5111 result.Add('name',TPasJSONItemString.Create(aObject.fName));
5112 end;
5113 case aObject.Type_ of
5114 TPasGLTF.TCamera.TCameraType.Orthographic:begin
5115 result.Add('type',TPasJSONItemString.Create('orthographic'));
5116 if not aObject.Orthographic.Empty then begin
5117 JSONObject:=TPasJSONItemObject.Create;
5118 try
5119 if aObject.Orthographic.fXMag<>TDefaults.FloatSentinel then begin
5120 JSONObject.Add('xmag',TPasJSONItemNumber.Create(aObject.Orthographic.fXMag));
5121 end;
5122 if aObject.Orthographic.fYMag<>TDefaults.FloatSentinel then begin
5123 JSONObject.Add('ymag',TPasJSONItemNumber.Create(aObject.Orthographic.fYMag));
5124 end;
5125 if aObject.Orthographic.fZNear<>-TDefaults.FloatSentinel then begin
5126 JSONObject.Add('znear',TPasJSONItemNumber.Create(aObject.Orthographic.fZNear));
5127 end;
5128 if aObject.Orthographic.fZFar<>-TDefaults.FloatSentinel then begin
5129 JSONObject.Add('zfar',TPasJSONItemNumber.Create(aObject.Orthographic.fZfar));
5130 end;
5131 ProcessExtensionsAndExtras(JSONObject,aObject.Orthographic);
5132 finally
5133 result.Add('orthographic',JSONObject);
5134 end;
5135 end;
5136 end;
5137 TPasGLTF.TCamera.TCameraType.Perspective:begin
5138 result.Add('type',TPasJSONItemString.Create('perspective'));
5139 if not aObject.Perspective.Empty then begin
5140 JSONObject:=TPasJSONItemObject.Create;
5141 try
5142 JSONObject.Add('aspectRatio',TPasJSONItemNumber.Create(aObject.Perspective.fAspectRatio));
5143 JSONObject.Add('yfov',TPasJSONItemNumber.Create(aObject.Perspective.fYFov));
5144 JSONObject.Add('znear',TPasJSONItemNumber.Create(aObject.Perspective.fZNear));
5145 JSONObject.Add('zfar',TPasJSONItemNumber.Create(aObject.Perspective.fZfar));
5146 ProcessExtensionsAndExtras(JSONObject,aObject.Perspective);
5147 finally
5148 result.Add('perspective',JSONObject);
5149 end;
5150 end;
5151 end;
5152 else begin
5153 Assert(false);
5154 end;
5155 end;
5156 ProcessExtensionsAndExtras(result,aObject);
5157 except
5158 FreeAndNil(result);
5159 raise;
5160 end;
5161 end;
5162 var Camera:TCamera;
5163 begin
5164 result:=TPasJSONItemArray.Create;
5165 try
5166 for Camera in fCameras do begin
5167 result.Add(ProcessCamera(Camera));
5168 end;
5169 except
5170 FreeAndNil(result);
5171 raise;
5172 end;
5173 end;
5174 function ProcessImages:TPasJSONItemArray;
5175 function ProcessImage(const aObject:TImage):TPasJSONItemObject;
5176 begin
5177 result:=TPasJSONItemObject.Create;
5178 try
5179 if ((aObject.fBufferView>=0) and (length(aObject.fURI)=0)) or
5180 ((aObject.fBufferView>0) and (length(aObject.fURI)>0)) then begin
5181 result.Add('bufferView',TPasJSONItemNumber.Create(aObject.fBufferView));
5182 end;
5183 if length(aObject.fName)>0 then begin
5184 result.Add('name',TPasJSONItemString.Create(aObject.fName));
5185 end;
5186 if length(aObject.fMimeType)>0 then begin
5187 result.Add('mimeType',TPasJSONItemString.Create(aObject.fMimeType));
5188 end;
5189 if length(aObject.fURI)>0 then begin
5190 result.Add('uri',TPasJSONItemString.Create(aObject.fURI));
5191 end;
5192 ProcessExtensionsAndExtras(result,aObject);
5193 except
5194 FreeAndNil(result);
5195 raise;
5196 end;
5197 end;
5198 var Image:TImage;
5199 begin
5200 result:=TPasJSONItemArray.Create;
5201 try
5202 for Image in fImages do begin
5203 result.Add(ProcessImage(Image));
5204 end;
5205 except
5206 FreeAndNil(result);
5207 raise;
5208 end;
5209 end;
5210 function ProcessMaterials:TPasJSONItemArray;
5211 function ProcessMaterial(const aObject:TMaterial):TPasJSONItemObject;
5212 var JSONArray:TPasJSONItemArray;
5213 JSONObject,JSONSubObject:TPasJSONItemObject;
5214 begin
5215 result:=TPasJSONItemObject.Create;
5216 try
5217 if aObject.fAlphaCutOff<>TDefaults.MaterialAlphaCutoff then begin
5218 result.Add('alphaCutoff',TPasJSONItemNumber.Create(aObject.fAlphaCutOff));
5219 end;
5220 case aObject.fAlphaMode of
5221 TPasGLTF.TMaterial.TAlphaMode.Opaque:begin
5222 // Default value
5223 // result.Add('alphaMode',TPasJSONItemString.Create('OPAQUE'));
5224 end;
5225 TPasGLTF.TMaterial.TAlphaMode.Mask:begin
5226 result.Add('alphaMode',TPasJSONItemString.Create('MASK'));
5227 end;
5228 TPasGLTF.TMaterial.TAlphaMode.Blend:begin
5229 result.Add('alphaMode',TPasJSONItemString.Create('BLEND'));
5230 end;
5231 else begin
5232 Assert(false);
5233 end;
5234 end;
5235 if aObject.fDoubleSided<>TDefaults.MaterialDoubleSided then begin
5236 result.Add('doubleSided',TPasJSONItemBoolean.Create(aObject.fDoubleSided));
5237 end;
5238 if not CompareMem(@aObject.EmissiveFactor,@TDefaults.NullVector3,SizeOf(TVector3)) then begin
5239 JSONArray:=TPasJSONItemArray.Create;
5240 try
5241 JSONArray.Add(TPasJSONItemNumber.Create(aObject.EmissiveFactor[0]));
5242 JSONArray.Add(TPasJSONItemNumber.Create(aObject.EmissiveFactor[1]));
5243 JSONArray.Add(TPasJSONItemNumber.Create(aObject.EmissiveFactor[2]));
5244 finally
5245 result.Add('emissiveFactor',JSONArray);
5246 end;
5247 end;
5248 if not aObject.fEmissiveTexture.Empty then begin
5249 JSONObject:=TPasJSONItemObject.Create;
5250 try
5251 if aObject.fEmissiveTexture.fIndex>=0 then begin
5252 JSONObject.Add('index',TPasJSONItemNumber.Create(aObject.fEmissiveTexture.fIndex));
5253 end;
5254 if aObject.fEmissiveTexture.fTexCoord>0 then begin
5255 JSONObject.Add('texCoord',TPasJSONItemNumber.Create(aObject.fEmissiveTexture.fTexCoord));
5256 end;
5257 ProcessExtensionsAndExtras(JSONObject,aObject.fEmissiveTexture);
5258 finally
5259 result.Add('emissiveTexture',JSONObject);
5260 end;
5261 end;
5262 if length(aObject.fName)>0 then begin
5263 result.Add('name',TPasJSONItemString.Create(aObject.fName));
5264 end;
5265 if not aObject.fNormalTexture.Empty then begin
5266 JSONObject:=TPasJSONItemObject.Create;
5267 try
5268 if aObject.fNormalTexture.fIndex>=0 then begin
5269 JSONObject.Add('index',TPasJSONItemNumber.Create(aObject.fNormalTexture.fIndex));
5270 end;
5271 if aObject.fNormalTexture.fTexCoord>0 then begin
5272 JSONObject.Add('texCoord',TPasJSONItemNumber.Create(aObject.fNormalTexture.fTexCoord));
5273 end;
5274 if aObject.fNormalTexture.fScale<>TDefaults.IdentityScalar then begin
5275 JSONObject.Add('scale',TPasJSONItemNumber.Create(aObject.fNormalTexture.fScale));
5276 end;
5277 ProcessExtensionsAndExtras(JSONObject,aObject.fNormalTexture);
5278 finally
5279 result.Add('normalTexture',JSONObject);
5280 end;
5281 end;
5282 if not aObject.fOcclusionTexture.Empty then begin
5283 JSONObject:=TPasJSONItemObject.Create;
5284 try
5285 if aObject.fOcclusionTexture.fIndex>=0 then begin
5286 JSONObject.Add('index',TPasJSONItemNumber.Create(aObject.fOcclusionTexture.fIndex));
5287 end;
5288 if aObject.fOcclusionTexture.fTexCoord>0 then begin
5289 JSONObject.Add('texCoord',TPasJSONItemNumber.Create(aObject.fOcclusionTexture.fTexCoord));
5290 end;
5291 if aObject.fOcclusionTexture.fStrength<>TDefaults.IdentityScalar then begin
5292 JSONObject.Add('strength',TPasJSONItemNumber.Create(aObject.fOcclusionTexture.fStrength));
5293 end;
5294 ProcessExtensionsAndExtras(JSONObject,aObject.fOcclusionTexture);
5295 finally
5296 result.Add('occlusionTexture',JSONObject);
5297 end;
5298 end;
5299 if not aObject.PBRMetallicRoughness.Empty then begin
5300 JSONObject:=TPasJSONItemObject.Create;
5301 try
5302 if not CompareMem(@aObject.PBRMetallicRoughness.fBaseColorFactor,@TDefaults.IdentityVector4,SizeOf(TVector4)) then begin
5303 JSONArray:=TPasJSONItemArray.Create;
5304 try
5305 JSONArray.Add(TPasJSONItemNumber.Create(aObject.PBRMetallicRoughness.fBaseColorFactor[0]));
5306 JSONArray.Add(TPasJSONItemNumber.Create(aObject.PBRMetallicRoughness.fBaseColorFactor[1]));
5307 JSONArray.Add(TPasJSONItemNumber.Create(aObject.PBRMetallicRoughness.fBaseColorFactor[2]));
5308 JSONArray.Add(TPasJSONItemNumber.Create(aObject.PBRMetallicRoughness.fBaseColorFactor[3]));
5309 finally
5310 JSONObject.Add('baseColorFactor',JSONArray);
5311 end;
5312 end;
5313 if not aObject.fPBRMetallicRoughness.fBaseColorTexture.Empty then begin
5314 JSONSubObject:=TPasJSONItemObject.Create;
5315 try
5316 if aObject.fPBRMetallicRoughness.fBaseColorTexture.fIndex>=0 then begin
5317 JSONSubObject.Add('index',TPasJSONItemNumber.Create(aObject.fPBRMetallicRoughness.fBaseColorTexture.fIndex));
5318 end;
5319 if aObject.fPBRMetallicRoughness.fBaseColorTexture.fTexCoord>0 then begin
5320 JSONSubObject.Add('texCoord',TPasJSONItemNumber.Create(aObject.fPBRMetallicRoughness.fBaseColorTexture.fTexCoord));
5321 end;
5322 ProcessExtensionsAndExtras(JSONSubObject,aObject.fPBRMetallicRoughness.fBaseColorTexture);
5323 finally
5324 JSONObject.Add('baseColorTexture',JSONSubObject);
5325 end;
5326 end;
5327 if aObject.fPBRMetallicRoughness.fMetallicFactor<>TDefaults.IdentityScalar then begin
5328 JSONObject.Add('metallicFactor',TPasJSONItemNumber.Create(aObject.fPBRMetallicRoughness.fMetallicFactor));
5329 end;
5330 if not aObject.fPBRMetallicRoughness.fMetallicRoughnessTexture.Empty then begin
5331 JSONSubObject:=TPasJSONItemObject.Create;
5332 try
5333 if aObject.fPBRMetallicRoughness.fMetallicRoughnessTexture.fIndex>=0 then begin
5334 JSONSubObject.Add('index',TPasJSONItemNumber.Create(aObject.fPBRMetallicRoughness.fMetallicRoughnessTexture.fIndex));
5335 end;
5336 if aObject.fPBRMetallicRoughness.fMetallicRoughnessTexture.fTexCoord>0 then begin
5337 JSONSubObject.Add('texCoord',TPasJSONItemNumber.Create(aObject.fPBRMetallicRoughness.fMetallicRoughnessTexture.fTexCoord));
5338 end;
5339 ProcessExtensionsAndExtras(JSONSubObject,aObject.fPBRMetallicRoughness.fMetallicRoughnessTexture);
5340 finally
5341 JSONObject.Add('metallicRoughnessTexture',JSONSubObject);
5342 end;
5343 end;
5344 if aObject.fPBRMetallicRoughness.fRoughnessFactor<>TDefaults.IdentityScalar then begin
5345 JSONObject.Add('roughnessFactor',TPasJSONItemNumber.Create(aObject.fPBRMetallicRoughness.fRoughnessFactor));
5346 end;
5347 ProcessExtensionsAndExtras(JSONObject,aObject.fPBRMetallicRoughness);
5348 finally
5349 result.Add('pbrMetallicRoughness',JSONObject);
5350 end;
5351 end;
5352 ProcessExtensionsAndExtras(result,aObject);
5353 except
5354 FreeAndNil(result);
5355 raise;
5356 end;
5357 end;
5358 var Material:TMaterial;
5359 begin
5360 result:=TPasJSONItemArray.Create;
5361 try
5362 for Material in fMaterials do begin
5363 result.Add(ProcessMaterial(Material));
5364 end;
5365 except
5366 FreeAndNil(result);
5367 raise;
5368 end;
5369 end;
5370 function ProcessMeshes:TPasJSONItemArray;
5371 function ProcessMesh(const aObject:TMesh):TPasJSONItemObject;
5372 var Index:TPasJSONSizeInt;
5373 JSONArray:TPasJSONItemArray;
5374 JSONObject,JSONSubObject:TPasJSONItemObject;
5375 Primitive:TMesh.TPrimitive;
5376 Attributes:TAttributes;
5377 Used:boolean;
5378 begin
5379 result:=TPasJSONItemObject.Create;
5380 try
5381 if aObject.fPrimitives.Count>0 then begin
5382 JSONArray:=TPasJSONItemArray.Create;
5383 try
5384 for Primitive in aObject.fPrimitives do begin
5385 JSONObject:=TPasJSONItemObject.Create;
5386 try
5387 begin
5388 Used:=false;
5389 for Index:=0 to Primitive.fAttributes.fSize-1 do begin
5390 if Primitive.fAttributes.fEntityToCellIndex[Index]>=0 then begin
5391 Used:=true;
5392 break;
5393 end;
5394 end;
5395 if Used then begin
5396 JSONSubObject:=TPasJSONItemObject.Create;
5397 try
5398 for Index:=0 to Primitive.fAttributes.fSize-1 do begin
5399 if Primitive.fAttributes.fEntityToCellIndex[Index]>=0 then begin
5400 JSONSubObject.Add(Primitive.fAttributes.fEntities[Index].Key,TPasJSONItemNumber.Create(Primitive.fAttributes.fEntities[Index].Value));
5401 end;
5402 end;
5403 finally
5404 JSONObject.Add('attributes',JSONSubObject);
5405 end;
5406 end;
5407 end;
5408 if Primitive.fIndices>=0 then begin
5409 JSONObject.Add('indices',TPasJSONItemNumber.Create(Primitive.fIndices));
5410 end;
5411 if Primitive.fMaterial>=0 then begin
5412 JSONObject.Add('material',TPasJSONItemNumber.Create(Primitive.fMaterial));
5413 end;
5414 if Primitive.fMode<>TMesh.TPrimitive.TMode.Triangles then begin
5415 JSONObject.Add('mode',TPasJSONItemNumber.Create(TPasGLTFInt64(Primitive.fMode)));
5416 end;
5417 if Primitive.fTargets.Count>0 then begin
5418 JSONArray:=TPasJSONItemArray.Create;
5419 try
5420 for Attributes in Primitive.fTargets do begin
5421 JSONSubObject:=TPasJSONItemObject.Create;
5422 try
5423 for Index:=0 to Attributes.fSize-1 do begin
5424 if Attributes.fEntityToCellIndex[Index]>=0 then begin
5425 JSONSubObject.Add(Attributes.fEntities[Index].Key,TPasJSONItemNumber.Create(Attributes.fEntities[Index].Value));
5426 end;
5427 end;
5428 finally
5429 JSONArray.Add(JSONSubObject);
5430 end;
5431 end;
5432 finally
5433 JSONObject.Add('targets',JSONArray);
5434 end;
5435 end;
5436 ProcessExtensionsAndExtras(JSONObject,Primitive);
5437 finally
5438 JSONArray.Add(JSONObject);
5439 end;
5440 end;
5441 finally
5442 result.Add('primitives',JSONArray);
5443 end;
5444 end;
5445 if aObject.fWeights.Count>0 then begin
5446 JSONArray:=TPasJSONItemArray.Create;
5447 try
5448 for Index:=0 to aObject.fWeights.Count-1 do begin
5449 JSONArray.Add(TPasJSONItemNumber.Create(aObject.fWeights.Items[Index]));
5450 end;
5451 finally
5452 result.Add('weights',JSONArray);
5453 end;
5454 end;
5455 ProcessExtensionsAndExtras(result,aObject);
5456 except
5457 FreeAndNil(result);
5458 raise;
5459 end;
5460 end;
5461 var Mesh:TMesh;
5462 begin
5463 result:=TPasJSONItemArray.Create;
5464 try
5465 for Mesh in fMeshes do begin
5466 result.Add(ProcessMesh(Mesh));
5467 end;
5468 except
5469 FreeAndNil(result);
5470 raise;
5471 end;
5472 end;
5473 function ProcessNodes:TPasJSONItemArray;
5474 function ProcessNode(const aObject:TNode):TPasJSONItemObject;
5475 var Index:TPasJSONSizeInt;
5476 JSONArray:TPasJSONItemArray;
5477 begin
5478 result:=TPasJSONItemObject.Create;
5479 try
5480 if aObject.fCamera>=0 then begin
5481 result.Add('camera',TPasJSONItemNumber.Create(aObject.fCamera));
5482 end;
5483 if aObject.fChildren.Count>0 then begin
5484 JSONArray:=TPasJSONItemArray.Create;
5485 try
5486 for Index:=0 to aObject.fChildren.Count-1 do begin
5487 JSONArray.Add(TPasJSONItemNumber.Create(aObject.fChildren.Items[Index]));
5488 end;
5489 finally
5490 result.Add('children',JSONArray);
5491 end;
5492 end;
5493 if not CompareMem(@aObject.fMatrix,@TDefaults.IdentityMatrix4x4,SizeOf(TMatrix4x4)) then begin
5494 JSONArray:=TPasJSONItemArray.Create;
5495 try
5496 for Index:=0 to 15 do begin
5497 JSONArray.Add(TPasJSONItemNumber.Create(aObject.fMatrix[Index]));
5498 end;
5499 finally
5500 result.Add('matrix',JSONArray);
5501 end;
5502 end;
5503 if length(aObject.fName)>0 then begin
5504 result.Add('name',TPasJSONItemString.Create(aObject.fName));
5505 end;
5506 if aObject.fMesh>=0 then begin
5507 result.Add('mesh',TPasJSONItemNumber.Create(aObject.fMesh));
5508 end;
5509 if not CompareMem(@aObject.fRotation,@TDefaults.IdentityQuaternion,SizeOf(TVector4)) then begin
5510 JSONArray:=TPasJSONItemArray.Create;
5511 try
5512 for Index:=0 to 3 do begin
5513 JSONArray.Add(TPasJSONItemNumber.Create(aObject.fRotation[Index]));
5514 end;
5515 finally
5516 result.Add('rotation',JSONArray);
5517 end;
5518 end;
5519 if not CompareMem(@aObject.fScale,@TDefaults.IdentityVector3,SizeOf(TVector3)) then begin
5520 JSONArray:=TPasJSONItemArray.Create;
5521 try
5522 for Index:=0 to 2 do begin
5523 JSONArray.Add(TPasJSONItemNumber.Create(aObject.fScale[Index]));
5524 end;
5525 finally
5526 result.Add('scale',JSONArray);
5527 end;
5528 end;
5529 if aObject.fSkin>=0 then begin
5530 result.Add('skin',TPasJSONItemNumber.Create(aObject.fSkin));
5531 end;
5532 if not CompareMem(@aObject.fTranslation,@TDefaults.NullVector3,SizeOf(TVector3)) then begin
5533 JSONArray:=TPasJSONItemArray.Create;
5534 try
5535 for Index:=0 to 2 do begin
5536 JSONArray.Add(TPasJSONItemNumber.Create(aObject.fTranslation[Index]));
5537 end;
5538 finally
5539 result.Add('translation',JSONArray);
5540 end;
5541 end;
5542 if aObject.fWeights.Count>0 then begin
5543 JSONArray:=TPasJSONItemArray.Create;
5544 try
5545 for Index:=0 to aObject.fWeights.Count-1 do begin
5546 JSONArray.Add(TPasJSONItemNumber.Create(aObject.fWeights.Items[Index]));
5547 end;
5548 finally
5549 result.Add('weights',JSONArray);
5550 end;
5551 end;
5552 ProcessExtensionsAndExtras(result,aObject);
5553 except
5554 FreeAndNil(result);
5555 raise;
5556 end;
5557 end;
5558 var Node:TNode;
5559 begin
5560 result:=TPasJSONItemArray.Create;
5561 try
5562 for Node in fNodes do begin
5563 result.Add(ProcessNode(Node));
5564 end;
5565 except
5566 FreeAndNil(result);
5567 raise;
5568 end;
5569 end;
5570 function ProcessSamplers:TPasJSONItemArray;
5571 function ProcessSampler(const aObject:TSampler):TPasJSONItemObject;
5572 begin
5573 result:=TPasJSONItemObject.Create;
5574 try
5575 if not aObject.Empty then begin
5576 if aObject.fMinFilter<>TSampler.TMinFilter.None then begin
5577 result.Add('minFilter',TPasJSONItemNumber.Create(TPasGLTFInt64(aObject.fMinFilter)));
5578 end;
5579 if aObject.fMagFilter<>TSampler.TMagFilter.None then begin
5580 result.Add('magFilter',TPasJSONItemNumber.Create(TPasGLTFInt64(aObject.fMagFilter)));
5581 end;
5582 if length(aObject.fName)>0 then begin
5583 result.Add('name',TPasJSONItemString.Create(aObject.fName));
5584 end;
5585 if aObject.fWrapS<>TSampler.TWrappingMode.Repeat_ then begin
5586 result.Add('wrapS',TPasJSONItemNumber.Create(TPasGLTFInt64(aObject.fWrapS)));
5587 end;
5588 if aObject.fWrapT<>TSampler.TWrappingMode.Repeat_ then begin
5589 result.Add('wrapS',TPasJSONItemNumber.Create(TPasGLTFInt64(aObject.fWrapT)));
5590 end;
5591 ProcessExtensionsAndExtras(result,aObject);
5592 end;
5593 except
5594 FreeAndNil(result);
5595 raise;
5596 end;
5597 end;
5598 var Sampler:TSampler;
5599 begin
5600 result:=TPasJSONItemArray.Create;
5601 try
5602 for Sampler in fSamplers do begin
5603 result.Add(ProcessSampler(Sampler));
5604 end;
5605 except
5606 FreeAndNil(result);
5607 raise;
5608 end;
5609 end;
5610 function ProcessScenes:TPasJSONItemArray;
5611 function ProcessScene(const aObject:TScene):TPasJSONItemObject;
5612 var Index:TPasJSONSizeInt;
5613 JSONArray:TPasJSONItemArray;
5614 begin
5615 result:=TPasJSONItemObject.Create;
5616 try
5617 if length(aObject.fName)>0 then begin
5618 result.Add('name',TPasJSONItemString.Create(aObject.fName));
5619 end;
5620 if aObject.fNodes.Count>0 then begin
5621 JSONArray:=TPasJSONItemArray.Create;
5622 try
5623 for Index:=0 to aObject.fNodes.Count-1 do begin
5624 JSONArray.Add(TPasJSONItemNumber.Create(aObject.fNodes.Items[Index]));
5625 end;
5626 finally
5627 result.Add('nodes',JSONArray);
5628 end;
5629 end;
5630 ProcessExtensionsAndExtras(result,aObject);
5631 except
5632 FreeAndNil(result);
5633 raise;
5634 end;
5635 end;
5636 var Scene:TScene;
5637 begin
5638 result:=TPasJSONItemArray.Create;
5639 try
5640 for Scene in fScenes do begin
5641 result.Add(ProcessScene(Scene));
5642 end;
5643 except
5644 FreeAndNil(result);
5645 raise;
5646 end;
5647 end;
5648 function ProcessSkins:TPasJSONItemArray;
5649 function ProcessSkin(const aObject:TSkin):TPasJSONItemObject;
5650 var Index:TPasJSONSizeInt;
5651 JSONArray:TPasJSONItemArray;
5652 begin
5653 result:=TPasJSONItemObject.Create;
5654 try
5655 if aObject.fInverseBindMatrices>=0 then begin
5656 result.Add('inverseBindMatrices',TPasJSONItemNumber.Create(aObject.fInverseBindMatrices));
5657 end;
5658 if aObject.fJoints.Count>0 then begin
5659 JSONArray:=TPasJSONItemArray.Create;
5660 try
5661 for Index:=0 to aObject.fJoints.Count-1 do begin
5662 JSONArray.Add(TPasJSONItemNumber.Create(aObject.fJoints.Items[Index]));
5663 end;
5664 finally
5665 result.Add('joints',JSONArray);
5666 end;
5667 end;
5668 if length(aObject.fName)>0 then begin
5669 result.Add('name',TPasJSONItemString.Create(aObject.fName));
5670 end;
5671 if aObject.fSkeleton>=0 then begin
5672 result.Add('skeleton',TPasJSONItemNumber.Create(aObject.fSkeleton));
5673 end;
5674 ProcessExtensionsAndExtras(result,aObject);
5675 except
5676 FreeAndNil(result);
5677 raise;
5678 end;
5679 end;
5680 var Skin:TSkin;
5681 begin
5682 result:=TPasJSONItemArray.Create;
5683 try
5684 for Skin in fSkins do begin
5685 result.Add(ProcessSkin(Skin));
5686 end;
5687 except
5688 FreeAndNil(result);
5689 raise;
5690 end;
5691 end;
5692 function ProcessTextures:TPasJSONItemArray;
5693 function ProcessTexture(const aObject:TTexture):TPasJSONItemObject;
5694 begin
5695 result:=TPasJSONItemObject.Create;
5696 try
5697 if length(aObject.fName)>0 then begin
5698 result.Add('name',TPasJSONItemString.Create(aObject.fName));
5699 end;
5700 if aObject.fSampler>=0 then begin
5701 result.Add('sampler',TPasJSONItemNumber.Create(aObject.fSampler));
5702 end;
5703 if aObject.fSource>=0 then begin
5704 result.Add('source',TPasJSONItemNumber.Create(aObject.fSource));
5705 end;
5706 ProcessExtensionsAndExtras(result,aObject);
5707 except
5708 FreeAndNil(result);
5709 raise;
5710 end;
5711 end;
5712 var Texture:TTexture;
5713 begin
5714 result:=TPasJSONItemArray.Create;
5715 try
5716 for Texture in fTextures do begin
5717 result.Add(ProcessTexture(Texture));
5718 end;
5719 except
5720 FreeAndNil(result);
5721 raise;
5722 end;
5723 end;
5724 var JSONRootItem:TPasJSONItemObject;
5725 JSONArray:TPasJSONItemArray;
5726 Extension:String;
5727 begin
5728 JSONRootItem:=TPasJSONItemObject.Create;
5729 try
5730 if fAccessors.Count>0 then begin
5731 JSONRootItem.Add('accessors',ProcessAccessors);
5732 end;
5733 if fAnimations.Count>0 then begin
5734 JSONRootItem.Add('animations',ProcessAnimations);
5735 end;
5736 JSONRootItem.Add('asset',ProcessAsset);
5737 if fBuffers.Count>0 then begin
5738 JSONRootItem.Add('buffers',ProcessBuffers);
5739 end;
5740 if fBufferViews.Count>0 then begin
5741 JSONRootItem.Add('bufferViews',ProcessBufferViews);
5742 end;
5743 if fCameras.Count>0 then begin
5744 JSONRootItem.Add('cameras',ProcessCameras);
5745 end;
5746 if fImages.Count>0 then begin
5747 JSONRootItem.Add('images',ProcessImages);
5748 end;
5749 if fMaterials.Count>0 then begin
5750 JSONRootItem.Add('materials',ProcessMaterials);
5751 end;
5752 if fMeshes.Count>0 then begin
5753 JSONRootItem.Add('meshes',ProcessMeshes);
5754 end;
5755 if fNodes.Count>0 then begin
5756 JSONRootItem.Add('nodes',ProcessNodes);
5757 end;
5758 if fSamplers.Count>0 then begin
5759 JSONRootItem.Add('samplers',ProcessSamplers);
5760 end;
5761 if fScene>=0 then begin
5762 JSONRootItem.Add('scene',TPasJSONItemNumber.Create(fScene));
5763 end;
5764 if fScenes.Count>0 then begin
5765 JSONRootItem.Add('scenes',ProcessScenes);
5766 end;
5767 if fSkins.Count>0 then begin
5768 JSONRootItem.Add('skins',ProcessSkins);
5769 end;
5770 if fTextures.Count>0 then begin
5771 JSONRootItem.Add('textures',ProcessTextures);
5772 end;
5773 if fExtensionsUsed.Count>0 then begin
5774 JSONArray:=TPasJSONItemArray.Create;
5775 try
5776 for Extension in fExtensionsUsed do begin
5777 JSONArray.Add(TPasJSONItemString.Create(TPasJSONUTF8String(Extension)));
5778 end;
5779 finally
5780 JSONRootItem.Add('extensionsUsed',JSONArray);
5781 end;
5782 end;
5783 if fExtensionsRequired.Count>0 then begin
5784 JSONArray:=TPasJSONItemArray.Create;
5785 try
5786 for Extension in fExtensionsRequired do begin
5787 JSONArray.Add(TPasJSONItemString.Create(TPasJSONUTF8String(Extension)));
5788 end;
5789 finally
5790 JSONRootItem.Add('extensionsRequired',JSONArray);
5791 end;
5792 end;
5793 ProcessExtensionsAndExtras(JSONRootItem,self);
5794 result:=TPasJSON.Stringify(JSONRootItem,aFormatted,[]);
5795 finally
5796 FreeAndNil(JSONRootItem);
5797 end;
5798 end;
5799
5800 procedure TPasGLTF.TDocument.SaveToBinary(const aStream:TStream);
5801 var JSONRawByteString:TPasJSONRawByteString;
5802 GLBHeader:TGLBHeader;
5803 ChunkHeader:TChunkHeader;
5804 begin
5805 JSONRawByteString:=SaveToJSON(false);
5806 while (length(JSONRawByteString)=0) or ((length(JSONRawByteString) and 3)<>0) do begin
5807 JSONRawByteString:=JSONRawByteString+#32;
5808 end;
5809 GLBHeader.Magic:=GLBHeaderMagicNativeEndianness;
5810 GLBHeader.Version:=$00000002;
5811 GLBHeader.Length:=SizeOf(TGLBHeader)+Length(JSONRawByteString);
5812 if (fBuffers.Count>0) and (fBuffers[0].fData.Size>0) then begin
5813 inc(GLBHeader.Length,SizeOf(TChunkHeader)+fBuffers[0].fData.Size);
5814 end;
5815 GLBHeader.JSONChunkHeader.ChunkLength:=Length(JSONRawByteString);
5816 GLBHeader.JSONChunkHeader.ChunkType:=GLBChunkJSONNativeEndianness;
5817 aStream.WriteBuffer(GLBHeader,SizeOf(TGLBHeader));
5818 aStream.WriteBuffer(JSONRawByteString[1],Length(JSONRawByteString));
5819 if (fBuffers.Count>0) and (fBuffers[0].fData.Size>0) then begin
5820 ChunkHeader.ChunkLength:=fBuffers[0].fData.Size;
5821 ChunkHeader.ChunkType:=GLBChunkBinaryNativeEndianness;
5822 aStream.WriteBuffer(ChunkHeader,SizeOf(TChunkHeader));
5823 aStream.WriteBuffer(fBuffers[0].fData.Memory^,fBuffers[0].fData.Size);
5824 end;
5825 end;
5826
5827 procedure TPasGLTF.TDocument.SaveToStream(const aStream:TStream;const aBinary:boolean=false;const aFormatted:boolean=false);
5828 var JSONRawByteString:TPasJSONRawByteString;
5829 begin
5830 if aBinary then begin
5831 SaveToBinary(aStream);
5832 end else begin
5833 JSONRawByteString:=SaveToJSON(aFormatted);
5834 if length(JSONRawByteString)>0 then begin
5835 aStream.WriteBuffer(JSONRawByteString[1],length(JSONRawByteString));
5836 end;
5837 end;
5838 end;
5839
5840 end.