| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - gltf loader</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="example.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
- <div class="title-wrapper">
- <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>glTF Loader</span>
- </div>
- <small>
- <a href="https://hdrihaven.com/hdri/?h=royal_esplanade" target="_blank" rel="noopener">Royal Esplanade</a> by <a href="https://hdrihaven.com/" target="_blank" rel="noopener">HDRI Haven</a>
- </small>
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.webgpu.js",
- "three/webgpu": "../build/three.webgpu.js",
- "three/tsl": "../build/three.tsl.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three/webgpu';
- import { UltraHDRLoader } from 'three/addons/loaders/UltraHDRLoader.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
- import { Inspector } from 'three/addons/inspector/Inspector.js';
- let camera, scene, renderer, controls;
- let currentModel, mixer;
- let currentLoadId = 0;
- const timer = new THREE.Timer();
- init().then( render );
- async function init() {
- const container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
- camera.position.set( - 1.8, 0.6, 2.7 );
- scene = new THREE.Scene();
- new UltraHDRLoader()
- .setPath( 'textures/equirectangular/' )
- .load( 'royal_esplanade_2k.hdr.jpg', function ( texture ) {
- texture.mapping = THREE.EquirectangularReflectionMapping;
- //texture.minFilter = THREE.LinearMipmapLinearFilter;
- //texture.generateMipmaps = true;
- scene.background = texture;
- scene.environment = texture;
- // model
- fetch( 'https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/model-index.json' )
- .then( response => response.json() )
- .then( models => {
- const gui = renderer.inspector.createParameters( 'Model' );
- const modelNames = models.map( m => m.name );
- const params = { model: 'DamagedHelmet' };
- if ( ! modelNames.includes( params.model ) && modelNames.length > 0 ) {
- params.model = modelNames[ 0 ];
- }
- gui.add( params, 'model', modelNames ).onChange( name => {
- const modelInfo = models.find( m => m.name === name );
- loadModel( modelInfo );
- } );
- gui.add( scene, 'backgroundBlurriness', 0, 1 );
- const initialModel = models.find( m => m.name === params.model );
- if ( initialModel ) loadModel( initialModel );
- } );
- } );
- renderer = new THREE.WebGPURenderer( { antialias: true/*, compatibilityMode: true*/ } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( render );
- renderer.toneMapping = THREE.ACESFilmicToneMapping;
- renderer.inspector = new Inspector();
- container.appendChild( renderer.domElement );
- await renderer.init();
- controls = new OrbitControls( camera, renderer.domElement );
- controls.enableDamping = true;
- controls.minDistance = 2;
- controls.maxDistance = 10;
- controls.target.set( 0, 0, - 0.2 );
- controls.update();
- window.addEventListener( 'resize', onWindowResize );
- }
- function loadModel( modelInfo ) {
- const variants = modelInfo.variants;
- const variant = variants[ 'glTF-Binary' ] || variants[ 'glTF' ];
- const url = `https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/${ modelInfo.name }/${ variant.endsWith( '.glb' ) ? 'glTF-Binary' : 'glTF' }/${ variant }`;
- if ( currentModel ) {
- scene.remove( currentModel );
- currentModel = null;
- }
- if ( mixer ) {
- mixer.stopAllAction();
- mixer = null;
- }
- const loadId = ++ currentLoadId;
- const loader = new GLTFLoader();
- loader.load( url, async function ( gltf ) {
- if ( loadId !== currentLoadId ) return;
- currentModel = gltf.scene;
- // wait until the model can be added to the scene without blocking due to shader compilation
- await renderer.compileAsync( currentModel, camera, scene );
- if ( loadId !== currentLoadId ) return;
- scene.add( currentModel );
- fitCameraToSelection( camera, controls, currentModel );
- // animations
- if ( gltf.animations.length > 0 ) {
- mixer = new THREE.AnimationMixer( currentModel );
- for ( const animation of gltf.animations ) {
- mixer.clipAction( animation ).play();
- }
- }
- } );
- }
- function fitCameraToSelection( camera, controls, selection, fitOffset = 1.3 ) {
- const box = new THREE.Box3();
- box.setFromObject( selection );
- const size = box.getSize( new THREE.Vector3() );
- const center = box.getCenter( new THREE.Vector3() );
- const maxSize = Math.max( size.x, size.y, size.z );
- const fitHeightDistance = maxSize / ( 2 * Math.atan( Math.PI * camera.fov / 360 ) );
- // const fitWidthDistance = fitHeightDistance / camera.aspect;
- // const distance = fitOffset * Math.max( fitHeightDistance, fitWidthDistance );
- const distance = fitOffset * fitHeightDistance;
- const direction = controls.target.clone().sub( camera.position ).normalize().multiplyScalar( distance );
- controls.maxDistance = distance * 10;
- controls.minDistance = distance / 10;
- controls.target.copy( center );
- camera.near = distance / 100;
- camera.far = distance * 100;
- camera.updateProjectionMatrix();
- camera.position.copy( controls.target ).sub( direction );
- controls.update();
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function render() {
- timer.update();
- controls.update();
- if ( mixer ) mixer.update( timer.getDelta() );
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|