Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added camera export feature, fixed some camera serialization bugs #158

Merged
merged 2 commits into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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