misc_exporter_gltf.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - exporter - gltf</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 - exporter - gltf">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/misc_exporter_gltf.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/misc_exporter_gltf.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - exporter - gltf
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { GLTFExporter } from 'three/addons/exporters/GLTFExporter.js';
  28. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  29. import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
  30. import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js';
  31. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  32. import * as TextureUtils from 'three/addons/utils/WebGLTextureUtils.js';
  33. function exportGLTF( input ) {
  34. const gltfExporter = new GLTFExporter().setTextureUtils( TextureUtils );
  35. const options = {
  36. trs: params.trs,
  37. onlyVisible: params.onlyVisible,
  38. binary: params.binary,
  39. maxTextureSize: params.maxTextureSize
  40. };
  41. gltfExporter.parse(
  42. input,
  43. function ( result ) {
  44. if ( result instanceof ArrayBuffer ) {
  45. saveArrayBuffer( result, 'scene.glb' );
  46. } else {
  47. const output = JSON.stringify( result, null, 2 );
  48. console.log( output );
  49. saveString( output, 'scene.gltf' );
  50. }
  51. },
  52. function ( error ) {
  53. console.log( 'An error happened during parsing', error );
  54. },
  55. options
  56. );
  57. }
  58. const link = document.createElement( 'a' );
  59. link.style.display = 'none';
  60. document.body.appendChild( link ); // Firefox workaround, see #6594
  61. function save( blob, filename ) {
  62. link.href = URL.createObjectURL( blob );
  63. link.download = filename;
  64. link.click();
  65. // URL.revokeObjectURL( url ); breaks Firefox...
  66. }
  67. function saveString( text, filename ) {
  68. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  69. }
  70. function saveArrayBuffer( buffer, filename ) {
  71. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  72. }
  73. let container;
  74. let camera, object, object2, material, geometry, scene1, scene2, renderer;
  75. let gridHelper, sphere, model, coffeemat, webpBox;
  76. const params = {
  77. trs: false,
  78. onlyVisible: true,
  79. binary: false,
  80. maxTextureSize: 4096,
  81. exportScene1: exportScene1,
  82. exportScenes: exportScenes,
  83. exportSphere: exportSphere,
  84. exportModel: exportModel,
  85. exportObjects: exportObjects,
  86. exportSceneObject: exportSceneObject,
  87. exportCompressedObject: exportCompressedObject,
  88. exportWebPModel: exportWebPModel,
  89. };
  90. init();
  91. function init() {
  92. container = document.createElement( 'div' );
  93. document.body.appendChild( container );
  94. // Make linear gradient texture
  95. const data = new Uint8ClampedArray( 100 * 100 * 4 );
  96. for ( let y = 0; y < 100; y ++ ) {
  97. for ( let x = 0; x < 100; x ++ ) {
  98. const stride = 4 * ( 100 * y + x );
  99. data[ stride ] = Math.round( 255 * y / 99 );
  100. data[ stride + 1 ] = Math.round( 255 - 255 * y / 99 );
  101. data[ stride + 2 ] = 0;
  102. data[ stride + 3 ] = 255;
  103. }
  104. }
  105. const gradientTexture = new THREE.DataTexture( data, 100, 100, THREE.RGBAFormat );
  106. gradientTexture.minFilter = THREE.LinearFilter;
  107. gradientTexture.magFilter = THREE.LinearFilter;
  108. gradientTexture.needsUpdate = true;
  109. scene1 = new THREE.Scene();
  110. scene1.name = 'Scene1';
  111. // ---------------------------------------------------------------------
  112. // Perspective Camera
  113. // ---------------------------------------------------------------------
  114. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  115. camera.position.set( 600, 400, 0 );
  116. camera.name = 'PerspectiveCamera';
  117. scene1.add( camera );
  118. // ---------------------------------------------------------------------
  119. // Ambient light
  120. // ---------------------------------------------------------------------
  121. const ambientLight = new THREE.AmbientLight( 0xcccccc );
  122. ambientLight.name = 'AmbientLight';
  123. scene1.add( ambientLight );
  124. // ---------------------------------------------------------------------
  125. // DirectLight
  126. // ---------------------------------------------------------------------
  127. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  128. dirLight.target.position.set( 0, 0, - 1 );
  129. dirLight.add( dirLight.target );
  130. dirLight.lookAt( - 1, - 1, 0 );
  131. dirLight.name = 'DirectionalLight';
  132. scene1.add( dirLight );
  133. // ---------------------------------------------------------------------
  134. // Grid
  135. // ---------------------------------------------------------------------
  136. gridHelper = new THREE.GridHelper( 2000, 20, 0xc1c1c1, 0x8d8d8d );
  137. gridHelper.position.y = - 50;
  138. gridHelper.name = 'Grid';
  139. scene1.add( gridHelper );
  140. // ---------------------------------------------------------------------
  141. // Axes
  142. // ---------------------------------------------------------------------
  143. const axes = new THREE.AxesHelper( 500 );
  144. axes.name = 'AxesHelper';
  145. scene1.add( axes );
  146. // ---------------------------------------------------------------------
  147. // Simple geometry with basic material
  148. // ---------------------------------------------------------------------
  149. // Icosahedron
  150. const mapGrid = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
  151. mapGrid.wrapS = mapGrid.wrapT = THREE.RepeatWrapping;
  152. mapGrid.colorSpace = THREE.SRGBColorSpace;
  153. material = new THREE.MeshBasicMaterial( {
  154. color: 0xffffff,
  155. map: mapGrid
  156. } );
  157. object = new THREE.Mesh( new THREE.IcosahedronGeometry( 75, 0 ), material );
  158. object.position.set( - 200, 0, 200 );
  159. object.name = 'Icosahedron';
  160. scene1.add( object );
  161. // Octahedron
  162. material = new THREE.MeshBasicMaterial( {
  163. color: 0x0000ff,
  164. wireframe: true
  165. } );
  166. object = new THREE.Mesh( new THREE.OctahedronGeometry( 75, 1 ), material );
  167. object.position.set( 0, 0, 200 );
  168. object.name = 'Octahedron';
  169. scene1.add( object );
  170. // Tetrahedron
  171. material = new THREE.MeshBasicMaterial( {
  172. color: 0xff0000,
  173. transparent: true,
  174. opacity: 0.5
  175. } );
  176. object = new THREE.Mesh( new THREE.TetrahedronGeometry( 75, 0 ), material );
  177. object.position.set( 200, 0, 200 );
  178. object.name = 'Tetrahedron';
  179. scene1.add( object );
  180. // ---------------------------------------------------------------------
  181. // Buffered geometry primitives
  182. // ---------------------------------------------------------------------
  183. // Sphere
  184. material = new THREE.MeshStandardMaterial( {
  185. color: 0xffff00,
  186. metalness: 0.5,
  187. roughness: 1.0,
  188. flatShading: true,
  189. } );
  190. material.map = gradientTexture;
  191. material.bumpMap = mapGrid;
  192. sphere = new THREE.Mesh( new THREE.SphereGeometry( 70, 10, 10 ), material );
  193. sphere.position.set( 0, 0, 0 );
  194. sphere.name = 'Sphere';
  195. scene1.add( sphere );
  196. // Cylinder
  197. material = new THREE.MeshStandardMaterial( {
  198. color: 0xff00ff,
  199. flatShading: true
  200. } );
  201. object = new THREE.Mesh( new THREE.CylinderGeometry( 10, 80, 100 ), material );
  202. object.position.set( 200, 0, 0 );
  203. object.name = 'Cylinder';
  204. scene1.add( object );
  205. // TorusKnot
  206. material = new THREE.MeshStandardMaterial( {
  207. color: 0xff0000,
  208. roughness: 1
  209. } );
  210. object = new THREE.Mesh( new THREE.TorusKnotGeometry( 50, 15, 40, 10 ), material );
  211. object.position.set( - 200, 0, 0 );
  212. object.name = 'Cylinder';
  213. scene1.add( object );
  214. // ---------------------------------------------------------------------
  215. // Hierarchy
  216. // ---------------------------------------------------------------------
  217. const mapWood = new THREE.TextureLoader().load( 'textures/hardwood2_diffuse.jpg' );
  218. material = new THREE.MeshStandardMaterial( { map: mapWood, side: THREE.DoubleSide } );
  219. object = new THREE.Mesh( new THREE.BoxGeometry( 40, 100, 100 ), material );
  220. object.position.set( - 200, 0, 400 );
  221. object.name = 'Cube';
  222. scene1.add( object );
  223. object2 = new THREE.Mesh( new THREE.BoxGeometry( 40, 40, 40, 2, 2, 2 ), material );
  224. object2.position.set( 0, 0, 50 );
  225. object2.rotation.set( 0, 45, 0 );
  226. object2.name = 'SubCube';
  227. object.add( object2 );
  228. // ---------------------------------------------------------------------
  229. // Groups
  230. // ---------------------------------------------------------------------
  231. const group1 = new THREE.Group();
  232. group1.name = 'Group';
  233. scene1.add( group1 );
  234. const group2 = new THREE.Group();
  235. group2.name = 'subGroup';
  236. group2.position.set( 0, 50, 0 );
  237. group1.add( group2 );
  238. object2 = new THREE.Mesh( new THREE.BoxGeometry( 30, 30, 30 ), material );
  239. object2.name = 'Cube in group';
  240. object2.position.set( 0, 0, 400 );
  241. group2.add( object2 );
  242. // ---------------------------------------------------------------------
  243. // THREE.Line Strip
  244. // ---------------------------------------------------------------------
  245. geometry = new THREE.BufferGeometry();
  246. let numPoints = 100;
  247. let positions = new Float32Array( numPoints * 3 );
  248. for ( let i = 0; i < numPoints; i ++ ) {
  249. positions[ i * 3 ] = i;
  250. positions[ i * 3 + 1 ] = Math.sin( i / 2 ) * 20;
  251. positions[ i * 3 + 2 ] = 0;
  252. }
  253. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  254. object = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
  255. object.position.set( - 50, 0, - 200 );
  256. scene1.add( object );
  257. // ---------------------------------------------------------------------
  258. // THREE.Line Loop
  259. // ---------------------------------------------------------------------
  260. geometry = new THREE.BufferGeometry();
  261. numPoints = 5;
  262. const radius = 70;
  263. positions = new Float32Array( numPoints * 3 );
  264. for ( let i = 0; i < numPoints; i ++ ) {
  265. const s = i * Math.PI * 2 / numPoints;
  266. positions[ i * 3 ] = radius * Math.sin( s );
  267. positions[ i * 3 + 1 ] = radius * Math.cos( s );
  268. positions[ i * 3 + 2 ] = 0;
  269. }
  270. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  271. object = new THREE.LineLoop( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
  272. object.position.set( 0, 0, - 200 );
  273. scene1.add( object );
  274. // ---------------------------------------------------------------------
  275. // THREE.Points
  276. // ---------------------------------------------------------------------
  277. numPoints = 100;
  278. const pointsArray = new Float32Array( numPoints * 3 );
  279. for ( let i = 0; i < numPoints; i ++ ) {
  280. pointsArray[ 3 * i ] = - 50 + Math.random() * 100;
  281. pointsArray[ 3 * i + 1 ] = Math.random() * 100;
  282. pointsArray[ 3 * i + 2 ] = - 50 + Math.random() * 100;
  283. }
  284. const pointsGeo = new THREE.BufferGeometry();
  285. pointsGeo.setAttribute( 'position', new THREE.BufferAttribute( pointsArray, 3 ) );
  286. const pointsMaterial = new THREE.PointsMaterial( { color: 0xffff00, size: 5 } );
  287. const pointCloud = new THREE.Points( pointsGeo, pointsMaterial );
  288. pointCloud.name = 'Points';
  289. pointCloud.position.set( - 200, 0, - 200 );
  290. scene1.add( pointCloud );
  291. // ---------------------------------------------------------------------
  292. // Ortho camera
  293. // ---------------------------------------------------------------------
  294. const height = 1000; // frustum height
  295. const aspect = window.innerWidth / window.innerHeight;
  296. const cameraOrtho = new THREE.OrthographicCamera( - height * aspect, height * aspect, height, - height, 0, 2000 );
  297. cameraOrtho.position.set( 600, 400, 0 );
  298. cameraOrtho.lookAt( 0, 0, 0 );
  299. scene1.add( cameraOrtho );
  300. cameraOrtho.name = 'OrthographicCamera';
  301. material = new THREE.MeshLambertMaterial( {
  302. color: 0xffff00,
  303. side: THREE.DoubleSide
  304. } );
  305. object = new THREE.Mesh( new THREE.CircleGeometry( 50, 20, 0, Math.PI * 2 ), material );
  306. object.position.set( 200, 0, - 400 );
  307. scene1.add( object );
  308. object = new THREE.Mesh( new THREE.RingGeometry( 10, 50, 20, 5, 0, Math.PI * 2 ), material );
  309. object.position.set( 0, 0, - 400 );
  310. scene1.add( object );
  311. object = new THREE.Mesh( new THREE.CylinderGeometry( 25, 75, 100, 40, 5 ), material );
  312. object.position.set( - 200, 0, - 400 );
  313. scene1.add( object );
  314. //
  315. const points = [];
  316. for ( let i = 0; i < 50; i ++ ) {
  317. points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * Math.sin( i * 0.1 ) * 15 + 50, ( i - 5 ) * 2 ) );
  318. }
  319. object = new THREE.Mesh( new THREE.LatheGeometry( points, 20 ), material );
  320. object.position.set( 200, 0, 400 );
  321. scene1.add( object );
  322. // ---------------------------------------------------------------------
  323. // Big red box hidden just for testing `onlyVisible` option
  324. // ---------------------------------------------------------------------
  325. material = new THREE.MeshBasicMaterial( {
  326. color: 0xff0000
  327. } );
  328. object = new THREE.Mesh( new THREE.BoxGeometry( 200, 200, 200 ), material );
  329. object.position.set( 0, 0, 0 );
  330. object.name = 'CubeHidden';
  331. object.visible = false;
  332. scene1.add( object );
  333. // ---------------------------------------------------------------------
  334. // Model requiring KHR_mesh_quantization
  335. // ---------------------------------------------------------------------
  336. const loader = new GLTFLoader();
  337. loader.load( 'models/gltf/ShaderBall.glb', function ( gltf ) {
  338. model = gltf.scene;
  339. model.scale.setScalar( 50 );
  340. model.position.set( 200, - 40, - 200 );
  341. scene1.add( model );
  342. } );
  343. // ---------------------------------------------------------------------
  344. // Model requiring KHR_mesh_quantization
  345. // ---------------------------------------------------------------------
  346. material = new THREE.MeshBasicMaterial( {
  347. color: 0xffffff,
  348. } );
  349. object = new THREE.InstancedMesh( new THREE.BoxGeometry( 10, 10, 10, 2, 2, 2 ), material, 50 );
  350. const matrix = new THREE.Matrix4();
  351. const color = new THREE.Color();
  352. for ( let i = 0; i < 50; i ++ ) {
  353. matrix.setPosition( Math.random() * 100 - 50, Math.random() * 100 - 50, Math.random() * 100 - 50 );
  354. object.setMatrixAt( i, matrix );
  355. object.setColorAt( i, color.setHSL( i / 50, 1, 0.5 ) );
  356. }
  357. object.position.set( 400, 0, 200 );
  358. scene1.add( object );
  359. // ---------------------------------------------------------------------
  360. // 2nd THREE.Scene
  361. // ---------------------------------------------------------------------
  362. scene2 = new THREE.Scene();
  363. object = new THREE.Mesh( new THREE.BoxGeometry( 100, 100, 100 ), material );
  364. object.position.set( 0, 0, 0 );
  365. object.name = 'Cube2ndScene';
  366. scene2.name = 'Scene2';
  367. scene2.add( object );
  368. //
  369. renderer = new THREE.WebGLRenderer( { antialias: true } );
  370. renderer.setPixelRatio( window.devicePixelRatio );
  371. renderer.setSize( window.innerWidth, window.innerHeight );
  372. renderer.setAnimationLoop( animate );
  373. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  374. renderer.toneMappingExposure = 1;
  375. container.appendChild( renderer.domElement );
  376. //
  377. window.addEventListener( 'resize', onWindowResize );
  378. // ---------------------------------------------------------------------
  379. // Exporting compressed textures and meshes (KTX2 / Draco / Meshopt)
  380. // ---------------------------------------------------------------------
  381. const ktx2Loader = new KTX2Loader()
  382. .detectSupport( renderer );
  383. const gltfLoader = new GLTFLoader().setPath( 'models/gltf/' );
  384. gltfLoader.setKTX2Loader( ktx2Loader );
  385. gltfLoader.setMeshoptDecoder( MeshoptDecoder );
  386. gltfLoader.load( 'coffeemat.glb', function ( gltf ) {
  387. gltf.scene.position.x = 400;
  388. gltf.scene.position.z = - 200;
  389. scene1.add( gltf.scene );
  390. coffeemat = gltf.scene;
  391. } );
  392. // ---------------------------------------------------------------------
  393. // Box with WebP texture (EXT_texture_webp)
  394. // ---------------------------------------------------------------------
  395. const canvas = document.createElement( 'canvas' );
  396. canvas.width = 64;
  397. canvas.height = 64;
  398. const ctx = canvas.getContext( '2d' );
  399. ctx.fillStyle = '#005BBB';
  400. ctx.fillRect( 0, 0, 64, 64 );
  401. ctx.fillStyle = '#FFD500';
  402. ctx.fillRect( 16, 16, 32, 32 );
  403. const webpTexture = new THREE.CanvasTexture( canvas );
  404. webpTexture.userData.mimeType = 'image/webp';
  405. webpTexture.colorSpace = THREE.SRGBColorSpace;
  406. webpBox = new THREE.Mesh(
  407. new THREE.BoxGeometry( 100, 100, 100 ),
  408. new THREE.MeshBasicMaterial( { map: webpTexture } )
  409. );
  410. webpBox.position.set( 400, 0, 0 );
  411. webpBox.name = 'WebPBox';
  412. scene1.add( webpBox );
  413. //
  414. const gui = new GUI();
  415. let h = gui.addFolder( 'Settings' );
  416. h.add( params, 'trs' ).name( 'Use TRS' );
  417. h.add( params, 'onlyVisible' ).name( 'Only Visible Objects' );
  418. h.add( params, 'binary' ).name( 'Binary (GLB)' );
  419. h.add( params, 'maxTextureSize', 2, 8192 ).name( 'Max Texture Size' ).step( 1 );
  420. h = gui.addFolder( 'Export' );
  421. h.add( params, 'exportScene1' ).name( 'Export Scene 1' );
  422. h.add( params, 'exportScenes' ).name( 'Export Scene 1 and 2' );
  423. h.add( params, 'exportSphere' ).name( 'Export Sphere' );
  424. h.add( params, 'exportModel' ).name( 'Export Model' );
  425. h.add( params, 'exportObjects' ).name( 'Export Sphere With Grid' );
  426. h.add( params, 'exportSceneObject' ).name( 'Export Scene 1 and Object' );
  427. h.add( params, 'exportCompressedObject' ).name( 'Export Coffeemat (from compressed data)' );
  428. h.add( params, 'exportWebPModel' ).name( 'Export WebP Model (EXT_texture_webp)' );
  429. gui.open();
  430. }
  431. function exportScene1() {
  432. exportGLTF( scene1 );
  433. }
  434. function exportScenes() {
  435. exportGLTF( [ scene1, scene2 ] );
  436. }
  437. function exportSphere() {
  438. exportGLTF( sphere );
  439. }
  440. function exportModel() {
  441. exportGLTF( model );
  442. }
  443. function exportObjects() {
  444. exportGLTF( [ sphere, gridHelper ] );
  445. }
  446. function exportSceneObject() {
  447. exportGLTF( [ scene1, gridHelper ] );
  448. }
  449. function exportCompressedObject() {
  450. exportGLTF( [ coffeemat ] );
  451. }
  452. function exportWebPModel() {
  453. exportGLTF( webpBox );
  454. }
  455. function onWindowResize() {
  456. camera.aspect = window.innerWidth / window.innerHeight;
  457. camera.updateProjectionMatrix();
  458. renderer.setSize( window.innerWidth, window.innerHeight );
  459. }
  460. //
  461. function animate() {
  462. const timer = Date.now() * 0.0001;
  463. camera.position.x = Math.cos( timer ) * 800;
  464. camera.position.z = Math.sin( timer ) * 800;
  465. camera.lookAt( scene1.position );
  466. renderer.render( scene1, camera );
  467. }
  468. </script>
  469. </body>
  470. </html>
粤ICP备19079148号