index.html 12 KB

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