Skip to content

Commit

Permalink
[CCK.Debugger] Fixes to game object visualizers.
Browse files Browse the repository at this point in the history
  • Loading branch information
kafeijao committed May 13, 2023
1 parent 3706e3a commit a2c5261
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Kafe.CCK.Debugger.Components.GameObjectVisualizers;

public class BoneVisualizer : GameObjectVisualizer {

protected override string GetName() => "[CCK.Debugger] Bone Visualizer";

public static BoneVisualizer Create(GameObject target, float scale) {

// Check if the component already exists, if so ignore the creation request but enable it
Expand All @@ -14,7 +16,7 @@ public static BoneVisualizer Create(GameObject target, float scale) {
}

visualizer = target.AddComponent<BoneVisualizer>();
visualizer.InitializeVisualizer(Resources.AssetBundleLoader.GetBoneVisualizerObject(), target, visualizer);
visualizer.InitializeVisualizer(Resources.AssetBundleLoader.GetBoneVisualizerObject(), target);
visualizer.SetupVisualizer(scale);

visualizer.enabled = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Kafe.CCK.Debugger.Components.GameObjectVisualizers;

public class EyeTargetVisualizer : GameObjectVisualizer {

protected override string GetName() => "[CCK.Debugger] Eye Target Visualizer";

private CVREyeControllerCandidate _candidate;
private static readonly Color DarkerColor = new Color(1f, 1f, 1f, 0.5f);
private static readonly Color BrighterColor = new Color(2f, 2f, 2f, 0.75f);
Expand All @@ -27,7 +29,7 @@ public static void Create(GameObject parentOfTarget, string guid, CVREyeControll
// If the component still doesn't exist, create it!
if (!target.TryGetComponent(out EyeTargetVisualizer visualizer)) {
visualizer = target.AddComponent<EyeTargetVisualizer>();
visualizer.InitializeVisualizer(Resources.AssetBundleLoader.GetBoneVisualizerObject(), target, visualizer);
visualizer.InitializeVisualizer(Resources.AssetBundleLoader.GetBoneVisualizerObject(), target);
}

visualizer._candidate = candidate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ public abstract class GameObjectVisualizer : MonoBehaviour {
protected static readonly Dictionary<GameObject, GameObjectVisualizer> VisualizersAll = new();
protected static readonly Dictionary<GameObject, GameObjectVisualizer> VisualizersActive = new();

private const string GameObjectName = "[CCK.Debugger] GameObject Visualizer";
protected abstract string GetName();

private GameObject _targetGo;
protected GameObject VisualizerGo;
protected Material Material;

internal void InitializeVisualizer(GameObject prefab, GameObject target, GameObjectVisualizer visualizer) {
internal void InitializeVisualizer(GameObject prefab, GameObject target) {

_targetGo = target;

// Instantiate the visualizer GameObject inside of the target
VisualizerGo = Instantiate(prefab, target.transform);
VisualizerGo.layer = target.layer;
VisualizerGo.name = GameObjectName;
VisualizerGo.name = GetName();

// Get the renderer and assign material
var renderer = VisualizerGo.GetComponent<MeshRenderer>();
Expand All @@ -48,7 +48,7 @@ internal void InitializeVisualizer(GameObject prefab, GameObject target, GameObj
VisualizersAll[_targetGo] = this;
}

private void UpdateState() {
protected void UpdateState() {
if (VisualizerGo == null || _targetGo == null) return;
VisualizerGo.SetActive(enabled);
if (enabled && !VisualizersActive.ContainsKey(_targetGo)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Kafe.CCK.Debugger.Components.GameObjectVisualizers;

public class LabeledVisualizer : GameObjectVisualizer {

protected override string GetName() => "[CCK.Debugger] Label Visualizer";

private string _label;
private RectTransform _labelTransform;
private TextMeshPro _labelTMP;
Expand All @@ -18,17 +20,13 @@ public static void Create(GameObject target, string label = "") {
// If the component still doesn't exist, create it!
if (!target.TryGetComponent(out LabeledVisualizer visualizer)) {
visualizer = target.AddComponent<LabeledVisualizer>();
visualizer.InitializeVisualizer(Resources.AssetBundleLoader.GetLabelVisualizerObject(), target, visualizer);
visualizer.InitializeVisualizer(Resources.AssetBundleLoader.GetLabelVisualizerObject(), target);
}

visualizer._label = label;
visualizer.SetupVisualizer();
}

private static Vector3 GetLocalScale(Transform target) {
return Misc.GetScaleFromAbsolute(target.transform, 5.0f) * PlayerSetup.Instance._avatarHeight;
}

protected override void SetupVisualizer(float scale = 1f) {

// VisualizerGo.layer = LayerMask.NameToLayer("UI Internal");
Expand All @@ -37,7 +35,6 @@ protected override void SetupVisualizer(float scale = 1f) {
var visualizerTransform = VisualizerGo.transform;
visualizerTransform.localPosition = Vector3.zero;
visualizerTransform.localRotation = Quaternion.identity;
visualizerTransform.localScale = GetLocalScale(visualizerTransform);

// Setup Label
_labelTransform = (RectTransform) visualizerTransform.Find("Label");
Expand All @@ -63,8 +60,7 @@ private void Update() {
_labelTransform.rotation = Quaternion.LookRotation(_labelTransform.position - cameraPos);

// Keep the scale updated
var visualizerTransform = VisualizerGo.transform;
visualizerTransform.localScale = GetLocalScale(visualizerTransform);
VisualizerGo.transform.localScale = Misc.GetScaleFromAbsolute(transform, 5.0f) * PlayerSetup.Instance._avatarHeight;
}

internal static bool HasLabeledVisualizersActive() {
Expand All @@ -80,6 +76,7 @@ internal static void ToggleLabeledVisualizers(bool isOn) {
foreach (var visualizer in VisualizersAll.Values.ToArray()) {
if (visualizer is LabeledVisualizer vis) {
vis.enabled = isOn;
vis.UpdateState();
}
}
}
Expand Down
77 changes: 36 additions & 41 deletions CCK.Debugger/Components/GameObjectVisualizers/TrackerVisualizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,60 @@ namespace Kafe.CCK.Debugger.Components.GameObjectVisualizers;

public class TrackerVisualizer : GameObjectVisualizer {

protected override string GetName() => "[CCK.Debugger] Tracker Visualizer";

public static void ToggleTrackers(bool isOn) {

DisableAllTrackerVisualizers();
// Toggle controller visualizers
IKSystem.Instance.leftHandModel.SetActive(isOn);
IKSystem.Instance.rightHandModel.SetActive(isOn);

// We already turned off all the trackers, so only proceed if we want to turn them on
if (!isOn) return;
var activeTrackers = new HashSet<TrackerVisualizer>();
foreach (var visualizer in VisualizersActive) {
if (visualizer.Value is TrackerVisualizer trackerVisualizer) {
activeTrackers.Add(trackerVisualizer);
}
}

var avatarHeight = PlayerSetup.Instance._avatarHeight;
var trackers = IKSystem.Instance.AllTrackingPoints.FindAll(t => t.isActive && t.isValid && t.suggestedRole != TrackingPoint.TrackingRole.Invalid);
// If it's turning on, enable the current trackers
if (isOn) {

// Add visualizers to the trackers
foreach (var tracker in trackers) {
var avatarHeight = PlayerSetup.Instance._avatarHeight;
var trackers = IKSystem.Instance.AllTrackingPoints.FindAll(t => t.isActive && t.isValid && t.suggestedRole != TrackingPoint.TrackingRole.Invalid);

// Ignore invalid trackers
if (tracker.assignedRole == TrackingPoint.TrackingRole.Invalid) continue;
// Iterate the visualizers for the trackers, creating if needed
foreach (var tracker in trackers) {

var target = tracker.referenceGameObject;
// Ignore invalid trackers
if (tracker.assignedRole == TrackingPoint.TrackingRole.Invalid) continue;

// Check if the component already exists, if so ignore the creation request but enable it
if (target.TryGetComponent(out TrackerVisualizer visualizer)) {
visualizer.SetupVisualizer(avatarHeight);
visualizer.enabled = true;
continue;
}
var target = tracker.referenceGameObject;

visualizer = target.AddComponent<TrackerVisualizer>();
visualizer.InitializeVisualizer(Resources.AssetBundleLoader.GetTrackerVisualizerObject(), target, visualizer);
visualizer.SetupVisualizer(avatarHeight);
visualizer.enabled = true;
}
// Create the component if doesn't exist
if (!target.TryGetComponent(out TrackerVisualizer visualizer)) {
visualizer = target.AddComponent<TrackerVisualizer>();
visualizer.InitializeVisualizer(Resources.AssetBundleLoader.GetTrackerVisualizerObject(), target);
}

// Enable controller visualizers
IKSystem.Instance.leftHandModel.SetActive(true);
IKSystem.Instance.rightHandModel.SetActive(true);
}
// Since we're enabling remove from the list to disable
if (activeTrackers.Contains(visualizer)) activeTrackers.Remove(visualizer);

private static void DisableAllTrackerVisualizers() {
var activeTrackers = new HashSet<TrackerVisualizer>();
foreach (var visualizer in VisualizersActive) {
if (visualizer.Value is TrackerVisualizer trackerVisualizer) {
activeTrackers.Add(trackerVisualizer);
visualizer.SetupVisualizer(avatarHeight);
visualizer.enabled = true;

visualizer.UpdateState();
}
}
foreach (var trackerVisualizer in activeTrackers) {

// Disable remaining trackers
foreach (var trackerVisualizer in activeTrackers.ToArray()) {
trackerVisualizer.enabled = false;
}
// Disable controller visualizers
IKSystem.Instance.leftHandModel.SetActive(false);
IKSystem.Instance.rightHandModel.SetActive(false);
}

internal static bool HasTrackersActive() {
if (IKSystem.Instance.leftHandModel.activeSelf || IKSystem.Instance.rightHandModel.activeSelf) return true;
foreach (var visualizer in VisualizersActive) {
if (visualizer.Value is TrackerVisualizer { enabled: true }) {
return true;
}
}
return false;
return VisualizersActive.Any(visualizer => visualizer.Value is TrackerVisualizer { enabled: true });
}

protected override void SetupVisualizer(float scale = 1f) {
Expand All @@ -74,7 +69,7 @@ protected override void SetupVisualizer(float scale = 1f) {
var visualizerTransform = VisualizerGo.transform;
visualizerTransform.localPosition = Vector3.zero;
visualizerTransform.localRotation = Quaternion.identity;
visualizerTransform.localScale = Misc.GetScaleFromAbsolute(transform);
visualizerTransform.localScale = Misc.GetScaleFromAbsolute(transform, scale) * 0.5f;
}

}

0 comments on commit a2c5261

Please sign in to comment.