glTFast icon indicating copy to clipboard operation
glTFast copied to clipboard

Object type members that are not present in the original JSON have a non-null value

Open sandr01d opened this issue 1 year ago • 0 comments

Describe the bug

When importing a JSON glTF, all members that are not present in the JSON file have a non-null value. This is caused by Unitys JsonUtility which instantiates these member by calling their parameterless constructors. Other people appear to have the same issue.

This is especially problematic when re-serializing the glTF using Root.GltfSerialize() because the members that are supposed to be null will be serialized as empty objects. This does create bugs further down the line. Here is an example:

I'm currently working on adding JSON serialization for the types that do not have this implemented yet (I plan to create a PR once finished). I've added JSON serialization for the TextureExtension class. Due to the issue described below, the following happens when deserializing and serializing a file that does have a texture, but does not use any texture extensions, multiple times:

  1. On the first import, JsonUtility creates non-null values for the extensions member in every texture.
  2. When serializing the glTF again, an empty object is created in the JSON for each textures extension member.
  3. When deserializing the created JSON again, JsonUtility will assign a non-null value to TextureExtension.KHR_texture_basisu

At this point, Texture.IsKtx will return true and the texture transform will be flipped in MaterialGenerator.TrySetTextureTransform(). I've noticed this comment when browsing the code base:

https://github.com/atteneder/glTFast/blob/5c36f4405012b13ad14ec871b77ba063c53cbbc3/Runtime/Scripts/JsonParser.cs#L27-L36C23

I'm not sure what exactly the mentioned work-around is, but it does not appear to be working.

Files I've attached the file I use for testing, but I think that the issue affects any JSON glTF file.

gltf.zip

To Reproduce

Pass the path to a glTF file that has a texture but doesn't have any extensions defined to the following function:

public static async void Test(string path)
{
    using GltfImport gltf = new();
    bool success = await gltf.Load("file://" + path);
    Root root = gltf.GetSourceRoot();
    Debug.Assert(root.textures[0].extensions == null);
}

Expected behavior Texture.extensions should be null and the assertion should pass.

Actual behavior Texture.extensions is not null and the assertion fails.

Desktop (please complete the following information):

  • glTFast version: latest master (5c36f4405012b13ad14ec871b77ba063c53cbbc3)
  • Unity Editor version: 2021.3.26
  • Render Pipeline and version: Universal Render Pipeline 12.1.11
  • Platform: Editor

sandr01d avatar Sep 04 '23 12:09 sandr01d