Skip to content

Commit 4451dcb

Browse files
Merge pull request #852 from Zod-/CameraCache
Implement camera cache utility class
2 parents b356f6e + 419dd32 commit 4451dcb

File tree

41 files changed

+274
-194
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+274
-194
lines changed

Assets/HoloToolkit-Examples/GazeRuler/Scripts/LineManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public void AddPoint(GameObject LinePrefab, GameObject PointPrefab, GameObject T
3030
if (lastPoint != null && lastPoint.IsStart)
3131
{
3232
Vector3 centerPos = (lastPoint.Position + hitPoint) * 0.5f;
33+
Vector3 cameraPosition = CameraCache.Main.transform.position;
34+
Vector3 directionFromCamera = centerPos - cameraPosition;
3335

34-
Vector3 directionFromCamera = centerPos - Camera.main.transform.position;
35-
36-
float distanceA = Vector3.Distance(lastPoint.Position, Camera.main.transform.position);
37-
float distanceB = Vector3.Distance(hitPoint, Camera.main.transform.position);
36+
float distanceA = Vector3.Distance(lastPoint.Position, cameraPosition);
37+
float distanceB = Vector3.Distance(hitPoint, cameraPosition);
3838

3939
Debug.Log("A: " + distanceA + ",B: " + distanceB);
4040
Vector3 direction;

Assets/HoloToolkit-Examples/InteractiveElements/Scripts/GestureInteractive.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
using System.Collections;
5+
using HoloToolkit.Unity;
56
using HoloToolkit.Unity.InputModule;
67
using UnityEngine;
78
using Cursor = HoloToolkit.Unity.InputModule.Cursor;
@@ -138,8 +139,8 @@ private void HandleStartGesture()
138139
mCurrentInputSource = mTempInputSource;
139140
mCurrentInputSourceId = mTempInputSourceId;
140141

141-
mStartHeadPosition = Camera.main.transform.position;
142-
mStartHeadRay = Camera.main.transform.forward;
142+
mStartHeadPosition = CameraCache.Main.transform.position;
143+
mStartHeadRay = CameraCache.Main.transform.forward;
143144

144145
Vector3 handPosition;
145146
mCurrentInputSource.TryGetPosition(mCurrentInputSourceId, out handPosition);

Assets/HoloToolkit-Examples/InteractiveElements/Scripts/GestureInteractiveControl.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using UnityEngine;
55
using System.Collections;
6+
using HoloToolkit.Unity;
67

78
namespace HoloToolkit.Examples.InteractiveElements
89
{
@@ -86,7 +87,7 @@ public enum GestureDataType { Raw, Camera, Aligned }
8687
/// <summary>
8788
/// Camera reference
8889
/// </summary>
89-
protected Camera MainCamera { get { return Camera.main; } }
90+
protected Camera MainCamera { get { return CameraCache.Main; } }
9091

9192
/// <summary>
9293
/// Orientation based on the user facing direction.
@@ -385,7 +386,7 @@ private Vector3 RotateVectorOnY(Vector3 vector, Vector3 pivot, float angle)
385386
protected float FlipDistanceOnFacingControl(float toFlip, Vector3 controlPosition, Vector3 controlForward)
386387
{
387388
Vector3 cameraRay = StartHeadPosition - controlPosition;
388-
bool facingForward = Vector3.Dot(Camera.main.transform.forward, StartHeadRay) >= 0;
389+
bool facingForward = Vector3.Dot(MainCamera.transform.forward, StartHeadRay) >= 0;
389390
bool facingControl = Vector3.Dot(cameraRay, controlForward) >= 0;
390391

391392
if (!facingForward)

Assets/HoloToolkit-Examples/Medical/Scripts/TapToPlaceScene.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ void Update()
3131
{
3232
if (Placing)
3333
{
34-
var headPosition = Camera.main.transform.position;
35-
var forward = Camera.main.transform.forward;
34+
var cameraTransform = CameraCache.Main.transform;
35+
var headPosition = cameraTransform.position;
36+
var forward = cameraTransform.forward;
3637
var scenePosition = headPosition + DistanceFromHead * forward;
3738

38-
var facingRotation = Camera.main.transform.localRotation * this.initialRotation;
39+
var facingRotation = cameraTransform.localRotation * this.initialRotation;
3940
//only yaw
4041
facingRotation.x = 0;
4142
facingRotation.z = 0;

Assets/HoloToolkit-Examples/Prototyping/Scripts/MoveWithObject.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
using HoloToolkit.Unity.InputModule;
5-
using System.Collections;
6-
using System.Collections.Generic;
5+
using HoloToolkit.Unity;
76
using UnityEngine;
8-
using UnityEngine.Networking;
97

108
namespace HoloToolkit.Examples.Prototyping
119
{
@@ -85,7 +83,7 @@ private void Awake()
8583
{
8684
if (ReferenceObject == null)
8785
{
88-
ReferenceObject = Camera.main.gameObject;
86+
ReferenceObject = CameraCache.Main.gameObject;
8987
}
9088
}
9189

@@ -94,7 +92,7 @@ public void StartRunning()
9492
{
9593

9694
if (ReferenceObject == null)
97-
ReferenceObject = Camera.main.gameObject;
95+
ReferenceObject = CameraCache.Main.gameObject;
9896

9997
mOffsetDirection = this.transform.position - ReferenceObject.transform.position;
10098
mOffsetDistance = mOffsetDirection.magnitude;

Assets/HoloToolkit-Examples/Prototyping/Scripts/ScaleByDistance.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
using HoloToolkit.Unity.InputModule;
5-
using System.Collections;
6-
using System.Collections.Generic;
5+
using HoloToolkit.Unity;
76
using UnityEngine;
8-
using UnityEngine.Networking;
97

108
namespace HoloToolkit.Examples.Prototyping
119
{
@@ -57,7 +55,7 @@ void Start()
5755

5856
if (RefernceObject == null)
5957
{
60-
RefernceObject = Camera.main.gameObject;
58+
RefernceObject = CameraCache.Main.gameObject;
6159
}
6260
}
6361

Assets/HoloToolkit-Examples/SharingWithUNET/Scripts/PlayerController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4+
using HoloToolkit.Unity;
45
using UnityEngine;
56
using UnityEngine.Networking;
67
using HoloToolkit.Unity.InputModule;
@@ -99,8 +100,8 @@ private void Update()
99100

100101
// if we are the remote player then we need to update our worldPosition and then set our
101102
// local (to the shared world anchor) position for other clients to update our position in their world.
102-
transform.position = Camera.main.transform.position;
103-
transform.rotation = Camera.main.transform.rotation;
103+
transform.position = CameraCache.Main.transform.position;
104+
transform.rotation = CameraCache.Main.transform.rotation;
104105

105106
// Depending on if you are host or client, either setting the SyncVar (client)
106107
// or calling the Cmd (host) will update the other users in the session.

Assets/HoloToolkit-Examples/SpatialMappingComponent/DropCube.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4+
using HoloToolkit.Unity;
45
using UnityEngine;
56

67
#if UNITY_EDITOR || UNITY_WSA
@@ -32,10 +33,14 @@ private void OnDestroy()
3233

3334
private void Recognizer_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay)
3435
{
35-
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); // Create a cube
36-
cube.transform.localScale = Vector3.one * 0.3f; // Make the cube smaller
37-
cube.transform.position = Camera.main.transform.position + Camera.main.transform.forward; // Start to drop it in front of the camera
38-
cube.AddComponent<Rigidbody>(); // Apply physics
36+
// Create a cube
37+
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
38+
// Make the cube smaller
39+
cube.transform.localScale = Vector3.one * 0.3f;
40+
// Start to drop it in front of the camera
41+
cube.transform.position = CameraCache.Main.transform.position + CameraCache.Main.transform.forward;
42+
// Apply physics
43+
cube.AddComponent<Rigidbody>();
3944
}
4045
#endif
4146
}

Assets/HoloToolkit-Examples/SpatialUnderstanding/SpatialUnderstanding-FeatureOverview/Scripts/AppState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public string DetailsText
202202
private void Start()
203203
{
204204
// Default the scene & the HoloToolkit objects to the camera
205-
Vector3 sceneOrigin = Camera.main.transform.position;
205+
Vector3 sceneOrigin = CameraCache.Main.transform.position;
206206
Parent_Scene.transform.position = sceneOrigin;
207207
MappingObserver.SetObserverOrigin(sceneOrigin);
208208
InputManager.Instance.AddGlobalListener(gameObject);

Assets/HoloToolkit-Examples/SpatialUnderstanding/SpatialUnderstanding-FeatureOverview/Scripts/LevelSolver.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ public void Query_RandomInAir_AwayFromMe()
361361
new PlacementQuery(SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_RandomInAir(new Vector3(halfDimSize, halfDimSize, halfDimSize)),
362362
new List<SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule>() {
363363
SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule.Create_AwayFromOtherObjects(halfDimSize * 3.0f),
364-
SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule.Create_AwayFromPosition(Camera.main.transform.position, 2.5f),
364+
SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule.Create_AwayFromPosition(CameraCache.Main.transform.position, 2.5f),
365365
}));
366366
}
367367
PlaceObjectAsync("RandomInAir - AwayFromMe", placementQuery);
@@ -395,7 +395,7 @@ public void Query_OnFloor_AwayFromMe()
395395
placementQuery.Add(
396396
new PlacementQuery(SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnFloor(new Vector3(halfDimSize, halfDimSize, halfDimSize)),
397397
new List<SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule>() {
398-
SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule.Create_AwayFromPosition(Camera.main.transform.position, 2.0f),
398+
SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule.Create_AwayFromPosition(CameraCache.Main.transform.position, 2.0f),
399399
SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule.Create_AwayFromOtherObjects(halfDimSize * 3.0f),
400400
}));
401401
}
@@ -414,7 +414,7 @@ public void Query_OnFloor_NearMe()
414414
SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule.Create_AwayFromOtherObjects(halfDimSize * 3.0f),
415415
},
416416
new List<SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint>() {
417-
SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint.Create_NearPoint(Camera.main.transform.position, 0.5f, 2.0f)
417+
SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint.Create_NearPoint(CameraCache.Main.transform.position, 0.5f, 2.0f)
418418
}));
419419
}
420420
PlaceObjectAsync("OnFloor - NearMe", placementQuery);

0 commit comments

Comments
 (0)