misc_exporter_usdz.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - USDZ exporter</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. <meta property="og:title" content="three.js webgl - USDZ exporter">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/misc_exporter_usdz.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/misc_exporter_usdz.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. body {
  14. background-color: #eee;
  15. }
  16. #info {
  17. color: #222;
  18. }
  19. a {
  20. color: #00f
  21. }
  22. #button {
  23. position: absolute;
  24. bottom: 15px;
  25. left: calc(50% - 40px);
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="info">
  31. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - USDZ exporter<br />
  32. Carbon Frame Bike by
  33. <a href="https://sketchfab.com/prefrontalcortex" target="_blank" rel="noopener">prefrontal cortex</a>
  34. </div>
  35. <a id="link" rel="ar" href="" download="asset.usdz">
  36. <img id="button" width="100" src="files/arkit.png">
  37. </a>
  38. <script type="importmap">
  39. {
  40. "imports": {
  41. "three": "../build/three.module.js",
  42. "three/addons/": "./jsm/"
  43. }
  44. }
  45. </script>
  46. <script type="module">
  47. import * as THREE from 'three';
  48. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  49. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  50. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  51. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  52. import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
  53. import { USDZExporter } from 'three/addons/exporters/USDZExporter.js';
  54. import * as WebGLTextureUtils from 'three/addons/utils/WebGLTextureUtils.js';
  55. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  56. let camera, scene, renderer, mixer, timer, controls;
  57. const params = {
  58. exportUSDZ: exportUSDZ
  59. };
  60. init();
  61. function init() {
  62. timer = new THREE.Timer();
  63. renderer = new THREE.WebGLRenderer( { antialias: true } );
  64. renderer.setPixelRatio( window.devicePixelRatio );
  65. renderer.setSize( window.innerWidth, window.innerHeight );
  66. renderer.setAnimationLoop( animate );
  67. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  68. document.body.appendChild( renderer.domElement );
  69. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.25, 20 );
  70. camera.position.set( - 2.5, 1.6, 3.0 );
  71. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  72. scene = new THREE.Scene();
  73. scene.background = new THREE.Color( 0xf0f0f0 );
  74. scene.environment = pmremGenerator.fromScene( new RoomEnvironment(), 0.04 ).texture;
  75. const ktx2loader = new KTX2Loader().detectSupport( renderer );
  76. const dracoLoader = new DRACOLoader();
  77. const gltfLoader = new GLTFLoader();
  78. gltfLoader.setDRACOLoader( dracoLoader );
  79. gltfLoader.setKTX2Loader( ktx2loader );
  80. gltfLoader.setPath( 'models/gltf/' );
  81. gltfLoader.load( 'CarbonFrameBike.glb', async function ( gltf ) {
  82. scene.add( gltf.scene );
  83. if ( gltf.animations.length > 0 ) {
  84. mixer = new THREE.AnimationMixer( gltf.scene );
  85. const action = mixer.clipAction( gltf.animations[ 0 ] );
  86. action.setLoop( THREE.LoopRepeat, Infinity );
  87. action.play();
  88. }
  89. // USDZ
  90. const exporter = new USDZExporter();
  91. exporter.setTextureUtils( WebGLTextureUtils ); // for texture decompresssing
  92. const arraybuffer = await exporter.parseAsync( gltf.scene, { animations: gltf.animations } );
  93. const blob = new Blob( [ arraybuffer ], { type: 'application/octet-stream' } );
  94. const link = document.getElementById( 'link' );
  95. link.href = URL.createObjectURL( blob );
  96. } );
  97. controls = new OrbitControls( camera, renderer.domElement );
  98. controls.minDistance = 2;
  99. controls.maxDistance = 10;
  100. controls.enableDamping = true;
  101. controls.target.set( 0, 0.7, 0 );
  102. controls.update();
  103. window.addEventListener( 'resize', onWindowResize );
  104. const isIOS = /iPad|iPhone|iPod/.test( navigator.userAgent );
  105. if ( isIOS === false ) {
  106. const gui = new GUI();
  107. gui.add( params, 'exportUSDZ' ).name( 'Export USDZ' );
  108. gui.open();
  109. }
  110. }
  111. function onWindowResize() {
  112. camera.aspect = window.innerWidth / window.innerHeight;
  113. camera.updateProjectionMatrix();
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. }
  116. function exportUSDZ() {
  117. const link = document.getElementById( 'link' );
  118. link.click();
  119. }
  120. //
  121. function animate() {
  122. timer.update();
  123. const delta = timer.getDelta();
  124. if ( mixer ) mixer.update( delta );
  125. controls.update();
  126. renderer.render( scene, camera );
  127. }
  128. </script>
  129. </body>
  130. </html>
粤ICP备19079148号