Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there still exist GltfDataReader in the new JglTF version? #26

Closed
Frank-Chan opened this issue Apr 12, 2018 · 11 comments
Closed

Is there still exist GltfDataReader in the new JglTF version? #26

Frank-Chan opened this issue Apr 12, 2018 · 11 comments

Comments

@Frank-Chan
Copy link

Frank-Chan commented Apr 12, 2018

Hi,

Is there still exist GltfDataReader in the new JglTF package? @javagl

With the jgltf-model-0.0.0-SNAPSHOT.jar package I can use the code below to get and convert glTF1.0 file to glb.

    GltfDataReader r = new GltfDataReader();
    GltfData gltfData = r.readGltfData(gltfuri);
    gltfData = new GltfDataToBinaryConverter().convert(gltfData);
    BinaryGltfDataWriter w = new BinaryGltfDataWriter();
    w.writeBinaryGltfData(gltfData, glburi);

Recently, I want to use GltfDataReader to get glTF2.0 file and convert it to glb. But in the new new JglTF package, I can't find GltfDataReader, gltfData, GltfDataToBinaryConverter() and BinaryGltfDataWriter.

Sincerely,
Frank-Chan

@javagl
Copy link
Owner

javagl commented Apr 12, 2018

During the transition from glTF 1.0 to glTF 2.0, there has been a considerable refactoring. The goal was to be able to support both versions, as far as reasonably possible. Therefore, an abstraction layer has been introduced to better handle the different representations of glTF 1.0 and 2.0 assets.

The class that most closely resembles the original GltfData is the GltfAsset class. But in order to be as version-agnostic as possible, you'd rather use the (version-independent) GltfModel.

So in order to read a glTF file and write it as a GLB file, the following code can be used:

import java.io.IOException;
import java.net.URI;
import java.nio.file.Paths;

import de.javagl.jgltf.model.GltfModel;
import de.javagl.jgltf.model.io.GltfModelReader;
import de.javagl.jgltf.model.io.GltfModelWriter;

public class GltfToBinaryConversionExample
{
    public static void main(String[] args) throws IOException
    {
        String pathString = "C:/Assets/";
        String inputFileName = "Triangle.gltf";
        URI uri = Paths.get(pathString + inputFileName).toUri();
        
        GltfModelReader gltfModelReader = new GltfModelReader();
        GltfModel gltfModel = gltfModelReader.read(uri);

        String outputFileName = "Triangle.glb";
        GltfModelWriter gltfModelWriter = new GltfModelWriter();
        gltfModelWriter.writeBinary(gltfModel, 
            Paths.get(pathString, outputFileName).toFile());
    }
}

@Frank-Chan
Copy link
Author

Dear @javagl ,
Thanks for your timely feedback. It's a good idea to support both versions for glTF 1.0 to glTF 2.0. I tried both GltfAsset and GltfModel, however, the convert result of glTF2.0 to glb seems not properly, it can't display in the glb Viewers(gltf-viewervirtualgis).

Here is my convert result.
gltf2glb.zip

I would be greatly appreciated if you could spend some of your time check the problems for me.

Best regards

@javagl
Copy link
Owner

javagl commented Apr 13, 2018

Throwing the original glTF it into the glTF-Validator at http://github.khronos.org/glTF-Validator/ reveals many errors, but they seem to fall into two categories:

            "code": "MESH_PRIMITIVE_INVALID_ATTRIBUTE",
            "message": "Invalid attribute name 'BINORMAL'.",
            "severity": 0,
            "pointer": "/meshes/0/primitives/45/attributes"

and

            "code": "MESH_PRIMITIVE_ATTRIBUTES_ACCESSOR_INVALID_FORMAT",
            "message": "Invalid accessor format '{VEC3, FLOAT}' for this attribute semantic. Must be one of ('{VEC4, FLOAT}').",
            "severity": 0,
            "pointer": "/meshes/0/primitives/59/attributes/TANGENT"

The first one should be covered by KhronosGroup/COLLADA2GLTF#174

The second one might be a bit more tricky. Depending on which version of COLLADA2GLTF you used, we might consider filing another bug report here.

--

In any case: I see that, for example, the glTF Viewer can render the original glTF nevertheless.

But for the GLB, there seems to be another error, related to the GLB chunks. This is obviously an error in jgltf-obj. I'll investigate this.

@javagl
Copy link
Owner

javagl commented Apr 13, 2018

I just tried to reproduce the error, but the resulting binary glTF asset seems to be valid.

Did you run it through a program like the GltfToBinaryConversionExample that I posted above? For me, this results in a 674KB GLB file (in contrast to the 1052KB one that you posted). This file can be validated (causing the same errors as the glTF), and it can be viewed in the gltf-viewer.

A quick look at the file contents showed that the JSON part of the binary glTF that you provided still contained the embedded data. Do you perform any additional operations on the GltfAsset or related objects during the conversion?

@Frank-Chan
Copy link
Author

I did run the program which you posted above. The program which i used to convert the gltf file is below.

20180426184322

@javagl
Copy link
Owner

javagl commented Apr 29, 2018

Did it also not work for you with the code at #26 (comment) ?

I tried the approach that you posted (BTW: http://idownvotedbecau.se/imageofcode ), and there indeed is some problem with the chunk size.

I'll try to figure out what is wrong there and provide an update ASAP.

@Frank-Chan
Copy link
Author

Thanks!

@javagl
Copy link
Owner

javagl commented May 2, 2018

It's somehow important to know:

Did it work when you used the GltfModelReader and GltfModelWriter?

(If it worked, the issue that appeared with the GltfAssetWriter would have a lower priority...)

@Frank-Chan
Copy link
Author

Yes, they can convert glTF to glb, but can't display in the glTF Viewer.

@javagl
Copy link
Owner

javagl commented May 8, 2018

Using the model that you provided, it worked for me in the https://gltf-viewer.donmccurdy.com/ , at least.

  • Are there any error messages displayed in the console (F12) when you try to load the model?
  • Can you provide the output of your conversion (i.e. the model that was created by the GltfModelWriter, and which does not work)?

In any case, I'll try to investigate this further, and also try to fix the GltfAssetWriter issue.

@javagl
Copy link
Owner

javagl commented Nov 10, 2018

The main point here is some sort of ambiguity in what exactly constitutes a "binary", "embedded" or "default" glTF model.

The model in question contains images as embedded resources (i.e. using data URIs). When this is written as a binary glTF, then it is not clear how to decide whether these embedded resources should become part of the binary buffer, or whether they should remain embedded in the glTF asset.

For now, the functionality of writing different model types is offered via the GtlfModel structures. When using

gltfModelWriter.writeDefault(gltfModel, outputFile); // or
gltfModelWriter.writeEmbedded(gltfModel, outputFile); // or
gltfModelWriter.writeBinary(gltfModel, outputFile);

the resulting files will be "pure", meaning that they will either use external files for all resources, data URIs for all resources, or a binary blob for all resources.

Compared to the GtlfModel, the GltfAsset is a "low-level" representation of the asset, which also allows for "mixed type" assets, and the corresponding writers will write the GltfAsset as-it-is, without converting the representation of the resources.

Nevertheless, the handling of "mixed type assets" contained some ambiguities that should largely be resolved via 3eff092

I'll close this one, and opened a dedicated issue to track how the manual conversions should be handled at #31

@javagl javagl closed this as completed Nov 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants