Skip to content

Commit

Permalink
Merge pull request #158 from ziedbha/camera-feature
Browse files Browse the repository at this point in the history
Added camera export feature, fixed some camera serialization bugs
  • Loading branch information
sbtron authored Apr 27, 2018
2 parents 5df26ab + 3a93c5a commit d5d371a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ public override void Serialize(JsonWriter writer)
writer.WritePropertyName("xmag");
writer.WriteValue(XMag);

writer.WritePropertyName("Ymag");
writer.WritePropertyName("ymag");
writer.WriteValue(YMag);

writer.WritePropertyName("ZFar");
writer.WritePropertyName("zfar");
writer.WriteValue(ZFar);

writer.WritePropertyName("ZNear");
writer.WritePropertyName("znear");
writer.WriteValue(ZNear);

base.Serialize(writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public override void Serialize(JsonWriter writer)
writer.WriteValue(ZFar);
}

writer.WritePropertyName("ZNear");
writer.WritePropertyName("znear");
writer.WriteValue(ZNear);

base.Serialize(writer);
Expand Down
69 changes: 69 additions & 0 deletions UnityGLTF/Assets/UnityGLTF/Scripts/GLTFSceneExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public GLTFSceneExporter(Transform[] rootTransforms, RetrieveTexturePathDelegate
},
Buffers = new List<GLTF.Schema.Buffer>(),
BufferViews = new List<BufferView>(),
Cameras = new List<GLTF.Schema.Camera>(),
Images = new List<Image>(),
Materials = new List<GLTF.Schema.Material>(),
Meshes = new List<GLTF.Schema.Mesh>(),
Expand Down Expand Up @@ -422,6 +423,13 @@ private NodeId ExportNode(Transform nodeTransform)
node.Name = nodeTransform.name;
}

//export camera attached to node
UnityEngine.Camera unityCamera = nodeTransform.GetComponent<UnityEngine.Camera>();
if (unityCamera != null)
{
node.Camera = ExportCamera(unityCamera);
}

node.SetUnityTransform(nodeTransform);

var id = new NodeId
Expand Down Expand Up @@ -460,6 +468,67 @@ private NodeId ExportNode(Transform nodeTransform)
return id;
}

private CameraId ExportCamera(UnityEngine.Camera unityCamera)
{
GLTF.Schema.Camera camera = new GLTF.Schema.Camera();
//name
camera.Name = unityCamera.name;

//type
bool isOrthographic = unityCamera.orthographic;
camera.Type = isOrthographic ? GLTF.Schema.CameraType.orthographic : GLTF.Schema.CameraType.perspective;
Matrix4x4 matrix = unityCamera.projectionMatrix;

//matrix properties: compute the fields from the projection matrix
if (isOrthographic)
{
GLTF.Schema.CameraOrthographic ortho = new GLTF.Schema.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
{
GLTF.Schema.CameraPerspective perspective = new GLTF.Schema.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
};

_root.Cameras.Add(camera);

return id;
}

private void FilterPrimitives(Transform transform, out GameObject[] primitives, out GameObject[] nonPrimitives)
{
var childCount = transform.childCount;
Expand Down

0 comments on commit d5d371a

Please sign in to comment.