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
4 changes: 1 addition & 3 deletions DFUVR/DFUVR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\dependencies\Windows\x64\DaggerfallUnity_Data\Managed\UnityEngine.XRModule.dll</HintPath>
</Reference>
<Reference Include="uWindowCapture.Runtime">
<HintPath>..\dependencies\Windows\x64\DaggerfallUnity_Data\Managed\uWindowCapture.Runtime.dll</HintPath>
</Reference>
<!-- uWindowCapture removed: doesn't work under Proton/Wine, replaced with direct render-to-texture -->
</ItemGroup>
<ItemGroup>
<Compile Include="BodyRotationController.cs" />
Expand Down
178 changes: 67 additions & 111 deletions DFUVR/SPC.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using DaggerfallWorkshop.Game;
using UnityEngine;
using UnityEngine.SpatialTracking;
using UnityEngine.UI;
using UnityEngine.XR;
using uWindowCapture;

namespace DFUVR
{
Expand All @@ -24,8 +24,6 @@ public class SPC : MonoBehaviour

private bool isClicking = false;

private UwcWindowTexture wTexture;

[DllImport("user32.dll")]
private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, int dwExtraInfo);

Expand All @@ -42,13 +40,12 @@ public void Initialize(LineRenderer line, GraphicRaycaster graphicRaycaster, Tra
{
if (Var.isFirst)
{
wTexture = GameObject.Find("VRUI").GetComponent<UwcWindowTexture>();
wTexture.type = WindowTextureType.Window;
wTexture.partialWindowTitle = "Daggerfall";
// Use physics raycast for main menu UI (no uWindowCapture)
windowHeight = Var.windowHeight;
windowWidth = Var.windowWidth;
}
else
{
//wTexture = GameObject.Find("VRUI").GetComponent<UwcWindowTexture>();
windowHeight = Var.windowHeight;
windowWidth = Var.windowWidth;
}
Expand All @@ -61,36 +58,6 @@ public void Initialize(LineRenderer line, GraphicRaycaster graphicRaycaster, Tra

void Update()
{
//foreach (KeyCode kcode in Enum.GetValues(typeof(KeyCode)))
//{
// if (Input.GetKey(kcode))
// Plugin.LoggerInstance.LogInfo("KeyCode down: " + kcode);
//}
//float input = Input.GetAxis("Axis" + Var.debugInt2);

//Plugin.LoggerInstance.LogInfo("Input:" + input);
//Plugin.LoggerInstance.LogInfo("Axis:" + Var.debugInt2);
//Debug.Log(Var.heightOffset);

//if (Input.GetKeyDown(Var.acceptButton)) { Var.debugInt2 += 1; }
//Plugin.LoggerInstance.LogInfo(wTexture);
//DaggerfallUI daggerfallUI = GameObject.Find("DaggerfallUI").GetComponent<DaggerfallUI>();
//FieldInfo directionField = AccessTools.Field(typeof(DaggerfallUI), "customRenderTarget");
//Plugin.LoggerInstance.LogInfo(directionField.GetValue(daggerfallUI));
//if (Input.GetKeyDown(KeyCode.K))
//{
// Plugin.LoggerInstance.LogInfo("Pressed");
// StartCoroutine(SpawnUI.Calibrate());
//}
//wTexture = GameObject.Find("VRUI").GetComponent<UwcWindowTexture>();
//if (wTexture != null)
//{
// windowPosX = wTexture.window.x;
// windowPosY = wTexture.window.y;
// windowWidth = wTexture.window.width;
// windowHeight = wTexture.window.height;
// Plugin.LoggerInstance.LogInfo(windowWidth+", "+windowHeight);
//}
TriggerProvider.CheckPressedRight();
bool clicked = Input.GetKeyDown(Var.acceptButton) || TriggerProvider.pressedDone;

Expand All @@ -107,67 +74,38 @@ void Update()

if (!Var.fStartMenu)
{
if (!Var.isCalibrated)
{
if (clicked)
{
var result = UwcWindowTexture.RayCast(lineStart, trackedPoseDriver.transform.forward, raycastDistance, -1);
if (result.hit)
{
//uWindowCapture really needs a proper documentation. I wish I knew sooner that it had a precise raycast function built in before making one myself...
//windowCoord = result.windowCoord;
Vector2 desktopCoord = result.desktopCoord;
SetCursorPos((int)desktopCoord.x, (int)desktopCoord.y);
mouse_event(MOUSEEVENTF_LEFTDOWN, (uint)desktopCoord.x, (uint)desktopCoord.y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, (uint)desktopCoord.x, (uint)desktopCoord.y, 0, 0);
}
}
}
else
// Continuously update DaggerfallUI mouse position from laser pointer
RaycastHit vruiHit;
bool hitVRUI = false;
RaycastHit[] hits = Physics.RaycastAll(lineStart, trackedPoseDriver.transform.forward, raycastDistance);

foreach (var hit in hits)
{
//RaycastHit hit;

//if (Physics.Raycast(lineStart, trackedPoseDriver.transform.forward, out hit, raycastDistance))
//{
// if (hit.collider.gameObject.name == "VRUI")
// {
// if (Input.GetKeyDown(Var.acceptButton))
// {
// Plugin.LoggerInstance.LogInfo("Raycast Hit: " + hit.point);
// SimulateMouseClick(hit.point);
// }
// }
// if (Input.GetKeyDown(Var.acceptButton))
// {
// if (hit.collider.gameObject.name != "VRUI")
// { Plugin.LoggerInstance.LogInfo("Hit " + hit.collider.gameObject.name); }
// }
//}
RaycastHit[] hits = Physics.RaycastAll(lineStart, trackedPoseDriver.transform.forward, raycastDistance);

foreach (var hit in hits)
if (hit.collider.gameObject.name == "VRUI")
{
Plugin.LoggerInstance.LogInfo("clicked");
if (hit.collider.gameObject.name == "VRUI")
vruiHit = hit;
hitVRUI = true;
UpdateVRMousePosition(hit.point);

if (clicked)
{
Plugin.LoggerInstance.LogInfo("Raycast Hit: " + hit.point);
SimulateMouseClick(hit.point);
break;
}

if (Input.GetKeyDown(Var.acceptButton) || Var.rTriggerDone || Var.lTriggerDone)
hit.collider.gameObject.GetComponent<Button>()?.onClick.Invoke();
break;
}

// scroll
InputDevice hand = Var.leftHanded ? InputDevices.GetDeviceAtXRNode(XRNode.LeftHand) : InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
if (Input.GetKeyDown(Var.acceptButton) || Var.rTriggerDone || Var.lTriggerDone)
hit.collider.gameObject.GetComponent<Button>()?.onClick.Invoke();
}

Vector2 rThumbStick;
hand.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxis, out rThumbStick);
// scroll
InputDevice hand = Var.leftHanded ? InputDevices.GetDeviceAtXRNode(XRNode.LeftHand) : InputDevices.GetDeviceAtXRNode(XRNode.RightHand);

float inputY = rThumbStick.y;
mouse_event(MOUSEEVENTF_WHEEL, 0, 0, (uint)(inputY * Var.scrollSpeed), 0);
}
Vector2 rThumbStick;
hand.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxis, out rThumbStick);

float inputY = rThumbStick.y;
mouse_event(MOUSEEVENTF_WHEEL, 0, 0, (uint)(inputY * Var.scrollSpeed), 0);
}
else if (clicked)
{
Expand Down Expand Up @@ -200,29 +138,48 @@ void Update()
}

//deprecated
private void SimulateMouseClick(Vector3 hitPoint)
private Vector2 HitPointToScreenPos(Vector3 hitPoint)
{
GameObject vrui = GameObject.Find("VRUI");
Vector3 localHitPoint = vrui.transform.InverseTransformPoint(hitPoint);
Plugin.LoggerInstance.LogInfo($"Local Hit Point: {localHitPoint}");
Plugin.LoggerInstance.LogInfo("Width: " + windowWidth);
Plugin.LoggerInstance.LogInfo("Height: " + windowHeight);
double asp = (double)windowWidth / windowHeight;
Plugin.LoggerInstance.LogInfo("Aspect ratio: " + asp);

double normalizedX = (localHitPoint.x * asp + (vrui.transform.localScale.x * 0.5f)) / vrui.transform.localScale.x;//(1.77f)
double normalizedY = (localHitPoint.y + (vrui.transform.localScale.y * 0.5f)) / vrui.transform.localScale.y;
//double normalizedX = (localHitPoint.x * 1.60f + (vrui.transform.localScale.x * 0.5f)) / vrui.transform.localScale.x;//(1.77f)
//double normalizedY = (localHitPoint.y *0.9f + (vrui.transform.localScale.y * 0.5f)) / vrui.transform.localScale.y;
Plugin.LoggerInstance.LogInfo($"Normalized Hit Point: ({normalizedX}, {normalizedY})");

int screenX = (int)(normalizedX * windowWidth); //+ windowPosX;
int screenY = (int)((1 - normalizedY) * windowHeight);// + windowPosY;
Plugin.LoggerInstance.LogInfo($"Screen Coordinates: ({screenX}, {screenY})");

MouseClick((uint)screenX, (uint)screenY);
SetCursorPos((int)screenX, (int)screenY);
//Plugin.LoggerInstance.LogInfo($"Simulated mouse click at ({screenX}, {screenY})");

Bounds bounds = vrui.GetComponent<MeshFilter>().mesh.bounds;
float normalizedX = (localHitPoint.x - bounds.min.x) / bounds.size.x;
float normalizedY = (localHitPoint.y - bounds.min.y) / bounds.size.y;

// DaggerfallUI CustomMousePosition uses screen coords with (0,0) at top-left
float screenX = normalizedX * Screen.width;
float screenY = (1f - normalizedY) * Screen.height;

return new Vector2(screenX, screenY);
}

private void UpdateVRMousePosition(Vector3 hitPoint)
{
Vector2 screenPos = HitPointToScreenPos(hitPoint);

// Set DaggerfallUI's custom mouse position directly
try
{
DaggerfallUI.Instance.CustomMousePosition = screenPos;
}
catch { }
}

private void SimulateMouseClick(Vector3 hitPoint)
{
Vector2 screenPos = HitPointToScreenPos(hitPoint);
Plugin.LoggerInstance.LogInfo($"Click at screen pos: ({screenPos.x}, {screenPos.y})");

// Use both DaggerfallUI override and Win32 for compatibility
try
{
DaggerfallUI.Instance.CustomMousePosition = screenPos;
}
catch { }

MouseClick((uint)screenPos.x, (uint)screenPos.y);
SetCursorPos((int)screenPos.x, (int)screenPos.y);
}

private bool MouseClick(uint x, uint y)
Expand All @@ -243,4 +200,3 @@ private bool MouseClick(uint x, uint y)
}



