Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Fixed a few things with the new camera rig #405

Merged
merged 16 commits into from
Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ internal async Task TryRenderControllerModelAsync(Type controllerType, byte[] gl
if (useSystemDefaultModels && gltfObject != null)
{
controllerModel.name = $"{controllerType.Name}_Visualization";
controllerModel.transform.SetParent(MixedRealityToolkit.CameraSystem.CameraRig.BodyTransform);
controllerModel.transform.SetParent(MixedRealityToolkit.CameraSystem.CameraRig.PlayspaceTransform);
var visualizationType = visualizationProfile.GetControllerVisualizationTypeOverride(controllerType, ControllerHandedness) ??
visualizationProfile.ControllerVisualizationType;
controllerModel.AddComponent(visualizationType.Type);
Expand All @@ -268,7 +268,7 @@ internal async Task TryRenderControllerModelAsync(Type controllerType, byte[] gl
//If the model was a prefab
else
{
var controllerObject = UnityEngine.Object.Instantiate(controllerModel, MixedRealityToolkit.CameraSystem.CameraRig.BodyTransform);
var controllerObject = UnityEngine.Object.Instantiate(controllerModel, MixedRealityToolkit.CameraSystem.CameraRig.PlayspaceTransform);
controllerObject.name = $"{controllerType.Name}_Visualization";
Visualizer = controllerObject.GetComponent<IMixedRealityControllerVisualizer>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected virtual IMixedRealityPointer[] RequestPointers(SystemType controllerTy
if (((useSpecificType && pointerProfile.ControllerType.Type == controllerType.Type) || (!useSpecificType && pointerProfile.ControllerType.Type == null)) &&
(pointerProfile.Handedness == Handedness.Any || pointerProfile.Handedness == Handedness.Both || pointerProfile.Handedness == controllingHand))
{
var pointerObject = Object.Instantiate(pointerProfile.PointerPrefab, MixedRealityToolkit.CameraSystem.CameraRig.BodyTransform);
var pointerObject = Object.Instantiate(pointerProfile.PointerPrefab, MixedRealityToolkit.CameraSystem.CameraRig.PlayspaceTransform);
var pointer = pointerObject.GetComponent<IMixedRealityPointer>();

if (pointer != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public Transform PlayspaceTransform
return playspaceTransform;
}

if (MixedRealityToolkit.IsApplicationQuitting)
{
return null;
}

var playspaceTransformLookup = GameObject.Find(playspaceName);

playspaceTransform = playspaceTransformLookup == null
Expand Down Expand Up @@ -60,7 +65,7 @@ public Transform PlayspaceTransform
}

/// <inheritdoc />
public Transform CameraTransform => PlayerCamera.transform;
public Transform CameraTransform => PlayerCamera == null ? null : playerCamera.transform;

[SerializeField]
private Camera playerCamera = null;
Expand All @@ -75,6 +80,11 @@ public Camera PlayerCamera
return playerCamera;
}

if (MixedRealityToolkit.IsApplicationQuitting)
{
return null;
}

// Currently the XRTK only supports a single player/user
// So for now we will always reference the tagged MainCamera.
if (playerCamera == null)
Expand Down Expand Up @@ -122,6 +132,11 @@ public TrackedPoseDriver CameraPoseDriver
return cameraPoseDriver;
}

if (MixedRealityToolkit.IsApplicationQuitting)
{
return null;
}

cameraPoseDriver = PlayerCamera.gameObject.EnsureComponent<TrackedPoseDriver>();
cameraPoseDriver.UseRelativeTransform = true;
return cameraPoseDriver;
Expand All @@ -139,6 +154,16 @@ public Transform BodyTransform
{
get
{
if (bodyTransform != null)
{
return bodyTransform;
}

if (MixedRealityToolkit.IsApplicationQuitting)
{
return null;
}

if (bodyTransform == null)
{
bodyTransform = PlayspaceTransform.Find(playerBodyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ public float HeadHeight

headHeight = value;
CameraRig.CameraPoseDriver.originPose = new Pose(new Vector3(0f, headHeight, 0f), Quaternion.identity);
var bodyLocalPosition = CameraRig.BodyTransform.localPosition;
bodyLocalPosition.y = headHeight;
CameraRig.BodyTransform.localPosition = bodyLocalPosition;

SyncRigTransforms();
}
}

Expand Down Expand Up @@ -159,9 +158,7 @@ public override void LateUpdate()

if (!CameraRig.BodyTransform.localPosition.y.Equals(headHeight))
{
var cameraPosition = CameraRig.BodyTransform.localPosition;
cameraPosition.y = headHeight;
CameraRig.BodyTransform.localPosition = cameraPosition;
SyncRigTransforms();
}
}

Expand All @@ -170,13 +167,15 @@ public override void Disable()
{
base.Disable();

if (CameraRig == null) { return; }
var camera = CameraCache.Main;

if (CameraRig.CameraTransform != null)
if (camera != null)
{
CameraRig.CameraTransform.SetParent(null);
camera.transform.SetParent(null);
}

if (CameraRig == null) { return; }

if (CameraRig.PlayspaceTransform != null)
{
if (Application.isPlaying)
Expand All @@ -189,7 +188,8 @@ public override void Disable()
}
}

if (CameraRig is Component component)
if (CameraRig is Component component &&
component is IMixedRealityCameraRig)
{
if (Application.isPlaying)
{
Expand Down Expand Up @@ -223,5 +223,18 @@ private void ApplySettingsForTransparentDisplay()
CameraCache.Main.nearClipPlane = profile.NearClipPlaneTransparentDisplay;
QualitySettings.SetQualityLevel(profile.TransparentQualityLevel, false);
}

private void SyncRigTransforms()
StephenHodgson marked this conversation as resolved.
Show resolved Hide resolved
{
// Sync the body position to be the correct height offset.
var bodyLocalPosition = CameraRig.BodyTransform.localPosition;
bodyLocalPosition.y = headHeight;
CameraRig.BodyTransform.localPosition = bodyLocalPosition;

// Offset the playspace by the amount we've applied to the head height.
var playspacePosition = CameraRig.PlayspaceTransform.position;
playspacePosition.y -= headHeight;
CameraRig.PlayspaceTransform.position = playspacePosition;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public float WindowFollowSpeed
private void CreateVisualizations()
{
diagnosticVisualizationParent = new GameObject("Diagnostics");
diagnosticVisualizationParent.transform.parent = MixedRealityToolkit.CameraSystem.CameraRig.BodyTransform;
diagnosticVisualizationParent.transform.parent = MixedRealityToolkit.CameraSystem.CameraRig.PlayspaceTransform;
diagnosticVisualizationParent.SetActive(ShowDiagnostics);

// visual profiler settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private GameObject CreateSpatialAwarenessParent
get
{
var spatialAwarenessSystemObject = new GameObject("Spatial Awareness System");
spatialAwarenessSystemObject.transform.parent = MixedRealityToolkit.CameraSystem.CameraRig.BodyTransform;
spatialAwarenessSystemObject.transform.parent = MixedRealityToolkit.CameraSystem.CameraRig.PlayspaceTransform;
return spatialAwarenessSystemObject;
}
}
Expand Down
6 changes: 6 additions & 0 deletions XRTK-Core/Packages/com.xrtk.core/Utilities/CameraCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using UnityEngine;
using XRTK.Services;

namespace XRTK.Utilities
{
Expand All @@ -24,6 +25,11 @@ public static Camera Main

if (mainCamera == null)
{
if (MixedRealityToolkit.IsApplicationQuitting)
{
return null;
}

mainCamera = new GameObject("Main Camera", typeof(Camera), typeof(AudioListener)) { tag = "MainCamera" }.GetComponent<Camera>();
}

Expand Down