Skip to content

Commit f1c293f

Browse files
authored
Merge pull request #60 from De-Panther/use_fake_event_not_dom_overlay
Use fake Mouse Event instead of DOM Overlay for touch
2 parents 0526b33 + 862f4a8 commit f1c293f

File tree

7 files changed

+266
-10
lines changed

7 files changed

+266
-10
lines changed

Assets/WebGLTemplates/WebXR/webxr.js

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
this.handRight = new XRHandData();
1515
this.viewerHitTestPose = new XRHitPoseData();
1616
this.frameNumber = 0;
17+
this.handHeldMove = false;
1718
this.xrData = null;
1819
}
1920

@@ -68,6 +69,56 @@
6869
this.rotation = [0, 0, 0, 1];
6970
}
7071

72+
function lerp(start, end, percentage)
73+
{
74+
return start + (end - start) * percentage;
75+
}
76+
77+
function XRMouseEvent(eventName, pageElement, xPercentage, yPercentage, buttonNumber) {
78+
let rect = pageElement.getBoundingClientRect();
79+
this.type = eventName;
80+
this.clientX = lerp(rect.left, rect.left + pageElement.width / window.devicePixelRatio, xPercentage);
81+
this.clientY = lerp(rect.top, rect.top + pageElement.height / window.devicePixelRatio, yPercentage);
82+
this.layerX = this.clientX;
83+
this.layerY = this.clientY;
84+
this.offsetX = this.clientX;
85+
this.offsetY = this.clientY;
86+
this.pageX = this.clientX;
87+
this.pageY = this.clientY;
88+
this.x = this.clientX;
89+
this.y = this.clientY;
90+
this.screenX = this.clientX;
91+
this.screenY = this.clientY;
92+
this.movementX = 0; // diff between movements
93+
this.movementY = 0; // diff between movements
94+
this.button = 0; // 0 none or main, 1 middle, 2 secondary
95+
this.buttons = 0; // 0 none, 1 main, 4 middle, 2 secondary
96+
switch (buttonNumber)
97+
{
98+
case -1:
99+
this.button = 0;
100+
this.buttons = 0;
101+
break;
102+
case 0:
103+
this.button = 0;
104+
this.buttons = 1;
105+
break;
106+
case 1:
107+
this.button = 1;
108+
this.buttons = 4;
109+
break;
110+
case 2:
111+
this.button = 2;
112+
this.buttons = 2;
113+
break;
114+
}
115+
this.ctrlKey = false;
116+
this.altKey = false;
117+
this.metaKey = false;
118+
this.shiftKey = false;
119+
this.detail = 0;
120+
}
121+
71122
function XRManager() {
72123
this.xrSession = null;
73124
this.inlineSession = null;
@@ -147,8 +198,7 @@
147198
window.requestAnimationFrame( tempRender );
148199
navigator.xr.requestSession('immersive-ar', {
149200
requiredFeatures: ['local-floor'], // TODO: Get this value from Unity
150-
optionalFeatures: ['dom-overlay', 'hand-tracking', 'hit-test'],
151-
domOverlay: {root: this.canvas.parentElement}
201+
optionalFeatures: ['hand-tracking', 'hit-test']
152202
}).then(async (session) => {
153203
this.waitingHandheldARHack = false;
154204
session.isImmersive = true;
@@ -208,7 +258,7 @@
208258

209259
XRManager.prototype.onInputSourceEvent = function (xrInputSourceEvent) {
210260
if (xrInputSourceEvent.type && xrInputSourceEvent.inputSource
211-
&& xrInputSourceEvent.inputSource.handedness) {
261+
&& xrInputSourceEvent.inputSource.handedness != 'none') {
212262
var hand = 0;
213263
var inputSource = xrInputSourceEvent.inputSource;
214264
var xrData = this.xrData;
@@ -253,6 +303,33 @@
253303
xrData.handLeft.trigger = controller.trigger;
254304
xrData.handLeft.squeeze = controller.squeeze;
255305
}
306+
} else {
307+
let xPercentage = 0.5;
308+
let yPercentage = 0.5;
309+
if (xrInputSourceEvent.inputSource &&
310+
xrInputSourceEvent.inputSource.gamepad &&
311+
xrInputSourceEvent.inputSource.gamepad.axes) {
312+
xPercentage = (xrInputSourceEvent.inputSource.gamepad.axes[0] + 1.0) * 0.5;
313+
yPercentage = (xrInputSourceEvent.inputSource.gamepad.axes[1] + 1.0) * 0.5;
314+
}
315+
switch (xrInputSourceEvent.type) {
316+
case "select": // mousemove 5
317+
unityInstance.Module.InternalJSEvents.eventHandlers[5].eventListenerFunc(
318+
new XRMouseEvent("mousemove", this.canvas, xPercentage, yPercentage, 0));
319+
break;
320+
case "selectstart": // mousedown 4
321+
this.xrData.handHeldMove = true;
322+
unityInstance.Module.InternalJSEvents.eventHandlers[5].eventListenerFunc(
323+
new XRMouseEvent("mousemove", this.canvas, xPercentage, yPercentage, 0));
324+
unityInstance.Module.InternalJSEvents.eventHandlers[4].eventListenerFunc(
325+
new XRMouseEvent("mousedown", this.canvas, xPercentage, yPercentage, 0));
326+
break;
327+
case "selectend": // mouseup 3
328+
this.xrData.handHeldMove = false;
329+
unityInstance.Module.InternalJSEvents.eventHandlers[3].eventListenerFunc(
330+
new XRMouseEvent("mouseup", this.canvas, xPercentage, yPercentage, 0));
331+
break;
332+
}
256333
}
257334
}
258335

@@ -501,6 +578,14 @@
501578
xrData.controllerB = controller;
502579
}
503580
}
581+
} else if (xrData.handHeldMove && inputSource.gamepad && inputSource.gamepad.axes) {
582+
if (xrData.handHeldMove)
583+
{
584+
unityInstance.Module.InternalJSEvents.eventHandlers[5].eventListenerFunc(
585+
new XRMouseEvent("mousemove", this.canvas,
586+
(inputSource.gamepad.axes[0] + 1.0) * 0.5,
587+
(inputSource.gamepad.axes[1] + 1.0) * 0.5, 0));
588+
}
504589
}
505590
}
506591
}

