Skip to content

Commit e515e00

Browse files
Merge pull request #431 from Unity-Technologies/improvements/dylanu/scene-camera-settings-copy
EditorVR camera better represents what is seen in the game view
2 parents b250338 + 29e0cd1 commit e515e00

11 files changed

Lines changed: 323 additions & 241 deletions

File tree

Editor/EditingContextManagerEditor.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ void Awake()
2121
public override void OnInspectorGUI()
2222
{
2323
GUILayout.Label("Available Contexts");
24-
EditingContextManager.DoGUI(m_ContextNames, ref m_SelectedContextIndex, () => { m_Settings.defaultContextName = m_ContextNames[m_SelectedContextIndex]; });
24+
25+
m_SelectedContextIndex = EditorGUILayout.Popup(string.Empty, m_SelectedContextIndex, m_ContextNames);
26+
if (GUI.changed)
27+
{
28+
m_Settings.defaultContextName = m_ContextNames[m_SelectedContextIndex];
29+
GUIUtility.ExitGUI();
30+
}
2531

2632
EditorGUILayout.Space();
2733

Models/Vive/Materials/ViveController.mat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Material:
7575
- _SpecularHighlights: 1
7676
- _SrcBlend: 5
7777
- _UVSec: 0
78-
- _ZWrite: 0
78+
- _ZWrite: 1
7979
m_Colors:
8080
- _Color: {r: 1, g: 1, b: 1, a: 1}
8181
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}

Scripts/Core/Contexts/EditingContextManager.cs

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,32 @@ sealed class EditingContextManager : MonoBehaviour
2222

2323
const string k_LaunchOnExitPlaymode = "EditingContextManager.LaunchOnExitPlaymode";
2424

25+
IEditingContext m_CurrentContext;
26+
2527
internal static EditingContextManager s_Instance;
2628
static InputManager s_InputManager;
29+
static List<IEditingContext> s_AvailableContexts;
30+
static EditingContextManagerSettings s_Settings;
31+
static UnityObject s_DefaultContext;
2732

28-
EditingContextManagerSettings m_Settings = null;
29-
30-
List<IEditingContext> m_AvailableContexts;
31-
string[] m_ContextNames = null;
33+
string[] m_ContextNames;
3234
int m_SelectedContextIndex;
3335

34-
IEditingContext m_CurrentContext;
36+
Rect m_EditingContextPopupRect = new Rect(0, 0, 150, 20); // Y and X position will be set based on window size
37+
3538
readonly List<IEditingContext> m_PreviousContexts = new List<IEditingContext>();
3639

37-
internal IEditingContext defaultContext
40+
internal static IEditingContext defaultContext
3841
{
3942
get
4043
{
41-
var context = m_AvailableContexts.Find(c => c.Equals(m_DefaultContext)) ?? m_AvailableContexts.First();
44+
var availableContexts = GetAvailableEditingContexts();
45+
var context = availableContexts.Find(c => c.Equals(s_DefaultContext)) ?? availableContexts.First();
4246

43-
var defaultContextName = m_Settings.defaultContextName;
47+
var defaultContextName = settings.defaultContextName;
4448
if (!string.IsNullOrEmpty(defaultContextName))
4549
{
46-
var foundContext = m_AvailableContexts.Find(c => c.name == defaultContextName);
50+
var foundContext = availableContexts.Find(c => c.name == defaultContextName);
4751
if (foundContext != null)
4852
context = foundContext;
4953
}
@@ -52,7 +56,7 @@ internal IEditingContext defaultContext
5256
}
5357
set
5458
{
55-
m_Settings.defaultContextName = value.name;
59+
settings.defaultContextName = value.name;
5660
}
5761
}
5862

@@ -61,6 +65,17 @@ internal IEditingContext currentContext
6165
get { return m_CurrentContext; }
6266
}
6367

68+
static EditingContextManagerSettings settings
69+
{
70+
get
71+
{
72+
if (!s_Settings)
73+
s_Settings = LoadUserSettings();
74+
75+
return s_Settings;
76+
}
77+
}
78+
6479
static EditingContextManager()
6580
{
6681
VRView.viewEnabled += OnVRViewEnabled;
@@ -129,31 +144,34 @@ static void OnPlayModeStateChanged(PlayModeStateChange stateChange)
129144
}
130145
}
131146

