index.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 initialize = async() =>{
  78. var unityCanvas = document.querySelector("#unity-canvas");
  79. var videoCanvas = document.querySelector("#video-canvas");
  80. window.arCamera = new ARCamera(unityCanvas, videoCanvas);
  81. window.iTracker = new ImageTracker(arCamera);
  82. try{
  83. await window.iTracker.initialize();
  84. console.log("ImageTracker initialized!");
  85. }
  86. catch{
  87. console.error("Failed to initialize ImageTracker. Are you missing opencv.js? " + error);
  88. ShowError("Failed to initialize the experience.");
  89. return;
  90. }
  91. await LoadWebcams();
  92. document.getElementById("startARButton").style.display = "block";
  93. }
  94. initialize();
  95. var container = document.querySelector("#unity-container");
  96. var canvas = document.querySelector("#unity-canvas");
  97. var loadingBar = document.querySelector("#unity-loading-bar");
  98. var progressBarFull = document.querySelector("#unity-progress-bar-full");
  99. function StartAR() {
  100. canvas.style.width = window.innerWidth + "px";
  101. canvas.style.height = window.innerHeight + "px";
  102. document.getElementById("startARDiv").style.display = "none";
  103. createUnityInstance(document.querySelector("#unity-canvas"), {
  104. dataUrl: "Build/{{{ DATA_FILENAME }}}",
  105. frameworkUrl: "Build/{{{ FRAMEWORK_FILENAME }}}",
  106. #if USE_WASM
  107. codeUrl: "Build/{{{ CODE_FILENAME }}}",
  108. #endif
  109. #if MEMORY_FILENAME
  110. memoryUrl: "Build/{{{ MEMORY_FILENAME }}}",
  111. #endif
  112. #if SYMBOLS_FILENAME
  113. symbolsUrl: "Build/{{{ SYMBOLS_FILENAME }}}",
  114. #endif
  115. streamingAssetsUrl: "StreamingAssets",
  116. companyName: {{{ JSON.stringify(COMPANY_NAME) }}},
  117. productName: {{{ JSON.stringify(PRODUCT_NAME) }}},
  118. productVersion: {{{ JSON.stringify(PRODUCT_VERSION) }}},
  119. //webglContextAttributes: { "preserveDrawingBuffer": true },
  120. // matchWebGLToCanvasSize: false, // Uncomment this to separately control WebGL canvas render size and DOM element size.
  121. // devicePixelRatio: 1, // Uncomment this to override low DPI rendering on high DPI displays.
  122. },
  123. (progress) => {
  124. progressBarFull.style.width = 100 * progress + "%";
  125. }
  126. ).then((unityInstance) => {
  127. window.unityInstance = unityInstance;
  128. RequestWebcam();
  129. loadingBar.style.display = "none";
  130. });
  131. loadingBar.style.display = "block";
  132. }
  133. //Set Facing Mode here ('environment', 'user', '')
  134. window.unityFacingMode = "environment";
  135. window.WEBCAM_SETTINGS = {
  136. video: {
  137. facingMode: unityFacingMode,
  138. },
  139. audio: false
  140. };
  141. window.requestingForPermissions = false;
  142. async function RequestWebcam(){
  143. window.requestingForPermissions = true;
  144. try{
  145. window.webcamStream = await navigator.mediaDevices.getUserMedia(window.WEBCAM_SETTINGS);
  146. console.log("Webcam access granted");
  147. requestingForPermissions = false;
  148. }
  149. catch (err) {
  150. //user denied camera permission - show error panel
  151. console.error("getUserMedia error - " , err);
  152. ShowError("Failed to start the experience. Camera permission was denied");
  153. window.requestingForPermissions = false;
  154. }
  155. }
  156. async function StartWebcam(){
  157. console.log("StartWebcam")
  158. while (window.requestingForPermissions) {
  159. // Wait until requestingForPermissions becomes true.
  160. console.log("Waiting for permissions...");
  161. await new Promise(resolve => setTimeout(resolve, 100)); // Adjust the delay time as needed.
  162. }
  163. console.log("Got Permissions");
  164. if(window.webcamStream)
  165. {
  166. const video = document.querySelector("#webcam-video");
  167. video.srcObject = webcamStream;
  168. try {
  169. await arCamera.startWebcam(video);
  170. console.log("Webcam started successfully");
  171. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamSuccess');
  172. }
  173. catch(err){
  174. console.error("Webcam failed to start - ", err);
  175. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamFail');
  176. }
  177. }
  178. else{
  179. console.error("Webcam failed to start - permission not yet granted");
  180. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamFail');
  181. }
  182. }
  183. async function LoadWebcams(){
  184. let camDevices = [];
  185. // let backCams = [];
  186. let devices = await navigator.mediaDevices.enumerateDevices();
  187. var ctr = 0;
  188. devices.forEach(mediaDevice => {
  189. if (mediaDevice.kind === 'videoinput') {
  190. if(window.unityFacingMode == 'environment' && !mediaDevice.label.includes('facing front')){
  191. //back cam only
  192. camDevices.push(mediaDevice);
  193. }
  194. else if(window.unityFacingMode == 'user' && mediaDevice.label.includes('facing front')){
  195. //front cam only
  196. camDevices.push(mediaDevice);
  197. }
  198. else{
  199. //back and front
  200. camDevices.push(mediaDevice);
  201. }
  202. ctr++;
  203. }
  204. });
  205. var select = document.getElementById("chooseCamSel");
  206. select.style.display = "block";
  207. var count = 0;
  208. //reverse array because some Android phones can't distinguish front and back cams at first load
  209. //and when this happens, most of the time, front cam goes first and back cam goes last
  210. camDevices = camDevices.reverse();
  211. camDevices.forEach(mediaDevice => {
  212. const option = document.createElement('option');
  213. option.value = mediaDevice.deviceId;
  214. let label = `Camera ${count}`;
  215. if (mediaDevice.label) {
  216. label = mediaDevice.label
  217. }
  218. const textNode = document.createTextNode(label);
  219. option.appendChild(textNode);
  220. select.appendChild(option);
  221. count++;
  222. });
  223. iTracker.WEBCAM_NAME = select.options[select.selectedIndex].innerHTML;
  224. }
  225. function SelectCam(){
  226. var select = document.getElementById("chooseCamSel");
  227. window.deviceId = select.value;
  228. window.WEBCAM_SETTINGS.video['deviceId'] = deviceId;
  229. //console.log(window.WEBCAM_SETTINGS);
  230. iTracker.WEBCAM_NAME = select.options[select.selectedIndex].innerHTML;
  231. }
  232. async function FlipCam(){
  233. arCamera.stopWebcam();
  234. window.WEBCAM_SETTINGS.video.deviceId = '';
  235. if(window.WEBCAM_SETTINGS.video.facingMode == 'user'){
  236. window.WEBCAM_SETTINGS.video.facingMode = 'environment';
  237. arCamera.setFlipped(false);
  238. }
  239. else{
  240. window.WEBCAM_SETTINGS.video.facingMode = 'user';
  241. arCamera.setFlipped(true);
  242. }
  243. window.webcamStream = await navigator.mediaDevices.getUserMedia(window.WEBCAM_SETTINGS);
  244. const video = document.querySelector("#webcam-video");
  245. video.srcObject = webcamStream;
  246. await arCamera.startWebcam(video);
  247. }
  248. function ShowError(error){
  249. document.getElementById("errorDiv").style.display = "flex";
  250. document.getElementById("errorText").innerHTML = error;
  251. }
  252. function ShowScreenshot(dataUrl){
  253. document.getElementById("screenshotDiv").style.display = "flex";
  254. document.getElementById("screenshotImg").src = dataUrl;
  255. document.getElementById("screenshotImg").style.width = "80vw";
  256. document.getElementById("screenshotImg").style.height = 80 / window.innerWidth * window.innerHeight + "vw";
  257. }
  258. function ShowConfirmUrl(url){
  259. document.getElementById("confirmUrlDiv").style.display = "flex";
  260. window.newUrlString = url;
  261. document.getElementById("confirmUrlText").innerText = "Are you sure you want to visit " + url;
  262. }
  263. window.ITRACKER_GLOBALS = {
  264. //place global settings here
  265. INTERNAL_SMOOTHFACTOR_POS: .075,
  266. }
  267. </script>
  268. </body>
  269. </html>