Skip to content

Commit 8056457

Browse files
authored
Merge pull request #108 from De-Panther/some_improvements_and_fixes
Some improvements and fixes
2 parents 8006dbb + 059cee8 commit 8056457

File tree

11 files changed

+581
-296
lines changed

11 files changed

+581
-296
lines changed

Build/webxr.js

Lines changed: 113 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,32 @@
1414
this.handRight = new XRHandData();
1515
this.viewerHitTestPose = new XRHitPoseData();
1616
this.frameNumber = 0;
17-
this.handHeldMove = false;
18-
this.xrData = null;
17+
this.touchIDs = [];
18+
this.touches = [];
19+
this.CreateTouch = function (pageElement, xPercentage, yPercentage) {
20+
let touchID = 0;
21+
while (this.touchIDs.includes(touchID))
22+
{
23+
touchID++;
24+
}
25+
let touch = new XRTouch(touchID, pageElement, xPercentage, yPercentage);
26+
this.touchIDs.push(touchID);
27+
this.touches.push(touch);
28+
return touch;
29+
}
30+
this.RemoveTouch = function (touch) {
31+
touch.ended = true;
32+
this.touchIDs = this.touchIDs.filter(function(item) {
33+
return item !== touch.identifier
34+
});
35+
this.touches = this.touches.filter(function(item) {
36+
return item !== touch
37+
});
38+
}
39+
this.SendTouchEvent = function(JSEventsObject, eventID, eventName, target, changedTouches) {
40+
let touchEvent = new XRTouchEvent(eventName, target, this.touches, this.touches, changedTouches);
41+
JSEventsObject.eventHandlers[eventID].eventListenerFunc(touchEvent);
42+
}
1943
}
2044

2145
function XRControllerData() {
@@ -77,9 +101,10 @@
77101
return start + (end - start) * percentage;
78102
}
79103

80-
function XRMouseEvent(eventName, pageElement, xPercentage, yPercentage, buttonNumber) {
104+
function XRTouch(touchID, pageElement, xPercentage, yPercentage) {
105+
this.identifier = touchID;
106+
this.ended = false;
81107
let rect = pageElement.getBoundingClientRect();
82-
this.type = eventName;
83108
// It was pageElement.size / window.devicePixelRatio, but now we treat devicePixelRatio in XR session as 1
84109
this.clientX = lerp(rect.left, rect.left + pageElement.width / 1, xPercentage);
85110
this.clientY = lerp(rect.top, rect.top + pageElement.height / 1, yPercentage);
@@ -95,32 +120,45 @@
95120
this.screenY = this.clientY;
96121
this.movementX = 0; // diff between movements
97122
this.movementY = 0; // diff between movements
98-
this.button = 0; // 0 none or main, 1 middle, 2 secondary
99-
this.buttons = 0; // 0 none, 1 main, 4 middle, 2 secondary
100-
switch (buttonNumber)
101-
{
102-
case -1:
103-
this.button = 0;
104-
this.buttons = 0;
105-
break;
106-
case 0:
107-
this.button = 0;
108-
this.buttons = 1;
109-
break;
110-
case 1:
111-
this.button = 1;
112-
this.buttons = 4;
113-
break;
114-
case 2:
115-
this.button = 2;
116-
this.buttons = 2;
117-
break;
123+
this.UpdateTouch = function (pageElement, xPercentage, yPercentage) {
124+
let rect = pageElement.getBoundingClientRect();
125+
let newClientX = lerp(rect.left, rect.left + pageElement.width / 1, xPercentage);
126+
let newClientY = lerp(rect.top, rect.top + pageElement.height / 1, yPercentage);
127+
this.movementX = newClientX-this.clientX;
128+
this.movementY = newClientY-this.clientY;
129+
this.clientX = newClientX;
130+
this.clientY = newClientY;
131+
this.layerX = this.clientX;
132+
this.layerY = this.clientY;
133+
this.offsetX = this.clientX;
134+
this.offsetY = this.clientY;
135+
this.pageX = this.clientX;
136+
this.pageY = this.clientY;
137+
this.x = this.clientX;
138+
this.y = this.clientY;
139+
this.screenX = this.clientX;
140+
this.screenY = this.clientY;
141+
}
142+
this.HasMovement = function () {
143+
return (this.movementX != 0 || this.movementY != 0);
144+
}
145+
this.ResetMovement = function () {
146+
this.movementX = 0;
147+
this.movementY = 0;
118148
}
149+
}
150+
151+
function XRTouchEvent(eventName, target, touches, targetTouchs, changedTouches) {
152+
this.type = eventName;
153+
this.target = target;
154+
this.touches = touches;
155+
this.targetTouches = targetTouchs;
156+
this.changedTouches = changedTouches;
119157
this.ctrlKey = false;
120158
this.altKey = false;
121159
this.metaKey = false;
122160
this.shiftKey = false;
123-
this.detail = 0;
161+
this.preventDefault = function () {};
124162
}
125163

126164
function XRManager() {
@@ -259,6 +297,8 @@
259297
this.viewerHitTestSource.cancel();
260298
this.viewerHitTestSource = null;
261299
}
300+
301+
this.removeRemainingTouches();
262302

263303
this.xrData.controllerA.enabled = 0;
264304
this.xrData.controllerB.enabled = 0;
@@ -295,6 +335,15 @@
295335
});
296336
}
297337

