index.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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();
  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. window.WEBCAM_SETTINGS = {
  125. video: {
  126. facingMode: "environment",
  127. },
  128. audio: false
  129. };
  130. window.requestingForCameraPermission = false;
  131. async function RequestWebcam(){
  132. window.requestingForCameraPermission = true;
  133. try{
  134. window.webcamStream = await navigator.mediaDevices.getUserMedia(window.WEBCAM_SETTINGS);
  135. console.log("Webcam access granted");
  136. requestingForCameraPermission = false;
  137. }
  138. catch (err) {
  139. //user denied camera permission - show error panel
  140. console.error("getUserMedia error - " , err);
  141. ShowError("Failed to start the experience. Camera permission was denied");
  142. window.requestingForCameraPermission = false;
  143. }
  144. }
  145. async function StartWebcam(){
  146. console.log("StartWebcam")
  147. while (window.requestingForCameraPermission) {
  148. // Wait until requestingForCameraPermission becomes true.
  149. console.log("Waiting for permissions...");
  150. await new Promise(resolve => setTimeout(resolve, 100)); // Adjust the delay time as needed.
  151. }
  152. console.log("Got Permissions");
  153. if(window.webcamStream)
  154. {
  155. const video = document.querySelector("#webcam-video");
  156. video.srcObject = webcamStream;
  157. try {
  158. await arCamera.startWebcam(video);
  159. console.log("Webcam started successfully");
  160. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamSuccess');
  161. }
  162. catch(err){
  163. console.error("Webcam failed to start - ", err);
  164. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamFail');
  165. }
  166. }
  167. else{
  168. console.error("Webcam failed to start - permission not yet granted");
  169. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamFail');
  170. }
  171. }
  172. async function LoadWebcams(){
  173. let backCams = [];
  174. let devices = await navigator.mediaDevices.enumerateDevices();
  175. devices.forEach(mediaDevice => {
  176. if (mediaDevice.kind === 'videoinput' && !mediaDevice.label.includes('facing front')) {
  177. backCams.push(mediaDevice);
  178. }
  179. });
  180. var select = document.getElementById("chooseCamSel");
  181. select.style.display = "block";
  182. var count = 0;
  183. //reverse array because some Android phones can't distinguish front and back cams at first load
  184. //and when this happens, most of the time, front cam goes first and back cam goes last
  185. backCams = backCams.reverse();
  186. backCams.forEach(mediaDevice => {
  187. const option = document.createElement('option');
  188. option.value = mediaDevice.deviceId;
  189. let label = `Camera ${count}`;
  190. if (mediaDevice.label) {
  191. label = mediaDevice.label
  192. }
  193. const textNode = document.createTextNode(label);
  194. option.appendChild(textNode);
  195. select.appendChild(option);
  196. count++;
  197. });
  198. wTracker.WEBCAM_NAME = select.options[select.selectedIndex].innerHTML;
  199. }
  200. function SelectCam(){
  201. var select = document.getElementById("chooseCamSel");
  202. window.deviceId = select.value;
  203. window.WEBCAM_SETTINGS.video['deviceId'] = deviceId;
  204. //console.log(window.WEBCAM_SETTINGS);
  205. wTracker.WEBCAM_NAME = select.options[select.selectedIndex].innerHTML;
  206. }
  207. function StartMotionSensors(){
  208. window.wTracker.startAngles()
  209. .then(()=>{
  210. console.log("Motion sensors started");
  211. })
  212. .catch(error=>{
  213. console.error("Failed to start motion sensors - " + error);
  214. ShowError("Failed to start the experience. " + error);
  215. });
  216. }
  217. function StartGPS(){
  218. window.wTracker.startGPS()
  219. .then((pos)=>{
  220. console.log("GPS started", pos);
  221. })
  222. .catch(error=>{
  223. console.error("Failed to start GPS - " + error);
  224. //ShowError("Failed to start GPS " + error);
  225. });
  226. }
  227. function ShowError(error){
  228. document.getElementById("errorDiv").style.display = "flex";
  229. document.getElementById("errorText").innerHTML = error;
  230. }
  231. function ShowScreenshot(dataUrl){
  232. document.getElementById("screenshotDiv").style.display = "flex";
  233. document.getElementById("screenshotImg").src = dataUrl;
  234. document.getElementById("screenshotImg").style.width = "80vw";
  235. document.getElementById("screenshotImg").style.height = 80 / window.innerWidth * window.innerHeight + "vw";
  236. }
  237. </script>
  238. </body>
  239. </html>