132-
void OnEnable()
147+
void Awake()
133148
{
134-
m_Settings = LoadUserSettings();
135-
136-
ISetEditingContextMethods.getAvailableEditingContexts = GetAvailableEditingContexts;
137-
ISetEditingContextMethods.getPreviousEditingContexts = GetPreviousEditingContexts;
138-
ISetEditingContextMethods.setEditingContext = SetEditingContext;
139-
ISetEditingContextMethods.restorePreviousEditingContext = RestorePreviousContext;
149+
s_DefaultContext = m_DefaultContext;
140150

141151
var availableContexts = GetAvailableEditingContexts();
142152
m_ContextNames = availableContexts.Select(c => c.name).ToArray();
143153

144-
if (m_AvailableContexts.Count == 0)
154+
if (s_AvailableContexts.Count == 0)
145155
throw new Exception("You can't start EditorXR without at least one context. Try re-importing the package or use version control to restore the default context asset");
146156

147-
SetEditingContext(defaultContext);
148-
149-
if (m_AvailableContexts.Count > 1)
157+
if (s_AvailableContexts.Count > 1)
150158
VRView.afterOnGUI += OnVRViewGUI;
159+
}
160+
161+
void OnEnable()
162+
{
163+
ISetEditingContextMethods.getAvailableEditingContexts = GetAvailableEditingContexts;
164+
ISetEditingContextMethods.getPreviousEditingContexts = GetPreviousEditingContexts;
165+
ISetEditingContextMethods.setEditingContext = SetEditingContext;
166+
ISetEditingContextMethods.restorePreviousEditingContext = RestorePreviousContext;
151167

152168
// Force the window to repaint every tick, since we need live updating
153169
// This also allows scripts with [ExecuteInEditMode] to run
154170
EditorApplication.update += EditorApplication.QueuePlayerLoopUpdate;
155171

156172
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
173+
174+
SetEditingContext(defaultContext);
157175
}
158176

159177
void OnDisable()
@@ -170,40 +188,29 @@ void OnDisable()
170188
m_CurrentContext.Dispose();
171189
}
172190

173-
m_AvailableContexts = null;
191+
s_AvailableContexts = null;
192+
193+
SetEditingContext(null);
174194

175195
ISetEditingContextMethods.getAvailableEditingContexts = null;
176196
ISetEditingContextMethods.getPreviousEditingContexts = null;
177197
ISetEditingContextMethods.setEditingContext = null;
178198
ISetEditingContextMethods.restorePreviousEditingContext = null;
179199

180-
SaveUserSettings(m_Settings);
200+
SaveUserSettings(settings);
181201
}
182202
#endif
183203

184-
void OnVRViewGUI(EditorWindow window)
204+
void OnVRViewGUI(VRView view)
185205
{
186-
var view = (VRView)window;
187-
GUILayout.BeginArea(view.guiRect);
188-
{
189-
GUILayout.FlexibleSpace();
190-
GUILayout.BeginHorizontal();
191-
{
192-
DoGUI(m_ContextNames, ref m_SelectedContextIndex, () => SetEditingContext(m_AvailableContexts[m_SelectedContextIndex]));
193-
GUILayout.FlexibleSpace();
194-
}
195-
GUILayout.EndHorizontal();
196-
}
197-
GUILayout.EndArea();
198-
}
206+
var position = view.position;
207+
m_EditingContextPopupRect.y = position.height - m_EditingContextPopupRect.height;
208+
m_EditingContextPopupRect.x = position.width - m_EditingContextPopupRect.width;
199209

200-
internal static void DoGUI(string[] contextNames, ref int selectedContextIndex, Action callback = null)
201-
{
202-
selectedContextIndex = EditorGUILayout.Popup(string.Empty, selectedContextIndex, contextNames);
210+
m_SelectedContextIndex = EditorGUI.Popup(m_EditingContextPopupRect, string.Empty, m_SelectedContextIndex, m_ContextNames);
203211
if (GUI.changed)
204212
{
205-
if (callback != null)
206-
callback();
213+
SetEditingContext(s_AvailableContexts[m_SelectedContextIndex]);
207214
GUIUtility.ExitGUI();
208215
}
209216
}
@@ -216,13 +223,15 @@ internal void SetEditingContext(IEditingContext context)
216223
if (m_CurrentContext != null)
217224
{
218225
m_PreviousContexts.Insert(0, m_CurrentContext);
219-
m_CurrentContext.Dispose();
226+
227+
if (m_CurrentContext.instanceExists)
228+
m_CurrentContext.Dispose();
220229
}
221230

222231
context.Setup();
223232
m_CurrentContext = context;
224233

