gltf-rs / gltf

A crate for loading glTF 2.0
Apache License 2.0
535 stars 124 forks source link

Skip serialization of default values in materials? #438

Open Densaugeo opened 1 month ago

Densaugeo commented 1 month ago

I'm looking at replacing some custom GLTF structs for a model generator with the gltf_json crate from this project. One oddity I ran into involves serializing materials. If a material is created with default values:

gltf_root.push(gltf_json::Material::default());

When it is serialized many of the defaults are included in the JSON:

    {
      "alphaMode": "OPAQUE",
      "doubleSided": false,
      "pbrMetallicRoughness": {
        "baseColorFactor": [
          1.0,
          1.0,
          1.0,
          1.0
        ],
        "metallicFactor": 1.0,
        "roughnessFactor": 1.0
      },
      "emissiveFactor": [
        0.0,
        0.0,
        0.0
      ]
    }

The GLTF spec at https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-material states that emissiveFactor, alphaMode, doubleSided, and the values included under pbrMetallicRoughness are all optional AND have spec-defined default values. For spec-compliant renderers, there's no reason to include those values in the JSON.

Is there a way to tell the gltf crate to skip serializing these values? If not, would you accept a patch that updates gltf_json's materials to skip serializing values if they are equal to their spec-defined defaults?