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 #153

Closed
wants to merge 2 commits into from
Closed
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
67 changes: 61 additions & 6 deletions UnityGLTF/Assets/UnityGLTF/Scripts/GLTFSceneExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected struct PrimKey
public GLTFSceneExporter(Transform[] rootTransforms)
{
_rootTransforms = rootTransforms;

_root = new GLTFRoot{
Accessors = new List<Accessor>(),
Asset = new Asset {
Expand All @@ -49,6 +50,7 @@ public GLTFSceneExporter(Transform[] rootTransforms)
Samplers = new List<Sampler>(),
Scenes = new List<Scene>(),
Textures = new List<GLTF.Schema.Texture>(),
Cameras = new List<GLTF.Schema.Camera>(),
};

_images = new List<Texture2D>();
Expand Down Expand Up @@ -97,7 +99,8 @@ public void SaveGLTFandBin(string path, string fileName)
binFile.Close();
#endif

foreach (var image in _images)
GL.sRGBWrite = true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit- spaces

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't commit your changes from your other PR as part of this one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the rookie mistake of committing the other PR into master. Will fix this. Thanks for pointing this out!!

foreach (var image in _images)
{
Debug.Log(image.name);
var renderTexture = RenderTexture.GetTemporary(image.width, image.height);
Expand All @@ -108,7 +111,8 @@ public void SaveGLTFandBin(string path, string fileName)
exportTexture.Apply();
File.WriteAllBytes(Path.Combine(path, image.name + ".png"), exportTexture.EncodeToPNG());
}
}
GL.sRGBWrite = true;
}

private SceneId ExportScene(string name, Transform[] rootObjTransforms)
{
Expand Down Expand Up @@ -142,6 +146,14 @@ private NodeId ExportNode(Transform nodeTransform)
node.Name = nodeTransform.name;
}

//export camera
UnityEngine.Camera unityCamera = nodeTransform.GetComponent<UnityEngine.Camera>();
if (unityCamera != null)
{
//should we only export enabled cameras?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment. I think we should export all cameras

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it!

node.Camera = exportCamera(unityCamera);
}

node.SetUnityTransform(nodeTransform);

var id = new NodeId {
Expand Down Expand Up @@ -179,6 +191,49 @@ private NodeId ExportNode(Transform nodeTransform)
return id;
}

private CameraId exportCamera(UnityEngine.Camera unityCamera)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalize ExportCamera

{
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;

//matrix
if (isOrthographic)
{
GLTF.Schema.CameraOrthographic ortho = new GLTF.Schema.CameraOrthographic();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Unity doesn't directly show the properties, and since scripts can still modify properties of the matrix, I recommend just getting values of the matrix directly:
https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#projection-matrices

May as well do this for both perspective and orthographic. This is likely the solution for camera import as well (which you should feel free to tackle :D) #2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Sounds like a safer way to do it :)

ortho.ZFar = unityCamera.farClipPlane;
ortho.ZNear = unityCamera.nearClipPlane;
//x and y magnifications: Unity uses a combination of viewport size and origin, as well as "orthographic size"
//unsure how to combine these to get the xMag and yMag
ortho.XMag = unityCamera.orthographicSize;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the raw matrix and xMag is not equal to orthographic size. yMag is though. xMag is being set based on viewport variables. Like I said above, we should just calculate the values specified from the standard based on the camera.perspectiveMatrix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, your idea is definitely the safest!! Thanks for this.

ortho.YMag = unityCamera.orthographicSize;

camera.Orthographic = ortho;
} else
{
GLTF.Schema.CameraPerspective perspective = new GLTF.Schema.CameraPerspective();
perspective.ZFar = unityCamera.farClipPlane;
perspective.ZNear = unityCamera.nearClipPlane;
perspective.YFov = Mathf.Deg2Rad * unityCamera.fieldOfView;
perspective.AspectRatio = unityCamera.aspect;
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 Expand Up @@ -439,10 +494,10 @@ private MaterialId ExportMaterial(UnityEngine.Material materialObj)
switch (materialObj.shader.name)
{
case "Standard":
case "GLTF/GLTFStandard":
material.PbrMetallicRoughness = ExportPBRMetallicRoughness(materialObj);
break;
case "GLTF/GLTFConstant":
case "GLTF/PbrMetallicRoughness":
material.PbrMetallicRoughness = ExportPBRMetallicRoughness(materialObj);
break;
case "GLTF/GLTFConstant":
material.CommonConstant = ExportCommonConstant(materialObj);
break;
}
Expand Down