@@ -87,9 +87,24 @@ private static Object LoadModel (string fbxFileName)
87
87
}
88
88
}
89
89
90
+ FrameCameraOnModel ( modelGO ) ;
91
+
90
92
return modelGO as Object ;
91
93
}
92
94
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
+
93
108
private static void LoadLastSavedModel ( )
94
109
{
95
110
string fbxFileName = GetLastSavedFilePath ( ) ;
@@ -179,6 +194,22 @@ public static void LastSavedModel ()
179
194
// make turntable the active scene
180
195
UnityEngine . SceneManagement . SceneManager . SetActiveScene ( scene ) ;
181
196
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
+
182
213
// maximize game window and start playing
183
214
var gameWindow = GetMainGameView ( ) ;
184
215
if ( gameWindow ) {
0 commit comments