materialExtensionsToJson function

Map<String, Object?> materialExtensionsToJson(
  1. MaterialExtensions e, {
  2. required Object? texture(
    1. TextureBinding binding
    ),
})

The inverse of materialExtensionsFromJson: each extension whose values differ from its defaults, or that carries a texture, keyed by its glTF name. texture writes one binding in the caller's format.

An extension at its defaults is left out, for the reason the glTF writer leaves out KHR_materials_emissive_strength at one: it says nothing a reader without it would not already draw.

Implementation

Map<String, Object?> materialExtensionsToJson(
  MaterialExtensions e, {
  required Object? Function(TextureBinding binding) texture,
}) {
  Object? slot(TextureBinding? binding) =>
      binding == null ? null : texture(binding);
  final specularColor = e.specularColor;
  final specular = <String, Object?>{
    if (e.specular != 1.0) 'specularFactor': e.specular,
    'specularTexture': ?slot(e.specularTexture),
    if (specularColor.x != 1.0 ||
        specularColor.y != 1.0 ||
        specularColor.z != 1.0)
      'specularColorFactor': <double>[
        specularColor.x,
        specularColor.y,
        specularColor.z,
      ],
    'specularColorTexture': ?slot(e.specularColorTexture),
  };
  final coatNormal = slot(e.clearcoatNormalTexture);
  final clearcoat = <String, Object?>{
    if (e.clearcoat != 0.0) 'clearcoatFactor': e.clearcoat,
    'clearcoatTexture': ?slot(e.clearcoatTexture),
    if (e.clearcoatRoughness != 0.0)
      'clearcoatRoughnessFactor': e.clearcoatRoughness,
    'clearcoatRoughnessTexture': ?slot(e.clearcoatRoughnessTexture),
    if (coatNormal != null)
      'clearcoatNormalTexture':
          coatNormal is Map<String, Object?> && e.clearcoatNormalScale != 1.0
          ? <String, Object?>{...coatNormal, 'scale': e.clearcoatNormalScale}
          : coatNormal,
  };
  final sheenColor = e.sheenColor;
  final sheen = <String, Object?>{
    if (sheenColor.x != 0.0 || sheenColor.y != 0.0 || sheenColor.z != 0.0)
      'sheenColorFactor': <double>[sheenColor.x, sheenColor.y, sheenColor.z],
    'sheenColorTexture': ?slot(e.sheenColorTexture),
    if (e.sheenRoughness != 0.0) 'sheenRoughnessFactor': e.sheenRoughness,
    'sheenRoughnessTexture': ?slot(e.sheenRoughnessTexture),
  };
  final anisotropy = <String, Object?>{
    if (e.anisotropyStrength != 0.0) 'anisotropyStrength': e.anisotropyStrength,
    if (e.anisotropyRotation != 0.0) 'anisotropyRotation': e.anisotropyRotation,
    'anisotropyTexture': ?slot(e.anisotropyTexture),
  };
  final transmission = <String, Object?>{
    if (e.transmission != 0.0) 'transmissionFactor': e.transmission,
    'transmissionTexture': ?slot(e.transmissionTexture),
  };
  final attenuationColor = e.attenuationColor;
  final volume = <String, Object?>{
    if (e.thickness != 0.0) 'thicknessFactor': e.thickness,
    'thicknessTexture': ?slot(e.thicknessTexture),
    if (e.attenuationDistance.isFinite)
      'attenuationDistance': e.attenuationDistance,
    if (attenuationColor.x != 1.0 ||
        attenuationColor.y != 1.0 ||
        attenuationColor.z != 1.0)
      'attenuationColor': <double>[
        attenuationColor.x,
        attenuationColor.y,
        attenuationColor.z,
      ],
  };
  final iridescence = <String, Object?>{
    if (e.iridescence != 0.0) 'iridescenceFactor': e.iridescence,
    'iridescenceTexture': ?slot(e.iridescenceTexture),
    if (e.iridescenceIor != 1.3) 'iridescenceIor': e.iridescenceIor,
    if (e.iridescenceThicknessMinimum != 100.0)
      'iridescenceThicknessMinimum': e.iridescenceThicknessMinimum,
    if (e.iridescenceThicknessMaximum != 400.0)
      'iridescenceThicknessMaximum': e.iridescenceThicknessMaximum,
    'iridescenceThicknessTexture': ?slot(e.iridescenceThicknessTexture),
  };
  return <String, Object?>{
    if (e.ior != 1.5) 'KHR_materials_ior': <String, Object?>{'ior': e.ior},
    if (specular.isNotEmpty) 'KHR_materials_specular': specular,
    if (clearcoat.isNotEmpty) 'KHR_materials_clearcoat': clearcoat,
    if (sheen.isNotEmpty) 'KHR_materials_sheen': sheen,
    if (anisotropy.isNotEmpty) 'KHR_materials_anisotropy': anisotropy,
    if (transmission.isNotEmpty) 'KHR_materials_transmission': transmission,
    if (volume.isNotEmpty) 'KHR_materials_volume': volume,
    if (e.dispersion != 0.0)
      'KHR_materials_dispersion': <String, Object?>{'dispersion': e.dispersion},
    if (iridescence.isNotEmpty) 'KHR_materials_iridescence': iridescence,
  };
}