var rotationOK = false; var isMuted = false; var isLowPowerMode = false; var timeOutIntro1 = 7000; var intro1Timer; var timeoutLocalTimer = 60000; var timeout = 300000; var localTimer, finalTimer; var timeoutVr = 10000; var experienceOnceRunned = false; var sfxRunned = false; var tapToPlacePlane; const tapToPlaceRaycaster = new THREE.Raycaster(); var rayOrigin = new THREE.Vector2(0, 0); var cursorLocation = new THREE.Vector3(0, 0, 0); var ground; var currentNumber; var currentURL; var a_entity_1_model, a_entity_2_model, a_entity_3_4_sky, a_entity_5_model; var XrFaceOn = false; var scene; var video_outro; function errorLowPowerMode() { console.log("errorLowPowerMode"); isLowPowerMode = true; $("#iyp_loading").css("display", "block"); $("#low_power_warning").css("display", "block"); $("#loading_logo").css("display", "none"); } window.addEventListener("pageshow", function (event) { if (event.persisted) { window.location.reload(); } }); $(document).ready(function () { scene = document.querySelector("#main_scene"); //AFRAME.registerComponent("cubemap-realtime", cubeMapRealtimeComponent); AFRAME.registerComponent("xr-light", xrLightComponent); AFRAME.registerSystem("xr-light", xrLightSystem); //AFRAME.registerComponent("swap-cam", swapCamComponent()); scene.renderer.gammaInput = true; scene.renderer.gammaOutput = true; sfx_bgMusic = new Howl({ src: ["./assets/sfx/263_short3_we-re-not-stopping-now_0060.mp3"], autoplay: false, loop: true, }); a_entity_1_model = document.querySelector("#a_entity_1_model"); a_entity_2_model = document.querySelector("#a_entity_2_model"); a_entity_3_4_sky = document.querySelector("#a_entity_3_4_sky"); a_entity_5_model = document.querySelector("#a_entity_5_model"); video_outro = document.getElementById("outro"); $("#btn_restart").click(function () { startAgain(); }); $("#intro_visit_1").click(() => { switchPre360Screen(0); arModeScreen(); }); $("#intro_visit_2").click(() => { switchPre360Screen(1); arModeScreen(); }); $("#intro_visit_3").click(() => { switchPre360Screen(2); arModeScreen(); }); $("#intro_visit_4").click(() => { switchPre360Screen(3); arModeScreen(); }); $("#intro_visit_5").click(() => { switchPre360Screen(4); arModeScreen(); }); //$("#pre_360_screen_left_arrow").click(counter4to0); $("#pre_360_screen_right_arrow").click(counter0to4); $("#intro_text").click(() => { intro(); clearTimeout(intro1Timer); }); // Get Page Elements const shareData = { title: "Experience AR with Flight Centre", text: "The experience link", url: window.location.href, }; /* $("#bottom_bar").click(async () => { try { await navigator.share(shareData); console.log("AR Experience shared successfully"); } catch (err) { console.log("Error: " + err); } }); */ var handleCameraStatusChange = function handleCameraStatusChange(event) { console.log("camera status change", event.detail.status); if (event.detail.status == "hasStream") { $("#iyp_loading").css("display", "none"); rotationOK = true; intro(); } }; document .querySelector("#main_scene") .addEventListener("camerastatuschange", handleCameraStatusChange); $("#outro_restart").click(function () { startAgain(); }); $("#outro_share").click(async () => { try { await navigator.share(shareData); console.log("AR Experience shared successfully"); } catch (err) { console.log("Error: " + err); } }); $("#outro_find_out_more").click(function () { window.location.href = "https://www.flightcentre.co.uk/"; }); $("#pre_360_screen_bottom_left").click(function () { window.location.href = currentURL; }); /* //$("#ar_find_out_more").click(function () { //clearTimeout(runFinalTimer); finalFrame(); }); //$("#ar_share").click(async () => { try { await navigator.share(shareData); console.log("AR Experience shared successfully"); } catch (err) { console.log("Error: " + err); } }); $("#pre_360_screen_click_to_see_all").click(function () { console.log("#pre_360_screen_click_to_see_all"); startAgain(); }); //$("#screen_360_screen_go_back").click(function () { startAgain(); }); */ $("#btn_soundToggle").click(function () { isMuted = !isMuted; if (isMuted == true) { sfx_bgMusic.volume(0); $("#btn_soundToggle").removeClass("on"); $("#btn_soundToggle").addClass("off"); } else { sfx_bgMusic.volume(1); $("#btn_soundToggle").addClass("on"); $("#btn_soundToggle").removeClass("off"); } }); $("#btn_recenter").click(function () { console.log("Recenter"); scene.emit("recenter"); }); document .querySelector("#main_scene") .addEventListener("screenshotready", (e) => { // Hide the flash console.log("screenshotready"); // container.classList.remove('flash') // If an error occurs while trying to take the screenshot, e.detail will be empty. // We could either retry or return control to the user if (!e.detail) { return; } //Polaroid var strMime = "image/jpeg"; var imgData = "data:image/jpeg;base64," + e.detail; var canvasImg = document.createElement("img"); document.body.appendChild(canvasImg); canvasImg.onload = function () { var img_game_button_press_overlay = new Image(); // Create new img element img_game_button_press_overlay.addEventListener( "load", function () { var img2 = new Image(); // Create new img element img2.addEventListener( "load", function () { var img_game_left_overlay = new Image(); // Create new img element img_game_left_overlay.addEventListener( "load", function () { var img_img_logo = new Image(); // Create new img element img_img_logo.addEventListener( "load", function () { var img_game_count_down_bg_opverlay = new Image(); // Create new img element img_game_count_down_bg_opverlay.addEventListener( "load", function () { var img_game_right_overlay = new Image(); // Create new img element img_game_right_overlay.addEventListener( "load", function () { var canvas = $("#polaroidHolder")[0]; var ctx = canvas.getContext("2d"); ctx.canvas.width = canvasImg.width; ctx.canvas.height = canvasImg.height; var canvasWidth = canvas.width; var canvasHeight = canvas.height; ctx.fillStyle = "#ffffff"; ctx.fillRect(0, 0, canvasWidth, canvasHeight); ctx.drawImage( canvasImg, 20, 20, canvasWidth - 40, canvasHeight - 40 ); /* ctx.fillStyle = "#ffffff"; ctx.fillRect( 0, (canvasHeight/10)*9, canvasWidth, canvasHeight/10 ); */ ctx.drawImage( img2, 20, canvasHeight - canvasWidth * 0.3, canvasWidth - 40, (canvasWidth - 40) * 0.457 ); ctx.drawImage( img_game_button_press_overlay, 20, canvasHeight - canvasWidth, canvasWidth - 40, canvasWidth - 20 ); ctx.drawImage( img_game_count_down_bg_opverlay, 20, canvasHeight - canvasWidth * 0.55, canvasWidth / 4, canvasWidth / 4 ); ctx.drawImage( img_game_left_overlay, 20, canvasHeight - canvasWidth * 0.55, canvasWidth / 4, canvasWidth / 4 ); ctx.drawImage( img_img_logo, canvasWidth * 0.2, 20, canvasWidth * 0.6, canvasWidth * 0.6 ); ctx.drawImage( img_game_right_overlay, 3 * (canvasWidth / 4) - 20, canvasHeight - canvasWidth * 0.55, canvasWidth / 4, canvasWidth / 4 ); var myImage = canvas.toDataURL("image/png"); var imageElement = document.getElementById("polaroidImage"); imageElement.src = myImage; $("#polaroidShare").css("display", "block"); $("#hold_to_save").css("display", "block"); $("#btnClosePolaroid").css("display", "block"); }, false ); img_game_right_overlay.src = document.getElementById("empty").src; }, false ); img_game_count_down_bg_opverlay.src = document.getElementById("empty").src; }, false ); img_img_logo.src = document.getElementById("empty").src; }, false ); img_game_left_overlay.src = document.getElementById("empty").src; }, false ); img2.src = document.getElementById("empty").src; // Set source path }, false ); img_game_button_press_overlay.src = (currentNumber==1) ? document.getElementById("usa_filter_grafic").src : document.getElementById("empty").src; }; canvasImg.src = imgData.replace(strMime, "image/octet-stream"); //Polaroid // Tell the restart-camera script to start watching for issues window.dispatchEvent(new Event("ensurecamerastart")); }); $("#btnClosePolaroid").click(function () { $("#polaroidShare").css("display", "none"); $("#btnClosePolaroid").css("display", "none"); $("#ar_mode_takePhoto_button").css("display", "block"); }); $("#ar_mode_takePhoto_button").click(function () { console.log("clicked take photo"); $("#ar_mode_takePhoto_button").css("display", "none"); document.querySelector("#main_scene").emit("screenshotrequest"); }); document.addEventListener( "eventXrweb", function (e) { console.log(e); switchCameraScreen(); finalTimer.resume(); }, false ); document.addEventListener( "eventXrface", function (e) { console.log(e); switchCameraScreen(); finalTimer.resume(); }, false ); }); function intro() { $("#top_bar").css("display", "block"); $("#intro_background").css("display", "block"); $("#intro_text").css("display", "block"); $("#intro_visit_1").css("display", "block"); $("#intro_visit_2").css("display", "block"); $("#intro_visit_3").css("display", "block"); $("#intro_visit_4").css("display", "block"); $("#intro_visit_5").css("display", "block"); } function finalFrame() { $("#intro_text").css("display", "none"); // $("#ar_find_out_more").css("display", "none"); //$("#ar_share").css("display", "none"); $("#pre_360_screen").css("display", "none"); $("#pre_360_screen_bottom_left").css("display", "none"); //$("#screen_360_screen_go_back").css("display", "none"); $("#ar_mode_takePhoto_button").css("display", "none"); //$("#recorder").css("display", "none"); $("#polaroidShare").css("display", "none"); $("#closePreviewButton").click(); $("#outro_restart").css("display", "block"); $("#outro_share").css("display", "block"); $("#outro_find_out_more").css("display", "block"); $("#outro").css("display", "block"); video_outro.play(); } const eventChangeXrTracking = new Event("eventChangeXrTracking"); function clickedElement() { if (currentNumber == 0) { hideAllEntity(); currentURL = "https://www.flightcentre.co.uk/holidays/wildlife-holidays?utm_source=mm&utm_medium=partners&utm_campaign=takeaway-ar-apr-22"; hideAllEntity(); scene.emit("recenter"); a_entity_1_model.setAttribute("visible", "true"); a_entity_1_model.setAttribute("animation-mixer", { clip: "*", loop: "repeat", crossFadeDuration: 0.4, }); /* if (XrFaceOn) { document.dispatchEvent(eventChangeXrTracking); scene.addEventListener("camerastatuschange", (e) => { if (e.detail.status == "hasVideo") { aEntity1(); finalTimer.resume(); } }); finalTimer.pause(); } else aEntity1(); XrFaceOn = false; */ } if (currentNumber == 1) { hideAllEntity(); currentURL = "https://www.flightcentre.co.uk/holidays/fly-drive?utm_source=mm&utm_medium=partners&utm_campaign=takeaway-ar-apr-22"; scene.emit("recenter"); a_entity_2_model.setAttribute("visible", "true"); /* if (!XrFaceOn) document.dispatchEvent(eventChangeXrTracking); XrFaceOn = true; finalTimer.pause(); */ } if (currentNumber == 2) { currentURL = "https://www.flightcentre.co.uk/holidays/beach?utm_source=mm&utm_medium=partners&utm_campaign=takeaway-ar-apr-22"; hideAllEntity(); a_entity_3_4_sky.setAttribute("src", "#img_360_2"); a_entity_3_4_sky.setAttribute("visible", "true"); /* if (XrFaceOn) { document.dispatchEvent(eventChangeXrTracking); scene.addEventListener("camerastatuschange", (e) => { if (e.detail.status == "hasVideo") { aEntity3(); finalTimer.resume(); } }); finalTimer.pause(); } else aEntity3(); XrFaceOn = false; */ } if (currentNumber == 3) { currentURL = "https://www.flightcentre.co.uk/holidays/cultural-holidays?utm_source=mm&utm_medium=partners&utm_campaign=takeaway-ar-apr-22"; hideAllEntity(); a_entity_3_4_sky.setAttribute("src", "#img_360_3"); a_entity_3_4_sky.setAttribute("visible", "true"); /* if (XrFaceOn) document.dispatchEvent(eventChangeXrTracking); XrFaceOn = false; */ } if (currentNumber == 4) { currentURL = "https://www.flightcentre.co.uk/holidays/australia#sm_Itineraries?utm_source=mm&utm_medium=partners&utm_campaign=takeaway-ar-apr-22"; hideAllEntity(); a_entity_5_model.setAttribute("visible", "true"); a_entity_5_model.setAttribute("animation-mixer", { clip: "KangarooAnim_01_ComeIn", loop: "once", }); a_entity_5_model.addEventListener("animation-finished", () => { a_entity_5_model.setAttribute("animation-mixer", { clip: "KangarooAnim_02_Look", loop: "repeat", }); }); /* if (XrFaceOn) document.dispatchEvent(eventChangeXrTracking); XrFaceOn = false; */ } } /* function aEntity1() { scene.emit("recenter"); a_entity_1_model = document.querySelector("#a_entity_1_model"); a_entity_2_model = document.querySelector("#a_entity_2_model"); a_entity_3_4_sky = document.querySelector("#a_entity_3_4_sky"); a_entity_5_model = document.querySelector("#a_entity_5_model"); hideAllEntity(); a_entity_1_model.setAttribute("visible", "true"); a_entity_1_model.setAttribute("animation-mixer", { clip: "*", loop: "repeat", crossFadeDuration: 0.4, }); } function aEntity2() { scene.emit("recenter"); a_entity_1_model = document.querySelector("#a_entity_1_model"); a_entity_2_model = document.querySelector("#a_entity_2_model"); a_entity_3_4_sky = document.querySelector("#a_entity_3_4_sky"); a_entity_5_model = document.querySelector("#a_entity_5_model"); hideAllEntity(); a_entity_2_model.setAttribute("visible", "true"); } function aEntity3() { scene.emit("recenter"); a_entity_1_model = document.querySelector("#a_entity_1_model"); a_entity_2_model = document.querySelector("#a_entity_2_model"); a_entity_3_4_sky = document.querySelector("#a_entity_3_4_sky"); a_entity_5_model = document.querySelector("#a_entity_5_model"); hideAllEntity(); a_entity_3_4_sky.setAttribute("src", "#img_360_2"); a_entity_3_4_sky.setAttribute("visible", "true"); } */ function switchPre360Screen(key) { if (!experienceOnceRunned) runFinalTimer(); experienceOnceRunned = true; //clickedElement(); currentNumber = key; //console.log(currentNumber); hideVisitScreen(); //a_entity_4_sky.setAttribute("visible", "true"); clickedElement(); switch (key) { case 0: $("#pre_360_screen_text_place_name").css( "background-image", "url(./assets/ui/Flight_Centre_2022_TA-AR_Layers_B_1_Wildlife_Centre_Africa.png)" ); $("#pre_360_screen_bottom_left").css( "background-image", "url(./assets/ui/Flight_Centre_2022_TA-AR_Layers_B_1_Wildlife_Centre.png)" ); break; case 1: $("#pre_360_screen_text_place_name").css( "background-image", "url(./assets/ui/Flight_Centre_2022_TA-AR_Layers_B_2_Road_trip_Centre_usa.png)" ); $("#pre_360_screen_bottom_left").css( "background-image", "url(./assets/ui/Flight_Centre_2022_TA-AR_Layers_B_2_Road_trip_Centre.png)" ); break; case 2: $("#pre_360_screen_text_place_name").css( "background-image", "url(./assets/ui/Flight_Centre_2022_TA-AR_Layers_B_3_Beach_Centre_Maldives.png)" ); $("#pre_360_screen_bottom_left").css( "background-image", "url(./assets/ui/Flight_Centre_2022_TA-AR_Layers_B_3_Beach_Centre.png)" ); break; case 3: $("#pre_360_screen_text_place_name").css( "background-image", "url(./assets/ui/Flight_Centre_2022_TA-AR_Layers_B_4_Culture_Centre_Thailand.png)" ); $("#pre_360_screen_bottom_left").css( "background-image", "url(./assets/ui/Flight_Centre_2022_TA-AR_Layers_B_4_Culture_Centre.png)" ); break; case 4: $("#pre_360_screen_text_place_name").css( "background-image", "url(./assets/ui/Flight_Centre_2022_TA-AR_Layers_B_5_Adventure_Centre_australia.png)" ); $("#pre_360_screen_bottom_left").css( "background-image", "url(./assets/ui/Flight_Centre_2022_TA-AR_Layers_B_5_Down_Under_Centre.png)" ); break; default: break; } } function hideVisitScreen() { $("#intro_visit_1").css("display", "none"); $("#intro_visit_2").css("display", "none"); $("#intro_visit_3").css("display", "none"); $("#intro_visit_4").css("display", "none"); $("#intro_visit_5").css("display", "none"); } function showVisitScreen() { $("#intro_visit_1").css("display", "block"); $("#intro_visit_2").css("display", "block"); $("#intro_visit_3").css("display", "block"); $("#intro_visit_4").css("display", "block"); $("#intro_visit_5").css("display", "block"); } function counter0to4() { ++currentNumber; if (currentNumber > 4) currentNumber = 0; switchPre360Screen(currentNumber); //console.log(currentNumber); } function counter4to0() { --currentNumber; if (currentNumber < 0) currentNumber = 4; switchPre360Screen(currentNumber); //console.log(currentNumber); } function arModeScreen() { $("#pre_360_screen").css("display", "block"); //$("#pre_360_screen_left_arrow").css("display", "block"); //$("#pre_360_screen_right_arrow").css("display", "block"); $("#intro_background").css("display", "none"); $("#intro_text").css("display", "none"); $("#ar_mode_takePhoto_button").css("display", "block"); //$("#recorder").css("display", "block"); $("#pre_360_screen_bottom_left").css("display", "block"); if (!sfxRunned) sfx_bgMusic.play(); sfxRunned = true; localTimer = setTimeout(() => { startAgain(); }, timeoutLocalTimer); } function switchCameraScreen() { $("#pre_360_screen").css("display", "block"); $("#intro_background").css("display", "none"); $("#intro_text").css("display", "none"); $("#ar_mode_takePhoto_button").css("display", "block"); // $("#recorder").css("display", "block"); $("#pre_360_screen_bottom_left").css("display", "block"); hideVisitScreen(); } function startAgain() { $("#outro").css("display", "none"); //$("#outro_text").css("display", "none"); $("#outro_restart").css("display", "none"); $("#outro_share").css("display", "none"); $("#outro_find_out_more").css("display", "none"); //$("#outro_photos").css("display", "none"); //$("#ar_find_out_more").css("display", "none"); //$("#ar_share").css("display", "none"); $("#pre_360_screen").css("display", "none"); //$("#pre_360_screen_left_arrow").css("display", "none"); $("#pre_360_screen_right_arrow").css("display", "none"); $("#pre_360_screen_bottom_left").css("display", "none"); //$("#screen_360_screen_go_back").css("display", "none"); $("#ar_mode_takePhoto_button").css("display", "none"); //$("#recorder").css("display", "none"); $("#polaroidShare").css("display", "none"); $("#btnClosePolaroid").css("display", "none"); $("#closePreviewButton").click(); $("#intro_background").css("display", "block"); $("#intro_text").css("display", "block"); $("#intro_visit_1").css("display", "block"); $("#intro_visit_2").css("display", "block"); $("#intro_visit_3").css("display", "block"); $("#intro_visit_4").css("display", "block"); $("#intro_visit_5").css("display", "block"); hideAllEntity(); clearTimeout(finalTimer); experienceOnceRunned = false; clearTimeout(localTimer); } function runFinalTimer() { experienceOnceRunned = true; finalTimer = new Timer(function () { finalFrame(); clearTimeout(localTimer); }, timeout); } function runIntro1Timer() { experienceOnceRunned = true; intro1Timer = setTimeout(() => { intro(); }, timeOutIntro1); } const xrLightSystem = { init() { this.intensity = 1; const startListen = () => { window.XR8.XrController.configure({ enableLighting: true }); window.XR8.addCameraPipelineModule({ name: "xr-light", onUpdate: ({ processCpuResult }) => { if ( processCpuResult.reality && processCpuResult.reality.lighting && processCpuResult.reality.lighting.exposure ) { this.intensity = 1 + processCpuResult.reality.lighting.exposure; } }, }); }; window.XR8 ? startListen() : window.addEventListener("xrloaded", startListen); }, }; const xrLightComponent = { schema: { min: { default: 0 }, max: { default: 2 }, }, tick() { this.el.setAttribute( "light", `intensity: ${Math.max( this.data.min, Math.min(this.system.intensity, this.data.max) )}` ); }, }; const ensureMaterialArray = (material) => { if (!material) { return []; } if (Array.isArray(material)) { return material; } if (material.materials) { return material.materials; } return [material]; }; const applyEnvMap = (mesh, envMap) => { if (!mesh) return; mesh.traverse((node) => { if (!node.isMesh) { return; } const meshMaterials = ensureMaterialArray(node.material); meshMaterials.forEach((material) => { if (material && !("envMap" in material)) return; material.envMap = envMap; material.needsUpdate = true; }); }); }; /* const cubeMapRealtimeComponent = { schema: {}, init() { const { data } = this; const scene = this.el.sceneEl; const camTexture_ = new THREE.Texture(); const refMat = new THREE.MeshBasicMaterial({ side: THREE.DoubleSide, color: 0xffffff, map: camTexture_, }); const renderTarget = new THREE.WebGLCubeRenderTarget(256, { format: THREE.RGBFormat, generateMipmaps: true, minFilter: THREE.LinearMipmapLinearFilter, encoding: THREE.sRGBEncoding, }); // cubemap scene const cubeMapScene = new THREE.Scene(); const cubeCamera = new THREE.CubeCamera(1, 1000, renderTarget); const sphere = new THREE.SphereGeometry(100, 15, 15); const sphereMesh = new THREE.Mesh(sphere, refMat); sphereMesh.scale.set(-1, 1, 1); sphereMesh.rotation.set(Math.PI, -Math.PI / 2, 0); cubeMapScene.add(sphereMesh); window.XR8.XrController.configure({ enableLighting: true }); window.XR8.addCameraPipelineModule({ name: "cubemap-process", onUpdate: () => { cubeCamera.update(scene.renderer, cubeMapScene); }, onProcessCpu: ({ frameStartResult }) => { const { cameraTexture } = frameStartResult; // force initialization const texProps = scene.renderer.properties.get(camTexture_); texProps.__webglTexture = cameraTexture; }, }); this.el.addEventListener("model-loaded", () => { applyEnvMap(this.el.getObject3D("mesh"), cubeCamera.renderTarget.texture); }); }, }; */ function hideAllEntity() { a_entity_1_model.setAttribute("visible", "false"); a_entity_2_model.setAttribute("visible", "false"); a_entity_3_4_sky.setAttribute("visible", "false"); a_entity_5_model.setAttribute("visible", "false"); } /* const swapCamComponent = () => ({ init() { const scene = this.el.sceneEl; const eventXrweb = new Event("eventXrweb"); const eventXrface = new Event("eventXrface"); document.addEventListener("eventChangeXrTracking", () => { const faceAnchor = document.querySelector("xrextras-faceanchor"); const holo1 = document.getElementById("a_entity_1_model"); const holo2 = document.getElementById("a_entity_3_4_sky"); const holo3 = document.getElementById("a_entity_5_model"); let Xrface = false; setTimeout(() => { if (holo1) { Xrface = true; holo1.parentNode.removeChild(holo1); holo2.parentNode.removeChild(holo2); holo3.parentNode.removeChild(holo3); scene.removeAttribute("xrweb"); scene.setAttribute("xrface", { mirroredDisplay: true, cameraDirection: "front", }); scene.insertAdjacentHTML( "beforeend", ` ` ); console.log("component eventXrface"); } else { Xrface = false; faceAnchor.parentNode.removeChild(faceAnchor); scene.removeAttribute("xrface"); scene.setAttribute("xrweb", ""); scene.insertAdjacentHTML( "beforeend", ` ` ); console.log("component eventXrweb"); } scene.addEventListener("camerastatuschange", (e) => { if (e.detail.status == "hasVideo") if (Xrface) document.dispatchEvent(eventXrface); else document.dispatchEvent(eventXrweb); }); }, 500); }); }, }); */ var Timer = function (callback, delay) { var timerId, start, remaining = delay; this.pause = function () { window.clearTimeout(timerId); timerId = null; remaining -= Date.now() - start; }; this.resume = function () { if (timerId) { return; } start = Date.now(); timerId = window.setTimeout(callback, remaining); }; this.resume(); };