index.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content='width=device-width, height=device-height, initial-scale=1.0, user-scalable=no, shrink-to-fit=yes'>
  6. <title>Unity WebGL Player | {{{ PRODUCT_NAME }}}</title>
  7. <link rel="stylesheet" href="TemplateData/style.css">
  8. <style>
  9. html{
  10. height: -webkit-fill-available;
  11. }
  12. body {
  13. margin: 0;
  14. padding: 0;
  15. min-height: 100vh;
  16. min-height: -webkit-fill-available;
  17. width: 100vw;
  18. overflow: hidden;
  19. }
  20. .ctaDiv {
  21. display: flex;
  22. flex-direction: column;
  23. align-items: center;
  24. justify-content: center;
  25. position: absolute;
  26. left: 0;
  27. right: 0;
  28. top: 0;
  29. bottom: 0;
  30. background: #fffa;
  31. z-index: 99;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <video id="webcam-video" muted autoplay playsinline style="width:1px;position:absolute"></video>
  37. <canvas id="video-canvas" style="width:100%; height:100%; object-fit:cover; position:absolute"></canvas>
  38. <div id="startARDiv" class="ctaDiv">
  39. <select id="chooseCamSel" style="display: none;" onchange="SelectCam()"></select>
  40. <p style="text-align: center; width:60vw">This augmented reality experience requires access to your device's camera and motion sensors</p>
  41. <button id="startARButton" style="display:none" onclick="StartAR()">ALLOW ACCESS</button>
  42. </div>
  43. <div id="screenshotDiv" style="display: none;" class="ctaDiv">
  44. <div style="position:relative; background-color:white; padding:10px; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.3), 0 6px 20px 0 rgba(0, 0, 0, 0.25);">
  45. <img id="screenshotImg" src="" alt="screenshot" style="width:80vw; height:80vh">
  46. <button onclick="document.getElementById('screenshotDiv').style.display = 'none';" style="position:absolute; transform:translateY(-100%); right:0; top:0">Close</button>
  47. </div>
  48. </div>
  49. <div id="errorDiv" class="ctaDiv" style="display: none; background:#aaa">
  50. <p id="errorText" style="text-align: center; width:60vw; color:white"></p>
  51. </div>
  52. <div id="unity-container" class="unity-mobile">
  53. <canvas id="unity-canvas" style="width: 100%; height: 100%; background: #0000; z-index: -99;"></canvas>
  54. <div id="unity-loading-bar">
  55. <div id="unity-logo"></div>
  56. <div id="unity-progress-bar-empty">
  57. <div id="unity-progress-bar-full"></div>
  58. </div>
  59. </div>
  60. <canvas id="video-canvas"></canvas>
  61. </div>
  62. <script src="arcamera.js" type="text/javascript"></script>
  63. <script src="wtracker.js" type="text/javascript"></script>
  64. <script src="Build/{{{ LOADER_FILENAME }}}"></script>
  65. <script>
  66. var initialize = async() =>{
  67. var unityCanvas = document.querySelector("#unity-canvas");
  68. var videoCanvas = document.querySelector("#video-canvas");
  69. window.arCamera = new ARCamera(unityCanvas, videoCanvas);
  70. window.wTracker = new WorldTracker(arCamera);
  71. try{
  72. await wTracker.initialize("./opencv.js");
  73. console.log("World tracker initialized!");
  74. }
  75. catch (error) {
  76. console.error("Failed to initialize world tracker. Are you missing opencv.js?", error);
  77. ShowError("Failed to initialize the World Tracker.\n" + error);
  78. return;
  79. }
  80. await LoadWebcams();
  81. document.getElementById("startARButton").style.display = "block";
  82. }
  83. initialize();
  84. var container = document.querySelector("#unity-container");
  85. var canvas = document.querySelector("#unity-canvas");
  86. var loadingBar = document.querySelector("#unity-loading-bar");
  87. var progressBarFull = document.querySelector("#unity-progress-bar-full");
  88. function StartAR() {
  89. canvas.style.width = window.innerWidth + "px";
  90. canvas.style.height = window.innerHeight + "px";
  91. document.getElementById('startARDiv').style.display = 'none';
  92. createUnityInstance(document.querySelector("#unity-canvas"), {
  93. dataUrl: "Build/{{{ DATA_FILENAME }}}",
  94. frameworkUrl: "Build/{{{ FRAMEWORK_FILENAME }}}",
  95. #if USE_WASM
  96. codeUrl: "Build/{{{ CODE_FILENAME }}}",
  97. #endif
  98. #if MEMORY_FILENAME
  99. memoryUrl: "Build/{{{ MEMORY_FILENAME }}}",
  100. #endif
  101. #if SYMBOLS_FILENAME
  102. symbolsUrl: "Build/{{{ SYMBOLS_FILENAME }}}",
  103. #endif
  104. streamingAssetsUrl: "StreamingAssets",
  105. companyName: {{{ JSON.stringify(COMPANY_NAME) }}},
  106. productName: {{{ JSON.stringify(PRODUCT_NAME) }}},
  107. productVersion: {{{ JSON.stringify(PRODUCT_VERSION) }}},
  108. //webglContextAttributes: { "preserveDrawingBuffer": true },
  109. // matchWebGLToCanvasSize: false, // Uncomment this to separately control WebGL canvas render size and DOM element size.
  110. // devicePixelRatio: 1, // Uncomment this to override low DPI rendering on high DPI displays.
  111. },
  112. (progress) => {
  113. progressBarFull.style.width = 100 * progress + "%";
  114. }
  115. ).then((unityInstance) => {
  116. window.unityInstance = unityInstance;
  117. RequestWebcam();
  118. loadingBar.style.display = "none";
  119. });
  120. StartMotionSensors();
  121. //Call Start GPS here --> StartGPS();
  122. loadingBar.style.display = "block";
  123. }
  124. //Set Facing Mode here ('environment', 'user', '')
  125. window.unityFacingMode = "environment";
  126. window.WEBCAM_SETTINGS = {
  127. video: {
  128. facingMode: unityFacingMode,
  129. },
  130. audio: false
  131. };
  132. window.requestingForCameraPermission = false;
  133. async function RequestWebcam(){
  134. window.requestingForCameraPermission = true;
  135. try{
  136. window.webcamStream = await navigator.mediaDevices.getUserMedia(window.WEBCAM_SETTINGS);
  137. arCamera.setFlipped(window.WEBCAM_SETTINGS.video.facingMode == 'user');
  138. console.log("Webcam access granted");
  139. requestingForCameraPermission = false;
  140. }
  141. catch (err) {
  142. //user denied camera permission - show error panel
  143. console.error("getUserMedia error - " , err);
  144. ShowError("Failed to start the experience. Camera permission was denied");
  145. window.requestingForCameraPermission = false;
  146. }
  147. }
  148. async function StartWebcam(){
  149. console.log("StartWebcam")
  150. while (window.requestingForCameraPermission) {
  151. // Wait until requestingForCameraPermission becomes true.
  152. console.log("Waiting for permissions...");
  153. await new Promise(resolve => setTimeout(resolve, 100)); // Adjust the delay time as needed.
  154. }
  155. console.log("Got Permissions");
  156. if(window.webcamStream)
  157. {
  158. const video = document.querySelector("#webcam-video");
  159. video.srcObject = webcamStream;
  160. try {
  161. await arCamera.startWebcam(video);
  162. console.log("Webcam started successfully");
  163. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamSuccess');
  164. }
  165. catch(err){
  166. console.error("Webcam failed to start - ", err);
  167. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamFail');
  168. }
  169. }
  170. else{
  171. console.error("Webcam failed to start - permission not yet granted");
  172. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamFail');
  173. }
  174. }
  175. async function LoadWebcams(){
  176. let camDevices = [];
  177. let devices = await navigator.mediaDevices.enumerateDevices();
  178. var ctr = 0;
  179. devices.forEach(mediaDevice => {
  180. if (mediaDevice.kind === 'videoinput') {
  181. if(window.unityFacingMode == 'environment' && !mediaDevice.label.includes('facing front')){
  182. //back cam only
  183. camDevices.push(mediaDevice);
  184. }
  185. else if(window.unityFacingMode == 'user' && mediaDevice.label.includes('facing front')){
  186. //front cam only
  187. camDevices.push(mediaDevice);
  188. }
  189. else{
  190. //back and front
  191. camDevices.push(mediaDevice);
  192. console.log('pushed', mediaDevice);
  193. }
  194. ctr++;
  195. }
  196. });
  197. var select = document.getElementById("chooseCamSel");
  198. select.style.display = "block";
  199. var count = 0;
  200. //reverse array because some Android phones can't distinguish front and back cams at first load
  201. //and when this happens, most of the time, front cam goes first and back cam goes last
  202. camDevices = camDevices.reverse();
  203. camDevices.forEach(mediaDevice => {
  204. const option = document.createElement('option');
  205. option.value = mediaDevice.deviceId;
  206. let label = `Camera ${count}`;
  207. if (mediaDevice.label) {
  208. label = mediaDevice.label
  209. }
  210. const textNode = document.createTextNode(label);
  211. option.appendChild(textNode);
  212. select.appendChild(option);
  213. count++;
  214. });
  215. wTracker.WEBCAM_NAME = select.options[select.selectedIndex].innerHTML;
  216. }
  217. function SelectCam(){
  218. var select = document.getElementById("chooseCamSel");
  219. window.deviceId = select.value;
  220. window.WEBCAM_SETTINGS.video['deviceId'] = deviceId;
  221. //console.log(window.WEBCAM_SETTINGS);
  222. wTracker.WEBCAM_NAME = select.options[select.selectedIndex].innerHTML;
  223. }
  224. async function FlipCam(){
  225. arCamera.stopWebcam();
  226. window.WEBCAM_SETTINGS.video.deviceId = '';
  227. if(window.WEBCAM_SETTINGS.video.facingMode == 'user'){
  228. window.WEBCAM_SETTINGS.video.facingMode = 'environment';
  229. arCamera.setFlipped(false);
  230. }
  231. else{
  232. window.WEBCAM_SETTINGS.video.facingMode = 'user';
  233. arCamera.setFlipped(true);
  234. }
  235. window.webcamStream = await navigator.mediaDevices.getUserMedia(window.WEBCAM_SETTINGS);
  236. const video = document.querySelector("#webcam-video");
  237. video.srcObject = webcamStream;
  238. await arCamera.startWebcam(video);
  239. }
  240. function StartMotionSensors(){
  241. window.wTracker.startAngles()
  242. .then(()=>{
  243. console.log("Motion sensors started");
  244. })
  245. .catch(error=>{
  246. console.error("Failed to start motion sensors - " + error);
  247. ShowError("Failed to start the experience. " + error);
  248. });
  249. }
  250. function StartGPS(){
  251. window.wTracker.startGPS()
  252. .then((pos)=>{
  253. console.log("GPS started", pos);
  254. })
  255. .catch(error=>{
  256. console.error("Failed to start GPS - " + error);
  257. //ShowError("Failed to start GPS " + error);
  258. });
  259. }
  260. function ShowError(error){
  261. document.getElementById("errorDiv").style.display = "flex";
  262. document.getElementById("errorText").innerHTML = error;
  263. }
  264. function ShowScreenshot(dataUrl){
  265. document.getElementById("screenshotDiv").style.display = "flex";
  266. document.getElementById("screenshotImg").src = dataUrl;
  267. document.getElementById("screenshotImg").style.width = "80vw";
  268. document.getElementById("screenshotImg").style.height = 80 / window.innerWidth * window.innerHeight + "vw";
  269. }
  270. </script>
  271. </body>
  272. </html>