// SPDX-FileCopyrightText: 2023 Unity Technologies and the glTFast authors // SPDX-License-Identifier: Apache-2.0 using System; using UnityEngine; namespace GLTFast.Schema { /// [Serializable] public class Camera : CameraBase { } /// /// Orthographic camera type /// Perspective camera type [Serializable] public abstract class CameraBase : CameraBase where TOrthographic : CameraOrthographic where TPerspective : CameraPerspective { /// public TOrthographic orthographic; /// public TPerspective perspective; /// public override CameraOrthographic Orthographic => orthographic; /// public override CameraPerspective Perspective => perspective; } /// /// A camera’s projection /// [Serializable] public abstract class CameraBase : NamedObject { /// /// Camera projection type /// public enum Type { /// /// Orthogonal projection /// Orthographic, /// /// Perspective projection /// Perspective } /// // Field is public for unified serialization only. Warn via Obsolete attribute. [Obsolete("Use GetCameraType and SetCameraType for access.")] public string type; Type? m_TypeEnum; /// /// typed and cached getter onto string. /// /// Camera type, if it was retrieved correctly. otherwise public Type GetCameraType() { if (m_TypeEnum.HasValue) { return m_TypeEnum.Value; } #pragma warning disable CS0618 // Type or member is obsolete if (Enum.TryParse(type, true, out var typeEnum)) { m_TypeEnum = typeEnum; type = null; return m_TypeEnum.Value; } #pragma warning restore CS0618 // Type or member is obsolete if (Orthographic != null) m_TypeEnum = Type.Orthographic; if (Perspective != null) m_TypeEnum = Type.Perspective; return m_TypeEnum ?? Type.Perspective; } /// /// typed setter for string. /// /// Camera type public void SetCameraType(Type cameraType) { #pragma warning disable CS0618 // Type or member is obsolete type = null; #pragma warning restore CS0618 // Type or member is obsolete m_TypeEnum = cameraType; } /// public abstract CameraOrthographic Orthographic { get; } /// public abstract CameraPerspective Perspective { get; } internal void GltfSerialize(JsonWriter writer) { writer.AddObject(); GltfSerializeName(writer); writer.AddProperty("type", m_TypeEnum.ToString().ToLowerInvariant()); if (Perspective != null) { writer.AddProperty("perspective"); Perspective.GltfSerialize(writer); } if (Orthographic != null) { writer.AddProperty("orthographic"); Orthographic.GltfSerialize(writer); } writer.Close(); } } /// /// An orthographic camera containing properties to create an orthographic projection matrix. /// [Serializable] public class CameraOrthographic { /// /// The floating-point horizontal magnification of the view. Must not be zero. /// // ReSharper disable once IdentifierTypo public float xmag; /// /// The floating-point vertical magnification of the view. Must not be zero. /// // ReSharper disable once IdentifierTypo public float ymag; /// /// The floating-point distance to the far clipping plane. /// must be greater than . /// // ReSharper disable once IdentifierTypo public float zfar; /// /// The floating-point distance to the near clipping plane. /// // ReSharper disable once IdentifierTypo public float znear; internal void GltfSerialize(JsonWriter writer) { writer.AddObject(); // ReSharper disable StringLiteralTypo writer.AddProperty("xmag", xmag); writer.AddProperty("ymag", ymag); writer.AddProperty("zfar", zfar); writer.AddProperty("znear", znear); // ReSharper restore StringLiteralTypo writer.Close(); } } /// /// A perspective camera containing properties to create a perspective projection matrix. /// [Serializable] public class CameraPerspective { /// /// The floating-point aspect ratio of the field of view. /// public float aspectRatio = -1; /// /// The floating-point vertical field of view in radians. /// // ReSharper disable once IdentifierTypo public float yfov; /// /// The floating-point distance to the far clipping plane. /// // ReSharper disable once IdentifierTypo public float zfar = -1f; /// /// The floating-point distance to the near clipping plane. /// // ReSharper disable once IdentifierTypo public float znear; internal void GltfSerialize(JsonWriter writer) { writer.AddObject(); if (aspectRatio > 0) { writer.AddProperty("aspectRatio", aspectRatio); } // ReSharper disable StringLiteralTypo writer.AddProperty("yfov", yfov); if (zfar < float.MaxValue) { writer.AddProperty("zfar", zfar); } writer.AddProperty("znear", znear); // ReSharper restore StringLiteralTypo writer.Close(); } } }