xrfragment/example/threejs/sandbox/index.html

259 lines
8.8 KiB
HTML
Raw Normal View History

2023-04-20 18:49:18 +02:00
<!DOCTYPE html>
<html lang="en">
<head>
2023-05-09 17:42:29 +02:00
<title>THREE.js - xrfragment sandbox</title>
2023-04-20 18:49:18 +02:00
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
2023-05-22 14:10:44 +02:00
<link rel="stylesheet" href="./../../assets/css/axist.min.css" />
<link type="text/css" rel="stylesheet" href="./../../assets/css/style.css"/>
2023-04-20 18:49:18 +02:00
</head>
<body>
2023-05-10 19:12:15 +02:00
<div id="overlay" x-data="{ urls: ['#pos=0,1.6,15','#pos=0,1.6,15&rot=0,360,0'] }">
2023-04-21 17:36:03 +02:00
<img src="./../../assets/logo.png" class="logo"/>
<input type="submit" value="load 3D asset"></input>
2023-05-18 17:11:11 +02:00
<input type="text" id="uri" value="" onchange="AFRAME.XRF.navigator.to( $('#uri').value )"/>
2023-04-21 17:36:03 +02:00
</div>
2023-07-04 17:15:23 +02:00
<a class="btn-foot" id="source" target="_blank" href="https://github.com/coderofsalvation/xrfragment-helloworld"> clone project</a>
2023-07-04 15:43:15 +02:00
<a class="btn-foot" id="embed" target="_blank" onclick="embed()">📺 embed</a>
2023-07-04 17:28:05 +02:00
<a class="btn-foot" id="model" target="_blank" href="example.gltf">⬇️ model</a>
2023-04-28 16:04:32 +02:00
<textarea style="display:none"></textarea>
2023-04-21 17:36:03 +02:00
2023-06-07 17:42:21 +02:00
<script async src="./../../assets/js/alpine.min.js"></script>
2023-04-20 18:49:18 +02:00
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.6.3/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.151.3/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.151.3/examples/jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import xrf from './../../../dist/xrfragment.three.module.js';
2023-05-22 14:10:44 +02:00
import { loadFile, setupConsole, setupUrlBar, notify } from './../../assets/js/utils.js';
2023-04-20 18:49:18 +02:00
import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
import { Lensflare, LensflareElement } from 'three/addons/objects/Lensflare.js';
import { BoxLineGeometry } from 'three/addons/geometries/BoxLineGeometry.js';
import { Reflector } from 'three/addons/objects/Reflector.js';
import { VRButton } from 'three/addons/webxr/VRButton.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';
2023-04-20 18:49:18 +02:00
import { HTMLMesh } from 'three/addons/interactive/HTMLMesh.js';
import { InteractiveGroup } from 'three/addons/interactive/InteractiveGroup.js';
import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
import Stats from 'three/addons/libs/stats.module.js';
window.$ = (s) => document.querySelector(s)
2023-04-20 18:49:18 +02:00
let camera, scene, renderer, controls;
let reflector;
let stats, statsMesh;
2023-05-04 21:28:12 +02:00
let vrbutton;
2023-04-20 18:49:18 +02:00
const parameters = {
2023-05-04 17:00:55 +02:00
env: 1.0,
2023-04-20 18:49:18 +02:00
};
init();
2023-06-22 12:15:02 +02:00
alert("for now, back-button (navigation) is only implemented in the AFRAME demo-implementation")
2023-04-20 18:49:18 +02:00
function init() {
scene = new THREE.Scene();
2023-04-20 22:30:04 +02:00
scene.background = new THREE.Color( 0xcccccc );
2023-04-20 18:49:18 +02:00
2023-04-20 22:30:04 +02:00
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 2000 );
2023-04-20 18:49:18 +02:00
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.autoClear = false;
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.xr.enabled = true;
2023-05-04 17:00:55 +02:00
renderer.domElement.className = "render"
document.body.appendChild( renderer.domElement );
2023-05-04 21:28:12 +02:00
document.body.appendChild( vrbutton = VRButton.createButton( renderer ) );
2023-04-20 18:49:18 +02:00
window.addEventListener( 'resize', onWindowResize );
2023-05-22 17:18:15 +02:00
let cameraRig = new THREE.Group()
cameraRig.position.set( 0, 0, 0 );
2023-05-23 16:46:27 +02:00
camera.position.set(0,1.6,0)
2023-05-22 17:18:15 +02:00
cameraRig.add(camera)
scene.add(cameraRig)
// enable XR fragments
let XRF = xrf.init({
THREE,
2023-05-22 17:18:15 +02:00
camera:cameraRig,
scene,
renderer,
debug: true,
loaders: { gltf: GLTFLoader, fbx: FBXLoader }, // which 3D assets (extensions) to check for XR fragments?
})
2023-05-09 17:42:29 +02:00
// optional: react/extend/hook into XR fragment
2023-05-04 21:28:12 +02:00
XRF.env = (xrf,v,opts) => {
let { mesh, model, camera, scene, renderer, THREE} = opts
xrf(v,opts)
}
2023-05-05 18:53:42 +02:00
// *TODO* lowhanging fruit: during XR fragments-query milestone, target objects using XR fragment queries
// to provide jquery-ish interface for three.js)
//
// XRF('example1.gltf#q=.foo', (objs) => {
//
// })
window.XRF = XRF // expose to form
2023-06-22 12:15:02 +02:00
let file = document.location.search.length > 2 ? document.location.search.substr(1) + document.location.hash : 'example.gltf#pos=1,0,4&rot=0,-30,0'
2023-07-04 17:28:05 +02:00
// optional decoration of href event
XRF.addEventListener('href',(e) => {
if( e.click ){
const { mesh, model, camera, scene, renderer, THREE} = e.XRF
const url = e.xrf.string
const isExtern = url[0] != '#'
const promise = e.promise() // promisify event
document.querySelector('#model').setAttribute('href',url.replace(/.*\?/,'') )
promise.resolve()
}
})
2023-05-17 21:31:28 +02:00
XRF.navigator.to( file )
2023-04-20 18:49:18 +02:00
// setup mouse controls
2023-05-08 14:36:13 +02:00
//controls = new OrbitControls( camera, renderer.domElement );
//controls.listenToKeyEvents( window ); // optional
//controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
//controls.dampingFactor = 0.1;
2023-04-20 18:49:18 +02:00
2023-05-08 14:36:13 +02:00
//controls.screenSpacePanning = false;
2023-04-20 18:49:18 +02:00
2023-05-08 14:36:13 +02:00
//controls.minDistance = 0.1;
//controls.maxDistance = 5000;
//controls.maxPolarAngle = Math.PI / 2;
//controls.target = new THREE.Vector3(0,1.6,0)
2023-04-20 18:49:18 +02:00
//controls.update()
2023-04-20 18:49:18 +02:00
const geometry = new THREE.BufferGeometry();
geometry.setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 5 ) ] );
const controller1 = renderer.xr.getController( 0 );
controller1.add( new THREE.Line( geometry ) );
2023-05-22 17:18:15 +02:00
cameraRig.add( controller1 );
2023-04-20 18:49:18 +02:00
const controller2 = renderer.xr.getController( 1 );
controller2.add( new THREE.Line( geometry ) );
2023-05-22 17:18:15 +02:00
cameraRig.add( controller2 );
2023-04-20 18:49:18 +02:00
const controllerModelFactory = new XRControllerModelFactory();
const controllerGrip1 = renderer.xr.getControllerGrip( 0 );
controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
2023-05-22 17:18:15 +02:00
cameraRig.add( controllerGrip1 );
2023-04-20 18:49:18 +02:00
const controllerGrip2 = renderer.xr.getControllerGrip( 1 );
controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
2023-05-22 17:18:15 +02:00
cameraRig.add( controllerGrip2 );
2023-04-20 18:49:18 +02:00
2023-05-17 21:31:28 +02:00
setupConsole()
setupUrlBar( $('input#uri'), XRF )
2023-04-28 16:04:32 +02:00
2023-04-20 18:49:18 +02:00
// GUI
function onChange() {
2023-05-04 17:00:55 +02:00
renderer.toneMappingExposure = parameters.env;
2023-04-20 18:49:18 +02:00
}
const gui = new GUI( { width: 300 } );
2023-05-04 17:00:55 +02:00
gui.add( parameters, 'env', 0.2, 3.0, 0.1 ).onChange( onChange );
2023-04-20 18:49:18 +02:00
const mesh = new HTMLMesh( gui.domElement );
mesh.position.x = - 0.75;
mesh.position.y = 1.5;
2023-05-04 17:00:55 +02:00
mesh.position.z = 0.3;
2023-04-20 18:49:18 +02:00
mesh.rotation.y = Math.PI / 4;
mesh.scale.setScalar( 2 );
// Add stats.js
stats = new Stats();
stats.dom.style.width = '80px';
stats.dom.style.height = '48px';
document.body.appendChild( stats.dom );
statsMesh = new HTMLMesh( stats.dom );
statsMesh.position.x = - 0.75;
statsMesh.position.y = 2;
2023-05-04 17:00:55 +02:00
statsMesh.position.z = 0.3;
2023-04-20 18:49:18 +02:00
statsMesh.rotation.y = Math.PI / 4;
statsMesh.scale.setScalar( 2.5 );
vrbutton.addEventListener('click', () => {
// show gui inside VR scene
gui.domElement.style.visibility = 'hidden';
XRF.interactive.add( mesh );
XRF.interactive.add( statsMesh );
})
2023-04-20 18:49:18 +02:00
2023-04-21 17:36:03 +02:00
let fileLoaders = loadFile({
2023-07-04 17:15:23 +02:00
".gltf": (file) => file.arrayBuffer().then( (data) => xrf.navigator.to(file.name,null,xrf.loaders.gltf,data) ),
".glb": (file) => file.arrayBuffer().then( (data) => xrf.navigator.to(file.name,null,xrf.loaders.gltf,data) )
2023-04-21 17:36:03 +02:00
})
$("#overlay > input[type=submit]").addEventListener("click", fileLoaders )
2023-04-21 17:36:03 +02:00
2023-04-20 18:49:18 +02:00
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
renderer.setAnimationLoop( render );
}
function render() {
const time = performance.now() * 0.0002;
//const torus = scene.getObjectByName( 'torus' );
//torus.rotation.x = time * 0.4;
//torus.rotation.y = time;
2023-05-08 14:36:13 +02:00
//controls.update()
2023-04-20 18:49:18 +02:00
renderer.render( scene, camera );
stats.update();
// Canvas elements doesn't trigger DOM updates, so we have to update the texture
statsMesh.material.map.update();
}
2023-05-08 12:46:33 +02:00
animate();
2023-04-20 18:49:18 +02:00
</script>
</body>
</html>