Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitGeslain committed Apr 22, 2024
1 parent 892db5b commit fcce066
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,16 @@ class SteerInDirection : WorldRedirectionStrategy {

public class APFP2R : WorldRedirectionStrategy {

List<Collider> colliders;
public List<Collider> colliders;

private Func<Vector3, float> RepulsiveFunction() {
if (colliders == null)
colliders = GameObject.FindGameObjectsWithTag("Obstacle").Select(o => o.GetComponent<Collider>()).ToList();
return MathTools.RepulsivePotential3D(colliders);
public APFP2R() : base() {
colliders = GameObject.FindGameObjectsWithTag("Obstacle").Select(o => o.GetComponent<Collider>()).ToList();
}

public override Vector3 SteerTo(Scene scene) => ComputeGradient(scene);

private Vector2 ComputeGradient(Scene scene) => Vector3.ProjectOnPlane(MathTools.Gradient3(
RepulsiveFunction(),
MathTools.RepulsivePotential3D(colliders),
MathTools.ProjectToHorizontalPlane(scene.physicalHead.position)
), Vector3.up);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private void UpdateTechnique() {
_ => null
};


if (techniqueInstance is null)
Debug.LogError("Error Unknown Redirection technique.");

Expand All @@ -55,7 +56,7 @@ private void UpdateTechnique() {
/// Start function called once when the game is starting. This function calls updateTechnique() to instantiate the technique class and
/// initializes the previous head positions.
/// </summary>
private void Start() {
private void OnEnable() {
UpdateTechnique();
previousTechnique = Technique;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
using Meta.WitAi;
using Oculus.Interaction.Input;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;

using UnityEngine;

using VHToolkit;
using VHToolkit.Redirection;
using static OVRPlugin;
using VHToolkit.Redirection.WorldRedirection;

public class GradientVisuals : MonoBehaviour
{
Expand Down Expand Up @@ -53,6 +48,8 @@ public class GradientVisuals : MonoBehaviour
private List<GameObject> vfVectors;


[SerializeField] private WorldRedirection script;


// FUNCS & SUBS //

Expand Down Expand Up @@ -98,12 +95,12 @@ private void Checks()

if (vfToggled)
{
if (vectorsEnabled) { vectorsEnabled = InitVectorsField(); }
else { CloseVectorsField(); }
if (vectorsEnabled) { vectorsEnabled = InitVectorField(); }
else { CloseVectorField(); }
}
else if (updateVisuals && vfEnabledCurrentState)
{
vectorsEnabled = InitVectorsField();
vectorsEnabled = InitVectorField();
}

// save state
Expand All @@ -114,63 +111,42 @@ private void Checks()
/// <summary>
/// Updates the repulsive function used to calculate gradient.
/// </summary>
private void UpdateRepulsiveFunc()
{
private void UpdateRepulsiveFunc() {
// cancel invokes while repulsive function update is ongoing
CancelInvoke(nameof(UpdateHeatmap));
CancelInvoke(nameof(UpdateVectorsField));
CancelInvoke(nameof(UpdateVectorField));

// get gameobjects with obstacle tag
obstaclesCollider = new(GameObject.FindGameObjectsWithTag(obstacleTag).Where(o => o.GetComponent<Collider>() != null).Select(o => o.GetComponent<Collider>()));

//var allColliderBounds = FindObjectsOfType<Collider>().Select(o => o.bounds).ToArray();
//max = allColliderBounds.Select(b => b.max).Aggregate(Pointwise(Mathf.Max));
//min = allColliderBounds.Select(b => b.min).Aggregate(Pointwise(Mathf.Min));
//min.y = max.y = 0f;
obstaclesCollider = ((APFP2R)script.strategyInstance).colliders;

// Compute the bounding box and repulsive function for all colliders
if (obstaclesCollider.Any())
{
static Func<Vector3, Vector3, Vector3> Pointwise(Func<float, float, float> f) =>
(a, b) => new(f(a.x, b.x), f(a.y, b.y), f(a.z, b.z));

repulsiveFunction = MathTools.RepulsivePotential3D(obstaclesCollider);

var allColliderBounds = FindObjectsOfType<Collider>().Select(o => o.bounds.max).ToArray();
max = allColliderBounds.Aggregate(Pointwise(Mathf.Max));
min = allColliderBounds.Aggregate(Pointwise(Mathf.Min));
// var allColliderBounds = FindObjectsOfType<Collider>().Select(o => o.bounds.max).ToArray();
max = FindObjectsOfType<Collider>().Select(o => o.bounds.max).ToArray().Aggregate(Pointwise(Mathf.Max));
min = FindObjectsOfType<Collider>().Select(o => o.bounds.min).ToArray().Aggregate(Pointwise(Mathf.Min));
min.y = max.y = 0f;

width = max.x - min.x;
depth = max.z - min.z;

// Debug.Log($"Visuals init : X[{min.x}:{max.x}] Z[{min.z}:{max.z}]");
// Debug.Log($"Visuals init : width:{width} height:{depth}");

}
else
{
repulsiveFunction = null;
// Debug.Log("No colliders detected, can't generate visuals.");
} else {
repulsiveFunction = (x) => 0;
}
}

// TODO : change to SceneUpdate and call it manually when scene is changed
/// <summary>
/// Checks for changes with gameobjects tagged as obstacles, and also for changes is scene area size.
/// </summary>
/// <returns>True if any change found</returns>
private bool CheckForSceneUpdate()
{
if (GameObject.FindGameObjectsWithTag(obstacleTag).Where(o => o.GetComponent<Collider>() != null).Count() != obstaclesCollider.Count) return true;

foreach (var objCol in FindObjectsOfType<Collider>())
{
if (objCol.bounds.max.x > max.x) return true;
if (objCol.bounds.min.x < min.x) return true;
if (objCol.bounds.max.z > max.z) return true;
if (objCol.bounds.min.z < min.z) return true;
}

return false;
return true;
}

#endregion
Expand All @@ -179,18 +155,13 @@ private bool CheckForSceneUpdate()

#region "heatmap"

//TODO : always returns true, should be void?
/// <summary>
/// Init heatmap quad with shahder, and invokes update func.
/// </summary>
/// <returns>True if init ended sucessfully</returns>
private bool InitHeatmap()
{
if (repulsiveFunction == null)
{
Console.Write("APF visuals : cannot init heatmap, no repulsive function (is there any obstacle with collider ?).");
CloseHeatmap();
return false;
}

if (hmEnabledCurrentState)
{
Expand Down Expand Up @@ -229,9 +200,8 @@ private void UpdateHeatmap()
{

Vector3 position = new Vector3(min.x + (x * hmStepX) + (hmStepX / 2), 0, min.z + (z * hmStepZ) + (hmStepZ / 2));
Vector2 gradient = MathTools.Gradient3(repulsiveFunction, position);

float hmValue = gradient.magnitude;
float hmValue = ((Vector2)MathTools.Gradient3(repulsiveFunction, position)).magnitude;

hmDensityTable[x + (z * heatmapMeshFineness)] = hmValue;
// Debug.Log($"{hmStepX * x:0.0} - {hmStepZ * z:0.0} : {hmValue}");
Expand Down Expand Up @@ -265,18 +235,18 @@ private void CloseHeatmap()
/// Init vectors field, and invokes update func.
/// </summary>
/// <returns>True if init ended sucessfully</returns>
private bool InitVectorsField()
private bool InitVectorField()
{
if (repulsiveFunction == null)
{
Console.Write("APF visuals : cannot init vectors field, no repulsive function (is there any obstacle with collider ?).");
CloseVectorsField();
Debug.Log("APF visuals : cannot init vectors field, no repulsive function (is there any obstacle with collider ?).");
CloseVectorField();
return false;
}

if (vfEnabledCurrentState)
{
CloseVectorsField();
CloseVectorField();
}

int i = 0;
Expand All @@ -295,11 +265,9 @@ private bool InitVectorsField()

if (!float.IsNaN(gradient.x) && !float.IsNaN(gradient.y))
{
float angleRadian = Mathf.Atan2(gradient.y, gradient.x);
float angleEnDegres = angleRadian * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.Euler(-90, 0, angleEnDegres);
float angleInDegrees = Mathf.Atan2(gradient.y, gradient.x) * Mathf.Rad2Deg;

vectorObj.transform.rotation = rotation;
vectorObj.transform.rotation = Quaternion.Euler(-90, 0, angleInDegrees);
vectorObj.GetComponent<SpriteRenderer>().sprite = vfArrow;
}

Expand All @@ -314,16 +282,16 @@ private bool InitVectorsField()

}

UpdateVectorsField();
InvokeRepeating(nameof(UpdateVectorsField), 1f, refreshRate);
UpdateVectorField();
InvokeRepeating(nameof(UpdateVectorField), 1f, refreshRate);

return true;
}

/// <summary>
/// Vectors field update method.
/// </summary>
private void UpdateVectorsField()
private void UpdateVectorField()
{
if (!vectorsEnabled) { return; }

Expand All @@ -344,9 +312,9 @@ private void UpdateVectorsField()
/// <summary>
/// Handles the ending of the vector field.
/// </summary>
private void CloseVectorsField()
private void CloseVectorField()
{
CancelInvoke(nameof(UpdateVectorsField));
CancelInvoke(nameof(UpdateVectorField));

//for (int i = vfVectors.Count - 1; i >= 0; i--)
//{
Expand Down

0 comments on commit fcce066

Please sign in to comment.