// SPDX-FileCopyrightText: 2023 Unity Technologies and the glTFast authors
// SPDX-License-Identifier: Apache-2.0
using System;
namespace GLTFast.Schema
{
///
[Serializable]
public class Texture : TextureBase { }
///
/// Texture extensions type
[Serializable]
public abstract class TextureBase : TextureBase
where TExtensions : TextureExtensions
{
///
public TExtensions extensions;
///
public override TextureExtensions Extensions => extensions;
///
internal override void UnsetExtensions()
{
extensions = null;
}
}
///
/// A texture is defined by an image and a sampler.
///
[Serializable]
public abstract class TextureBase : NamedObject
{
///
/// The index of the sampler used by this texture.
///
public int sampler = -1;
///
/// The index of the image used by this texture.
///
public int source = -1;
///
public abstract TextureExtensions Extensions { get; }
///
/// Retrieves the final image index.
///
/// Final image index
public int GetImageIndex()
{
if (Extensions != null)
{
if (Extensions.KHR_texture_basisu != null && Extensions.KHR_texture_basisu.source >= 0)
{
return Extensions.KHR_texture_basisu.source;
}
}
return source;
}
///
/// True, if the texture is of the KTX format.
///
public bool IsKtx => Extensions?.KHR_texture_basisu != null;
internal void GltfSerialize(JsonWriter writer)
{
writer.AddObject();
GltfSerializeName(writer);
if (source >= 0)
{
writer.AddProperty("source", source);
}
if (sampler >= 0)
{
writer.AddProperty("sampler", sampler);
}
if (Extensions != null)
{
writer.AddProperty("extensions");
Extensions.GltfSerialize(writer);
}
writer.Close();
}
///
/// Determines whether two object instances are equal.
///
/// The object to compare with the current object.
/// true if the specified object is equal to the current object; otherwise, false.
// TODO: Remove upon next major release. This serves no purpose anymore except keeping the API intact.
public override bool Equals(object obj)
{
// ReSharper disable once BaseObjectEqualsIsObjectEquals
return base.Equals(obj);
}
///
/// Default hash function.
///
/// A hash code for the current object.
// TODO: Remove upon next major release. This serves no purpose anymore except keeping the API intact.
public override int GetHashCode()
{
// ReSharper disable once BaseObjectGetHashCodeCallInGetHashCode
return base.GetHashCode();
}
///
/// Sets to null.
///
internal abstract void UnsetExtensions();
///
/// Cleans up invalid parsing artifacts created by .
///
internal void JsonUtilityCleanup()
{
var e = Extensions;
if (e != null)
{
// Check if Basis Universal extension is valid
if ((e.KHR_texture_basisu?.source ?? -1) < 0)
{
e.KHR_texture_basisu = null;
UnsetExtensions();
}
}
}
}
}