-
Notifications
You must be signed in to change notification settings - Fork 494
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
Added camera export #153
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ protected struct PrimKey | |
public GLTFSceneExporter(Transform[] rootTransforms) | ||
{ | ||
_rootTransforms = rootTransforms; | ||
|
||
_root = new GLTFRoot{ | ||
Accessors = new List<Accessor>(), | ||
Asset = new Asset { | ||
|
@@ -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>(); | ||
|
@@ -97,7 +99,8 @@ public void SaveGLTFandBin(string path, string fileName) | |
binFile.Close(); | ||
#endif | ||
|
||
foreach (var image in _images) | ||
GL.sRGBWrite = true; | ||
foreach (var image in _images) | ||
{ | ||
Debug.Log(image.name); | ||
var renderTexture = RenderTexture.GetTemporary(image.width, image.height); | ||
|
@@ -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) | ||
{ | ||
|
@@ -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? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comment. I think we should export all cameras There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -179,6 +191,49 @@ private NodeId ExportNode(Transform nodeTransform) | |
return id; | ||
} | ||
|
||
private CameraId exportCamera(UnityEngine.Camera unityCamera) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit- spaces
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!!