index.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 | WXGame</title>
  7. <link rel="stylesheet" href="TemplateData/style.css">
  8. <style>
  9. html{
  10. height: -webkit-fill-available;
  11. position: fixed; /* 或者 absolute,取决于你的布局需求 */
  12. top: 0;
  13. left: 0;
  14. width: 100%;
  15. height: 100%;
  16. background-image: url('StreamingAssets/ui/bg.png');
  17. background-size: cover;
  18. background-position: center;
  19. background-repeat: no-repeat;
  20. }
  21. body {
  22. margin: 0;
  23. padding: 0;
  24. min-height: 100vh;
  25. min-height: -webkit-fill-available;
  26. width: 100vw;
  27. overflow: hidden;
  28. }
  29. .ctaDiv {
  30. display: flex;
  31. flex-direction: column;
  32. align-items: center;
  33. justify-content: center;
  34. position: absolute;
  35. left: 0;
  36. right: 0;
  37. top: 0;
  38. bottom: 0;
  39. z-index: 99;
  40. } .image-button {
  41. background-image: url('bgbt.png'); /* 替换为您的图片路径 */
  42. background-size: cover; /* 背景图片覆盖整个按钮 */
  43. background-color: transparent;
  44. border: none;
  45. padding: 40px 150px; /* 内边距 */
  46. position: relative; top: 210px; left: -30px;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <!--IMAGETARGETS-->
  52. <imagetarget id='bear' src='targets/bear.png'></imagetarget>
  53. <imagetarget id='bfly' src='targets/bfly.png'></imagetarget>
  54. <imagetarget id='flower' src='targets/flower.png'></imagetarget>
  55. <imagetarget id='lady' src='targets/lady.png'></imagetarget>
  56. <imagetarget id='reel' src='targets/reel.png'></imagetarget>
  57. <imagetarget id='rmn' src='targets/rmn.png'></imagetarget>
  58. <imagetarget id='kkk' src='targets/kkk.png'></imagetarget>
  59. <video id="webcam-video" muted autoplay playsinline style="width:1px;position:absolute"></video>
  60. <!-- <video id="webcam-video" muted autoplay playsinline style="width:100%; height:100%; object-fit:cover; position:absolute"></video> -->
  61. <canvas id="video-canvas" style="width:100%; height:100%; object-fit:cover; position:absolute"></canvas>
  62. <div id="startARDiv" class="ctaDiv">
  63. <!-- <select id="chooseCamSel" style="display: block;" onchange="SelectCam()"></select>
  64. <p style="text-align: center; width:60vw;">This augmented reality experience requires access to your device's camera</p>
  65. <button class="image-button" id="startARButton" onclick="StartAR()" style="display:none"> </button>
  66. <div id="unity-loading-bar2" style="display:block">
  67. <div id="unity-progress-bar-empty2" style="display:block; height: 18px; margin-top: 10px; margin-left: 6.5px; background: url('TemplateData/progress-bar-empty-light.png') no-repeat center ">
  68. <div id="unity-progress-bar-full2" style="display:block;position: relative; top: 210px;width: 0%; height: 18px; margin-top: 10px; background: url('TemplateData/progress-bar-full-light.png') no-repeat center "></div>
  69. </div>
  70. </div>-->
  71. </div>
  72. <div id="screenshotDiv" style="display: none;" class="ctaDiv">
  73. <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);">
  74. <img id="screenshotImg" src="" alt="screenshot" style="width:80vw; height:80vh">
  75. <button onclick="document.getElementById('screenshotDiv').style.display = 'none';" style="position:absolute; transform:translateY(-100%); right:0; top:0">Close</button>
  76. </div>
  77. </div>
  78. <div id="confirmUrlDiv" style="display: none;" class="ctaDiv">
  79. <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;">
  80. <p id="confirmUrlText" style="text-align: center; width:80%; overflow: hidden; text-overflow: ellipsis;">Are you sure you want to visit url.com?</p>
  81. <div>
  82. <button onclick="window.open(newUrlString, '_blank').focus(); document.getElementById('confirmUrlDiv').style.display = 'none'">VISIT SITE</button>
  83. <button onclick="document.getElementById('confirmUrlDiv').style.display = 'none'">GO BACK</button>
  84. </div>
  85. </div>
  86. </div>
  87. <div id="errorDiv" class="ctaDiv" style="display: none; background:#aaa">
  88. <p id="errorText" style="text-align: center; width:60vw; color:white"></p>
  89. </div>
  90. <div id="unity-container" class="unity-mobile">
  91. <canvas id="unity-canvas" style="width: 100%; height: 100%; background: #0000; z-index: -99"></canvas>
  92. <canvas id="video-canvas"></canvas>
  93. </div>
  94. <script src="arcamera.js" type="text/javascript"></script>
  95. <script src="itracker.js" type="text/javascript"></script>
  96. <script src="wtracker.js" type="text/javascript"></script>
  97. <script src="Build/wxgame.loader.js"></script>
  98. <script>
  99. var initialize = async() =>{
  100. var unityCanvas = document.querySelector("#unity-canvas");
  101. var videoCanvas = document.querySelector("#video-canvas");
  102. window.arCamera = new ARCamera(unityCanvas, videoCanvas);
  103. window.wTracker = new WorldTracker(arCamera);
  104. try{
  105. await wTracker.initialize("./opencv.js");
  106. console.log("World tracker initialized!");
  107. }
  108. catch (error) {
  109. console.error("Failed to initialize world tracker. Are you missing opencv.js?", error);
  110. ShowError("Failed to initialize the World Tracker.\n" + error);
  111. return;
  112. }
  113. window.iTracker = new ImageTracker(arCamera);
  114. try{
  115. await window.iTracker.initialize();
  116. console.log("ImageTracker initialized!");
  117. }
  118. catch{
  119. console.error("Failed to initialize ImageTracker. Are you missing opencv.js? " + error);
  120. ShowError("Failed to initialize the experience.");
  121. return;
  122. }
  123. await LoadWebcams();
  124. StartAR();
  125. // document.getElementById("startARButton").style.display = "block";
  126. }
  127. initialize();
  128. var container = document.querySelector("#unity-container");
  129. var canvas = document.querySelector("#unity-canvas");
  130. // var loadingBar = document.querySelector("#unity-loading-bar2");
  131. // var progressBarFull = document.querySelector("#unity-progress-bar-full2");
  132. function StartAR() {
  133. canvas.style.width = window.innerWidth + "px";
  134. canvas.style.height = window.innerHeight + "px";
  135. document.getElementById("startARDiv").style.display = "none";
  136. createUnityInstance(document.querySelector("#unity-canvas"), {
  137. dataUrl: "Build/wxgame.data.unityweb",
  138. frameworkUrl: "Build/wxgame.framework.js.unityweb",
  139. codeUrl: "Build/wxgame.wasm.unityweb",
  140. streamingAssetsUrl: "StreamingAssets",
  141. companyName: "DefaultCompany",
  142. productName: "WXGame",
  143. productVersion: "0.1",
  144. //webglContextAttributes: { "preserveDrawingBuffer": true },
  145. // matchWebGLToCanvasSize: false, // Uncomment this to separately control WebGL canvas render size and DOM element size.
  146. // devicePixelRatio: 1, // Uncomment this to override low DPI rendering on high DPI displays.
  147. },
  148. (progress) => {
  149. // progressBarFull.style.width = 100 * progress + "%";
  150. }
  151. ).then((unityInstance) => {
  152. window.unityInstance = unityInstance;
  153. RequestWebcam();
  154. // loadingBar.style.display = "none";
  155. });
  156. StartMotionSensors();
  157. StartGPS();
  158. //loadingBar.style.display = "block";
  159. }
  160. //Set Facing Mode here ('environment', 'user', '')
  161. window.unityFacingMode = "environment"
  162. window.WEBCAM_SETTINGS = {
  163. video: {
  164. facingMode: unityFacingMode,
  165. },
  166. audio: false
  167. };
  168. window.requestingForPermissions = false;
  169. async function RequestWebcam(){
  170. window.requestingForPermissions = true;
  171. try{
  172. window.webcamStream = await navigator.mediaDevices.getUserMedia(window.WEBCAM_SETTINGS);
  173. console.log("Webcam access granted");
  174. requestingForPermissions = false;
  175. }
  176. catch (err) {
  177. //user denied camera permission - show error panel
  178. console.error("getUserMedia error - " , err);
  179. ShowError("Failed to start the experience. Camera permission was denied");
  180. window.requestingForPermissions = false;
  181. }
  182. }
  183. async function StartWebcam(){
  184. console.log("StartWebcam")
  185. while (window.requestingForPermissions) {
  186. // Wait until requestingForPermissions becomes true.
  187. console.log("Waiting for permissions...");
  188. await new Promise(resolve => setTimeout(resolve, 100)); // Adjust the delay time as needed.
  189. }
  190. console.log("Got Permissions");
  191. if(window.webcamStream)
  192. {
  193. const video = document.querySelector("#webcam-video");
  194. video.srcObject = webcamStream;
  195. try {
  196. await arCamera.startWebcam(video);
  197. console.log("Webcam started successfully");
  198. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamSuccess');
  199. }
  200. catch(err){
  201. console.error("Webcam failed to start - ", err);
  202. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamFail');
  203. }
  204. }
  205. else{
  206. console.error("Webcam failed to start - permission not yet granted");
  207. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamFail');
  208. }
  209. }
  210. async function LoadWebcams(){
  211. let camDevices = [];
  212. // let backCams = [];
  213. let devices = await navigator.mediaDevices.enumerateDevices();
  214. var ctr = 0;
  215. devices.forEach(mediaDevice => {
  216. if (mediaDevice.kind === 'videoinput') {
  217. if(window.unityFacingMode == 'environment' && !mediaDevice.label.includes('facing front')){
  218. //back cam only
  219. camDevices.push(mediaDevice);
  220. }
  221. else if(window.unityFacingMode == 'user' && mediaDevice.label.includes('facing front')){
  222. //front cam only
  223. camDevices.push(mediaDevice);
  224. }
  225. else{
  226. //back and front
  227. camDevices.push(mediaDevice);
  228. }
  229. ctr++;
  230. }
  231. });
  232. // var select = document.getElementById("chooseCamSel");
  233. // select.style.display = "block";
  234. var count = 0;
  235. //reverse array because some Android phones can't distinguish front and back cams at first load
  236. //and when this happens, most of the time, front cam goes first and back cam goes last
  237. camDevices = camDevices.reverse();
  238. camDevices.forEach(mediaDevice => {
  239. const option = document.createElement('option');
  240. option.value = mediaDevice.deviceId;
  241. let label = `Camera ${count}`;
  242. if (mediaDevice.label) {
  243. label = mediaDevice.label
  244. }
  245. const textNode = document.createTextNode(label);
  246. option.appendChild(textNode);
  247. // select.appendChild(option);
  248. count++;
  249. });
  250. // iTracker.WEBCAM_NAME = select.options[select.selectedIndex].innerHTML;
  251. }
  252. function SelectCam(){
  253. var select = document.getElementById("chooseCamSel");
  254. window.deviceId = select.value;
  255. window.WEBCAM_SETTINGS.video['deviceId'] = deviceId;
  256. //console.log(window.WEBCAM_SETTINGS);
  257. // iTracker.WEBCAM_NAME = select.options[select.selectedIndex].innerHTML;
  258. }
  259. async function FlipCam(){
  260. arCamera.stopWebcam();
  261. window.WEBCAM_SETTINGS.video.deviceId = '';
  262. if(window.WEBCAM_SETTINGS.video.facingMode == 'user'){
  263. window.WEBCAM_SETTINGS.video.facingMode = 'environment';
  264. arCamera.setFlipped(false);
  265. }
  266. else{
  267. window.WEBCAM_SETTINGS.video.facingMode = 'user';
  268. arCamera.setFlipped(true);
  269. }
  270. window.webcamStream = await navigator.mediaDevices.getUserMedia(window.WEBCAM_SETTINGS);
  271. const video = document.querySelector("#webcam-video");
  272. video.srcObject = webcamStream;
  273. await arCamera.startWebcam(video);
  274. }
  275. function ShowError(error){
  276. document.getElementById("errorDiv").style.display = "flex";
  277. document.getElementById("errorText").innerHTML = error;
  278. }
  279. function ShowScreenshot(dataUrl){
  280. document.getElementById("screenshotDiv").style.display = "flex";
  281. document.getElementById("screenshotImg").src = dataUrl;
  282. document.getElementById("screenshotImg").style.width = "80vw";
  283. document.getElementById("screenshotImg").style.height = 80 / window.innerWidth * window.innerHeight + "vw";
  284. }
  285. function ShowConfirmUrl(url){
  286. document.getElementById("confirmUrlDiv").style.display = "flex";
  287. window.newUrlString = url;
  288. document.getElementById("confirmUrlText").innerText = "Are you sure you want to visit " + url;
  289. }
  290. window.ITRACKER_GLOBALS = {
  291. //place global settings here
  292. INTERNAL_SMOOTHFACTOR_POS: .075,
  293. }
  294. function StartMotionSensors(){
  295. window.wTracker.startAngles()
  296. .then(()=>{
  297. console.log("Motion sensors started");
  298. })
  299. .catch(error=>{
  300. console.error("Failed to start motion sensors - " + error);
  301. ShowError("Failed to start the experience. " + error);
  302. });
  303. }
  304. function StartGPS(){
  305. window.wTracker.startGPS()
  306. .then((pos)=>{
  307. console.log("GPS started", pos);
  308. })
  309. .catch(error=>{
  310. console.error("Failed to start GPS - " + error);
  311. //ShowError("Failed to start GPS " + error);
  312. });
  313. }
  314. </script>
  315. </body>
  316. </html>