index.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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, minimum-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. <!--IMAGETARGETS-->
  37. <video id="webcam-video" muted autoplay playsinline style="width:1px;position:absolute"></video>
  38. <!-- <video id="webcam-video" muted autoplay playsinline style="width:100%; height:100%; object-fit:cover; position:absolute"></video> -->
  39. <canvas id="video-canvas" style="width:100%; height:100%; object-fit:cover; position:absolute"></canvas>
  40. <div id="startARDiv" class="ctaDiv">
  41. <select id="chooseCamSel" style="display: none;" onchange="SelectCam()"></select>
  42. <p style="text-align: center; width:60vw;">This augmented reality experience requires access to your device's camera</p>
  43. <button id="startARButton" onclick="StartAR()" style="display:none">ALLOW ACCESS</button>
  44. </div>
  45. <div id="screenshotDiv" style="display: none;" class="ctaDiv">
  46. <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);">
  47. <img id="screenshotImg" src="" alt="screenshot" style="width:80vw; height:80vh">
  48. <button onclick="document.getElementById('screenshotDiv').style.display = 'none';" style="position:absolute; transform:translateY(-100%); right:0; top:0">Close</button>
  49. </div>
  50. </div>
  51. <div id="confirmUrlDiv" style="display: none;" class="ctaDiv">
  52. <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); width: 80vw; display:flex; flex-direction: column; align-items: center;">
  53. <p id="confirmUrlText" style="text-align: center; width:80%; overflow: hidden; text-overflow: ellipsis;">Are you sure you want to visit url.com?</p>
  54. <div>
  55. <button onclick="window.open(newUrlString, '_blank').focus(); document.getElementById('confirmUrlDiv').style.display = 'none'">VISIT SITE</button>
  56. <button onclick="document.getElementById('confirmUrlDiv').style.display = 'none'">GO BACK</button>
  57. </div>
  58. </div>
  59. </div>
  60. <div id="errorDiv" class="ctaDiv" style="display: none; background:#aaa">
  61. <p id="errorText" style="text-align: center; width:60vw; color:white"></p>
  62. </div>
  63. <div id="unity-container" class="unity-mobile">
  64. <canvas id="unity-canvas" style="width: 100%; height: 100%; background: #0000; z-index: -99"></canvas>
  65. <div id="unity-loading-bar">
  66. <div id="unity-logo"></div>
  67. <div id="unity-progress-bar-empty">
  68. <div id="unity-progress-bar-full"></div>
  69. </div>
  70. </div>
  71. <canvas id="video-canvas"></canvas>
  72. </div>
  73. <script src="arcamera.js" type="text/javascript"></script>
  74. <script src="itracker.js" type="text/javascript"></script>
  75. <script src="Build/{{{ LOADER_FILENAME }}}"></script>
  76. <script>
  77. var unityCanvas = document.querySelector("#unity-canvas");
  78. var videoCanvas = document.querySelector("#video-canvas");
  79. window.arCamera = new ARCamera(unityCanvas, videoCanvas);
  80. window.iTracker = new ImageTracker(arCamera);
  81. window.iTracker.initialize()
  82. .then(async ()=>{
  83. console.log("ImageTracker initialized!");
  84. await LoadWebcams();
  85. document.getElementById("startARButton").style.display = "block";
  86. })
  87. .catch(error => {
  88. console.error("Failed to initialize ImageTracker. Are you missing opencv.js? " + error);
  89. ShowError("Failed to initialize the experience.");
  90. });
  91. var container = document.querySelector("#unity-container");
  92. var canvas = document.querySelector("#unity-canvas");
  93. var loadingBar = document.querySelector("#unity-loading-bar");
  94. var progressBarFull = document.querySelector("#unity-progress-bar-full");
  95. function StartAR() {
  96. canvas.style.width = window.innerWidth + "px";
  97. canvas.style.height = window.innerHeight + "px";
  98. document.getElementById("startARDiv").style.display = "none";
  99. createUnityInstance(document.querySelector("#unity-canvas"), {
  100. dataUrl: "Build/{{{ DATA_FILENAME }}}",
  101. frameworkUrl: "Build/{{{ FRAMEWORK_FILENAME }}}",
  102. #if USE_WASM
  103. codeUrl: "Build/{{{ CODE_FILENAME }}}",
  104. #endif
  105. #if MEMORY_FILENAME
  106. memoryUrl: "Build/{{{ MEMORY_FILENAME }}}",
  107. #endif
  108. #if SYMBOLS_FILENAME
  109. symbolsUrl: "Build/{{{ SYMBOLS_FILENAME }}}",
  110. #endif
  111. streamingAssetsUrl: "StreamingAssets",
  112. companyName: {{{ JSON.stringify(COMPANY_NAME) }}},
  113. productName: {{{ JSON.stringify(PRODUCT_NAME) }}},
  114. productVersion: {{{ JSON.stringify(PRODUCT_VERSION) }}},
  115. //webglContextAttributes: { "preserveDrawingBuffer": true },
  116. // matchWebGLToCanvasSize: false, // Uncomment this to separately control WebGL canvas render size and DOM element size.
  117. // devicePixelRatio: 1, // Uncomment this to override low DPI rendering on high DPI displays.
  118. },
  119. (progress) => {
  120. progressBarFull.style.width = 100 * progress + "%";
  121. }
  122. ).then((unityInstance) => {
  123. window.unityInstance = unityInstance;
  124. RequestWebcam();
  125. loadingBar.style.display = "none";
  126. });
  127. loadingBar.style.display = "block";
  128. }
  129. window.WEBCAM_SETTINGS = {
  130. video: {
  131. facingMode: "environment",
  132. },
  133. audio: false
  134. };
  135. window.requestingForPermissions = false;
  136. async function RequestWebcam(){
  137. window.requestingForPermissions = true;
  138. try{
  139. window.webcamStream = await navigator.mediaDevices.getUserMedia(window.WEBCAM_SETTINGS);
  140. console.log("Webcam access granted");
  141. requestingForPermissions = false;
  142. }
  143. catch (err) {
  144. //user denied camera permission - show error panel
  145. console.error("getUserMedia error - " , err);
  146. ShowError("Failed to start the experience. Camera permission was denied");
  147. window.requestingForPermissions = false;
  148. }
  149. }
  150. async function StartWebcam(){
  151. console.log("StartWebcam")
  152. while (window.requestingForPermissions) {
  153. // Wait until requestingForPermissions becomes true.
  154. console.log("Waiting for permissions...");
  155. await new Promise(resolve => setTimeout(resolve, 100)); // Adjust the delay time as needed.
  156. }
  157. console.log("Got Permissions");
  158. if(window.webcamStream)
  159. {
  160. const video = document.querySelector("#webcam-video");
  161. video.srcObject = webcamStream;
  162. try {
  163. await arCamera.startWebcam(video);
  164. console.log("Webcam started successfully");
  165. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamSuccess');
  166. }
  167. catch(err){
  168. console.error("Webcam failed to start - ", err);
  169. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamFail');
  170. }
  171. }
  172. else{
  173. console.error("Webcam failed to start - permission not yet granted");
  174. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamFail');
  175. }
  176. }
  177. async function LoadWebcams(){
  178. let backCams = [];
  179. let devices = await navigator.mediaDevices.enumerateDevices();
  180. devices.forEach(mediaDevice => {
  181. if (mediaDevice.kind === 'videoinput' && !mediaDevice.label.includes('facing front')) {
  182. backCams.push(mediaDevice);
  183. }
  184. });
  185. var select = document.getElementById("chooseCamSel");
  186. select.style.display = "block";
  187. var count = 0;
  188. //reverse array because some Android phones can't distinguish front and back cams at first load
  189. //and when this happens, most of the time, front cam goes first and back cam goes last
  190. backCams = backCams.reverse();
  191. backCams.forEach(mediaDevice => {
  192. const option = document.createElement('option');
  193. option.value = mediaDevice.deviceId;
  194. let label = `Camera ${count}`;
  195. if (mediaDevice.label) {
  196. label = mediaDevice.label
  197. }
  198. const textNode = document.createTextNode(label);
  199. option.appendChild(textNode);
  200. select.appendChild(option);
  201. count++;
  202. });
  203. iTracker.WEBCAM_NAME = select.options[select.selectedIndex].innerHTML;
  204. }
  205. function SelectCam(){
  206. var select = document.getElementById("chooseCamSel");
  207. window.deviceId = select.value;
  208. window.WEBCAM_SETTINGS.video['deviceId'] = deviceId;
  209. //console.log(window.WEBCAM_SETTINGS);
  210. iTracker.WEBCAM_NAME = select.options[select.selectedIndex].innerHTML;
  211. }
  212. function ShowError(error){
  213. document.getElementById("errorDiv").style.display = "flex";
  214. document.getElementById("errorText").innerHTML = error;
  215. }
  216. function ShowScreenshot(dataUrl){
  217. document.getElementById("screenshotDiv").style.display = "flex";
  218. document.getElementById("screenshotImg").src = dataUrl;
  219. document.getElementById("screenshotImg").style.width = "80vw";
  220. document.getElementById("screenshotImg").style.height = 80 / window.innerWidth * window.innerHeight + "vw";
  221. }
  222. function ShowConfirmUrl(url){
  223. document.getElementById("confirmUrlDiv").style.display = "flex";
  224. window.newUrlString = url;
  225. document.getElementById("confirmUrlText").innerText = "Are you sure you want to visit " + url;
  226. }
  227. window.ITRACKER_GLOBALS = {
  228. //place global settings here
  229. INTERNAL_SMOOTHFACTOR_POS: .075,
  230. }
  231. </script>
  232. </body>
  233. </html>