338+
XRManager.prototype.removeRemainingTouches = function () {
339+
while (this.xrData.touches.length > 0)
340+
{
341+
let touch = this.xrData.touches[0];
342+
this.xrData.RemoveTouch(touch);
343+
this.xrData.SendTouchEvent(this.JSEventsObject, 8, "touchend", this.canvas, [touch]);
344+
}
345+
}
346+
298347
XRManager.prototype.onInputSourceEvent = function (xrInputSourceEvent) {
299348
if (xrInputSourceEvent.type && xrInputSourceEvent.inputSource
300349
&& xrInputSourceEvent.inputSource.handedness != 'none') {
@@ -345,29 +394,27 @@
345394
} else {
346395
let xPercentage = 0.5;
347396
let yPercentage = 0.5;
348-
if (xrInputSourceEvent.inputSource &&
349-
xrInputSourceEvent.inputSource.gamepad &&
350-
xrInputSourceEvent.inputSource.gamepad.axes) {
351-
xPercentage = (xrInputSourceEvent.inputSource.gamepad.axes[0] + 1.0) * 0.5;
352-
yPercentage = (xrInputSourceEvent.inputSource.gamepad.axes[1] + 1.0) * 0.5;
353-
}
354-
switch (xrInputSourceEvent.type) {
355-
case "select": // mousemove 5
356-
this.JSEventsObject.eventHandlers[5].eventListenerFunc(
357-
new XRMouseEvent("mousemove", this.canvas, xPercentage, yPercentage, 0));
358-
break;
359-
case "selectstart": // mousedown 4
360-
this.xrData.handHeldMove = true;
361-
this.JSEventsObject.eventHandlers[5].eventListenerFunc(
362-
new XRMouseEvent("mousemove", this.canvas, xPercentage, yPercentage, 0));
363-
this.JSEventsObject.eventHandlers[4].eventListenerFunc(
364-
new XRMouseEvent("mousedown", this.canvas, xPercentage, yPercentage, 0));
365-
break;
366-
case "selectend": // mouseup 3
367-
this.xrData.handHeldMove = false;
368-
this.JSEventsObject.eventHandlers[3].eventListenerFunc(
369-
new XRMouseEvent("mouseup", this.canvas, xPercentage, yPercentage, 0));
370-
break;
397+
let inputSource = xrInputSourceEvent.inputSource;
398+
if (inputSource) {
399+
if (inputSource.gamepad &&
400+
inputSource.gamepad.axes) {
401+
xPercentage = (inputSource.gamepad.axes[0] + 1.0) * 0.5;
402+
yPercentage = (inputSource.gamepad.axes[1] + 1.0) * 0.5;
403+
}
404+
switch (xrInputSourceEvent.type) {
405+
case "select": // 9 touchmove
406+
// no need to call touchmove here
407+
break;
408+
case "selectstart": // 7 touchstart
409+
inputSource.xrTouchObject = this.xrData.CreateTouch(this.canvas, xPercentage, yPercentage);
410+
this.xrData.SendTouchEvent(this.JSEventsObject, 7, "touchstart", this.canvas, [inputSource.xrTouchObject])
411+
break;
412+
case "selectend": // 8 touchend
413+
this.xrData.RemoveTouch(inputSource.xrTouchObject);
414+
this.xrData.SendTouchEvent(this.JSEventsObject, 8, "touchend", this.canvas, [inputSource.xrTouchObject]);
415+
inputSource.xrTouchObject = null;
416+
break;
417+
}
371418
}
372419
}
373420
}
@@ -506,9 +553,11 @@
506553
xrData.handRight.frame = xrData.frameNumber;
507554
xrData.controllerA.frame = xrData.frameNumber;
508555
xrData.controllerB.frame = xrData.frameNumber;
509-
if (!inputSources || !inputSources.length) {
556+
if (!inputSources || !inputSources.length || inputSources.length == 0) {
557+
this.removeRemainingTouches();
510558
return;
511559
}
560+
let touchesToSend = [];
512561
for (var i = 0; i < inputSources.length; i++) {
513562
let inputSource = inputSources[i];
514563
// Show the input source if it has a grip space
@@ -635,14 +684,19 @@
635684
xrData.controllerB = controller;
636685
}
637686
}
638-
} else if (xrData.handHeldMove && inputSource.gamepad && inputSource.gamepad.axes) {
639-
if (xrData.handHeldMove)
640-
{
641-
this.JSEventsObject.eventHandlers[5].eventListenerFunc(
642-
new XRMouseEvent("mousemove", this.canvas,
643-
(inputSource.gamepad.axes[0] + 1.0) * 0.5,
644-
(inputSource.gamepad.axes[1] + 1.0) * 0.5, 0));
645-
}
687+
} else if (inputSource.xrTouchObject && !inputSource.xrTouchObject.ended && inputSource.gamepad && inputSource.gamepad.axes) {
688+
inputSource.xrTouchObject.UpdateTouch( this.canvas,
689+
(inputSource.gamepad.axes[0] + 1.0) * 0.5,
690+
(inputSource.gamepad.axes[1] + 1.0) * 0.5);
691+
if (inputSource.xrTouchObject.HasMovement()) {
692+
touchesToSend.push(inputSource.xrTouchObject);
693+
}
694+
}
695+
}
696+
if (touchesToSend.length > 0) {
697+
this.xrData.SendTouchEvent(this.JSEventsObject, 9, "touchmove", this.canvas, touchesToSend);
698+
for (var i = 0; i < touchesToSend.length; i++) {
699+
touchesToSend[i].ResetMovement();
646700
}
647701
}
648702
}
@@ -737,6 +791,7 @@
737791
this.ctx.bindFramebuffer(this.ctx.FRAMEBUFFER, glLayer.framebuffer);
738792
if (session.isAR) {
739793
this.ctx.dontClearOnFrameStart = true;
794+
this.ctx.clear(this.ctx.STENCIL_BUFFER_BIT | this.ctx.DEPTH_BUFFER_BIT);
740795
} else {
741796
this.ctx.clear(this.ctx.COLOR_BUFFER_BIT | this.ctx.DEPTH_BUFFER_BIT);
742797
}
@@ -847,7 +902,7 @@
847902
leftRect.w = (viewport.width / glLayer.framebufferWidth) * (glLayer.framebufferWidth / this.canvas.width);
848903
leftRect.h = (viewport.height / glLayer.framebufferHeight) * (glLayer.framebufferHeight / this.canvas.height);
849904
}
850-
} else if (view.eye === 'right') {
905+
} else if (view.eye === 'right' && viewport.width != 0 && viewport.height != 0) {
851906
eyeCount = 2;
852907
if (viewport) {
853908
rightRect.x = (viewport.x / glLayer.framebufferWidth) * (glLayer.framebufferWidth / this.canvas.width);

MainProject/ProjectSettings/TagManager.asset

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ TagManager:
1414
- UI
1515
-
1616
-
17-
- WallWithCracks
18-
- Blob
17+
-
18+
-
1919
-
2020
-
2121
-

Packages/webxr-interactions/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Fixed
9+
- OnDisable in ControllerInteraction.
10+
- Missing material in default WebXRCameraSet.
811

912
## [0.5.1] - 2020-12-26
1013
### Fixed

Packages/webxr-interactions/Runtime/Prefabs/WebXRCameraSet.prefab

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ MeshRenderer:
740740
m_RenderingLayerMask: 1
741741
m_RendererPriority: 0
742742
m_Materials:
743-
- {fileID: 2100000, guid: cf55eb157040bca49b365493c72ed650, type: 2}
743+
- {fileID: 2100000, guid: b7268161509fb9b41adb91ff589d9274, type: 2}
744744
m_StaticBatchInfo:
745745
firstSubMesh: 0
746746
subMeshCount: 0
@@ -907,7 +907,7 @@ MeshRenderer:
907907
m_RenderingLayerMask: 1
908908
m_RendererPriority: 0
909909
m_Materials:
910-
- {fileID: 2100000, guid: cf55eb157040bca49b365493c72ed650, type: 2}
910+
- {fileID: 2100000, guid: b7268161509fb9b41adb91ff589d9274, type: 2}
911911
m_StaticBatchInfo:
912912
firstSubMesh: 0
913913
subMeshCount: 0

Packages/webxr-interactions/Runtime/Scripts/ControllerInteraction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private void OnEnable()
7474
controller.OnHandUpdate += OnHandUpdate;
7575
}
7676

77-
private void OnDisabled()
77+
private void OnDisable()
7878
{
7979
controller.OnControllerActive -= SetControllerVisible;
8080
controller.OnHandActive -= SetHandJointsVisible;

Packages/webxr/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- Multi touch support for Handheld AR.
10+
11+
### Fixed
12+
- OnDisable in WebXRController.
13+
- Depth and Stencil clear issue in Handheld AR.
14+
- Ugly hack to fix WebXR Viewer viewports on iOS.
815

916
## [0.5.1] - 2020-12-26
1017
### Fixed

0 commit comments

Comments
 (0)