Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -121,6 +121,7 @@ public void Initialize(string ID = "Desktop Eye", string subSystems = "BasisDesk
BasisPointRaycaster.UseWorldPosition = false;
}
BasisVirtualSpine.Initialize();
BasisLocalPlayer.AfterSimulateOnRender.AddAction(100, DoRenderRaycast);
HasEyeEvents = true;
}
LockEye();
Expand Down Expand Up @@ -174,6 +175,7 @@ private void OnCursorStateChange(CursorLockMode cursor, bool newCursorVisible)
{
BasisLocalPlayer.OnLocalAvatarChanged -= PlayerInitialized;
BasisCursorManagement.OnCursorStateChange -= OnCursorStateChange;
BasisLocalPlayer.AfterSimulateOnRender.RemoveAction(100, DoRenderRaycast);
HasEyeEvents = false;
BasisVirtualSpine.DeInitialize();
}
Expand Down Expand Up @@ -313,9 +315,18 @@ public override void LateDoPollData()
if (IsComputingRaycast)
{
ComputeRaycastDirection(ScaledDeviceCoord.position, ScaledDeviceCoord.rotation, Quaternion.identity);
UpdateInputEvents();
UpdateInputEvents(HasPlayerControlSupport: true, hasPlayerRaycastSupport: false); // control raycast
}
}
private void DoRenderRaycast()
{
if (!hasRoleAssigned || !IsComputingRaycast)
{
return;
}

UpdateInputEvents(HasPlayerControlSupport: false, hasPlayerRaycastSupport: true); // ui raycast
}
public bool IsComputingRaycast = true;
/// <summary>
/// Displays a visual tracker for the device if supported by the matched device definition.
Expand Down
11 changes: 11 additions & 0 deletions Basis/Packages/com.basis.framework/UI/BasisGraphicUIRayCaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ public void OnDisable()
BasisLocalCameraDriver.InstanceExists -= InstanceExists;
}

/// <summary>
/// Unity OnValidate hook. Attempts to set the canvas on the same GameObject if not already assigned.
/// </summary>
private void OnValidate()
{
if (Canvas == null)
{
TryGetComponent(out Canvas);
}
}

/// <summary>
/// Callback for when a <see cref="BasisLocalCameraDriver"/> instance becomes available.
/// </summary>
Expand Down
72 changes: 36 additions & 36 deletions Basis/Packages/com.basis.framework/UI/BasisUIRaycast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@ public class BasisUIRaycast
public static string LoadMaterialAddress = "Assets/UI/Material/RayCastMaterial.mat";
public static string LoadUIRedicalAddress = "Assets/UI/Prefabs/highlightQuad.prefab";
public GameObject highlightQuadInstance;
public ActiveStateOfHightlight HighlightState;
private ActiveStateOfHightlight _highlightState;
public ActiveStateOfHightlight HighlightState
{
get => _highlightState;
set
{
if (_highlightState == value) return;
_highlightState = value;
highlightQuadInstance?.SetActive(value == ActiveStateOfHightlight.On);
}
}

public enum ActiveStateOfHightlight
{
On,
Expand All @@ -42,6 +53,8 @@ public enum ActiveStateOfHightlight

public bool CachedLinerRenderState = false;
public RaycastHit PhysicHit;
public bool DidPhysicHit = false;
public Collider HitCollider;
public Canvas FoundCanvas;
public RaycastResult RaycastResult = new RaycastResult();

Expand Down Expand Up @@ -130,7 +143,6 @@ public void Initialize(BasisInput basisInput, BasisPointRaycaster pointRaycaster
Canvas.worldCamera = BasisLocalCameraDriver.Instance.Camera;
}
ReticleRenderer = highlightQuadInstance.GetComponentInChildren<Renderer>();
highlightQuadInstance.gameObject.SetActive(false);
HighlightState = ActiveStateOfHightlight.NA;
HasRedicalRenderer = true;
}
Expand Down Expand Up @@ -243,6 +255,8 @@ public void HandleUIRaycast()
SortedGraphics.Clear();
SortedRays.Clear();
PhysicHit = hits[hitIndex];
DidPhysicHit = true;
HitCollider = PhysicHit.collider;

