Skip to content

Commit 0a908ff

Browse files
committed
Support Unity 2020.1
Fixes for JS and Unity communication to support both Unity 2019.1 and Unity 2020.1. Added WebGLTemplates for Unity 2020.1. Added debug project for Unity 2020.1.
1 parent a4642e5 commit 0a908ff

File tree

124 files changed

+4874
-201
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+4874
-201
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
.DS_Store
2+
*[Bb]uilds/
23
*[Ll]ibrary/
34
*[Ll]ogs/
45
*[Tt]emp/
56
*[Oo]bj/
67
*Assets/AssetStoreTools*
78
*Build_*/
9+
**/UserSettings/EditorUserSettings.asset
810

911
# Autogenerated VS/MD solution and project files
1012
ExportedObj/
@@ -41,5 +43,3 @@ ExportedObj/
4143

4244
# Local server
4345
*.pem
44-
*/Assets/WebGLTemplates
45-
*/Assets/WebGLTemplates.meta

Build/Build/Build.data.unityweb

57 Bytes
Binary file not shown.

Build/Build/Build.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"companyName": "DefaultCompany",
3-
"productName": "Unity-WebXR-Export",
2+
"companyName": "De-Panther",
3+
"productName": "Unity WebXR Export",
44
"productVersion": "0.1",
55
"dataUrl": "Build.data.unityweb",
66
"wasmCodeUrl": "Build.wasm",

Build/Build/Build.wasm

-12 KB
Binary file not shown.

Build/Build/Build.wasm.framework.unityweb

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

Build/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="utf-8">
55
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6-
<title>Unity WebGL Player | Unity-WebXR-Export</title>
6+
<title>Unity WebGL Player | Unity WebXR Export</title>
77
<meta name="description" content="Complete interactive 3D scene demo made in Unity and exported to WebXR with the WebXR template of the Unity WebXR Export">
88
<link rel="shortcut icon" href="TemplateData/favicon.ico">
99
<link rel="stylesheet" href="TemplateData/style.css">
@@ -29,7 +29,7 @@
2929
<button class="entervr" id="entervr" value="Enter VR" disabled>VR</button>
3030
<button class="enterar" id="enterar" value="Enter AR" disabled>AR</button>
3131
<div class="webxr-link">Using <a href="https://github.com/De-Panther/unity-webxr-export" target="_blank" title="WebXR Export">WebXR Export</a></div>
32-
<div class="title">Unity-WebXR-Export</div>
32+
<div class="title">Unity WebXR Export</div>
3333
</div>
3434
</div>
3535
<script>

Build/webxr.js

Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@
7878
function XRMouseEvent(eventName, pageElement, xPercentage, yPercentage, buttonNumber) {
7979
let rect = pageElement.getBoundingClientRect();
8080
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);
8384
this.layerX = this.clientX;
8485
this.layerY = this.clientY;
8586
this.offsetX = this.clientX;
@@ -127,7 +128,7 @@
127128
this.xrData = new XRData();
128129
this.canvas = null;
129130
this.ctx = null;
130-
this.gameInstance = null;
131+
this.gameModule = null;
131132
this.polyfill = null;
132133
this.didNotifyUnity = false;
133134
this.isARSupported = false;
@@ -193,8 +194,8 @@
193194
}
194195
window.requestAnimationFrame( tempRender );
195196
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
198199
}).then(async (session) => {
199200
session.isImmersive = true;
200201
session.isInSession = true;
@@ -218,8 +219,8 @@
218219
}
219220
window.requestAnimationFrame( tempRender );
220221
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
223224
}).then(async (session) => {
224225
session.isImmersive = true;
225226
session.isInSession = true;
@@ -257,10 +258,10 @@
257258
this.viewerHitTestSource = null;
258259
}
259260

260-
this.gameInstance.Module.WebXR.OnEndXR();
261+
this.gameModule.WebXR.OnEndXR();
261262
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();
264265

265266
this.BrowserObject.pauseAsyncCallbacks();
266267
this.BrowserObject.mainLoop.pause();
@@ -350,7 +351,7 @@
350351
}
351352

352353
XRManager.prototype.toggleAr = function () {
353-
if (!this.gameInstance)
354+
if (!this.gameModule)
354355
{
355356
return;
356357
}
@@ -362,7 +363,7 @@
362363
}
363364

364365
XRManager.prototype.toggleVr = function () {
365-
if (!this.gameInstance)
366+
if (!this.gameModule)
366367
{
367368
return;
368369
}
@@ -374,7 +375,7 @@
374375
}
375376

376377
XRManager.prototype.toggleHitTest = function () {
377-
if (!this.gameInstance)
378+
if (!this.gameModule)
378379
{
379380
return;
380381
}
@@ -414,15 +415,15 @@
414415
}
415416
}
416417

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;
422423

423424
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();
426427
this.BrowserObject.requestAnimationFrame = function (func) {
427428
if (thisXRMananger.xrSession && thisXRMananger.xrSession.isInSession) {
428429
return thisXRMananger.xrSession.requestAnimationFrame((time, xrFrame) =>
@@ -449,10 +450,10 @@
449450
}
450451
}
451452

452-
XRManager.prototype.unityLoaded = function () {
453+
XRManager.prototype.unityLoaded = function (event) {
453454
document.body.dataset.unityLoaded = 'true';
454455

455-
this.setGameInstance(unityInstance);
456+
this.setGameModule(event.detail.module);
456457

457458
document.dispatchEvent(new CustomEvent('onARSupportedCheck', { detail:{supported:this.isARSupported} }));
458459
document.dispatchEvent(new CustomEvent('onVRSupportedCheck', { detail:{supported:this.isVRSupported} }));
@@ -464,7 +465,7 @@
464465

465466
XRManager.prototype.UpdateXRCapabilities = function() {
466467
// Send browser capabilities to Unity.
467-
this.gameInstance.Module.WebXR.OnXRCapabilities(
468+
this.gameModule.WebXR.OnXRCapabilities(
468469
JSON.stringify({
469470
canPresentAR: this.isARSupported,
470471
canPresentVR: this.isVRSupported,
@@ -659,9 +660,9 @@
659660

660661
let refSpaceType = 'viewer';
661662
if (session.isImmersive) {
662-
refSpaceType = this.gameInstance.Module.WebXR.Settings.VRRequiredReferenceSpace[0];
663+
refSpaceType = this.gameModule.WebXR.Settings.VRRequiredReferenceSpace[0];
663664
if (session.isAR) {
664-
refSpaceType = this.gameInstance.Module.WebXR.Settings.ARRequiredReferenceSpace[0];
665+
refSpaceType = this.gameModule.WebXR.Settings.ARRequiredReferenceSpace[0];
665666
}
666667

667668
var onSessionEnded = this.onEndSession.bind(this);
@@ -775,43 +776,43 @@
775776

776777
if (!this.didNotifyUnity)
777778
{
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);
810808
}
811809
}
812-
this.gameInstance.Module.WebXR.OnStartAR(eyeCount, leftRect, rightRect);
810+
}
811+
if (session.isAR)
812+
{
813+
this.gameModule.WebXR.OnStartAR(eyeCount, leftRect, rightRect);
813814
} else {
814-
this.gameInstance.Module.WebXR.OnStartVR(pose.views.length);
815+
this.gameModule.WebXR.OnStartVR(eyeCount, leftRect, rightRect);
815816
}
816817
this.didNotifyUnity = true;
817818
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../Packages/webxr/Hidden~/WebGLTemplates

DebugProjects/Unity2020.1/Assets/WebGLTemplates.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DebugProjects/Unity2020.1/Assets/WebXR.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)