225-
m_SelectedContextIndex = m_AvailableContexts.IndexOf(context);
234+
m_SelectedContextIndex = s_AvailableContexts.IndexOf(context);
226235
}
227236

228237
internal void RestorePreviousContext()
@@ -254,12 +263,12 @@ internal static string[] GetEditingContextNames()
254263
return availableContexts.Select(c => c.name).ToArray();
255264
}
256265

257-
List<IEditingContext> GetAvailableEditingContexts()
266+
static List<IEditingContext> GetAvailableEditingContexts()
258267
{
259-
if (m_AvailableContexts == null)
260-
m_AvailableContexts = GetEditingContextAssets();
268+
if (s_AvailableContexts == null)
269+
s_AvailableContexts = GetEditingContextAssets();
261270

262-
return m_AvailableContexts;
271+
return s_AvailableContexts;
263272
}
264273

265274
List<IEditingContext> GetPreviousEditingContexts()

Scripts/Core/Contexts/EditorVR.asset

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ MonoBehaviour:
1212
m_Name: EditorVR
1313
m_EditorClassIdentifier:
1414
m_RenderScale: 1
15+
m_CopySceneCameraSettings: 1
1516
m_DefaultToolStack:
1617
- {fileID: 11500000, guid: 882053426a5f76d46b5505d9a20be912, type: 3}
1718
- {fileID: 11500000, guid: aab8fcc587f237c4cb48fb7bc8a59909, type: 3}

Scripts/Core/Contexts/EditorVRContext.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,22 @@ class EditorVRContext : ScriptableObject, IEditingContext
1313
[SerializeField]
1414
float m_RenderScale = 1f;
1515

16+
[SerializeField]
17+
bool m_CopyExistingCameraSettings = true;
18+
1619
[SerializeField]
1720
internal List<MonoScript> m_DefaultToolStack;
1821

1922
EditorVR m_Instance;
2023

24+
public bool copyExistingCameraSettings { get { return m_CopyExistingCameraSettings; } }
25+
26+
public bool instanceExists { get { return m_Instance != null; } }
27+
2128
public void Setup()
2229
{
2330
EditorVR.defaultTools = m_DefaultToolStack.Select(ms => ms.GetClass()).ToArray();
2431
m_Instance = ObjectUtils.CreateGameObjectWithComponent<EditorVR>();
25-
2632
XRSettings.eyeTextureResolutionScale = m_RenderScale;
2733
}
2834

Scripts/Core/VRView.cs

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#if UNITY_EDITOR
22
using System;
3-
using UnityEngine;
4-
using UnityEngine.Assertions;
53
using System.Collections;
6-
using UnityEditor.Experimental.EditorVR.Helpers;
74
using System.Reflection;
5+
using UnityEditor.Experimental.EditorVR.Helpers;
6+
using UnityEditor.Experimental.EditorVR.Utilities;
7+
using UnityEngine;
8+
using UnityEngine.Assertions;
89
using UnityEngine.XR;
910

1011
#if ENABLE_STEAMVR_INPUT
@@ -18,6 +19,10 @@ sealed class VRView : EditorWindow
1819
public const float HeadHeight = 1.7f;
1920
const string k_ShowDeviceView = "VRView.ShowDeviceView";
2021
const string k_UseCustomPreviewCamera = "VRView.UseCustomPreviewCamera";
22+
const string k_CameraName = "VRCamera";
23+
24+
static Camera s_ExistingSceneMainCamera;
25+
static bool s_ExistingSceneMainCameraEnabledState;
2126

2227
DrawCameraMode m_RenderMode = DrawCameraMode.Textured;
2328

@@ -54,7 +59,7 @@ public static Camera customPreviewCamera
5459
bool m_UseCustomPreviewCamera;
5560

5661
Rect m_ToggleDeviceViewRect = new Rect(0, 0, 0, 20); // Width will be set based on window size
57-
Rect m_PresentationCameraRect = new Rect(0, 0, 0, 20); // Y position and width will be set based on window size
62+
Rect m_PresentationCameraRect = new Rect(0, 0, 160, 20); // Y position and width will be set based on window size
5863

