Hey @GeorgeButler,
Would you kindly advice if the following is the right way of doing it?
We are successfully switching between the 2 scenes, however as soon as we enter the hand tracking scene everything lags like hell and eventually the phone completely freezes and crash so I feel like we are not managing this properly.
import './index.css'
//
// app.js is the main entry point for your 8th Wall app. Code here will execute after head.html
// is loaded, and before body.html is loaded.
import './index.css'
import {initScenePipelineModule} from './eighti-scene-init'
import * as camerafeedHtml from './camerafeed.html'
import {handScenePipelineModule} from './hand-scene'
let watchName = ' '
let firstTime = true
const runHandPipeline = () => {
XR8.stop()
XR8.clearCameraPipelineModules()
// Add a canvas to the document for our xr scene.
document.body.insertAdjacentHTML('beforeend', `
<canvas id="camerafeed" width="${window.innerWidth}" height="${window.innerHeight}"></canvas>
`)
document.body.insertAdjacentHTML('beforeend', `
<div id="backfromAR" class=""></div>
`)
XR8.HandController.configure({
coordinates: {mirroredDisplay: false},
})
const moduleHand = [ // Add camera pipeline modules.
// Existing pipeline modules.
XR8.GlTextureRenderer.pipelineModule(), // Draws the camera feed.
XR8.Threejs.pipelineModule(), // Syncs threejs renderer to camera properties.
XR8.HandController.pipelineModule(), // Loads 8th Wall Face Engine
XR8.CanvasScreenshot.pipelineModule(), // Required for photo capture
window.LandingPage.pipelineModule(), // Detects unsupported browsers and gives hints.
XRExtras.FullWindowCanvas.pipelineModule(), // Modifies the canvas to fill the window.
XRExtras.Loading.pipelineModule(), // Manages the loading screen on startup.
XRExtras.RuntimeError.pipelineModule(), // Shows an error image on runtime error.
// Custom pipeline modules
handScenePipelineModule(watchName),
window.HandCoachingOverlay.pipelineModule(),
]
const moduleFirstTime = [ // Add camera pipeline modules.
// Existing pipeline modules.
XR8.GlTextureRenderer.pipelineModule(), // Draws the camera feed.
XR8.Threejs.pipelineModule(), // Syncs threejs renderer to camera properties.
XR8.HandController.pipelineModule(), // Loads 8th Wall Face Engine
XR8.CanvasScreenshot.pipelineModule(), // Required for photo capture
window.LandingPage.pipelineModule(), // Detects unsupported browsers and gives hints.
XRExtras.FullWindowCanvas.pipelineModule(), // Modifies the canvas to fill the window.
XRExtras.Loading.pipelineModule(), // Manages the loading screen on startup.
XRExtras.RuntimeError.pipelineModule(), // Shows an error image on runtime error.
// Custom pipeline modules
handScenePipelineModule(watchName),
]
XR8.addCameraPipelineModules(firstTime ? moduleFirstTime : moduleHand)
// Open the camera and start running the camera run loop.
XR8.run({
canvas: document.getElementById('camerafeed'),
cameraConfig: {direction: XR8.XrConfig.camera().BACK},
allowedDevices: XR8.XrConfig.device().ANY,
})
if (firstTime) {
MenuScreen()
firstTime = false
document.getElementById('backfromAR').style.display = 'none'
} else {
document.getElementById('backfromAR').style.display = 'block'
document.getElementById('backfromAR').addEventListener('click', () => {
console.log('Back from AR button clicked!')
// Add the function you want to call here, e.g., NormalScene();
NormalScene()
})
}
}
const MainScreen = () => {
// Assuming you have a WebGLRenderer already set up for Three.js
const overlay = document.createElement('div')
overlay.className = 'overlay-screen'
overlay.id = 'overlayID'
document.body.appendChild(overlay)
const button = document.createElement('button')
button.className = 'overlay-button'
button.innerHTML = 'Get Started'
button.id = 'buttonStartedID'
overlay.appendChild(button)
button.addEventListener('click', () => {
console.log('AR Button clicked! Changing scene...')
NormalScene()
})
}
const onxrloaded = () => {
XR8.stop()
XR8.clearCameraPipelineModules()
document.body.insertAdjacentHTML('beforeend', `
<button id="tryButton" class="">Try It</button>
`)
document.body.insertAdjacentHTML('beforeend', `
<button id="watch1Button">1</button>
`)
document.body.insertAdjacentHTML('beforeend', `
<button id="watch2Button">2</button>
`)
document.body.insertAdjacentHTML('beforeend', `
<button id="watch3Button">3</button>
`)
document.body.insertAdjacentHTML('beforeend', `
<button id="watch4Button">4</button>
`)
document.getElementById('tryButton').addEventListener('click', () => {
console.log('AR Button clicked! Changing scene...')
ARScene()
})
document.getElementById('watch1Button').addEventListener('click', () => {
document.getElementById('tryButton').style.display = 'block'
watchName = 'one'
})
document.getElementById('watch2Button').addEventListener('click', () => {
document.getElementById('tryButton').style.display = 'block'
watchName = 'two'
})
document.getElementById('watch3Button').addEventListener('click', () => {
document.getElementById('tryButton').style.display = 'block'
watchName = 'three'
})
document.getElementById('watch4Button').addEventListener('click', () => {
document.getElementById('tryButton').style.display = 'block'
watchName = 'four'
})
// Add a button dynamically
XR8.addCameraPipelineModules([ // Add camera pipeline modules.
// Existing pipeline modules.
XR8.GlTextureRenderer.pipelineModule(), // Draws the camera feed.
XR8.Threejs.pipelineModule(), // Creates a ThreeJS AR Scene.
XR8.XrController.pipelineModule(), // Enables SLAM tracking.
window.LandingPage.pipelineModule(), // Detects unsupported browsers and gives hints.
XRExtras.FullWindowCanvas.pipelineModule(), // Modifies the canvas to fill the window.
XRExtras.Loading.pipelineModule(), // Manages the loading screen on startup.
XRExtras.RuntimeError.pipelineModule(), // Shows an error image on runtime error.
// Custom pipeline modules.
initScenePipelineModule(), // Sets up the threejs camera and scene content.
])
// Add a canvas to the document for our xr scene.
document.body.insertAdjacentHTML('beforeend', camerafeedHtml)
const canvas = document.getElementById('camerafeed')
// Open the camera and start running the camera run loop.
XR8.run({canvas})
}
const stopHandPipeline = () => {
// Stop XR8 and clean up
// XR8.removeCameraPipelineModules()
// Remove the canvas and button from the DOM
const canvas = document.getElementById('camerafeed')
const button = document.getElementById('changeSceneButton')
const backbutton = document.getElementById('backButton')
const trybutton = document.getElementById('tryButton')
const button1 = document.getElementById('watch1Button')
const backfromARbutton = document.getElementById('backfromAR')
const button2 = document.getElementById('watch2Button')
const button3 = document.getElementById('watch3Button')
const button4 = document.getElementById('watch4Button')
const overlayID = document.getElementById('overlayID')
const buttonStartedID = document.getElementById('buttonStartedID')
if (backfromARbutton) {
backfromARbutton.remove()
}
if (trybutton) {
trybutton.remove()
}
if (overlayID) {
overlayID.remove()
}
if (buttonStartedID) {
buttonStartedID.remove()
}
if (button1) {
button4.remove()
button3.remove()
button2.remove()
button1.remove()
}
if (backbutton) {
backbutton.remove()
} if (canvas) {
document.body.removeChild(canvas)
}
if (button) button.remove()
}
const ARScene = () => {
stopHandPipeline()
window.XR8 ? runHandPipeline() : window.addEventListener('xrloaded', runHandPipeline)
}
const NormalScene = () => {
stopHandPipeline()
window.XR8 ? onxrloaded() : window.addEventListener('xrloaded', onxrloaded)
}
ARScene()