Skip to content

Commit c184e12

Browse files
authored
Merge pull request #86 from Unity-Technologies/UNI-23252-review-autofocus
UNI-23252 frame camera onto loaded model
2 parents a985e10 + ca3fe88 commit c184e12

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Assets/FbxExporters/Editor/ReviewLastSavedModel.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,24 @@ private static Object LoadModel (string fbxFileName)
8787
}
8888
}
8989

90+
FrameCameraOnModel (modelGO);
91+
9092
return modelGO as Object;
9193
}
9294

95+
private static void FrameCameraOnModel(GameObject modelGO)
96+
{
97+
// Set so camera frames model
98+
// Note: this code assumes the model is at 0,0,0
99+
Vector3 boundsSize = modelGO.GetComponent<Renderer>().bounds.size;
100+
float distance = Mathf.Max(boundsSize.x, boundsSize.y, boundsSize.z);
101+
distance /= (2.0f * Mathf.Tan(0.5f * Camera.main.fieldOfView * Mathf.Deg2Rad));
102+
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, -distance * 2.0f);
103+
104+
// rotate camera towards model
105+
Camera.main.transform.LookAt(modelGO.transform.position);
106+
}
107+
93108
private static void LoadLastSavedModel ()
94109
{
95110
string fbxFileName = GetLastSavedFilePath ();
@@ -179,6 +194,22 @@ public static void LastSavedModel ()
179194
// make turntable the active scene
180195
UnityEngine.SceneManagement.SceneManager.SetActiveScene (scene);
181196

197+
// create camera and light if none
198+
if (Camera.main == null) {
199+
GameObject camera = new GameObject ("MainCamera");
200+
camera.AddComponent<Camera> ();
201+
camera.tag = "MainCamera";
202+
}
203+
204+
if(!Object.FindObjectOfType<Light>()){
205+
GameObject light = new GameObject ("Light");
206+
light.transform.localEulerAngles = new Vector3 (50, -30, 0);
207+
Light lightComp = light.AddComponent<Light> ();
208+
lightComp.type = LightType.Directional;
209+
lightComp.intensity = 1;
210+
lightComp.shadows = LightShadows.Soft;
211+
}
212+
182213
// maximize game window and start playing
183214
var gameWindow = GetMainGameView();
184215
if (gameWindow) {

0 commit comments

Comments
 (0)