5964
public static Transform cameraRig
6065
{
@@ -111,8 +116,8 @@ public static Vector3 headCenteredOrigin
111116

112117
public static event Action viewEnabled;
113118
public static event Action viewDisabled;
114-
public static event Action<EditorWindow> beforeOnGUI;
115-
public static event Action<EditorWindow> afterOnGUI;
119+
public static event Action<VRView> beforeOnGUI;
120+
public static event Action<VRView> afterOnGUI;
116121
public static event Action<bool> hmdStatusChange;
117122

118123
public Rect guiRect { get; private set; }
@@ -134,18 +139,53 @@ public void OnEnable()
134139

135140
autoRepaintOnSceneChange = true;
136141
s_ActiveView = this;
142+
const float nearClipPlane = 0.01f;
143+
const float farClipPlane = 1000f;
144+
145+
s_ExistingSceneMainCamera = Camera.main;
146+
// TODO: Copy camera settings when changing contexts
147+
if (EditingContextManager.defaultContext.copyExistingCameraSettings && s_ExistingSceneMainCamera && s_ExistingSceneMainCamera.enabled)
148+
{
149+
GameObject cameraGO = EditorUtility.CreateGameObjectWithHideFlags(k_CameraName, HideFlags.HideAndDontSave);
150+
m_Camera = ObjectUtils.CopyComponent(s_ExistingSceneMainCamera, cameraGO);
151+
152+
if (m_Camera.nearClipPlane > nearClipPlane)
153+
{
154+
Debug.LogWarning("Copying settings from scene camera that is tagged 'MainCamera'." + Environment.NewLine +
155+
" Clipping issues may occur with NearClipPlane values is greater than " + nearClipPlane);
156+
157+
m_Camera.nearClipPlane = nearClipPlane;
158+
}
159+
160+
// TODO: Support multiple cameras
161+
if (m_Camera.clearFlags == CameraClearFlags.Nothing)
162+
m_Camera.clearFlags = CameraClearFlags.SolidColor;
163+
164+
m_Camera.stereoTargetEye = StereoTargetEyeMask.Both;
165+
// Force HDR on because of a bug in the mirror view
166+
m_Camera.allowHDR = true;
167+
}
168+
else
169+
{
170+
GameObject cameraGO = EditorUtility.CreateGameObjectWithHideFlags(k_CameraName, HideFlags.HideAndDontSave, typeof(Camera));
171+
m_Camera = cameraGO.GetComponent<Camera>();
172+
173+
m_Camera.nearClipPlane = nearClipPlane;
174+
m_Camera.farClipPlane = farClipPlane;
175+
}
176+
177+
if (s_ExistingSceneMainCamera)
178+
{
179+
s_ExistingSceneMainCameraEnabledState = s_ExistingSceneMainCamera.enabled;
180+
s_ExistingSceneMainCamera.enabled = false; // Disable existing MainCamera in the scene
181+
}
137182

138-
GameObject cameraGO = EditorUtility.CreateGameObjectWithHideFlags("VRCamera", HideFlags.HideAndDontSave, typeof(Camera));
139-
m_Camera = cameraGO.GetComponent<Camera>();
140-
m_Camera.useOcclusionCulling = false;
141183
m_Camera.enabled = false;
142184
m_Camera.cameraType = CameraType.VR;
143-
185+
m_Camera.useOcclusionCulling = false;
144186
GameObject rigGO = EditorUtility.CreateGameObjectWithHideFlags("VRCameraRig", HideFlags.HideAndDontSave, typeof(EditorMonoBehaviour));
145187
m_CameraRig = rigGO.transform;
146188
m_Camera.transform.parent = m_CameraRig;
147-
m_Camera.nearClipPlane = 0.01f;
148-
m_Camera.farClipPlane = 1000f;
149189
m_CameraRig.position = headCenteredOrigin;
150190
m_CameraRig.rotation = Quaternion.identity;
151191

@@ -186,6 +226,9 @@ public void OnDisable()
186226

187227
Assert.IsNotNull(s_ActiveView, "EditorXR should have an active view");
188228
s_ActiveView = null;
229+
230+
if (s_ExistingSceneMainCamera)
231+
s_ExistingSceneMainCamera.enabled = s_ExistingSceneMainCameraEnabledState;
189232
}
190233

191234
void UpdateCameraTransform()
@@ -279,7 +322,6 @@ void OnGUI()
279322

280323
m_ToggleDeviceViewRect.width = width;
281324
m_PresentationCameraRect.y = height - m_PresentationCameraRect.height;
282-
m_PresentationCameraRect.width = width;
283325

284326
if (GUI.Button(m_ToggleDeviceViewRect, "Toggle Device View", EditorStyles.toolbarButton))
285327
m_ShowDeviceView = !m_ShowDeviceView;

0 commit comments

Comments
 (0)