Skip to content

Commit

Permalink
Add simple mouse pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakvi committed Nov 27, 2022
1 parent db7161a commit c0acd34
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 10 deletions.
131 changes: 131 additions & 0 deletions Runtime/Prefabs/3D Mouse Pointer.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &2996132662166906403
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2854766037301228637}
- component: {fileID: 8689958221202446779}
- component: {fileID: 2404235370527888830}
m_Layer: 0
m_Name: Visual
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &2854766037301228637
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2996132662166906403}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0.1, y: 0.1, z: 0.1}
m_ConstrainProportionsScale: 1
m_Children: []
m_Father: {fileID: 4966045537531322762}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!33 &8689958221202446779
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2996132662166906403}
m_Mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0}
--- !u!23 &2404235370527888830
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2996132662166906403}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: 0b71baba0e05202428b9c88f33436989, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!1 &8031477956158385058
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 4966045537531322762}
- component: {fileID: -2201637356066042579}
m_Layer: 0
m_Name: 3D Mouse Pointer
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &4966045537531322762
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8031477956158385058}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 2854766037301228637}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &-2201637356066042579
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8031477956158385058}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: abdf188b06c920f43b64c54c7f390b00, type: 3}
m_Name:
m_EditorClassIdentifier:
mode: 0
7 changes: 7 additions & 0 deletions Runtime/Prefabs/3D Mouse Pointer.prefab.meta

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

32 changes: 32 additions & 0 deletions Runtime/Scripts/Components/MousePointer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using UnityEngine;
using static VenetStudio.Utility;

namespace VenetStudio
{
public class MousePointer : MonoBehaviour
{
public enum Mode
{
Plane,
Collision
}

public Mode mode;

private void Update()
{
switch (mode)
{
case Mode.Plane:
transform.position = GetMousePlanePos();
break;
case Mode.Collision:
transform.position = GetMousePhysicsHit(out var hit) ? hit.point : GetMousePlanePos();
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Scripts/Components/MousePointer.cs.meta

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

21 changes: 11 additions & 10 deletions Runtime/Scripts/Utility/MouseUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static Transform GetMainCamTransform()
private static readonly CachedData<Vector3> CameraPos = new ();
public static Vector3 GetCameraPos()
{
if (!CameraPos.IsRelevant())
if (!CameraPos.IsFresh())
{
CameraPos.value = GetMainCamTransform().position;
}
Expand All @@ -38,25 +38,26 @@ public static Vector3 GetMousePhysicsPos(int layerMask = ~0)
return hit.point;
}

// TODO(yakvi): This currently only works for one and only mask per frame. Expand MouseHit?
private static readonly CachedData<RaycastHit> MouseHit = new ();
private static readonly CachedData<int, RaycastHit> MouseHit = new ();
public static bool GetMousePhysicsHit(out RaycastHit hit, int layerMask = ~0)
{
if (!MouseHit.IsRelevant())
if (!MouseHit.IsFresh(layerMask))
{
var ray = GetMainCam().ScreenPointToRay(InputCenter.mousePos);
Raycast(ray, out MouseHit.value, float.MaxValue, layerMask);
if (Raycast(ray, out hit, float.MaxValue, layerMask)) MouseHit.SetValue(layerMask, hit);
}

hit = MouseHit.value;
return MouseHit.value.transform != null && !IsMouseOverUi();
else
{
hit = MouseHit.GetValue(layerMask);
}
return hit.transform != null && !IsMouseOverUi();
}

private static readonly CachedData<Vector3> MousePlanePos = new ();
private static Plane mousePlane = new (Vector3.up, Vector3.zero);
public static Vector3 GetMousePlanePos()
{
if (!MousePlanePos.IsRelevant())
if (!MousePlanePos.IsFresh())
{
var ray = GetMainCam().ScreenPointToRay(InputCenter.mousePos);
if (mousePlane.Raycast(ray, out var collisionDistance))
Expand All @@ -78,7 +79,7 @@ public static bool TryGetWorldMousePlanePos(out Vector3 mousePos)
private static readonly CachedData<float> DeltaTime = new ();
public static float GetDeltaTime()
{
if (!DeltaTime.IsRelevant()) DeltaTime.value = Time.deltaTime;
if (!DeltaTime.IsFresh()) DeltaTime.value = Time.deltaTime;
return DeltaTime.value;
}
}
Expand Down

0 comments on commit c0acd34

Please sign in to comment.