misc_exporter_usdz.html 4.3 KB

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