Skip to content

Commit

Permalink
Update initialization and shutdown code in EditorXRViewerModule to fi…
Browse files Browse the repository at this point in the history
…x `IsMatrixValid` assertion bug;

The issue was that InitializeCamera was not called before ViewerScale is deserialized, causing near/far planes to be set to 0, causing rendering errors;
Other initialization changes are code that went into Load/UnloadModule but should have been in Initialize/Shutdown
  • Loading branch information
mtschoen-unity committed Jun 6, 2019
1 parent 7716a31 commit 43ce8ee
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Scripts/Core/EditorXRInterfacesModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class EditorXRInterfacesModule : IInitializableModule
event Action<object, object> connectInterfaces;
event Action<object, object> disconnectInterfaces;

public int initializationOrder { get { return -1; } }
public int initializationOrder { get { return -4; } }
public int shutdownOrder { get { return 0; } }

public void LoadModule()
Expand Down
46 changes: 25 additions & 21 deletions Scripts/Core/EditorXRViewerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public float cameraRigScale
PlayerBody m_PlayerBody;
GameObject m_PlayerFloor;

bool m_Initialized;
float m_OriginalNearClipPlane;
float m_OriginalFarClipPlane;
readonly List<GameObject> m_VRPlayerObjects = new List<GameObject>();
Expand All @@ -80,7 +81,7 @@ public float cameraRigScale
SpatialHashModule m_SpatialHashModule;
IntersectionModule m_IntersectionModule;

public int initializationOrder { get { return 0; } }
public int initializationOrder { get { return -2; } }
public int shutdownOrder { get { return 0; } }

internal IPreviewCamera customPreviewCamera { get; private set; }
Expand Down Expand Up @@ -117,31 +118,14 @@ public void LoadModule()
VRView.hmdStatusChange += OnHMDStatusChange;
#endif

preserveCameraRig = true;

Shader.SetGlobalFloat(k_WorldScaleProperty, 1);

preserveCameraRig = EditorVR.preserveLayout;
}

public void UnloadModule()
{
#if UNITY_EDITOR
VRView.hmdStatusChange -= OnHMDStatusChange;
#endif

var cameraRig = CameraUtils.GetCameraRig();
if (cameraRig)
cameraRig.transform.parent = null;

if (m_PlayerBody)
UnityObjectUtils.Destroy(m_PlayerBody.gameObject);

if (m_PlayerFloor)
UnityObjectUtils.Destroy(m_PlayerFloor);

if (customPreviewCamera != null && customPreviewCamera as MonoBehaviour != null)
UnityObjectUtils.Destroy(((MonoBehaviour)customPreviewCamera).gameObject);
}

public void ConnectInterface(object target, object userData = null)
Expand Down Expand Up @@ -252,6 +236,7 @@ internal void UpdateCamera()
internal void AddPlayerFloor()
{
m_PlayerFloor = EditorXRUtils.Instantiate(m_PlayerFloorPrefab, CameraUtils.GetCameraRig().transform, false);
m_VRPlayerObjects.Add(m_PlayerFloor);
}

internal void AddPlayerModel()
Expand Down Expand Up @@ -380,27 +365,46 @@ void SetViewerScale(float scale)
{
var camera = CameraUtils.GetMainCamera();
CameraUtils.GetCameraRig().localScale = Vector3.one * scale;
camera.nearClipPlane = m_OriginalNearClipPlane * scale;
camera.farClipPlane = m_OriginalFarClipPlane * scale;
Shader.SetGlobalFloat(k_WorldScaleProperty, 1f / scale);
if (m_Initialized)
{
camera.nearClipPlane = m_OriginalNearClipPlane * scale;
camera.farClipPlane = m_OriginalFarClipPlane * scale;
}
}

public void Initialize()
{
preserveCameraRig = EditorVR.preserveLayout;

m_VRPlayerObjects.Clear();
InitializeCamera();
AddPlayerModel();
AddPlayerFloor();
m_Initialized = true;
}

public void Shutdown()
{
m_Initialized = true;
m_OriginalNearClipPlane = 0;
m_OriginalFarClipPlane = 0;
hmdReady = false;
preserveCameraRig = false;

foreach (var playerObject in m_VRPlayerObjects)
{
UnityObjectUtils.Destroy(playerObject);
}

UnityObjectUtils.Destroy(m_PlayerFloor);
m_VRPlayerObjects.Clear();

var cameraRig = CameraUtils.GetCameraRig();
if (cameraRig)
cameraRig.transform.parent = null;

if (customPreviewCamera != null && customPreviewCamera as MonoBehaviour != null)
UnityObjectUtils.Destroy(((MonoBehaviour)customPreviewCamera).gameObject);
}

public void OnBehaviorAwake() { }
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Modules/SerializedPreferencesModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ internal static string serializedPreferences
set { EditorPrefs.SetString(SerializedPreferencesKey, value); }
}

public int initializationOrder { get { return -2; } }
public int initializationOrder { get { return -1; } }
public int shutdownOrder { get { return 1; } }

public void Initialize()
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Modules/SpatialHashModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ sealed class SpatialHashModule : MonoBehaviour, IInitializableModule

public Func<GameObject, bool> shouldExcludeObject { private get; set; }

public int initializationOrder { get { return -1; } }
public int initializationOrder { get { return -3; } }
public int shutdownOrder { get { return 0; } }

public void LoadModule()
Expand Down

0 comments on commit 43ce8ee

Please sign in to comment.