|
78 | 78 | function XRMouseEvent(eventName, pageElement, xPercentage, yPercentage, buttonNumber) {
|
79 | 79 | let rect = pageElement.getBoundingClientRect();
|
80 | 80 | this.type = eventName;
|
81 |
| - this.clientX = lerp(rect.left, rect.left + pageElement.width / window.devicePixelRatio, xPercentage); |
82 |
| - this.clientY = lerp(rect.top, rect.top + pageElement.height / window.devicePixelRatio, yPercentage); |
| 81 | + // It was pageElement.size / window.devicePixelRatio, but now we treat devicePixelRatio in XR session as 1 |
| 82 | + this.clientX = lerp(rect.left, rect.left + pageElement.width / 1, xPercentage); |
| 83 | + this.clientY = lerp(rect.top, rect.top + pageElement.height / 1, yPercentage); |
83 | 84 | this.layerX = this.clientX;
|
84 | 85 | this.layerY = this.clientY;
|
85 | 86 | this.offsetX = this.clientX;
|
|
127 | 128 | this.xrData = new XRData();
|
128 | 129 | this.canvas = null;
|
129 | 130 | this.ctx = null;
|
130 |
| - this.gameInstance = null; |
| 131 | + this.gameModule = null; |
131 | 132 | this.polyfill = null;
|
132 | 133 | this.didNotifyUnity = false;
|
133 | 134 | this.isARSupported = false;
|
|
193 | 194 | }
|
194 | 195 | window.requestAnimationFrame( tempRender );
|
195 | 196 | navigator.xr.requestSession('immersive-ar', {
|
196 |
| - requiredFeatures: this.gameInstance.Module.WebXR.Settings.ARRequiredReferenceSpace, |
197 |
| - optionalFeatures: this.gameInstance.Module.WebXR.Settings.AROptionalFeatures |
| 197 | + requiredFeatures: this.gameModule.WebXR.Settings.ARRequiredReferenceSpace, |
| 198 | + optionalFeatures: this.gameModule.WebXR.Settings.AROptionalFeatures |
198 | 199 | }).then(async (session) => {
|
199 | 200 | session.isImmersive = true;
|
200 | 201 | session.isInSession = true;
|
|
218 | 219 | }
|
219 | 220 | window.requestAnimationFrame( tempRender );
|
220 | 221 | navigator.xr.requestSession('immersive-vr', {
|
221 |
| - requiredFeatures: this.gameInstance.Module.WebXR.Settings.VRRequiredReferenceSpace, |
222 |
| - optionalFeatures: this.gameInstance.Module.WebXR.Settings.VROptionalFeatures |
| 222 | + requiredFeatures: this.gameModule.WebXR.Settings.VRRequiredReferenceSpace, |
| 223 | + optionalFeatures: this.gameModule.WebXR.Settings.VROptionalFeatures |
223 | 224 | }).then(async (session) => {
|
224 | 225 | session.isImmersive = true;
|
225 | 226 | session.isInSession = true;
|
|
257 | 258 | this.viewerHitTestSource = null;
|
258 | 259 | }
|
259 | 260 |
|
260 |
| - this.gameInstance.Module.WebXR.OnEndXR(); |
| 261 | + this.gameModule.WebXR.OnEndXR(); |
261 | 262 | this.didNotifyUnity = false;
|
262 |
| - this.canvas.width = this.canvas.parentElement.clientWidth * window.devicePixelRatio; |
263 |
| - this.canvas.height = this.canvas.parentElement.clientHeight * window.devicePixelRatio; |
| 263 | + this.canvas.width = this.canvas.parentElement.clientWidth * this.gameModule.asmLibraryArg._JS_SystemInfo_GetPreferredDevicePixelRatio(); |
| 264 | + this.canvas.height = this.canvas.parentElement.clientHeight * this.gameModule.asmLibraryArg._JS_SystemInfo_GetPreferredDevicePixelRatio(); |
264 | 265 |
|
265 | 266 | this.BrowserObject.pauseAsyncCallbacks();
|
266 | 267 | this.BrowserObject.mainLoop.pause();
|
|
350 | 351 | }
|
351 | 352 |
|
352 | 353 | XRManager.prototype.toggleAr = function () {
|
353 |
| - if (!this.gameInstance) |
| 354 | + if (!this.gameModule) |
354 | 355 | {
|
355 | 356 | return;
|
356 | 357 | }
|
|
362 | 363 | }
|
363 | 364 |
|
364 | 365 | XRManager.prototype.toggleVr = function () {
|
365 |
| - if (!this.gameInstance) |
| 366 | + if (!this.gameModule) |
366 | 367 | {
|
367 | 368 | return;
|
368 | 369 | }
|
|
374 | 375 | }
|
375 | 376 |
|
376 | 377 | XRManager.prototype.toggleHitTest = function () {
|
377 |
| - if (!this.gameInstance) |
| 378 | + if (!this.gameModule) |
378 | 379 | {
|
379 | 380 | return;
|
380 | 381 | }
|
|
414 | 415 | }
|
415 | 416 | }
|
416 | 417 |
|
417 |
| - XRManager.prototype.setGameInstance = function (gameInstance) { |
418 |
| - if (!this.gameInstance) { |
419 |
| - this.gameInstance = gameInstance; |
420 |
| - this.canvas = this.gameInstance.Module.canvas; |
421 |
| - this.ctx = this.gameInstance.Module.ctx; |
| 418 | + XRManager.prototype.setGameModule = function (gameModule) { |
| 419 | + if (gameModule && !this.gameModule) { |
| 420 | + this.gameModule = gameModule; |
| 421 | + this.canvas = this.gameModule.canvas; |
| 422 | + this.ctx = this.gameModule.ctx; |
422 | 423 |
|
423 | 424 | var thisXRMananger = this;
|
424 |
| - this.JSEventsObject = this.gameInstance.Module.WebXR.GetJSEventsObject(); |
425 |
| - this.BrowserObject = this.gameInstance.Module.WebXR.GetBrowserObject(); |
| 425 | + this.JSEventsObject = this.gameModule.WebXR.GetJSEventsObject(); |
| 426 | + this.BrowserObject = this.gameModule.WebXR.GetBrowserObject(); |
426 | 427 | this.BrowserObject.requestAnimationFrame = function (func) {
|
427 | 428 | if (thisXRMananger.xrSession && thisXRMananger.xrSession.isInSession) {
|
428 | 429 | return thisXRMananger.xrSession.requestAnimationFrame((time, xrFrame) =>
|
|
449 | 450 | }
|
450 | 451 | }
|
451 | 452 |
|
452 |
| - XRManager.prototype.unityLoaded = function () { |
| 453 | + XRManager.prototype.unityLoaded = function (event) { |
453 | 454 | document.body.dataset.unityLoaded = 'true';
|
454 | 455 |
|
455 |
| - this.setGameInstance(unityInstance); |
| 456 | + this.setGameModule(event.detail.module); |
456 | 457 |
|
457 | 458 | document.dispatchEvent(new CustomEvent('onARSupportedCheck', { detail:{supported:this.isARSupported} }));
|
458 | 459 | document.dispatchEvent(new CustomEvent('onVRSupportedCheck', { detail:{supported:this.isVRSupported} }));
|
|
464 | 465 |
|
465 | 466 | XRManager.prototype.UpdateXRCapabilities = function() {
|
466 | 467 | // Send browser capabilities to Unity.
|
467 |
| - this.gameInstance.Module.WebXR.OnXRCapabilities( |
| 468 | + this.gameModule.WebXR.OnXRCapabilities( |
468 | 469 | JSON.stringify({
|
469 | 470 | canPresentAR: this.isARSupported,
|
470 | 471 | canPresentVR: this.isVRSupported,
|
|
659 | 660 |
|
660 | 661 | let refSpaceType = 'viewer';
|
661 | 662 | if (session.isImmersive) {
|
662 |
| - refSpaceType = this.gameInstance.Module.WebXR.Settings.VRRequiredReferenceSpace[0]; |
| 663 | + refSpaceType = this.gameModule.WebXR.Settings.VRRequiredReferenceSpace[0]; |
663 | 664 | if (session.isAR) {
|
664 |
| - refSpaceType = this.gameInstance.Module.WebXR.Settings.ARRequiredReferenceSpace[0]; |
| 665 | + refSpaceType = this.gameModule.WebXR.Settings.ARRequiredReferenceSpace[0]; |
665 | 666 | }
|
666 | 667 |
|
667 | 668 | var onSessionEnded = this.onEndSession.bind(this);
|
|
775 | 776 |
|
776 | 777 | if (!this.didNotifyUnity)
|
777 | 778 | {
|
778 |
| - if (session.isAR) |
779 |
| - { |
780 |
| - let eyeCount = 1; |
781 |
| - let leftRect = { |
782 |
| - x:0, |
783 |
| - y:0, |
784 |
| - w:1, |
785 |
| - h:1 |
786 |
| - } |
787 |
| - let rightRect = { |
788 |
| - x:0.5, |
789 |
| - y:0, |
790 |
| - w:0.5, |
791 |
| - h:1 |
792 |
| - } |
793 |
| - for (let view of pose.views) { |
794 |
| - let viewport = session.renderState.baseLayer.getViewport(view); |
795 |
| - if (view.eye === 'left') { |
796 |
| - if (viewport) { |
797 |
| - leftRect.x = viewport.x / glLayer.framebufferWidth; |
798 |
| - leftRect.y = viewport.y / glLayer.framebufferHeight; |
799 |
| - leftRect.w = viewport.width / glLayer.framebufferWidth; |
800 |
| - leftRect.h = viewport.height / glLayer.framebufferHeight; |
801 |
| - } |
802 |
| - } else if (view.eye === 'right') { |
803 |
| - eyeCount = 2; |
804 |
| - if (viewport) { |
805 |
| - rightRect.x = viewport.x / glLayer.framebufferWidth; |
806 |
| - rightRect.y = viewport.y / glLayer.framebufferHeight; |
807 |
| - rightRect.w = viewport.width / glLayer.framebufferWidth; |
808 |
| - rightRect.h = viewport.height / glLayer.framebufferHeight; |
809 |
| - } |
| 779 | + let eyeCount = 1; |
| 780 | + let leftRect = { |
| 781 | + x:0, |
| 782 | + y:0, |
| 783 | + w:1, |
| 784 | + h:1 |
| 785 | + } |
| 786 | + let rightRect = { |
| 787 | + x:0.5, |
| 788 | + y:0, |
| 789 | + w:0.5, |
| 790 | + h:1 |
| 791 | + } |
| 792 | + for (let view of pose.views) { |
| 793 | + let viewport = session.renderState.baseLayer.getViewport(view); |
| 794 | + if (view.eye === 'left') { |
| 795 | + if (viewport) { |
| 796 | + leftRect.x = (viewport.x / glLayer.framebufferWidth) * (glLayer.framebufferWidth / this.canvas.width); |
| 797 | + leftRect.y = (viewport.y / glLayer.framebufferHeight) * (glLayer.framebufferHeight / this.canvas.height); |
| 798 | + leftRect.w = (viewport.width / glLayer.framebufferWidth) * (glLayer.framebufferWidth / this.canvas.width); |
| 799 | + leftRect.h = (viewport.height / glLayer.framebufferHeight) * (glLayer.framebufferHeight / this.canvas.height); |
| 800 | + } |
| 801 | + } else if (view.eye === 'right') { |
| 802 | + eyeCount = 2; |
| 803 | + if (viewport) { |
| 804 | + rightRect.x = (viewport.x / glLayer.framebufferWidth) * (glLayer.framebufferWidth / this.canvas.width); |
| 805 | + rightRect.y = (viewport.y / glLayer.framebufferHeight) * (glLayer.framebufferHeight / this.canvas.height); |
| 806 | + rightRect.w = (viewport.width / glLayer.framebufferWidth) * (glLayer.framebufferWidth / this.canvas.width); |
| 807 | + rightRect.h = (viewport.height / glLayer.framebufferHeight) * (glLayer.framebufferHeight / this.canvas.height); |
810 | 808 | }
|
811 | 809 | }
|
812 |
| - this.gameInstance.Module.WebXR.OnStartAR(eyeCount, leftRect, rightRect); |
| 810 | + } |
| 811 | + if (session.isAR) |
| 812 | + { |
| 813 | + this.gameModule.WebXR.OnStartAR(eyeCount, leftRect, rightRect); |
813 | 814 | } else {
|
814 |
| - this.gameInstance.Module.WebXR.OnStartVR(pose.views.length); |
| 815 | + this.gameModule.WebXR.OnStartVR(eyeCount, leftRect, rightRect); |
815 | 816 | }
|
816 | 817 | this.didNotifyUnity = true;
|
817 | 818 | }
|
|
0 commit comments