webgpu_loader_gltf.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - gltf loader</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>glTF Loader</span>
  14. </div>
  15. <small>
  16. <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>
  17. </small>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.webgpu.js",
  23. "three/webgpu": "../build/three.webgpu.js",
  24. "three/tsl": "../build/three.tsl.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three/webgpu';
  31. import { UltraHDRLoader } from 'three/addons/loaders/UltraHDRLoader.js';
  32. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  33. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  34. import { Inspector } from 'three/addons/inspector/Inspector.js';
  35. let camera, scene, renderer, controls;
  36. let currentModel, mixer;
  37. let currentLoadId = 0;
  38. const timer = new THREE.Timer();
  39. init().then( render );
  40. async function init() {
  41. const container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  44. camera.position.set( - 1.8, 0.6, 2.7 );
  45. scene = new THREE.Scene();
  46. new UltraHDRLoader()
  47. .setPath( 'textures/equirectangular/' )
  48. .load( 'royal_esplanade_2k.hdr.jpg', function ( texture ) {
  49. texture.mapping = THREE.EquirectangularReflectionMapping;
  50. //texture.minFilter = THREE.LinearMipmapLinearFilter;
  51. //texture.generateMipmaps = true;
  52. scene.background = texture;
  53. scene.environment = texture;
  54. // model
  55. fetch( 'https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/model-index.json' )
  56. .then( response => response.json() )
  57. .then( models => {
  58. const gui = renderer.inspector.createParameters( 'Model' );
  59. const modelNames = models.map( m => m.name );
  60. const params = { model: 'DamagedHelmet' };
  61. if ( ! modelNames.includes( params.model ) && modelNames.length > 0 ) {
  62. params.model = modelNames[ 0 ];
  63. }
  64. gui.add( params, 'model', modelNames ).onChange( name => {
  65. const modelInfo = models.find( m => m.name === name );
  66. loadModel( modelInfo );
  67. } );
  68. gui.add( scene, 'backgroundBlurriness', 0, 1 );
  69. const initialModel = models.find( m => m.name === params.model );
  70. if ( initialModel ) loadModel( initialModel );
  71. } );
  72. } );
  73. renderer = new THREE.WebGPURenderer( { antialias: true/*, compatibilityMode: true*/ } );
  74. renderer.setPixelRatio( window.devicePixelRatio );
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. renderer.setAnimationLoop( render );
  77. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  78. renderer.inspector = new Inspector();
  79. container.appendChild( renderer.domElement );
  80. await renderer.init();
  81. controls = new OrbitControls( camera, renderer.domElement );
  82. controls.enableDamping = true;
  83. controls.minDistance = 2;
  84. controls.maxDistance = 10;
  85. controls.target.set( 0, 0, - 0.2 );
  86. controls.update();
  87. window.addEventListener( 'resize', onWindowResize );
  88. }
  89. function loadModel( modelInfo ) {
  90. const variants = modelInfo.variants;
  91. const variant = variants[ 'glTF-Binary' ] || variants[ 'glTF' ];
  92. const url = `https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/${ modelInfo.name }/${ variant.endsWith( '.glb' ) ? 'glTF-Binary' : 'glTF' }/${ variant }`;
  93. if ( currentModel ) {
  94. scene.remove( currentModel );
  95. currentModel = null;
  96. }
  97. if ( mixer ) {
  98. mixer.stopAllAction();
  99. mixer = null;
  100. }
  101. const loadId = ++ currentLoadId;
  102. const loader = new GLTFLoader();
  103. loader.load( url, async function ( gltf ) {
  104. if ( loadId !== currentLoadId ) return;
  105. currentModel = gltf.scene;
  106. // wait until the model can be added to the scene without blocking due to shader compilation
  107. await renderer.compileAsync( currentModel, camera, scene );
  108. if ( loadId !== currentLoadId ) return;
  109. scene.add( currentModel );
  110. fitCameraToSelection( camera, controls, currentModel );
  111. // animations
  112. if ( gltf.animations.length > 0 ) {
  113. mixer = new THREE.AnimationMixer( currentModel );
  114. for ( const animation of gltf.animations ) {
  115. mixer.clipAction( animation ).play();
  116. }
  117. }
  118. } );
  119. }
  120. function fitCameraToSelection( camera, controls, selection, fitOffset = 1.3 ) {
  121. const box = new THREE.Box3();
  122. box.setFromObject( selection );
  123. const size = box.getSize( new THREE.Vector3() );
  124. const center = box.getCenter( new THREE.Vector3() );
  125. const maxSize = Math.max( size.x, size.y, size.z );
  126. const fitHeightDistance = maxSize / ( 2 * Math.atan( Math.PI * camera.fov / 360 ) );
  127. // const fitWidthDistance = fitHeightDistance / camera.aspect;
  128. // const distance = fitOffset * Math.max( fitHeightDistance, fitWidthDistance );
  129. const distance = fitOffset * fitHeightDistance;
  130. const direction = controls.target.clone().sub( camera.position ).normalize().multiplyScalar( distance );
  131. controls.maxDistance = distance * 10;
  132. controls.minDistance = distance / 10;
  133. controls.target.copy( center );
  134. camera.near = distance / 100;
  135. camera.far = distance * 100;
  136. camera.updateProjectionMatrix();
  137. camera.position.copy( controls.target ).sub( direction );
  138. controls.update();
  139. }
  140. function onWindowResize() {
  141. camera.aspect = window.innerWidth / window.innerHeight;
  142. camera.updateProjectionMatrix();
  143. renderer.setSize( window.innerWidth, window.innerHeight );
  144. }
  145. //
  146. function render() {
  147. timer.update();
  148. controls.update();
  149. if ( mixer ) mixer.update( timer.getDelta() );
  150. renderer.render( scene, camera );
  151. }
  152. </script>
  153. </body>
  154. </html>
粤ICP备19079148号