using System.Collections.Generic; using GLTF.Schema; using UnityEngine; using CameraType = GLTF.Schema.CameraType; namespace UnityGLTF { public partial class GLTFSceneExporter { private Dictionary _exportedCameras; private CameraId ExportCamera(Camera unityCamera) { GLTFCamera camera = new GLTFCamera(); //name camera.Name = unityCamera.name; //type bool isOrthographic = unityCamera.orthographic; camera.Type = isOrthographic ? CameraType.orthographic : CameraType.perspective; Matrix4x4 matrix = unityCamera.projectionMatrix; //matrix properties: compute the fields from the projection matrix if (isOrthographic) { CameraOrthographic ortho = new CameraOrthographic(); ortho.XMag = 1 / matrix[0, 0]; ortho.YMag = 1 / matrix[1, 1]; float farClip = (matrix[2, 3] / matrix[2, 2]) - (1 / matrix[2, 2]); float nearClip = farClip + (2 / matrix[2, 2]); ortho.ZFar = farClip; ortho.ZNear = nearClip; camera.Orthographic = ortho; } else { CameraPerspective perspective = new CameraPerspective(); float fov = 2 * Mathf.Atan(1 / matrix[1, 1]); float aspectRatio = matrix[1, 1] / matrix[0, 0]; perspective.YFov = fov; perspective.AspectRatio = aspectRatio; if (matrix[2, 2] == -1) { //infinite projection matrix float nearClip = matrix[2, 3] * -0.5f; perspective.ZNear = nearClip; } else { //finite projection matrix float farClip = matrix[2, 3] / (matrix[2, 2] + 1); float nearClip = farClip * (matrix[2, 2] + 1) / (matrix[2, 2] - 1); perspective.ZFar = farClip; perspective.ZNear = nearClip; } camera.Perspective = perspective; } var id = new CameraId { Id = _root.Cameras.Count, Root = _root }; // Register nodes for animation parsing (could be disabled if animation is disabled) _exportedCameras.Add(unityCamera.GetInstanceID(), _root.Cameras.Count); _root.Cameras.Add(camera); return id; } } }