xrfragment/example/threejs/sandbox/index.html

278 lines
9.0 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js vr - sandbox</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="./../../assets/axist.min.css" />
<link type="text/css" rel="stylesheet" href="main.css">
<link type="text/css" rel="stylesheet" href="./../../assets/style.css"/>
</head>
<body>
<div id="overlay" x-data="{ urls: ['#pos=0,2,3'] }">
<img src="./../../assets/logo.png" class="logo"/>
<input type="submit" value="load 3D asset"></input>
<input type="text" id="uri" list="urls" value="#pos=0,2,3" x-on:change="XRF.eval( $('#uri').value, XRF.getLastModel() )"/>
<datalist id="urls" >
<template x-for="url in urls">
<option x-text="url"></option>
</template>
</datalist>
</div>
<a id="source" target="_blank" href="https://github.com/coderofsalvation/xrfragment/blob/main/example/threejs/sandbox/index.html#L92-L112">sourcecode</a>
<textarea style="display:none"></textarea>
<script async src="./../../assets/alpine.min.js"></script>
<!-- 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 xrfragment from './../../../dist/xrfragment.three.js';
import { loadFile, setupConsole } from './../../assets/utils.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 { 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';
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)
let camera, scene, renderer, controls;
let reflector;
let stats, statsMesh;
let vrbutton;
const parameters = {
env: 1.0,
};
init();
animate();
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 );
// enable XR fragments
let XRF = xrfragment.init({
THREE,
camera,
scene,
renderer,
debug: true,
loaders: [ GLTFLoader, FBXLoader ], // which 3D assets 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)
}
// optional: react/extend/hook into custom fragment (for non-standard custom framework logic e.g.)
XRF.foobar = (xrf,v,opts) => {
let { mesh, model, camera, scene, renderer, THREE} = opts
console.log("hello custom property 'foobar'")
}
// *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
// load 3D asset
let model;
const loader = new GLTFLoader().setPath( './../../assets/')
let loadGLTF = function ( gltf ) {
if( model ){
scene.remove(model)
//model.dispose()
}
gltf.scene.position.y = 1.5
gltf.scene.position.z = -4
gltf.scene.rotation.y = -0.5
gltf.scene.scale.x = gltf.scene.scale.y = gltf.scene.scale.z = 1;
const maxAnisotropy = renderer.capabilities.getMaxAnisotropy();
function recursivelySetChildrenUnlit(mesh,cb) {
cb(mesh)
if (mesh.children) {
for (var i = 0; i < mesh.children.length; i++) {
recursivelySetChildrenUnlit(mesh.children[i],cb);
}
}
}
scene.add( model = gltf.scene );
render();
};
loader.load( 'example2.gltf#pos=0,1,2', loadGLTF );
// 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)
camera.position.set( 0, 1.6, 1.5 );
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 ) );
scene.add( controller1 );
const controller2 = renderer.xr.getController( 1 );
controller2.add( new THREE.Line( geometry ) );
scene.add( controller2 );
const controllerModelFactory = new XRControllerModelFactory();
const controllerGrip1 = renderer.xr.getControllerGrip( 0 );
controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
scene.add( controllerGrip1 );
const controllerGrip2 = renderer.xr.getControllerGrip( 1 );
controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
scene.add( controllerGrip2 );
setupConsole( $('textarea') )
// GUI
function onChange() {
renderer.toneMappingExposure = parameters.env;
}
const gui = new GUI( { width: 300 } );
gui.add( parameters, 'env', 0.2, 3.0, 0.1 ).onChange( onChange );
const group = new InteractiveGroup( renderer, camera );
vrbutton.addEventListener('click', () => {
// show gui inside VR scene
gui.domElement.style.visibility = 'hidden';
scene.add( group );
})
const mesh = new HTMLMesh( gui.domElement );
mesh.position.x = - 0.75;
mesh.position.y = 1.5;
mesh.position.z = 0.3;
mesh.rotation.y = Math.PI / 4;
mesh.scale.setScalar( 2 );
group.add( mesh );
// 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 );
group.add( statsMesh );
let fileLoaders = loadFile({
".gltf": (file) => file.arrayBuffer().then( (data) => loader.parse( data, '', loadGLTF, console.error ) ),
".glb": (file) => file.arrayBuffer().then( (data) => loader.parse( data, '', loadGLTF, console.error ) )
})
$("#overlay > 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;
if( XRF.getLastModel() ) XRF.getLastModel().render(scene,camera)
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();
}
</script>
</body>
</html>