if (RaycastToUI())
{
Expand Down Expand Up @@ -299,6 +313,8 @@ private void HandleNoHit()
ResetCursorType();
RaycastResult = new RaycastResult();
PhysicHit = new RaycastHit();
DidPhysicHit = false;
HitCollider = null;
}

bool ContainsLayer(LayerMask mask, int layer)
Expand All @@ -308,7 +324,7 @@ bool ContainsLayer(LayerMask mask, int layer)

private void HandleDidHit()
{
WasCorrectLayer = ContainsLayer(UILayers, PhysicHit.transform.gameObject.layer);
WasCorrectLayer = ContainsLayer(UILayers, HitCollider.gameObject.layer);
if (WasCorrectLayer)
{
UpdateRayCastResult(); // sets all RaycastResult data
Expand All @@ -325,8 +341,7 @@ private void HandleDidHit()

private void UpdateRayCastResult()
{
var physicshit = PhysicHit.transform.gameObject;
RaycastResult.gameObject = physicshit;
RaycastResult.gameObject = HitCollider.gameObject;
RaycastResult.distance = PhysicHit.distance;
if (BasisPointRaycaster.UseWorldPosition)
{
Expand All @@ -337,7 +352,7 @@ private void UpdateRayCastResult()
// we assign screenpoint manually example in BasisLocalCameraDriver
}
RaycastResult.screenPosition = BasisPointRaycaster.ScreenPoint;
FoundCanvas = physicshit.GetComponentInParent<Canvas>();
FoundCanvas = HitCollider.GetComponentInParent<Canvas>();
if (FoundCanvas != null)
{
RaycastResult.sortingLayer = FoundCanvas.sortingLayerID;
Expand Down Expand Up @@ -374,36 +389,22 @@ private void UpdateLineRenderer()

private void UpdateReticleRenderer()
{
if (HasRedicalRenderer)
if (!HasRedicalRenderer)
{
if (PhysicHit.transform != null)
{
if (BasisDeviceManagement.IsUserInDesktop() && BasisCursorManagement.ActiveLockState() != CursorLockMode.Locked)
{
if (HighlightState != ActiveStateOfHightlight.Off)
{
highlightQuadInstance.SetActive(false);
HighlightState = ActiveStateOfHightlight.Off;
}
}
else
{
if (HighlightState != ActiveStateOfHightlight.On)
{
highlightQuadInstance.SetActive(true);
HighlightState = ActiveStateOfHightlight.On;
}
highlightQuadInstance.transform.SetPositionAndRotation(PhysicHit.point, Quaternion.LookRotation(PhysicHit.normal));
}
}
else
{
if (HighlightState != ActiveStateOfHightlight.Off)
{
highlightQuadInstance.SetActive(false);
HighlightState = ActiveStateOfHightlight.Off;
}
}
return;
}

// Hide on desktop while the cursor is unlocked (free mouse uses the OS cursor, not this reticle).
bool show = DidPhysicHit && !(BasisDeviceManagement.IsUserInDesktop() && BasisCursorManagement.ActiveLockState() != CursorLockMode.Locked);

if (show)
{
HighlightState = ActiveStateOfHightlight.On;
highlightQuadInstance.transform.SetPositionAndRotation(PhysicHit.point, Quaternion.LookRotation(PhysicHit.normal));
}
else
{
HighlightState = ActiveStateOfHightlight.Off;
}
}

Expand Down Expand Up @@ -508,7 +509,6 @@ private void ResetRenderers()

if (HasRedicalRenderer)
{
highlightQuadInstance.SetActive(false);
HighlightState = ActiveStateOfHightlight.Off;
}
}
Expand Down
Loading