xrfragment/example/model-viewer/index.html

216 lines
7.3 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html lang="en">
<head>
<title>&lt;model-viewer&gt; template</title>
<meta charset="utf-8" />
<meta name="description" content="&lt;model-viewer&gt; template" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body style="overflow:hidden; padding:10%">
<h1>&lt;model-viewer&gt; example</h1>
2024-07-12 18:19:19 +02:00
<br><br>
2024-07-12 18:19:19 +02:00
<model-viewer
src="./../assets/index.glb"
environment-image="https://cdn.glitch.global/8e507517-31ff-4aa5-80c1-10ea6de9483d/white_furnace.hdr"
ar alt="XR Fragments demo scene" touch-action="none" disable-pan disable-zoom
field-of-view="75deg" min-field-of-view="25deg" max-field-of-view="100deg"
style="width:80%; height:50vh; border-radius:5px; border:1px solid #CCC"
>
</model-viewer>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.165.0/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.165.0/examples/jsm/"
}
}
</script>
<script type="module">
import xrf from "./../../../dist/xrfragment.three.module.js";
import * as THREE from 'three'; // *TODO* get three handle from mv
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';
import { USDZLoader } from 'three/addons/loaders/USDZLoader.js';
import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
2024-07-12 18:19:19 +02:00
const mv = window.mv = document.querySelector("model-viewer");
const $url = document.querySelector('#url')
const orbitDefault = '50deg 90deg 1.0m'
function getSymbol(name) {
let obj = mv;
do {
const sym = Object.getOwnPropertySymbols(obj).find(
(x) => x.description === name
);
if (sym) {
return sym;
}
} while ((obj = Object.getPrototypeOf(obj)));
}
const createHotspot = (pos, n) => {
//.name, `${n.userData['aria-description'] || n.name}` )
const btn = document.createElement('button')
btn.innerText = n.name
btn.className = 'view-button'
btn.node = n
if( n.userData['aria-description']){
btn.alt = n.userData['aria-description']
btn.setAttribute("aria-description", n.userData['aria-description'])
}
btn.setAttribute('slot','hotspot-'+n.name)
btn.setAttribute('data-normal',"0 0 1")
btn.setAttribute('data-position',`${pos.x}m ${pos.y}m ${pos.z}m`)
btn.setAttribute('data-target',`${pos.x}m ${pos.y}m ${pos.z}m`)
btn.setAttribute('data-orbit', orbitDefault )
2024-07-12 18:19:19 +02:00
// btn.style.opacity = 0.01
btn.style.borderRadius = '50%'
btn.style.padding = '30px'
btn.style.cursor = 'pointer'
mv.appendChild(btn)
}
const setupHotspots = (scene) => {
scene.traverse( (n) => {
if( n.userData.href ){
let pos = new THREE.Vector3()
n.getWorldPosition(pos)
createHotspot(pos, n)
}
})
2024-07-12 18:19:19 +02:00
setupClicks()
}
const setupClicks = () => {
// implement click action
const annotationClicked = (annotation) => {
let dataset = annotation.dataset;
let node = annotation.node
mv.cameraTarget = dataset.target;
mv.cameraOrbit = dataset.orbit;
mv.fieldOfView = '45deg';
console.dir(node)
2024-07-12 18:19:19 +02:00
if( node && node.userData.href ){
// xrf.navigator.to( node.userData.href )
//node.userData.XRF.href.exec({type:'click'})
console.log("clicked!")
}
}
mv.querySelectorAll('button').forEach((hotspot) => {
hotspot.addEventListener('click', () => annotationClicked(hotspot));
});
}
2024-07-12 18:19:19 +02:00
const setupDefaultProjection = () => {
let frag = xrf.URI.parse( xrf.scene.children[0].userData['#'] )
if( frag.XRF.pos ){
let obj = xrf.scene.getObjectByName( frag.XRF.pos.string )
if( !obj ) return console.error('obj '+frag.XRF.pos.string+" not found")
console.log("updating cam")
const pos = new THREE.Vector3()
obj.getWorldPosition(pos)
obj.position.y += 1.6 // add person length
mv.cameraTarget = `${pos.x} ${pos.y} ${pos.z}`
console.log(`${pos.x} ${pos.y} ${pos.z}`)
mv.cameraOrbit = orbitDefault;
mv.fieldOfView = '45deg';
}
2024-07-12 18:19:19 +02:00
}
const setupCSS = (scene) => {
//if( document.querySelector('#viewbutton-css') ) return
//let style = document.createElement('style')
//style.type = 'text/css'
//style.innerHTML = `
// .view-button {
// background: #fff;
// border-radius: 4px;
// border: none;
// box-sizing: border-box;
// box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25);
// color: rgba(0, 0, 0, 0.8);
// display: block;
// font-family: Futura, Helvetica Neue, sans-serif;
// font-size: 12px;
// font-weight: 700;
// max-width: 128px;
// overflow-wrap: break-word;
// padding: 0.5em 1em;
// position: absolute;
// width: max-content;
// height: max-content;
// transform: translate3d(-50%, -50%, 0);
// }
//`
//document.body.appendChild(style)
}
const opts = {
xrf,
THREE,
}
const onLoad = (opts) => function(){
const scene = mv[getSymbol('scene')]
const renderer = mv[getSymbol("renderer")].threeRenderer
const controls = mv[getSymbol("controls")]
const camera = mv[getSymbol("scene")].getCamera()
2024-07-12 18:19:19 +02:00
const url = mv.src
2024-07-12 18:19:19 +02:00
opts = window.opts = {
...opts,
scene,
renderer,
camera,
loaders: { gltf: GLTFLoader, glb: GLTFLoader, fbx: FBXLoader, obj: OBJLoader, usdz: USDZLoader },
controls
}
window.opts = opts
2024-07-12 18:19:19 +02:00
// mark current loaded scene for deletion by xrfragment library (except camera)
//scene.traverse( (o) => o.isXRF = o.id != camera.id )
// enable XR fragments
2024-07-12 18:19:19 +02:00
if( camera.parent == null ) scene.add(camera) // xr fragments expects in-scene camera
let xrf = opts.xrf.init(opts)
window.xrf = xrf
2024-07-12 18:19:19 +02:00
xrf.sceneRoot.children.map( (c) => (camera.id != c.id) && (c.visible = false) )
xrf.addEventListener('href', (opts) => {
console.dir(opts)
})
2024-07-12 18:19:19 +02:00
xrf.addEventListener('navigateLoaded', function(opts){
console.dir(xrf.scene)
setupCSS()
setupHotspots(scene)
// setupDefaultProjection()
console.log("ready")
})
// now we re-insert the model via the XR Fragments lib (so it will parse the XRF metadata)
xrf.navigator.to(url)
}
mv.addEventListener("load", onLoad(opts) )
//mv.addEventListener('before-render', function(){
// const scene = mv[getSymbol('scene')]
// scene.visible = false
// console.log("before-render")
//})
</script>
<!-- Loads <model-viewer> for browsers: -->
<script
type="module"
src="https://ajax.googleapis.com/ajax/libs/model-viewer/3.4.0/model-viewer.min.js"
></script>
</body>
</html>