index.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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>{{{ PRODUCT_NAME }}}</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('StreamingAssets/ui/initbt.png'); /* 替换为您的图片路径 */
  42. background-size: cover; /* 背景图片覆盖整个按钮 */
  43. background-color: transparent;
  44. border: none;
  45. padding: 30px 130px; /* 内边距 */
  46. position: relative; top: 210px; left: -30px;
  47. }
  48. .loading {
  49. position: relative;
  50. top: 540px;
  51. width: 30px;
  52. height: 30px;
  53. border: 8px solid #f3f3f3;
  54. border-top: 8px solid #B0BBB6;
  55. border-radius: 50%;
  56. animation: spin 2s linear infinite;
  57. margin: 100px auto;
  58. }.progressBar {
  59. position: fixed;
  60. bottom: 0;
  61. width: 100%;
  62. }
  63. .progress {
  64. width: 100%;
  65. height:40px;
  66. }
  67. @keyframes spin {
  68. 0% { transform: rotate(0deg); }
  69. 100% { transform: rotate(360deg); }
  70. }
  71. </style>
  72. </head>
  73. <body>
  74. <!--IMAGETARGETS-->
  75. <video id="webcam-video" muted autoplay playsinline style="width:1px;position:absolute"></video>
  76. <!-- <video id="webcam-video" muted autoplay playsinline style="width:100%; height:100%; object-fit:cover; position:absolute"></video> -->
  77. <canvas id="video-canvas" style="width:100%; height:100%; object-fit:cover; position:absolute">
  78. </canvas>
  79. <div id="startARDiv" class="ctaDiv">
  80. <!-- <select id="chooseCamSel" style="display: block;" onchange="SelectCam()"></select>
  81. <p style="text-align: center; width:60vw;">This augmented reality experience requires access to your device's camera</p>
  82. <div id="unity-loading-bar2" style="display:block">
  83. <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 ">
  84. <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>
  85. </div>
  86. </div>-->
  87. <button class="image-button" id="startARButton" onclick="StartAR()" style="display:none"> </button>
  88. </div>
  89. <div id="screenshotDiv" style="display: none;" class="ctaDiv">
  90. <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);">
  91. <img id="screenshotImg" src="" alt="screenshot" style="width:80vw; height:80vh">
  92. <button onclick="document.getElementById('screenshotDiv').style.display = 'none';" style="position:absolute; transform:translateY(-100%); right:0; top:0">Close</button>
  93. </div>
  94. </div>
  95. <div id="confirmUrlDiv" style="display: none;" class="ctaDiv">
  96. <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;">
  97. <p id="confirmUrlText" style="text-align: center; width:80%; overflow: hidden; text-overflow: ellipsis;">Are you sure you want to visit url.com?</p>
  98. <div>
  99. <button onclick="window.open(newUrlString, '_blank').focus(); document.getElementById('confirmUrlDiv').style.display = 'none'">VISIT SITE</button>
  100. <button onclick="document.getElementById('confirmUrlDiv').style.display = 'none'">GO BACK</button>
  101. </div>
  102. </div>
  103. </div>
  104. <div id="errorDiv" class="ctaDiv" style="display: none; background:#aaa">
  105. <p id="errorText" style="text-align: center; width:60vw; color:white"></p>
  106. <button onclick="refreshPage()">刷新页面</button>
  107. </div>
  108. <div id="unity-container" class="unity-mobile">
  109. <canvas id="unity-canvas" style="width: 100%; height: 100%; background: #0000; z-index: -99">
  110. </canvas>
  111. <canvas id="video-canvas">
  112. </canvas>
  113. </div>
  114. <div class="progressBar" id="progressBar" style="display:none;">
  115. <id id="myProgress2" style="text-align: center; color: #2F4F4F;">0</id>
  116. <progress class="progress" id="myProgress" value="0" max="100"></progress>
  117. </div>
  118. <!--
  119. <div id="startmusic" class="music1">
  120. <button onclick="playMusic()">播放背景音乐</button>
  121. <button onclick="stopMusic()">停止背景音乐</button>
  122. </div>-->
  123. <div class="loading" id="loadingbar" style="display:none;"></div>
  124. <script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
  125. <script src="arcamera.js" type="text/javascript"></script>
  126. <script src="itracker.js" type="text/javascript"></script>
  127. <script src="wtracker.js" type="text/javascript"></script>
  128. <script src="Build/{{{ LOADER_FILENAME }}}"></script>
  129. <script> // JavaScript用于更新进度条的值
  130. function updateProgress(value) {
  131. var progressBar = document.getElementById("myProgress");
  132. progressBar.value = value;
  133. var progressBar2 = document.getElementById("myProgress2");
  134. progressBar2.textContent = value + '%';
  135. }
  136. function changeBackground() {
  137. var body = document.querySelector("html");
  138. body.style.backgroundImage = "url('StreamingAssets/ui/black.jpg')";
  139. }
  140. function refreshPage() {
  141. location.reload();
  142. }
  143. var initialize = async() =>{
  144. var unityCanvas = document.querySelector("#unity-canvas");
  145. var videoCanvas = document.querySelector("#video-canvas");
  146. window.arCamera = new ARCamera(unityCanvas, videoCanvas);
  147. window.wTracker = new WorldTracker(arCamera);
  148. try{
  149. await wTracker.initialize("./opencv.js");
  150. console.log("World tracker initialized!");
  151. }
  152. catch (error) {
  153. console.error("Failed to initialize world tracker. Are you missing opencv.js?", error);
  154. ShowError("Failed to initialize the World Tracker.\n" + error);
  155. return;
  156. }
  157. window.iTracker = new ImageTracker(arCamera);
  158. try{
  159. await window.iTracker.initialize();
  160. console.log("ImageTracker initialized!");
  161. }
  162. catch{
  163. console.error("Failed to initialize ImageTracker. Are you missing opencv.js? " + error);
  164. ShowError("Failed to initialize the experience.");
  165. return;
  166. }
  167. await LoadWebcams();
  168. // StartAR();
  169. document.getElementById("startARButton").style.display = "block";
  170. }
  171. initialize();
  172. var container = document.querySelector("#unity-container");
  173. var canvas = document.querySelector("#unity-canvas");
  174. var loadingBar = document.getElementById("progressBar");
  175. var progressBarFull = document.querySelector("#unity-progress-bar-full");
  176. function StartAR() {
  177. canvas.style.width = window.innerWidth + "px";
  178. canvas.style.height = window.innerHeight + "px";
  179. document.getElementById("startARDiv").style.display = "none";
  180. createUnityInstance(document.querySelector("#unity-canvas"), {
  181. dataUrl: "Build/{{{ DATA_FILENAME }}}",
  182. frameworkUrl: "Build/{{{ FRAMEWORK_FILENAME }}}",
  183. #if USE_WASM
  184. codeUrl: "Build/{{{ CODE_FILENAME }}}",
  185. #endif
  186. #if MEMORY_FILENAME
  187. memoryUrl: "Build/{{{ MEMORY_FILENAME }}}",
  188. #endif
  189. #if SYMBOLS_FILENAME
  190. symbolsUrl: "Build/{{{ SYMBOLS_FILENAME }}}",
  191. #endif
  192. streamingAssetsUrl: "StreamingAssets",
  193. companyName: {{{ JSON.stringify(COMPANY_NAME) }}},
  194. productName: {{{ JSON.stringify(PRODUCT_NAME) }}},
  195. productVersion: {{{ JSON.stringify(PRODUCT_VERSION) }}},
  196. //webglContextAttributes: { "preserveDrawingBuffer": true },
  197. // matchWebGLToCanvasSize: false, // Uncomment this to separately control WebGL canvas render size and DOM element size.
  198. // devicePixelRatio: 1, // Uncomment this to override low DPI rendering on high DPI displays.
  199. },
  200. (progress) => {
  201. // progressBarFull.style.width = 100 * progress + "%";
  202. updateProgress(100 * progress);
  203. }
  204. ).then((unityInstance) => {
  205. window.unityInstance = unityInstance;
  206. loadingBar.style.display = "none";
  207. changeBackground();
  208. }).catch((message) => {
  209. // alert("加载出错重新加载"+message);
  210. ShowError("网络异常请重新加载");
  211. });
  212. StartMotionSensors();
  213. StartGPS();
  214. loadingBar.style.display = "block";
  215. }
  216. //Set Facing Mode here ('environment', 'user', '')
  217. window.unityFacingMode = "environment"
  218. window.WEBCAM_SETTINGS = {
  219. video: {
  220. facingMode: unityFacingMode,
  221. },
  222. audio: false
  223. };
  224. window.requestingForPermissions = false;
  225. async function RequestWebcam(){
  226. window.requestingForPermissions = true;
  227. try{
  228. window.webcamStream = await navigator.mediaDevices.getUserMedia(window.WEBCAM_SETTINGS);
  229. console.log("Webcam access granted");
  230. requestingForPermissions = false;
  231. }
  232. catch (err) {
  233. //user denied camera permission - show error panel
  234. console.error("getUserMedia error - " , err);
  235. ShowError("允许权限并刷新页面后继续导览");
  236. window.requestingForPermissions = false;
  237. }
  238. }
  239. function ShowWx(urls)
  240. {
  241. console.log("Waiting for permissions..."+urls);
  242. wx.miniProgram.navigateTo({url: urls});
  243. }
  244. async function StartWebcam(){
  245. await RequestWebcam();
  246. console.log("StartWebcam")
  247. while (window.requestingForPermissions) {
  248. // Wait until requestingForPermissions becomes true.
  249. console.log("Waiting for permissions...");
  250. await new Promise(resolve => setTimeout(resolve, 100)); // Adjust the delay time as needed.
  251. }
  252. console.log("Got Permissions");
  253. if(window.webcamStream)
  254. {
  255. const video = document.querySelector("#webcam-video");
  256. video.srcObject = webcamStream;
  257. try {
  258. await arCamera.startWebcam(video);
  259. console.log("Webcam started successfully");
  260. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamSuccess');
  261. }
  262. catch(err){
  263. console.error("Webcam failed to start - ", err);
  264. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamFail');
  265. }
  266. }
  267. else{
  268. console.error("Webcam failed to start - permission not yet granted");
  269. window.unityInstance.SendMessage('ARCamera', 'OnStartWebcamFail');
  270. }
  271. }
  272. async function LoadWebcams(){
  273. let camDevices = [];
  274. // let backCams = [];
  275. let devices = await navigator.mediaDevices.enumerateDevices();
  276. var ctr = 0;
  277. devices.forEach(mediaDevice => {
  278. if (mediaDevice.kind === 'videoinput') {
  279. if(window.unityFacingMode == 'environment' && !mediaDevice.label.includes('facing front')){
  280. //back cam only
  281. camDevices.push(mediaDevice);
  282. }
  283. else if(window.unityFacingMode == 'user' && mediaDevice.label.includes('facing front')){
  284. //front cam only
  285. camDevices.push(mediaDevice);
  286. }
  287. else{
  288. //back and front
  289. camDevices.push(mediaDevice);
  290. }
  291. ctr++;
  292. }
  293. });
  294. // var select = document.getElementById("chooseCamSel");
  295. // select.style.display = "block";
  296. var count = 0;
  297. //reverse array because some Android phones can't distinguish front and back cams at first load
  298. //and when this happens, most of the time, front cam goes first and back cam goes last
  299. camDevices = camDevices.reverse();
  300. camDevices.forEach(mediaDevice => {
  301. const option = document.createElement('option');
  302. option.value = mediaDevice.deviceId;
  303. let label = `Camera ${count}`;
  304. if (mediaDevice.label) {
  305. label = mediaDevice.label
  306. }
  307. const textNode = document.createTextNode(label);
  308. option.appendChild(textNode);
  309. // select.appendChild(option);
  310. count++;
  311. });
  312. // iTracker.WEBCAM_NAME = select.options[select.selectedIndex].innerHTML;
  313. }
  314. function SelectCam(){
  315. var select = document.getElementById("chooseCamSel");
  316. window.deviceId = select.value;
  317. window.WEBCAM_SETTINGS.video['deviceId'] = deviceId;
  318. //console.log(window.WEBCAM_SETTINGS);
  319. // iTracker.WEBCAM_NAME = select.options[select.selectedIndex].innerHTML;
  320. }
  321. async function FlipCam(){
  322. arCamera.stopWebcam();
  323. window.WEBCAM_SETTINGS.video.deviceId = '';
  324. if(window.WEBCAM_SETTINGS.video.facingMode == 'user'){
  325. window.WEBCAM_SETTINGS.video.facingMode = 'environment';
  326. arCamera.setFlipped(false);
  327. }
  328. else{
  329. window.WEBCAM_SETTINGS.video.facingMode = 'user';
  330. arCamera.setFlipped(true);
  331. }
  332. window.webcamStream = await navigator.mediaDevices.getUserMedia(window.WEBCAM_SETTINGS);
  333. const video = document.querySelector("#webcam-video");
  334. video.srcObject = webcamStream;
  335. await arCamera.startWebcam(video);
  336. }
  337. function ShowError(error){
  338. document.getElementById("errorDiv").style.display = "flex";
  339. document.getElementById("errorText").innerHTML = error;
  340. }
  341. function ShowScreenshot(dataUrl){
  342. document.getElementById("screenshotDiv").style.display = "flex";
  343. document.getElementById("screenshotImg").src = dataUrl;
  344. document.getElementById("screenshotImg").style.width = "80vw";
  345. document.getElementById("screenshotImg").style.height = 80 / window.innerWidth * window.innerHeight + "vw";
  346. }
  347. function ShowConfirmUrl(url){
  348. document.getElementById("confirmUrlDiv").style.display = "flex";
  349. window.newUrlString = url;
  350. document.getElementById("confirmUrlText").innerText = "Are you sure you want to visit " + url;
  351. }
  352. window.ITRACKER_GLOBALS = {
  353. //place global settings here
  354. INTERNAL_SMOOTHFACTOR_POS: .075,
  355. }
  356. function StartMotionSensors(){
  357. window.wTracker.startAngles()
  358. .then(()=>{
  359. console.log("Motion sensors started");
  360. })
  361. .catch(error=>{
  362. console.error("Failed to start motion sensors - " + error);
  363. ShowError("允许权限并刷新页面后继续导览");
  364. });
  365. }
  366. function StartGPS(){
  367. window.wTracker.startGPS()
  368. .then((pos)=>{
  369. console.log("GPS started", pos);
  370. })
  371. .catch(error=>{
  372. console.error("Failed to start GPS - " + error);
  373. //ShowError("Failed to start GPS " + error);
  374. });
  375. }
  376. </script>
  377. </body>
  378. </html>