// SPDX-FileCopyrightText: 2023 Unity Technologies and the glTFast authors // SPDX-License-Identifier: Apache-2.0 using System; using System.Collections.Generic; namespace GLTFast.Schema { /// [Serializable] public class Mesh : MeshBase { } /// /// extras type /// Mesh primitive type [Serializable] public abstract class MeshBase : MeshBase, ICloneable where TPrimitive : MeshPrimitiveBase where TExtras : MeshExtras { /// public TExtras extras; /// public TPrimitive[] primitives; /// public override MeshExtras Extras => extras; /// public override IReadOnlyList Primitives => primitives; /// /// Clones the Mesh object /// /// Member-wise clone public object Clone() { var clone = (MeshBase)MemberwiseClone(); if (Primitives != null) { clone.primitives = new TPrimitive[primitives.Length]; for (var i = 0; i < primitives.Length; i++) { clone.primitives[i] = (TPrimitive)primitives[i].Clone(); } } return clone; } } /// /// A set of primitives to be rendered. Its global transform is defined by /// a node that references it. /// [Serializable] public abstract class MeshBase : NamedObject { /// /// An array of primitives, each defining geometry to be rendered with /// a material. /// public abstract IReadOnlyList Primitives { get; } /// /// Array of weights to be applied to the Morph Targets. /// public float[] weights; /// public abstract MeshExtras Extras { get; } internal void GltfSerialize(JsonWriter writer) { writer.AddObject(); GltfSerializeName(writer); if (Primitives != null) { writer.AddArray("primitives"); foreach (var primitive in Primitives) { primitive.GltfSerialize(writer); } writer.CloseArray(); } if (weights != null) { writer.AddArrayProperty("weights", weights); } if (Extras != null) { writer.AddProperty("extras"); Extras.GltfSerialize(writer); writer.Close(); } writer.Close(); } } /// /// Application-specific data for meshes /// [Serializable] public class MeshExtras { /// /// Morph targets' names /// public string[] targetNames; internal void GltfSerialize(JsonWriter writer) { if (targetNames != null) { writer.AddArrayPropertySafe("targetNames", targetNames); } } } }