misc_exporter_usdz.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. <script type="importmap">
  35. {
  36. "imports": {
  37. "three": "../build/three.module.js",
  38. "three/addons/": "./jsm/"
  39. }
  40. }
  41. </script>
  42. <script type="module">
  43. import * as THREE from 'three';
  44. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  45. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  46. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  47. import { USDZExporter } from 'three/addons/exporters/USDZExporter.js';
  48. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  49. let camera, scene, renderer;
  50. const params = {
  51. exportUSDZ: exportUSDZ
  52. };
  53. init();
  54. render();
  55. function init() {
  56. renderer = new THREE.WebGLRenderer( { antialias: true } );
  57. renderer.setPixelRatio( window.devicePixelRatio );
  58. renderer.setSize( window.innerWidth, window.innerHeight );
  59. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  60. document.body.appendChild( renderer.domElement );
  61. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  62. camera.position.set( - 2.5, 0.6, 3.0 );
  63. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  64. scene = new THREE.Scene();
  65. scene.background = new THREE.Color( 0xf0f0f0 );
  66. scene.environment = pmremGenerator.fromScene( new RoomEnvironment(), 0.04 ).texture;
  67. const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
  68. loader.load( 'DamagedHelmet.gltf', async function ( gltf ) {
  69. scene.add( gltf.scene );
  70. const shadowMesh = createSpotShadowMesh();
  71. shadowMesh.position.y = - 1.1;
  72. shadowMesh.position.z = - 0.25;
  73. shadowMesh.scale.setScalar( 2 );
  74. scene.add( shadowMesh );
  75. render();
  76. // USDZ
  77. const exporter = new USDZExporter();
  78. const arraybuffer = await exporter.parseAsync( gltf.scene );
  79. const blob = new Blob( [ arraybuffer ], { type: 'application/octet-stream' } );
  80. const link = document.getElementById( 'link' );
  81. link.href = URL.createObjectURL( blob );
  82. } );
  83. const controls = new OrbitControls( camera, renderer.domElement );
  84. controls.addEventListener( 'change', render ); // use if there is no animation loop
  85. controls.minDistance = 2;
  86. controls.maxDistance = 10;
  87. controls.target.set( 0, - 0.15, - 0.2 );
  88. controls.update();
  89. window.addEventListener( 'resize', onWindowResize );
  90. const isIOS = /iPad|iPhone|iPod/.test( navigator.userAgent );
  91. if ( isIOS === false ) {
  92. const gui = new GUI();
  93. gui.add( params, 'exportUSDZ' ).name( 'Export USDZ' );
  94. gui.open();
  95. }
  96. }
  97. function createSpotShadowMesh() {
  98. const canvas = document.createElement( 'canvas' );
  99. canvas.width = 128;
  100. canvas.height = 128;
  101. const context = canvas.getContext( '2d' );
  102. const gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  103. gradient.addColorStop( 0.1, 'rgba(130,130,130,1)' );
  104. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  105. context.fillStyle = gradient;
  106. context.fillRect( 0, 0, canvas.width, canvas.height );
  107. const shadowTexture = new THREE.CanvasTexture( canvas );
  108. const geometry = new THREE.PlaneGeometry();
  109. const material = new THREE.MeshBasicMaterial( {
  110. map: shadowTexture, blending: THREE.MultiplyBlending, toneMapped: false
  111. } );
  112. const mesh = new THREE.Mesh( geometry, material );
  113. mesh.rotation.x = - Math.PI / 2;
  114. return mesh;
  115. }
  116. function onWindowResize() {
  117. camera.aspect = window.innerWidth / window.innerHeight;
  118. camera.updateProjectionMatrix();
  119. renderer.setSize( window.innerWidth, window.innerHeight );
  120. render();
  121. }
  122. function exportUSDZ() {
  123. const link = document.getElementById( 'link' );
  124. link.click();
  125. }
  126. //
  127. function render() {
  128. renderer.render( scene, camera );
  129. }
  130. </script>
  131. </body>
  132. </html>