Skip to content

Commit 4e4d735

Browse files
committed
Compatability Update for Unity 2020+ ToughNutToCrack#19
1 parent 7477ff5 commit 4e4d735

File tree

5 files changed

+241
-27
lines changed

5 files changed

+241
-27
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
mergeInto(LibraryManager.library, {
2+
CameraReady : function() {
3+
cameraReady();
4+
},
5+
DetectionManagerReady : function () {
6+
detectionManagerReady();
7+
},
8+
9+
ObjectAvailable : function() {
10+
objectAvailable();
11+
},
12+
13+
GetCamName : function(camName) {
14+
var name = Pointer_stringify(camName)
15+
getCamName(name);
16+
},
17+
18+
});

Assets/Scripts/Common/CameraController.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
using UnityEngine;
2+
using System.Runtime.InteropServices;
23

34
namespace ARWT.Core{
45
public class CameraController : MonoBehaviour{
5-
6+
7+
[DllImport("__Internal")]
8+
private static extern void ObjectAvailable();
9+
10+
[DllImport("__Internal")]
11+
private static extern void CameraReady();
12+
13+
614
Matrix4x4 defProj;
715
Camera cam;
816

9-
[System.Obsolete]
1017
void Start() {
1118
cam = GetComponent<Camera>();
12-
Application.ExternalCall("cameraReady");
19+
20+
if(!Application.isEditor){
21+
CameraReady();
22+
}
1323

1424
defProj = cam.projectionMatrix;
1525
}
@@ -93,5 +103,17 @@ public void setEuler(string val){
93103
transform.eulerAngles = new Vector3(x, y, z);
94104
}
95105

106+
public void ObjectCheck(string objectName){
107+
108+
if(GameObject.Find(objectName) != null){
109+
if(!Application.isEditor){
110+
ObjectAvailable();
111+
}
112+
}else{
113+
Debug.LogError("There is no gameobject with the name " + objectName + " available for use!");
114+
}
115+
}
116+
117+
96118
}
97119
}