32 changes: 25 additions & 7 deletions DFUVR/SpawnUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using UnityEngine;
using UnityEngine.SpatialTracking;
using UnityEngine.UI;
using uWindowCapture;
using HarmonyLib;
using System.Reflection;
using HarmonyLib.Tools;
Expand Down Expand Up @@ -75,13 +74,32 @@ public static IEnumerator Spawn()


vrui.transform.position = Camera.main.transform.position + new Vector3(0, 0, 2f);
vrui.transform.localScale = new Vector3((float)Var.windowWidth / 1000f, (float)Var.windowHeight / 1000f, 1);



UwcWindowTexture wTexture = vrui.AddComponent<UwcWindowTexture>();
vrui.AddComponent<UwcWindowTextureChildrenManager>();
wTexture.partialWindowTitle = "Daggerfall";
wTexture.childWindowPrefab = vrui;
// Set up direct render-to-texture for the main menu UI
// (replaces uWindowCapture which doesn't work under Proton/Wine)
yield return new WaitForSecondsRealtime(0.5f);
UserInterfaceRenderTarget menuTarget = vrui.AddComponent<UserInterfaceRenderTarget>();
yield return new WaitForSecondsRealtime(0.5f);
DaggerfallUI menuDaggerfallUI = GameObject.Find("DaggerfallUI").GetComponent<DaggerfallUI>();
FieldInfo menuRenderField = AccessTools.Field(typeof(DaggerfallUI), "customRenderTarget");
if (menuRenderField != null)
{
menuRenderField.SetValue(menuDaggerfallUI, menuTarget);
}
sTx menuStx = menuDaggerfallUI.gameObject.GetComponent<sTx>();
if (menuStx != null && menuStx.sTxx != null)
{
string shaderBundlePath = Path.Combine(Paths.PluginPath, "AssetBundles", "assetbundle1");
AssetBundle shaderBundle = AssetBundle.LoadFromFile(shaderBundlePath);
Shader uIShader = shaderBundle.LoadAsset<Shader>("UniShade");
shaderBundle.Unload(false);
MeshRenderer mRenderer = vrui.GetComponent<MeshRenderer>();
Material cMat = new Material(uIShader);
cMat.mainTexture = menuStx.sTxx;
mRenderer.material = cMat;
}
Var.isCalibrated = true;

try
{
Expand Down
15 changes: 9 additions & 6 deletions DFUVR/sTx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ void Start()
Plugin.LoggerInstance.LogInfo("!");
string assetBundlePath = Path.Combine(Paths.PluginPath, "AssetBundles", "assetbundle1");


AssetBundle assetBundle = AssetBundle.LoadFromFile(assetBundlePath);

RenderTexture rT = assetBundle.LoadAsset<RenderTexture>("Rtxt");

assetBundle.Unload(false);

// Create a render texture at the actual screen resolution
// using the asset bundle one as a format template
RenderTexture screenRT = new RenderTexture(Screen.width, Screen.height, rT.depth, rT.format);
screenRT.filterMode = rT.filterMode;
screenRT.antiAliasing = rT.antiAliasing;
screenRT.Create();
Plugin.LoggerInstance.LogInfo($"Created RT at {Screen.width}x{Screen.height} (screen res)");

assetBundle.Unload(false);
sTxx = rT;//Resources.Load<RenderTexture>("Rtxt");
sTxx = screenRT;
Plugin.LoggerInstance.LogInfo(sTxx);

}
public IEnumerator UICal()
{
Expand Down