Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
208 lines (175 sloc) 6.45 KB
<html>
<!-- Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<head>
<title>Boombox</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<script src="../libs/three/three.min.js"></script>
<script src="../libs/three/loaders/GLTFLoader.js"></script>
<script module src="../../dist/webxr.js"></script>
<link rel="stylesheet" href="../common.css"/>
</head>
<body>
<div id="description">
<h2>Boombox</h2>
<h5>(click to dismiss)</h5>
<p>Shows a boombox in a Panoramic photo environment.</p>
</div>
<button type=button id=go-button>Go</button>
<script type=module>
// some dependencies and utilities
import * as mat4 from '../libs/gl-matrix/mat4.js'
import * as vec3 from '../libs/gl-matrix/vec3.js'
import XREngine from "../XREngine.js"
let device = null
let session = null
let eyeLevelFrameOfReference = null
let engine = null
// temporary working variables
const workingMatrix = mat4.create()
const workingVec3 = vec3.create()
// start the textures and the boombox model loading immediately
const startGroup = new THREE.Group()
// Create the environment map
const path = '../textures/Park2/'
const format = '.jpg'
var envMap = new THREE.CubeTextureLoader().load([
path + 'posx' + format, path + 'negx' + format,
path + 'posy' + format, path + 'negy' + format,
path + 'posz' + format, path + 'negz' + format
])
envMap.format = THREE.RGBFormat
var loader = new THREE.GLTFLoader().setPath( '../models/BoomBox/glTF-pbrSpecularGlossiness/' );
loader.load( 'BoomBox.gltf',
function ( gltf ) {
gltf.scene.scale.set(15, 15, 15)
gltf.scene.position.set(0, 0, -1)
gltf.scene.quaternion.setFromAxisAngle(new THREE.Vector3(0, 1, 0), Math.PI)
gltf.scene.traverse(node => {
if (node.material && (node.material.isMeshStandardMaterial || (node.material.isShaderMaterial && node.material.envMap !== undefined))){
node.material.envMap = envMap
node.material.needsUpdate = true
}
})
startGroup.add(gltf.scene)
},
null, // progress callback
function(e) {
console.error('could not load gltf', ...params)
})
function initializeScene(){
engine.addAmbientLight()
engine.addDirectionalLight()
// Add a box and axis at the origin of our starting coordinate system
// for debugging by uncommenting these lines
// engine.addBox([0, 0, 0], [0.025, 0.025, 0.025], 0x44ff44)
// engine.addAxesHelper([0, 0,0], [0.2,0.2,0.2])
engine.root.add(startGroup)
engine.scene.background = new THREE.Color(0x222222)
engine.scene.background = envMap
}
////////////////////////////////////////////////////
// BOILER PLATE. Can you feel the plates boiling?
//
// Create the output context where the XRSession will place composited renders
const xrCanvas = document.createElement('canvas')
xrCanvas.setAttribute('class', 'xr-canvas')
const xrContext = xrCanvas.getContext('xrpresent')
if(!xrContext){
console.error('No XR context', xrCanvas)
}
navigator.xr.requestDevice().then(xrDevice => {
device = xrDevice
}).catch(err => {
console.error('Error', err)
})
document.getElementById('description').addEventListener('touchstart', hideMe, {capture: true})
function hideMe(event) {
event.target.style.display = 'none'
event.stopPropagation()
}
document.getElementById('go-button').addEventListener('click', handleStartSessionRequest)
document.getElementById('go-button').addEventListener('touchstart', handleGoButtonTouch, true)
function handleGoButtonTouch(event) {
event.stopPropagation()
}
function handleStartSessionRequest(ev){
if(device === null){
console.error('No xr device')
return
}
if (!session) {
device.requestSession({ outputContext: xrContext })
.then(handleSessionStarted)
.catch(err => {
console.error('Session setup error', err)
})
document.getElementById('go-button').innerText = "End"
document.getElementById('go-button').style.display = "none"
} else {
session.end()
handleSessionEnded();
document.getElementById('description').style.display = 'block'
document.getElementById('go-button').style.display = "block"
document.getElementById('go-button').innerText = "Go"
}
}
function handleSessionEnded() {
session = null
}
function handleSessionStarted(xrSession){
session = xrSession
document.body.insertBefore(xrCanvas, document.body.firstChild)
// Create the context where we will render our 3D scene
const canvas = document.createElement('canvas')
var glContext = canvas.getContext('webgl', {
compatibleXRDevice: device
})
if(!glContext) throw new Error('Could not create a webgl context')
// Set up the base layer
session.baseLayer = new XRWebGLLayer(session, glContext)
// Create a simple test scene and renderer
// The engine's scene is in the eye-level coordinate system
engine = new XREngine(canvas, glContext)
createRootNode().then(() => {
// Kick off rendering
session.requestAnimationFrame(handleAnimationFrame)
})
initializeScene()
}
async function createRootNode() {
let headFrameOfReference = await session.requestFrameOfReference('head-model')
eyeLevelFrameOfReference = await session.requestFrameOfReference('eye-level')
// get the location of the device, and use it to create an
// anchor with the identity orientation
headFrameOfReference.getTransformTo(eyeLevelFrameOfReference, workingMatrix)
mat4.getTranslation(workingVec3, workingMatrix)
mat4.fromTranslation(workingMatrix, workingVec3)
let anchor = await session.addAnchor(workingMatrix, eyeLevelFrameOfReference)
engine.addAnchoredNode(anchor, engine.root)
return true
}
function handleAnimationFrame(t, frame){
if(!session || session.ended) return
session.requestAnimationFrame(handleAnimationFrame)
let pose = frame.getDevicePose(eyeLevelFrameOfReference)
if(!pose){
console.log('No pose')
return
}
engine.startFrame()
for (let view of frame.views) {
engine.preRender(
session.baseLayer.getViewport(view),
view.projectionMatrix,
pose.getViewMatrix(view)
)
engine.render()
}
engine.endFrame()
}
</script>
</body>
</html>
You can’t perform that action at this time.