misc_exporter_usdz.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #eee;
  11. }
  12. #info {
  13. color: #222;
  14. }
  15. a {
  16. color: #00f
  17. }
  18. #button {
  19. position: absolute;
  20. bottom: 15px;
  21. left: calc(50% - 40px);
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="info">
  27. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - USDZ exporter<br />
  28. Battle Damaged Sci-fi Helmet by
  29. <a href="https://sketchfab.com/theblueturtle_" target="_blank" rel="noopener">theblueturtle_</a>
  30. </div>
  31. <a id="link" rel="ar" href="" download="asset.usdz">
  32. <img id="button" width="100" src="files/arkit.png">
  33. </a>
  34. <!-- Import maps polyfill -->
  35. <!-- Remove this when import maps will be widely supported -->
  36. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  37. <script type="importmap">
  38. {
  39. "imports": {
  40. "three": "../build/three.module.js",
  41. "three/addons/": "./jsm/"
  42. }
  43. }
  44. </script>
  45. <script type="module">
  46. import * as THREE from 'three';
  47. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  48. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  49. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  50. import { USDZExporter } from 'three/addons/exporters/USDZExporter.js';
  51. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  52. let camera, scene, renderer;
  53. const params = {
  54. exportUSDZ: exportUSDZ
  55. };
  56. init();
  57. render();
  58. function init() {
  59. renderer = new THREE.WebGLRenderer( { antialias: true } );
  60. renderer.setPixelRatio( window.devicePixelRatio );
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  63. document.body.appendChild( renderer.domElement );
  64. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  65. camera.position.set( - 2.5, 0.6, 3.0 );
  66. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  67. scene = new THREE.Scene();
  68. scene.background = new THREE.Color( 0xf0f0f0 );
  69. scene.environment = pmremGenerator.fromScene( new RoomEnvironment( renderer ), 0.04 ).texture;
  70. const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
  71. loader.load( 'DamagedHelmet.gltf', async function ( gltf ) {
  72. scene.add( gltf.scene );
  73. const shadowMesh = createSpotShadowMesh();
  74. shadowMesh.position.y = - 1.1;
  75. shadowMesh.position.z = - 0.25;
  76. shadowMesh.scale.setScalar( 2 );
  77. scene.add( shadowMesh );
  78. render();
  79. // USDZ
  80. const exporter = new USDZExporter();
  81. const arraybuffer = await exporter.parse( gltf.scene );
  82. const blob = new Blob( [ arraybuffer ], { type: 'application/octet-stream' } );
  83. const link = document.getElementById( 'link' );
  84. link.href = URL.createObjectURL( blob );
  85. } );
  86. const controls = new OrbitControls( camera, renderer.domElement );
  87. controls.addEventListener( 'change', render ); // use if there is no animation loop
  88. controls.minDistance = 2;
  89. controls.maxDistance = 10;
  90. controls.target.set( 0, - 0.15, - 0.2 );
  91. controls.update();
  92. window.addEventListener( 'resize', onWindowResize );
  93. const isIOS = /iPad|iPhone|iPod/.test( navigator.userAgent );
  94. if ( isIOS === false ) {
  95. const gui = new GUI();
  96. gui.add( params, 'exportUSDZ' ).name( 'Export USDZ' );
  97. gui.open();
  98. }
  99. }
  100. function createSpotShadowMesh() {
  101. const canvas = document.createElement( 'canvas' );
  102. canvas.width = 128;
  103. canvas.height = 128;
  104. const context = canvas.getContext( '2d' );
  105. const gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  106. gradient.addColorStop( 0.1, 'rgba(130,130,130,1)' );
  107. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  108. context.fillStyle = gradient;
  109. context.fillRect( 0, 0, canvas.width, canvas.height );
  110. const shadowTexture = new THREE.CanvasTexture( canvas );
  111. const geometry = new THREE.PlaneGeometry();
  112. const material = new THREE.MeshBasicMaterial( {
  113. map: shadowTexture, blending: THREE.MultiplyBlending, toneMapped: false
  114. } );
  115. const mesh = new THREE.Mesh( geometry, material );
  116. mesh.rotation.x = - Math.PI / 2;
  117. return mesh;
  118. }
  119. function onWindowResize() {
  120. camera.aspect = window.innerWidth / window.innerHeight;
  121. camera.updateProjectionMatrix();
  122. renderer.setSize( window.innerWidth, window.innerHeight );
  123. render();
  124. }
  125. function exportUSDZ() {
  126. const link = document.getElementById( 'link' );
  127. link.click();
  128. }
  129. //
  130. function render() {
  131. renderer.render( scene, camera );
  132. }
  133. </script>
  134. </body>
  135. </html>