Files
beyond/Assets/Scripts/Input/UVInputModule.cs
2024-11-20 15:21:28 +01:00

99 lines
4.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
namespace Beyond
{
public class UVInputModule : StandaloneInputModule
{
public MeshCollider m_Mesh;
public RenderTexture m_UITex;
private Vector2 m_CursorPos;
public Camera m_GUICamera;
private readonly MouseState m_MouseState = new MouseState();
//protected override Ge
protected override MouseState GetMousePointerEventData(int id = 0)
{
Debug.Log("UVInputModule");
if (m_Mesh == null)
{
Debug.LogError("UVInputModule, no mesh found!");
return m_MouseState;
}
// Populate the left button...
PointerEventData leftData;
var created = GetPointerData(kMouseLeftId, out leftData, true);
leftData.Reset();
if (created)
leftData.position = m_CursorPos;
if (m_GUICamera == null)
{
var go = GameObject.FindGameObjectWithTag("GUICamera");
if (go != null)
m_GUICamera = go.GetComponent<Camera>();
}
// Ordinarily we'd just pass the screen coordinates of the cursor through.
//Vector2 pos = Input.mousePosition;
// Instead, I'm going to translate that position into the latitude longitude
// texture space used by my UI canvas:
Vector2 trueMousePosition = Input.mousePosition;
RaycastHit hit;
Ray ray;
ray = m_GUICamera.ScreenPointToRay(new Vector3(trueMousePosition.x, trueMousePosition.y, 0f));
Vector2 pos;
if (!m_Mesh.Raycast(ray, out hit, 2000f))
{
Debug.Log("UVInputModule m_mesh.raycast failed with mouse pos: " + trueMousePosition.x + " " + trueMousePosition.y);
return m_MouseState;
}
else
{
pos.x = m_UITex.width * hit.textureCoord.x;
pos.y = m_UITex.height * hit.textureCoord.y;
m_CursorPos = pos;
}
Debug.Log("GetMousePointerEventData trueMousePosition: " + trueMousePosition.x + " " + trueMousePosition.y);
Debug.Log("GetMousePointerEventData UITex:W/H: " + m_UITex.width + " " + m_UITex.height);
Debug.Log("GetMousePointerEventData textureCoord: " + hit.textureCoord.x + " " + hit.textureCoord.y);
Debug.Log("GetMousePointerEventData MousePos: " + m_CursorPos.x + " " + m_CursorPos.y);
// For UV-mapped meshes, you could fire a ray against its MeshCollider
// and determine the UV coordinates of the struck point.
leftData.delta = m_CursorPos - leftData.position;
leftData.position = m_CursorPos;
leftData.scrollDelta = Input.mouseScrollDelta;
leftData.button = PointerEventData.InputButton.Left;
eventSystem.RaycastAll(leftData, m_RaycastResultCache);
var raycast = FindFirstRaycast(m_RaycastResultCache);
leftData.pointerCurrentRaycast = raycast;
m_RaycastResultCache.Clear();
// copy the apropriate data into right and middle slots
PointerEventData rightData;
GetPointerData(kMouseRightId, out rightData, true);
CopyFromTo(leftData, rightData);
rightData.button = PointerEventData.InputButton.Right;
PointerEventData middleData;
GetPointerData(kMouseMiddleId, out middleData, true);
CopyFromTo(leftData, middleData);
middleData.button = PointerEventData.InputButton.Middle;
m_MouseState.SetButtonState(PointerEventData.InputButton.Left, StateForMouseButton(0), leftData);
m_MouseState.SetButtonState(PointerEventData.InputButton.Right, StateForMouseButton(1), rightData);
m_MouseState.SetButtonState(PointerEventData.InputButton.Middle, StateForMouseButton(2), middleData);
return m_MouseState;
}
}
}