webgpu_loader_gltf.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 { HDRLoader } from 'three/addons/loaders/HDRLoader.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;
  37. init().then( render );
  38. async function init() {
  39. const container = document.createElement( 'div' );
  40. document.body.appendChild( container );
  41. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  42. camera.position.set( - 1.8, 0.6, 2.7 );
  43. scene = new THREE.Scene();
  44. new HDRLoader()
  45. .setPath( 'textures/equirectangular/' )
  46. .load( 'royal_esplanade_1k.hdr', function ( texture ) {
  47. texture.mapping = THREE.EquirectangularReflectionMapping;
  48. //texture.minFilter = THREE.LinearMipmapLinearFilter;
  49. //texture.generateMipmaps = true;
  50. scene.background = texture;
  51. scene.environment = texture;
  52. // model
  53. fetch( 'https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/model-index.json' )
  54. .then( response => response.json() )
  55. .then( models => {
  56. const gui = renderer.inspector.createParameters( 'Model' );
  57. const modelNames = models.map( m => m.name );
  58. const params = { model: 'DamagedHelmet' };
  59. if ( ! modelNames.includes( params.model ) && modelNames.length > 0 ) {
  60. params.model = modelNames[ 0 ];
  61. }
  62. gui.add( params, 'model', modelNames ).onChange( name => {
  63. const modelInfo = models.find( m => m.name === name );
  64. loadModel( modelInfo );
  65. } );
  66. const initialModel = models.find( m => m.name === params.model );
  67. if ( initialModel ) loadModel( initialModel );
  68. } );
  69. } );
  70. renderer = new THREE.WebGPURenderer( { antialias: true/*, compatibilityMode: true*/ } );
  71. renderer.setPixelRatio( window.devicePixelRatio );
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. renderer.setAnimationLoop( render );
  74. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  75. renderer.inspector = new Inspector();
  76. container.appendChild( renderer.domElement );
  77. await renderer.init();
  78. controls = new OrbitControls( camera, renderer.domElement );
  79. controls.minDistance = 2;
  80. controls.maxDistance = 10;
  81. controls.target.set( 0, 0, - 0.2 );
  82. controls.update();
  83. window.addEventListener( 'resize', onWindowResize );
  84. }
  85. function loadModel( modelInfo ) {
  86. const variants = modelInfo.variants;
  87. const variant = variants[ 'glTF-Binary' ] || variants[ 'glTF' ];
  88. const url = `https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/${ modelInfo.name }/${ variant.endsWith( '.glb' ) ? 'glTF-Binary' : 'glTF' }/${ variant }`;
  89. if ( currentModel ) {
  90. scene.remove( currentModel );
  91. currentModel = null;
  92. }
  93. const loader = new GLTFLoader();
  94. loader.load( url, async function ( gltf ) {
  95. currentModel = gltf.scene;
  96. // wait until the model can be added to the scene without blocking due to shader compilation
  97. await renderer.compileAsync( currentModel, camera, scene );
  98. scene.add( currentModel );
  99. // scale to 1.0
  100. const box = new THREE.Box3().setFromObject( currentModel );
  101. const size = box.getSize( new THREE.Vector3() );
  102. const maxSize = Math.max( size.x, size.y, size.z );
  103. currentModel.scale.multiplyScalar( 1.0 / maxSize );
  104. fitCameraToSelection( camera, controls, currentModel );
  105. } );
  106. }
  107. function fitCameraToSelection( camera, controls, selection, fitOffset = 1.3 ) {
  108. const box = new THREE.Box3();
  109. box.setFromObject( selection );
  110. const size = box.getSize( new THREE.Vector3() );
  111. const center = box.getCenter( new THREE.Vector3() );
  112. const maxSize = Math.max( size.x, size.y, size.z );
  113. const fitHeightDistance = maxSize / ( 2 * Math.atan( Math.PI * camera.fov / 360 ) );
  114. // const fitWidthDistance = fitHeightDistance / camera.aspect;
  115. // const distance = fitOffset * Math.max( fitHeightDistance, fitWidthDistance );
  116. const distance = fitOffset * fitHeightDistance;
  117. const direction = controls.target.clone().sub( camera.position ).normalize().multiplyScalar( distance );
  118. controls.maxDistance = distance * 10;
  119. controls.minDistance = distance / 10;
  120. controls.target.copy( center );
  121. camera.near = distance / 100;
  122. camera.far = distance * 100;
  123. camera.updateProjectionMatrix();
  124. camera.position.copy( controls.target ).sub( direction );
  125. controls.update();
  126. }
  127. function onWindowResize() {
  128. camera.aspect = window.innerWidth / window.innerHeight;
  129. camera.updateProjectionMatrix();
  130. renderer.setSize( window.innerWidth, window.innerHeight );
  131. }
  132. //
  133. function render() {
  134. renderer.render( scene, camera );
  135. }
  136. </script>
  137. </body>
  138. </html>
粤ICP备19079148号