@@ -159,13 +159,14 @@ const Metadata = struct {
159
159
} = null ,
160
160
} = &.{},
161
161
textures : []struct {
162
- sampler : u16 ,
162
+ sampler : ? u16 = null ,
163
163
source : u16 ,
164
164
} = &.{},
165
165
images : []struct {
166
166
name : ? []u8 = null ,
167
- mimeType : []u8 ,
168
- bufferView : u16 ,
167
+ uri : ? []u8 = null ,
168
+ mimeType : ? []u8 = null ,
169
+ bufferView : ? u16 = null ,
169
170
} = &.{},
170
171
animations : []struct {
171
172
name : ? []u8 = null ,
@@ -298,37 +299,37 @@ pub const Options = struct {
298
299
};
299
300
300
301
pub const DefaultBufferManager = struct {
301
- cwd : ? std.fs.Dir ,
302
+ root_dir : ? std.fs.Dir ,
302
303
303
- pub const empty : DefaultBufferManager = .{ .cwd = null };
304
+ pub const empty : DefaultBufferManager = .{ .root_dir = null };
304
305
305
306
pub fn loadUri (
306
307
bm : * DefaultBufferManager ,
307
308
allocator : mem.Allocator ,
308
309
uri : []const u8 ,
309
- len : u32 ,
310
+ len : ? u32 ,
310
311
) error {LoadingAsset }! []const u8 {
311
312
if (std .Uri .parse (uri )) | puri | {
312
313
const i = mem .indexOf (u8 , puri .path .percent_encoded , "base64," ) orelse return error .LoadingAsset ;
313
314
const base64_data = puri .path .percent_encoded [i + 7 .. ];
314
315
const base64_len = base64 .Decoder .calcSizeForSlice (base64_data ) catch return error .LoadingAsset ;
315
316
const data = allocator .alloc (u8 , base64_len ) catch return error .LoadingAsset ;
316
317
base64 .Decoder .decode (data , base64_data ) catch return error .LoadingAsset ;
317
- if (data .len != len ) return error .LoadingAsset ;
318
+ if (len != null and data .len != len .? ) return error .LoadingAsset ;
318
319
return data ;
319
320
} else | _ | {}
320
321
321
- const cwd = if (bm .cwd ) | cwd | cwd else std .fs .cwd ();
322
- const data = cwd .readFileAlloc (allocator , uri , len ) catch return error .LoadingAsset ;
323
- return data [0. .len ];
322
+ const cwd = if (bm .root_dir ) | root_dir | root_dir else std .fs .cwd ();
323
+ const data = cwd .readFileAlloc (allocator , uri , len orelse std . math . maxInt ( usize ) ) catch return error .LoadingAsset ;
324
+ return data [0 .. len orelse data .len ];
324
325
}
325
326
};
326
327
327
328
pub fn parse (
328
329
gpa : mem.Allocator ,
329
330
data : []const u8 ,
330
331
ctx : anytype ,
331
- loadUri : * const fn (ctx : @TypeOf (ctx ), allocator : mem.Allocator , uri : []const u8 , len : u32 ) error {LoadingAsset }! []const u8 ,
332
+ loadUri : * const fn (ctx : @TypeOf (ctx ), allocator : mem.Allocator , uri : []const u8 , len : ? u32 ) error {LoadingAsset }! []const u8 ,
332
333
options : Options ,
333
334
) Error ! GLTF {
334
335
var fbs = std .io .fixedBufferStream (data );
@@ -429,30 +430,44 @@ pub fn parse(
429
430
if (pbr_metallic_roughness .baseColorTexture ) | tex_index | {
430
431
const tex = metadata .textures [tex_index .index ];
431
432
const img = metadata .images [tex .source ];
432
- const buffer_view = metadata .bufferViews [img .bufferView ];
433
- if (buffer_view .byteStride != null ) return error .InvalidAsset ;
434
- const buffer = metadata .buffers [buffer_view .buffer ];
435
- const buffer_data = if (is_bin )
436
- data [fbs .pos .. ][0.. buffer .byteLength ]
437
- else
438
- try loadUri (ctx , allocator , buffer .uri .? , buffer .byteLength );
439
- const ptr = buffer_data [buffer_view .byteOffset .. ][0.. buffer_view .byteLength ];
440
- base_color = try loadTexture (ptr );
433
+
434
+ if (img .uri ) | uri | {
435
+ const buffer_data = try loadUri (ctx , allocator , uri , null );
436
+ base_color = try loadTexture (buffer_data );
437
+ } else {
438
+ const buffer_view = metadata .bufferViews [img .bufferView .? ];
439
+ if (buffer_view .byteStride != null ) return error .InvalidAsset ;
440
+
441
+ const buffer = metadata .buffers [buffer_view .buffer ];
442
+ const buffer_data = if (is_bin )
443
+ data [fbs .pos .. ][0.. buffer .byteLength ]
444
+ else
445
+ try loadUri (ctx , allocator , buffer .uri .? , buffer .byteLength );
446
+ const ptr = buffer_data [buffer_view .byteOffset .. ][0.. buffer_view .byteLength ];
447
+ base_color = try loadTexture (ptr );
448
+ }
441
449
}
442
450
443
451
var metallic_roughness : ? Texture = null ;
444
452
if (pbr_metallic_roughness .metallicRoughnessTexture ) | tex_index | {
445
453
const tex = metadata .textures [tex_index .index ];
446
454
const img = metadata .images [tex .source ];
447
- const buffer_view = metadata .bufferViews [img .bufferView ];
448
- if (buffer_view .byteStride != null ) return error .InvalidAsset ;
449
- const buffer = metadata .buffers [buffer_view .buffer ];
450
- const buffer_data = if (is_bin )
451
- data [fbs .pos .. ][0.. buffer .byteLength ]
452
- else
453
- try loadUri (ctx , allocator , buffer .uri .? , buffer .byteLength );
454
- const ptr = buffer_data [buffer_view .byteOffset .. ][0.. buffer_view .byteLength ];
455
- metallic_roughness = try loadTexture (ptr );
455
+
456
+ if (img .uri ) | uri | {
457
+ const buffer_data = try loadUri (ctx , allocator , uri , null );
458
+ metallic_roughness = try loadTexture (buffer_data );
459
+ } else {
460
+ const buffer_view = metadata .bufferViews [img .bufferView .? ];
461
+ if (buffer_view .byteStride != null ) return error .InvalidAsset ;
462
+
463
+ const buffer = metadata .buffers [buffer_view .buffer ];
464
+ const buffer_data = if (is_bin )
465
+ data [fbs .pos .. ][0.. buffer .byteLength ]
466
+ else
467
+ try loadUri (ctx , allocator , buffer .uri .? , buffer .byteLength );
468
+ const ptr = buffer_data [buffer_view .byteOffset .. ][0.. buffer_view .byteLength ];
469
+ metallic_roughness = try loadTexture (ptr );
470
+ }
456
471
}
457
472
458
473
pbr = .{
@@ -465,15 +480,22 @@ pub fn parse(
465
480
if (material .normalTexture ) | tex_index | {
466
481
const tex = metadata .textures [tex_index .index ];
467
482
const img = metadata .images [tex .source ];
468
- const buffer_view = metadata .bufferViews [img .bufferView ];
469
- if (buffer_view .byteStride != null ) return error .InvalidAsset ;
470
- const buffer = metadata .buffers [buffer_view .buffer ];
471
- const buffer_data = if (is_bin )
472
- data [fbs .pos .. ][0.. buffer .byteLength ]
473
- else
474
- try loadUri (ctx , allocator , buffer .uri .? , buffer .byteLength );
475
- const ptr = buffer_data [buffer_view .byteOffset .. ][0.. buffer_view .byteLength ];
476
- normal = try loadTexture (ptr );
483
+
484
+ if (img .uri ) | uri | {
485
+ const buffer_data = try loadUri (ctx , allocator , uri , null );
486
+ normal = try loadTexture (buffer_data );
487
+ } else {
488
+ const buffer_view = metadata .bufferViews [img .bufferView .? ];
489
+ if (buffer_view .byteStride != null ) return error .InvalidAsset ;
490
+
491
+ const buffer = metadata .buffers [buffer_view .buffer ];
492
+ const buffer_data = if (is_bin )
493
+ data [fbs .pos .. ][0.. buffer .byteLength ]
494
+ else
495
+ try loadUri (ctx , allocator , buffer .uri .? , buffer .byteLength );
496
+ const ptr = buffer_data [buffer_view .byteOffset .. ][0.. buffer_view .byteLength ];
497
+ normal = try loadTexture (ptr );
498
+ }
477
499
}
478
500
479
501
out_material .* = .{
0 commit comments