misc_exporter_gltf.html 17 KB

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