Skip to content

Commit

Permalink
feat(Caching): add object cache to reduce expensive find calls
Browse files Browse the repository at this point in the history
Rather than use `Find` `FindChild` and `FindObjectOfType` when looking
up required objects, the system now has a new Cache static object that
has scripts register with and then other scripts can use this cache
rather than making expensive find calls.
  • Loading branch information
thestonefox committed Sep 3, 2016
1 parent 79ff3dc commit d2e1d55
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 18 deletions.
8 changes: 6 additions & 2 deletions Assets/VRTK/SDK/SteamVR/VRTK_SDK_Bridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,15 @@ public static bool IsControllerRightHand(GameObject controller)

public static Transform GetHeadset()
{
if (cachedHeadset == null)
{
#if (UNITY_5_4_OR_NEWER)
return FindObjectOfType<SteamVR_Camera>().GetComponent<Transform>();
cachedHeadset = FindObjectOfType<SteamVR_Camera>().GetComponent<Transform>();
#else
return FindObjectOfType<SteamVR_GameView>().GetComponent<Transform>();
cachedHeadset = FindObjectOfType<SteamVR_GameView>().GetComponent<Transform>();
#endif
}
return cachedHeadset;
}

public static Transform GetHeadsetCamera()
Expand Down
10 changes: 10 additions & 0 deletions Assets/VRTK/Scripts/Abstractions/VRTK_DestinationMarker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ public virtual void SetHeadsetPositionCompensation(bool state)
headsetPositionCompensation = state;
}

protected virtual void OnEnable()
{
VRTK_ObjectCache.registeredDestinationMarkers.Add(this);
}

protected virtual void OnDisable()
{
VRTK_ObjectCache.registeredDestinationMarkers.Remove(this);
}

protected DestinationMarkerEventArgs SetDestinationMarkerEvent(float distance, Transform target, Vector3 position, uint controllerIndex)
{
DestinationMarkerEventArgs e;
Expand Down
6 changes: 4 additions & 2 deletions Assets/VRTK/Scripts/Abstractions/VRTK_WorldPointer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ protected virtual void Awake()
playAreaCursorBoundaries = new GameObject[4];
}

protected virtual void OnEnable()
protected override void OnEnable()
{
base.OnEnable();
controller.AliasPointerOn += new ControllerInteractionEventHandler(EnablePointerBeam);
controller.AliasPointerOff += new ControllerInteractionEventHandler(DisablePointerBeam);
controller.AliasPointerSet += new ControllerInteractionEventHandler(SetPointerDestination);
Expand All @@ -116,8 +117,9 @@ protected virtual void OnEnable()
pointerMaterial.color = pointerMissColor;
}

