dmurdoch / rgl2gltf

Read and write .gltf and .glb files in R.
https://dmurdoch.github.io/rgl2gltf
7 stars 0 forks source link

glTF file cannot be loaded #34

Closed gdagstn closed 2 years ago

gdagstn commented 2 years ago

Specs

Issue

Hi, I am using rgl to generate scene3d objects to load as GLB files in a three.js renderer, using rgl2gltf for the conversion and writing.
Somehow the glTF files do not display properly in an engine that displays glTF scenes (three.js or otherwise), while the GLB files do.

When I load the glTF files on a viewer such as this one (https://sandbox.babylonjs.com/) an error code pops up regarding "missing the Uri or the binary chunk". In fact while the glTF files I can download from the internet are structured as JSON objects (that come together with a .bin file), the ones coming from rgl2gltf are different - actually .GLB and .gltf files look remarkably similar when opened in a text editor.

Reprex:

library(rgl)
library(rgl2gltf)

shade3d( translate3d( tetrahedron3d(col = "red"), 0, 0, 0) )
shade3d( translate3d( cube3d(col = "green"), 3, 0, 0) )
shade3d( translate3d( octahedron3d(col = "blue"), 6, 0, 0) )
shade3d( translate3d( dodecahedron3d(col = "cyan"), 9, 0, 0) )
shade3d( translate3d( icosahedron3d(col = "magenta"), 12, 0, 0) )

sc = scene3d()

writeGLB(as.gltf(sc), con =  "test_withnames.glb")
writeglTF(as.gltf(sc), path =  "test_withnames.gltf")

Then I go to the viewer and drag and drop the files; only the GLB files can be loaded.

Error message:

Unable to load from file:test.gltf: /buffers/0: Uri is missing or the binary glTF is missing its binary chunk

I wonder if there is anything I should be doing differently?

Thanks!

dmurdoch commented 2 years ago

glTF files aren't self contained. The data is in a separate file, and it needs to be available to the viewer as well. Unfortunately your viewer doesn't tell you the name that's missing, but if you run print(as.gltf(sc), verbose = "buffers") it will be in the uri field. It's probably test_withnames.bin.

gdagstn commented 2 years ago

Thanks for the quick response!

Indeed I did think I was missing a .bin file, but there are none to be found in the folder I'm working at. If I run print(as.gltf(sc), verbose = "buffers"), here's my output:

asset fields:
  GLtf version 2.0 file.
  Generated by rgl2gltf version  0.1.12.
Scenes ( 1 )
Nodes ( 13 )
Buffers:
  Buffer 0:
    byteLength: 9912
  Other buffer fields:  bytes.
Bufferviews ( 30 )
Meshes ( 10 )
Accessors ( 30 )
Materials ( 6 )
Other fields:  extras.

The uri field is missing.

If I do the same with another glTF file downloaded from somewhere else, I do see it:

asset fields:
  GLtf version 2.0 file.
  Generated by Sketchfab-12.74.0.
  extras Haider Al_Asady (https://sketchfab.com/haiderhabbeb313), CC-BY-4.0 (http://creativecommons.org/licenses/by/4.0/), https://sketchfab.com/3d-models/millitary-locker-cbe0943ab7dd4a2d907f6e77f4bee008, millitary locker
Scenes ( 1 )
Nodes ( 10 )
Buffers:
  Buffer 0:
    uri: scene.bin
    byteLength: 169872
Bufferviews ( 4 )
Meshes ( 4 )
Accessors ( 20 )
Materials ( 4 )
Textures ( 12 )
Images ( 12 )
Other fields:  samplers.
dmurdoch commented 2 years ago

Sorry, the .bin file gets created by writeglTF. You need to write it out, read it back in, and then do the verbose print.

gdagstn commented 2 years ago

Sorry, still no luck:

sc_gltf = as.gltf(sc)

writeglTF(sc_gltf, path =  "test.gltf")

sc_gltf_in = readglTF(path = "test.gltf")

print(sc_gltf_in, verbose = "buffers")
asset fields:
  GLtf version 2.0 file.
  Generated by rgl2gltf version  0.1.12.
Scenes ( 1 )
Nodes ( 18 )
Buffers:
  Buffer 0:
    byteLength: 14868
  Other buffer fields:  bytes.
Bufferviews ( 45 )
Meshes ( 15 )
Accessors ( 45 )
Materials ( 6 )
Other fields:  extras.

And I still don't have any .bin file in my directory... am I missing something?

dmurdoch commented 2 years ago

You're right, that's a bug in the design of writeglTF. It doesn't attempt to write the binary data.