Assets/WebGLTemplates/WebXRFullView/webxr.js

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
this.handRight = new XRHandData();
1515
this.viewerHitTestPose = new XRHitPoseData();
1616
this.frameNumber = 0;
17+
this.handHeldMove = false;
1718
this.xrData = null;
1819
}
1920

@@ -68,6 +69,56 @@
6869
this.rotation = [0, 0, 0, 1];
6970
}
7071

72+
function lerp(start, end, percentage)
73+
{
74+
return start + (end - start) * percentage;
75+
}
76+
77+
function XRMouseEvent(eventName, pageElement, xPercentage, yPercentage, buttonNumber) {
78+
let rect = pageElement.getBoundingClientRect();
79+
this.type = eventName;
80+
this.clientX = lerp(rect.left, rect.left + pageElement.width / window.devicePixelRatio, xPercentage);
81+
this.clientY = lerp(rect.top, rect.top + pageElement.height / window.devicePixelRatio, yPercentage);
82+
this.layerX = this.clientX;
83+
this.layerY = this.clientY;
84+
this.offsetX = this.clientX;
85+
this.offsetY = this.clientY;
86+
this.pageX = this.clientX;
87+
this.pageY = this.clientY;
88+
this.x = this.clientX;
89+
this.y = this.clientY;
90+
this.screenX = this.clientX;
91+
this.screenY = this.clientY;
92+
this.movementX = 0; // diff between movements
93+
this.movementY = 0; // diff between movements
94+
this.button = 0; // 0 none or main, 1 middle, 2 secondary
95+
this.buttons = 0; // 0 none, 1 main, 4 middle, 2 secondary
96+
switch (buttonNumber)
97+
{
98+
case -1:
99+
this.button = 0;
100+
this.buttons = 0;
101+
break;
102+
case 0:
103+
this.button = 0;
104+
this.buttons = 1;
105+
break;
106+
case 1:
107+
this.button = 1;
108+
this.buttons = 4;
109+
break;
110+
case 2:
111+
this.button = 2;
112+
this.buttons = 2;
113+
break;
114+
}
115+
this.ctrlKey = false;
116+
this.altKey = false;
117+
this.metaKey = false;
118+
this.shiftKey = false;
119+
this.detail = 0;
120+
}
121+
71122
function XRManager() {
72123
this.xrSession = null;
73124
this.inlineSession = null;
@@ -147,8 +198,7 @@
147198
window.requestAnimationFrame( tempRender );
148199
navigator.xr.requestSession('immersive-ar', {
149200
requiredFeatures: ['local-floor'], // TODO: Get this value from Unity
150-
optionalFeatures: ['dom-overlay', 'hand-tracking', 'hit-test'],
151-
domOverlay: {root: this.canvas.parentElement}
201+
optionalFeatures: ['hand-tracking', 'hit-test']
152202
}).then(async (session) => {
153203
this.waitingHandheldARHack = false;
154204
session.isImmersive = true;
@@ -208,7 +258,7 @@
208258

209259
XRManager.prototype.onInputSourceEvent = function (xrInputSourceEvent) {
210260
if (xrInputSourceEvent.type && xrInputSourceEvent.inputSource
211-
&& xrInputSourceEvent.inputSource.handedness) {
261+
&& xrInputSourceEvent.inputSource.handedness != 'none') {
212262
var hand = 0;
213263
var inputSource = xrInputSourceEvent.inputSource;
214264
var xrData = this.xrData;
@@ -253,6 +303,33 @@
253303
xrData.handLeft.trigger = controller.trigger;
254304
xrData.handLeft.squeeze = controller.squeeze;
255305
}
306+
} else {
307+
let xPercentage = 0.5;
308+
let yPercentage = 0.5;
309+
if (xrInputSourceEvent.inputSource &&
310+
xrInputSourceEvent.inputSource.gamepad &&
311+
xrInputSourceEvent.inputSource.gamepad.axes) {
312+
xPercentage = (xrInputSourceEvent.inputSource.gamepad.axes[0] + 1.0) * 0.5;
313+
yPercentage = (xrInputSourceEvent.inputSource.gamepad.axes[1] + 1.0) * 0.5;
314+
}
315+
switch (xrInputSourceEvent.type) {
316+
case "select": // mousemove 5
317+
unityInstance.Module.InternalJSEvents.eventHandlers[5].eventListenerFunc(
318+
new XRMouseEvent("mousemove", this.canvas, xPercentage, yPercentage, 0));
319+
break;
320+
case "selectstart": // mousedown 4
321+
this.xrData.handHeldMove = true;
322+
unityInstance.Module.InternalJSEvents.eventHandlers[5].eventListenerFunc(
323+
new XRMouseEvent("mousemove", this.canvas, xPercentage, yPercentage, 0));
324+
unityInstance.Module.InternalJSEvents.eventHandlers[4].eventListenerFunc(
325+
new XRMouseEvent("mousedown", this.canvas, xPercentage, yPercentage, 0));
326+
break;
327+
case "selectend": // mouseup 3
328+
this.xrData.handHeldMove = false;
329+
unityInstance.Module.InternalJSEvents.eventHandlers[3].eventListenerFunc(
330+
new XRMouseEvent("mouseup", this.canvas, xPercentage, yPercentage, 0));
331+
break;
332+
}
256333
}
257334
}
258335

@@ -501,6 +578,14 @@
501578
xrData.controllerB = controller;
502579
}
503580
}
581+
} else if (xrData.handHeldMove && inputSource.gamepad && inputSource.gamepad.axes) {
582+
if (xrData.handHeldMove)
583+
{
584+
unityInstance.Module.InternalJSEvents.eventHandlers[5].eventListenerFunc(
585+
new XRMouseEvent("mousemove", this.canvas,
586+
(inputSource.gamepad.axes[0] + 1.0) * 0.5,
587+
(inputSource.gamepad.axes[1] + 1.0) * 0.5, 0));
588+
}
504589
}
505590
}
506591
}

Assets/WebXR/Plugins/WebGL/webxr.jspre

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
setTimeout(function () {
22
Module['InternalBrowser'] = Browser || {};
3+
Module["InternalJSEvents"] = JSEvents || {};
34
if (GL && GL.createContext)
45
{
56
GL.createContextOld = GL.createContext;

Build/Build/Build.data.unityweb

-1 Bytes
Binary file not shown.

Build/Build/Build.wasm

-8 Bytes
Binary file not shown.

Build/Build/Build.wasm.framework.unityweb

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)