protected virtual void OnDisable()
protected override void OnDisable()
{
base.OnDisable();
DisableBeam();
destinationSetActive = false;
pointerContactDistance = 0f;
Expand Down
12 changes: 12 additions & 0 deletions Assets/VRTK/Scripts/Helper/VRTK_ObjectCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace VRTK
{
using UnityEngine;
using System.Collections.Generic;

public class VRTK_ObjectCache : MonoBehaviour
{
public static List<VRTK_BasicTeleport> registeredTeleporters = new List<VRTK_BasicTeleport>();
public static List<VRTK_DestinationMarker> registeredDestinationMarkers = new List<VRTK_DestinationMarker>();
public static VRTK_HeadsetCollision registeredHeadsetCollider = null;
}
}
12 changes: 12 additions & 0 deletions Assets/VRTK/Scripts/Helper/VRTK_ObjectCache.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions Assets/VRTK/Scripts/VRTK_BasicTeleport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace VRTK
{
using UnityEngine;
using System.Collections;

public delegate void TeleportEventHandler(object sender, DestinationMarkerEventArgs e);

Expand Down Expand Up @@ -68,14 +69,15 @@ protected virtual void OnEnable()
{
adjustYForTerrain = false;
enableTeleport = true;
InitDestinationMarkerListeners(true);
InitHeadsetCollisionListener(true);
StartCoroutine(InitListenersAtEndOfFrame());
VRTK_ObjectCache.registeredTeleporters.Add(this);
}

protected virtual void OnDisable()
{
InitDestinationMarkerListeners(false);
InitHeadsetCollisionListener(false);
VRTK_ObjectCache.registeredTeleporters.Remove(this);
}

protected void OnTeleporting(object sender, DestinationMarkerEventArgs e)
Expand Down Expand Up @@ -177,14 +179,20 @@ private void ReleaseBlink()
fadeInTime = 0f;
}

private IEnumerator InitListenersAtEndOfFrame()
{
yield return new WaitForEndOfFrame();
InitDestinationMarkerListeners(true);
InitHeadsetCollisionListener(true);
}

private void InitDestinationMarkerListeners(bool state)
{
var leftHand = VRTK_SDK_Bridge.GetControllerLeftHand();
var rightHand = VRTK_SDK_Bridge.GetControllerRightHand();
InitDestinationSetListener(leftHand, state);
InitDestinationSetListener(rightHand, state);

foreach (var destinationMarker in FindObjectsOfType<VRTK_DestinationMarker>())
foreach (var destinationMarker in VRTK_ObjectCache.registeredDestinationMarkers)
{
if (destinationMarker.gameObject != leftHand && destinationMarker.gameObject != rightHand)
{
Expand All @@ -195,7 +203,7 @@ private void InitDestinationMarkerListeners(bool state)

private void InitHeadsetCollisionListener(bool state)
{
var headset = FindObjectOfType<VRTK_HeadsetCollision>();
var headset = VRTK_ObjectCache.registeredHeadsetCollider;
if (headset)
{
if (state)
Expand Down
16 changes: 13 additions & 3 deletions Assets/VRTK/Scripts/VRTK_ControllerActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class VRTK_ControllerActions : MonoBehaviour
private uint controllerIndex;
private ushort maxHapticVibration = 3999;
private bool controllerHighlighted = false;

private Dictionary<GameObject, Material> storedMaterials;
private Dictionary<string, Transform> cachedElements;

public bool IsControllerVisible()
{
Expand Down Expand Up @@ -140,7 +140,7 @@ public void ToggleHighlightControllerElement(bool state, GameObject element, Col

public void ToggleHighlightTrigger(bool state, Color? highlight = null, float duration = 0f)
{
if(!state && controllerHighlighted)
if (!state && controllerHighlighted)
{
return;
}
Expand Down Expand Up @@ -212,6 +212,7 @@ private void Awake()
{
gameObject.layer = LayerMask.NameToLayer("Ignore Raycast");
storedMaterials = new Dictionary<GameObject, Material>();
cachedElements = new Dictionary<string, Transform>();
}

private void Update()
Expand Down Expand Up @@ -248,9 +249,18 @@ private IEnumerator CycleColor(Material material, Color startColor, Color endCol
}
}

private Transform GetElementTransform(string path)
{
if (!cachedElements.ContainsKey(path))
{
cachedElements[path] = transform.Find(path);
}
return cachedElements[path];
}

private void ToggleHighlightAlias(bool state, string transformPath, Color? highlight, float duration = 0f)
{
var element = transform.Find(transformPath);
var element = GetElementTransform(transformPath);
if (element)
{
ToggleHighlightControllerElement(state, element.gameObject, highlight, duration);
Expand Down
2 changes: 2 additions & 0 deletions Assets/VRTK/Scripts/VRTK_HeadsetCollision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public virtual bool IsColliding()

private void OnEnable()
{
VRTK_ObjectCache.registeredHeadsetCollider = this;
headsetColliding = false;
Utilities.SetPlayerObject(gameObject, VRTK_PlayerObject.ObjectTypes.Headset);

Expand All @@ -56,6 +57,7 @@ private void OnEnable()

private void OnDisable()
{
VRTK_ObjectCache.registeredHeadsetCollider = null;
headsetColliding = false;
Destroy(gameObject.GetComponent<BoxCollider>());
Destroy(gameObject.GetComponent<Rigidbody>());
Expand Down
18 changes: 12 additions & 6 deletions Assets/VRTK/Scripts/VRTK_InteractableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,7 @@ public void SetGrabbedSnapHandle(Transform handle)

public void RegisterTeleporters()
{
foreach (var teleporter in FindObjectsOfType<VRTK_BasicTeleport>())
{
teleporter.Teleporting += new TeleportEventHandler(OnTeleporting);
teleporter.Teleported += new TeleportEventHandler(OnTeleported);
}
StartCoroutine(RegisterTeleportersAtEndOfFrame());
}

protected virtual void Awake()
Expand Down Expand Up @@ -460,7 +456,7 @@ protected virtual void OnEnable()

protected virtual void OnDisable()
{
foreach (var teleporter in FindObjectsOfType<VRTK_BasicTeleport>())
foreach (var teleporter in VRTK_ObjectCache.registeredTeleporters)
{
teleporter.Teleporting -= new TeleportEventHandler(OnTeleporting);
teleporter.Teleported -= new TeleportEventHandler(OnTeleported);
Expand Down Expand Up @@ -704,6 +700,16 @@ private void OnTeleported(object sender, DestinationMarkerEventArgs e)
}
}

private IEnumerator RegisterTeleportersAtEndOfFrame()
{
yield return new WaitForEndOfFrame();
foreach (var teleporter in VRTK_ObjectCache.registeredTeleporters)
{
teleporter.Teleporting += new TeleportEventHandler(OnTeleporting);
teleporter.Teleported += new TeleportEventHandler(OnTeleported);
}
}

private IEnumerator StopUsingOnControllerChange(GameObject previousController)
{
yield return new WaitForEndOfFrame();
Expand Down

0 comments on commit d2e1d55

Please sign in to comment.