261 lines
8.7 KiB
HTML
261 lines
8.7 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<title>THREE.js - xrfragment sandbox</title>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
|
|
|
</head>
|
|
<body style="margin:0">
|
|
<div id="overlay" style="text-align:right;position:absolute;width:100%;height:100%;top:0;left:0;bottom:0;right:0">
|
|
<b>NOTE:</b> this is a THREE barebones example (AFRAME viewer has more features)<br><Br>
|
|
<input type="submit" value="load 3D asset"/>
|
|
</div>
|
|
|
|
<!-- 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.165.0/build/three.module.js",
|
|
"three/addons/": "https://unpkg.com/three@0.165.0/examples/jsm/"
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<script type="module">
|
|
import * as THREE from 'three';
|
|
|
|
import xrf from "./../../../dist/xrfragment.three.module.js";
|
|
|
|
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 { VRButton } from 'three/addons/webxr/VRButton.js';
|
|
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';
|
|
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 { OculusHandModel } from 'three/addons/webxr/OculusHandModel.js';
|
|
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
|
|
import Stats from 'three/addons/libs/stats.module.js';
|
|
|
|
window.$ = (s) => document.querySelector(s)
|
|
|
|
let camera, scene, renderer, controls;
|
|
let reflector;
|
|
let stats, statsMesh;
|
|
let vrbutton;
|
|
|
|
const parameters = {
|
|
env: 1.0,
|
|
};
|
|
|
|
init();
|
|
|
|
function init() {
|
|
|
|
scene = new THREE.Scene();
|
|
scene.background = new THREE.Color( 0xcccccc );
|
|
|
|
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 2000 );
|
|
|
|
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;
|
|
renderer.domElement.className = "render"
|
|
document.body.appendChild( renderer.domElement );
|
|
|
|
document.body.appendChild( vrbutton = VRButton.createButton( renderer ) );
|
|
|
|
window.addEventListener( 'resize', onWindowResize );
|
|
|
|
let cameraRig = new THREE.Group()
|
|
cameraRig.position.set( 0, 0, 0 );
|
|
camera.position.set(0,1.6,0)
|
|
cameraRig.add(camera)
|
|
scene.add(cameraRig)
|
|
|
|
|
|
// enable XR fragments
|
|
let XRF = xrf.init({
|
|
THREE,
|
|
camera:cameraRig,
|
|
scene,
|
|
renderer,
|
|
loaders: { gltf: GLTFLoader, glb: GLTFLoader, fbx: FBXLoader }, // which 3D assets (extensions) to check for XR fragments?
|
|
})
|
|
|
|
|
|
// optional: react/extend/hook into XR fragment
|
|
XRF.env = (xrf,v,opts) => {
|
|
let { mesh, model, camera, scene, renderer, THREE} = opts
|
|
xrf(v,opts)
|
|
}
|
|
|
|
// *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
|
|
|
|
let file = document.location.search.length > 2 ? document.location.search.substr(1) + document.location.hash : 'index.glb'
|
|
|
|
// 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()
|
|
}
|
|
})
|
|
|
|
XRF.navigator.to( file )
|
|
|
|
|
|
// setup mouse controls
|
|
//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;
|
|
|
|
//controls.screenSpacePanning = false;
|
|
|
|
//controls.minDistance = 0.1;
|
|
//controls.maxDistance = 5000;
|
|
//controls.maxPolarAngle = Math.PI / 2;
|
|
//controls.target = new THREE.Vector3(0,1.6,0)
|
|
|
|
//controls.update()
|
|
|
|
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 ) );
|
|
cameraRig.add( controller1 );
|
|
|
|
const controller2 = renderer.xr.getController( 1 );
|
|
controller2.add( new THREE.Line( geometry ) );
|
|
cameraRig.add( controller2 );
|
|
|
|
|
|
const controllerModelFactory = new XRControllerModelFactory();
|
|
|
|
const controllerGrip1 = renderer.xr.getControllerGrip( 0 );
|
|
controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
|
|
cameraRig.add( controllerGrip1 );
|
|
|
|
// hand1
|
|
const hand1 = renderer.xr.getHand( 0 );
|
|
const handModel1 = new OculusHandModel( hand1 );
|
|
hand1.add( handModel1 );
|
|
cameraRig.add( hand1 );
|
|
|
|
const controllerGrip2 = renderer.xr.getControllerGrip( 1 );
|
|
controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
|
|
cameraRig.add( controllerGrip2 );
|
|
|
|
// hand2
|
|
const hand2 = renderer.xr.getHand( 1 );
|
|
const handModel2 = new OculusHandModel( hand2 );
|
|
hand2.add( handModel2 );
|
|
cameraRig.add( hand2 );
|
|
|
|
// 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;
|
|
statsMesh.position.z = 0.3;
|
|
statsMesh.rotation.y = Math.PI / 4;
|
|
statsMesh.scale.setScalar( 2.5 );
|
|
|
|
vrbutton.addEventListener('click', () => {
|
|
XRF.interactive.add( statsMesh );
|
|
})
|
|
|
|
// util func to bind uploaded files to a loader
|
|
let loadFile = function(contentLoaders, multiple){
|
|
return function(){
|
|
alert("if you're on Meta browser, file-uploads might be disabled")
|
|
let input = document.createElement('input');
|
|
input.type = 'file';
|
|
input.multiple = multiple;
|
|
input.accept = Object.keys(contentLoaders).join(",");
|
|
input.onchange = function(){
|
|
let files = Array.from(input.files);
|
|
let file = files.slice ? files[0] : files
|
|
for( var i in contentLoaders ){
|
|
let r = new RegExp('\\'+i+'$')
|
|
if( file.name.match(r) ) return contentLoaders[i](file)
|
|
}
|
|
alert(file.name+" is not supported")
|
|
};
|
|
input.click();
|
|
}
|
|
}
|
|
|
|
|
|
let fileLoaders = loadFile({
|
|
".gltf": (file) => file.arrayBuffer().then( (data) => xrf.navigator.to(file.name,null, (new xrf.loaders.gltf()), data) ),
|
|
".glb": (file) => file.arrayBuffer().then( (data) => xrf.navigator.to(file.name,null, (new xrf.loaders.gltf()), data) )
|
|
})
|
|
$("input[type=submit]").addEventListener("click", fileLoaders )
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
//controls.update()
|
|
renderer.render( scene, camera );
|
|
stats.update();
|
|
|
|
// Canvas elements doesn't trigger DOM updates, so we have to update the texture
|
|
statsMesh.material.map.update();
|
|
|
|
}
|
|
animate();
|
|
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|