Assets/Scripts/Common/PostBuild/PostBuildActions.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,18 @@ public class PostBuildActions{
1717
static string markersPath = "data/markers";
1818
static string markersImagesPath = "data/markersImages";
1919
static string index = "index.html";
20-
static string appJS = "js/app.js";
21-
22-
2320

2421
[PostProcessBuild]
2522
public static void OnPostProcessBuild(BuildTarget target, string targetPath){
2623
if(PlayerSettings.WebGL.template.Contains("WebAR")){
27-
var path = Path.Combine(targetPath, "Build/UnityLoader.js");
24+
var path = Path.Combine(targetPath, "Build/" + getName(targetPath) + ".loader.js");
2825
var text = File.ReadAllText(path);
2926
text = text.Replace("UnityLoader.SystemInfo.mobile", "false");
3027
File.WriteAllText(path, text);
3128

32-
string buildJsonPath = "Build/" + getName(targetPath) + ".json";
33-
path = Path.Combine(targetPath, "Build/" + getName(targetPath) + ".json");
34-
replaceInFile(path, "backgroundColor", "");
35-
3629
generateHTML(targetPath);
3730
copyImages(targetPath);
3831

39-
string unityDeclaration = $"const unityInstance = UnityLoader.instantiate(\"unityContainer\", \"{buildJsonPath}\");";
40-
replaceInFile(Path.Combine(targetPath, appJS), buildPlaceholder, unityDeclaration);
4132
}
4233
}
4334

Assets/Scripts/WebAR/DetectionManager.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ARWT.Core;
22
using UnityEngine;
3+
using System.Runtime.InteropServices;
34

45
namespace ARWT.Marker{
56
public class MarkerInfo{
@@ -20,14 +21,18 @@ public MarkerInfo(string name, bool isVisible, Vector3 position, Quaternion rota
2021

2122
public class DetectionManager : Singleton<DetectionManager>{
2223

24+
[DllImport("__Internal")]
25+
private static extern void DetectionManagerReady();
26+
2327
public delegate void MarkerDetection(MarkerInfo m);
2428
public static event MarkerDetection onMarkerDetected;
2529
public static event MarkerDetection onMarkerVisible;
2630
public static event MarkerDetection onMarkerLost;
2731

28-
[System.Obsolete]
2932
void Start() {
30-
Application.ExternalCall("detectionManagerReady");
33+
if(!Application.isEditor){
34+
DetectionManagerReady();
35+
}
3136
}
3237

3338
public void markerInfos(string infos){

Assets/WebGLTemplates/WebAR/index.html

Lines changed: 190 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,201 @@
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
66
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7-
<title>Unity WebGL Player | WebGL Test AR</title>
7+
<title>Unity WebGL Player | {{{ PRODUCT_NAME }}}</title>
8+
<script src="Build/{{{ LOADER_FILENAME }}}"></script>
89
<script src="js/lib/aframe.min.js"></script>
910
<script src="js/lib/aframe-ar.js"></script>
10-
<script src="Build/UnityLoader.js"></script>
11-
<script src="js/app.js"></script>
12-
1311
</head>
12+
<body>
13+
<!-- style for the loader -->
14+
<style>
15+
html, body {
16+
overflow: hidden;
17+
}
18+
19+
.arjs-loader {
20+
height: 100%;
21+
width: 100%;
22+
position: absolute;
23+
top: 0;
24+
left: 0;
25+
background-color: rgba(0, 0, 0, 1);
26+
z-index: 9999;
27+
display: flex;
28+
justify-content: center;
29+
align-items: center;
30+
}
31+
32+
.arjs-loader div {
33+
text-align: center;
34+
font-size: 1.25em;
35+
color: white;
36+
}
37+
</style>
38+
39+
<div class="arjs-loader" id="arjs-loader">
40+
<div>Loading, please wait...</div>
41+
</div>
42+
43+
<div id="unity-container" class="unity-desktop" style="z-index: 2; position: absolute; top: 0; left: 0;">
44+
<canvas id="unity-canvas"></canvas>
45+
</div>
46+
47+
<script>
48+
var container = document.querySelector("#unity-container");
49+
var unityCanvas = document.querySelector("#unity-canvas");
50+
51+
var buildUrl = "Build";
52+
var loaderUrl = buildUrl + "/{{{ LOADER_FILENAME }}}";
53+
var config = {
54+
dataUrl: buildUrl + "/{{{ DATA_FILENAME }}}",
55+
frameworkUrl: buildUrl + "/{{{ FRAMEWORK_FILENAME }}}",
56+
codeUrl: buildUrl + "/{{{ CODE_FILENAME }}}",
57+
#if MEMORY_FILENAME
58+
memoryUrl: buildUrl + "/{{{ MEMORY_FILENAME }}}",
59+
#endif
60+
#if SYMBOLS_FILENAME
61+
symbolsUrl: buildUrl + "/{{{ SYMBOLS_FILENAME }}}",
62+
#endif
63+
streamingAssetsUrl: "StreamingAssets",
64+
companyName: "{{{ COMPANY_NAME }}}",
65+
productName: "{{{ PRODUCT_NAME }}}",
66+
productVersion: "{{{ PRODUCT_VERSION }}}",
67+
};
68+
69+
70+
var script = document.createElement("script");
71+
var unityInstance;
72+
73+
script.src = loaderUrl;
74+
script.onload = () => {
75+
createUnityInstance(unityCanvas, config, (progress) => {
76+
77+
}).then((ui) => {
78+
unityInstance = ui;
79+
80+
}).catch((message) => {
81+
alert(message);
82+
});
83+
};
84+
document.body.appendChild(script);
85+
86+
let isCameraReady = false;
87+
let isDetectionManagerReady = false;
88+
let gl = null;
89+
90+
function cameraReady() {
91+
isCameraReady = true;
92+
gl = unityInstance.Module.ctx;
93+
console.log("Camera Ready");
94+
}
95+
96+
let isObjectAvailable = false;
97+
function objectAvailable() {
98+
99+
isObjectAvailable = true;
100+
101+
}
102+
103+
function detectionManagerReady() {
104+
isDetectionManagerReady = true;
105+
}
106+
107+
let camName = null;
108+
function getCamName(name) {
109+
camName = name;
110+
}
111+
112+
function createUnityMatrix(el) {
113+
const m = el.matrix.clone();
114+
const zFlipped = new THREE.Matrix4().makeScale(1, 1, -1).multiply(m);
115+
const rotated = zFlipped.multiply(new THREE.Matrix4().makeRotationX(-Math.PI / 2));
116+
return rotated;
117+
}
118+
119+
AFRAME.registerComponent('markercontroller', {
120+
schema: {
121+
name: { type: 'string' }
122+
},
123+
tock: function (time, timeDelta) {
124+
125+
let position = new THREE.Vector3();
126+
let rotation = new THREE.Quaternion();
127+
let scale = new THREE.Vector3();
128+
129+
createUnityMatrix(this.el.object3D).decompose(position, rotation, scale);
130+
131+
const serializedInfos = `${this.data.name},${this.el.object3D.visible},${position.toArray()},${rotation.toArray()},${scale.toArray()}`;
132+
133+
if (isDetectionManagerReady) {
134+
unityInstance.SendMessage("DetectionManager", "markerInfos", serializedInfos);
135+
}
136+
}
137+
});
138+
139+
AFRAME.registerComponent('cameratransform', {
140+
tock: function (time, timeDelta) {
141+
142+
let camtr = new THREE.Vector3();
143+
let camro = new THREE.Quaternion();
144+
let camsc = new THREE.Vector3();
145+
146+
this.el.object3D.matrix.clone().decompose(camtr, camro, camsc);
147+
148+
const projection = this.el.components.camera.camera.projectionMatrix.clone();
149+
const serializedProj = `${[...projection.elements]}`
150+
151+
const posCam = `${[...camtr.toArray()]}`
152+
const rotCam = `${[...camro.toArray()]}`
153+
154+
if (isCameraReady) {
155+
156+
setTimeout(() => {
157+
document.querySelector("#arjs-loader").style.display = "none";
158+
}, 1000);
159+
160+
unityInstance.SendMessage(camName, "setProjection", serializedProj);
161+
unityInstance.SendMessage(camName, "setPosition", posCam);
162+
unityInstance.SendMessage(camName, "setRotation", rotCam);
163+
164+
let w = window.innerWidth;
165+
let h = window.innerHeight;
166+
167+
const ratio = unityCanvas.height / h;
168+
169+
w *= ratio;
170+
h *= ratio;
171+
172+
const size = `${w},${h}`;
173+
174+
unityInstance.SendMessage(camName, "ObjectCheck", "Canvas");
175+
176+
if (isObjectAvailable) {
177+
unityInstance.SendMessage("Canvas", "setSize", size);
178+
}
179+
}
180+
181+
if (gl != null) {
182+
gl.dontClearOnFrameStart = true;
183+
}
184+
}
185+
});
186+
187+
AFRAME.registerComponent('copycanvas', {
188+
tick: function (time, timeDelta) {
189+
190+
unityCanvas.style.width = window.innerWidth + 'px';
191+
unityCanvas.style.height = window.innerHeight + 'px';
192+
193+
}
194+
});
195+
196+
</script>
14197

15-
<body style="margin: 0px; overflow: hidden;">
16-
<a-scene embedded arjs vr-mode-ui="enabled: false" copycanvas>
17-
<div id="unityContainer" style="z-index: 2" ></div>
18-
<!-- <a-marker preset="hiro"> <a-box></a-box> </a-marker> -->
19-
<!-- <a-marker type="pattern" url="data/markers/pattern-arjs.patt"><a-box></a-box></a-marker> -->
20-
<!-- <a-marker type="pattern" url="data/markers/test.patt" markercontroller="name : hiro"></a-marker> -->
198+
<a-scene embedded arjs='trackingMethod: best; patternRatio: 0.5; debugUIEnabled: false;' vr-mode-ui="enabled: false" copycanvas>
21199

22-
--MARKERS--
23-
<a-entity camera cameratransform></a-entity>
200+
--MARKERS--
201+
<a-entity camera cameratransform></a-entity>
24202
</a-scene>
25203
</body>
26204
</html>

0 commit comments

Comments
 (0)