misc_exporter_gltf.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.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( 0xcccccc );
  120. ambientLight.name = 'AmbientLight';
  121. scene1.add( ambientLight );
  122. // ---------------------------------------------------------------------
  123. // DirectLight
  124. // ---------------------------------------------------------------------
  125. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  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. mapGrid.colorSpace = THREE.SRGBColorSpace;
  151. material = new THREE.MeshBasicMaterial( {
  152. color: 0xffffff,
  153. map: mapGrid
  154. } );
  155. object = new THREE.Mesh( new THREE.IcosahedronGeometry( 75, 0 ), material );
  156. object.position.set( - 200, 0, 200 );
  157. object.name = 'Icosahedron';
  158. scene1.add( object );
  159. // Octahedron
  160. material = new THREE.MeshBasicMaterial( {
  161. color: 0x0000ff,
  162. wireframe: true
  163. } );
  164. object = new THREE.Mesh( new THREE.OctahedronGeometry( 75, 1 ), material );
  165. object.position.set( 0, 0, 200 );
  166. object.name = 'Octahedron';
  167. scene1.add( object );
  168. // Tetrahedron
  169. material = new THREE.MeshBasicMaterial( {
  170. color: 0xff0000,
  171. transparent: true,
  172. opacity: 0.5
  173. } );
  174. object = new THREE.Mesh( new THREE.TetrahedronGeometry( 75, 0 ), material );
  175. object.position.set( 200, 0, 200 );
  176. object.name = 'Tetrahedron';
  177. scene1.add( object );
  178. // ---------------------------------------------------------------------
  179. // Buffered geometry primitives
  180. // ---------------------------------------------------------------------
  181. // Sphere
  182. material = new THREE.MeshStandardMaterial( {
  183. color: 0xffff00,
  184. metalness: 0.5,
  185. roughness: 1.0,
  186. flatShading: true
  187. } );
  188. material.map = gradientTexture;
  189. sphere = new THREE.Mesh( new THREE.SphereGeometry( 70, 10, 10 ), material );
  190. sphere.position.set( 0, 0, 0 );
  191. sphere.name = 'Sphere';
  192. scene1.add( sphere );
  193. // Cylinder
  194. material = new THREE.MeshStandardMaterial( {
  195. color: 0xff00ff,
  196. flatShading: true
  197. } );
  198. object = new THREE.Mesh( new THREE.CylinderGeometry( 10, 80, 100 ), material );
  199. object.position.set( 200, 0, 0 );
  200. object.name = 'Cylinder';
  201. scene1.add( object );
  202. // TorusKnot
  203. material = new THREE.MeshStandardMaterial( {
  204. color: 0xff0000,
  205. roughness: 1
  206. } );
  207. object = new THREE.Mesh( new THREE.TorusKnotGeometry( 50, 15, 40, 10 ), material );
  208. object.position.set( - 200, 0, 0 );
  209. object.name = 'Cylinder';
  210. scene1.add( object );
  211. // ---------------------------------------------------------------------
  212. // Hierarchy
  213. // ---------------------------------------------------------------------
  214. const mapWood = new THREE.TextureLoader().load( 'textures/hardwood2_diffuse.jpg' );
  215. material = new THREE.MeshStandardMaterial( { map: mapWood, side: THREE.DoubleSide } );
  216. object = new THREE.Mesh( new THREE.BoxGeometry( 40, 100, 100 ), material );
  217. object.position.set( - 200, 0, 400 );
  218. object.name = 'Cube';
  219. scene1.add( object );
  220. object2 = new THREE.Mesh( new THREE.BoxGeometry( 40, 40, 40, 2, 2, 2 ), material );
  221. object2.position.set( 0, 0, 50 );
  222. object2.rotation.set( 0, 45, 0 );
  223. object2.name = 'SubCube';
  224. object.add( object2 );
  225. // ---------------------------------------------------------------------
  226. // Groups
  227. // ---------------------------------------------------------------------
  228. const group1 = new THREE.Group();
  229. group1.name = 'Group';
  230. scene1.add( group1 );
  231. const group2 = new THREE.Group();
  232. group2.name = 'subGroup';
  233. group2.position.set( 0, 50, 0 );
  234. group1.add( group2 );
  235. object2 = new THREE.Mesh( new THREE.BoxGeometry( 30, 30, 30 ), material );
  236. object2.name = 'Cube in group';
  237. object2.position.set( 0, 0, 400 );
  238. group2.add( object2 );
  239. // ---------------------------------------------------------------------
  240. // THREE.Line Strip
  241. // ---------------------------------------------------------------------
  242. geometry = new THREE.BufferGeometry();
  243. let numPoints = 100;
  244. let positions = new Float32Array( numPoints * 3 );
  245. for ( let i = 0; i < numPoints; i ++ ) {
  246. positions[ i * 3 ] = i;
  247. positions[ i * 3 + 1 ] = Math.sin( i / 2 ) * 20;
  248. positions[ i * 3 + 2 ] = 0;
  249. }
  250. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  251. object = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
  252. object.position.set( - 50, 0, - 200 );
  253. scene1.add( object );
  254. // ---------------------------------------------------------------------
  255. // THREE.Line Loop
  256. // ---------------------------------------------------------------------
  257. geometry = new THREE.BufferGeometry();
  258. numPoints = 5;
  259. const radius = 70;
  260. positions = new Float32Array( numPoints * 3 );
  261. for ( let i = 0; i < numPoints; i ++ ) {
  262. const s = i * Math.PI * 2 / numPoints;
  263. positions[ i * 3 ] = radius * Math.sin( s );
  264. positions[ i * 3 + 1 ] = radius * Math.cos( s );
  265. positions[ i * 3 + 2 ] = 0;
  266. }
  267. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  268. object = new THREE.LineLoop( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
  269. object.position.set( 0, 0, - 200 );
  270. scene1.add( object );
  271. // ---------------------------------------------------------------------
  272. // THREE.Points
  273. // ---------------------------------------------------------------------
  274. numPoints = 100;
  275. const pointsArray = new Float32Array( numPoints * 3 );
  276. for ( let i = 0; i < numPoints; i ++ ) {
  277. pointsArray[ 3 * i ] = - 50 + Math.random() * 100;
  278. pointsArray[ 3 * i + 1 ] = Math.random() * 100;
  279. pointsArray[ 3 * i + 2 ] = - 50 + Math.random() * 100;
  280. }
  281. const pointsGeo = new THREE.BufferGeometry();
  282. pointsGeo.setAttribute( 'position', new THREE.BufferAttribute( pointsArray, 3 ) );
  283. const pointsMaterial = new THREE.PointsMaterial( { color: 0xffff00, size: 5 } );
  284. const pointCloud = new THREE.Points( pointsGeo, pointsMaterial );
  285. pointCloud.name = 'Points';
  286. pointCloud.position.set( - 200, 0, - 200 );
  287. scene1.add( pointCloud );
  288. // ---------------------------------------------------------------------
  289. // Ortho camera
  290. // ---------------------------------------------------------------------
  291. const cameraOrtho = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, 0.1, 10 );
  292. scene1.add( cameraOrtho );
  293. cameraOrtho.name = 'OrthographicCamera';
  294. material = new THREE.MeshLambertMaterial( {
  295. color: 0xffff00,
  296. side: THREE.DoubleSide
  297. } );
  298. object = new THREE.Mesh( new THREE.CircleGeometry( 50, 20, 0, Math.PI * 2 ), material );
  299. object.position.set( 200, 0, - 400 );
  300. scene1.add( object );
  301. object = new THREE.Mesh( new THREE.RingGeometry( 10, 50, 20, 5, 0, Math.PI * 2 ), material );
  302. object.position.set( 0, 0, - 400 );
  303. scene1.add( object );
  304. object = new THREE.Mesh( new THREE.CylinderGeometry( 25, 75, 100, 40, 5 ), material );
  305. object.position.set( - 200, 0, - 400 );
  306. scene1.add( object );
  307. //
  308. const points = [];
  309. for ( let i = 0; i < 50; i ++ ) {
  310. points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * Math.sin( i * 0.1 ) * 15 + 50, ( i - 5 ) * 2 ) );
  311. }
  312. object = new THREE.Mesh( new THREE.LatheGeometry( points, 20 ), material );
  313. object.position.set( 200, 0, 400 );
  314. scene1.add( object );
  315. // ---------------------------------------------------------------------
  316. // Big red box hidden just for testing `onlyVisible` option
  317. // ---------------------------------------------------------------------
  318. material = new THREE.MeshBasicMaterial( {
  319. color: 0xff0000
  320. } );
  321. object = new THREE.Mesh( new THREE.BoxGeometry( 200, 200, 200 ), material );
  322. object.position.set( 0, 0, 0 );
  323. object.name = 'CubeHidden';
  324. object.visible = false;
  325. scene1.add( object );
  326. // ---------------------------------------------------------------------
  327. // Model requiring KHR_mesh_quantization
  328. // ---------------------------------------------------------------------
  329. const loader = new GLTFLoader();
  330. loader.load( 'models/gltf/ShaderBall.glb', function ( gltf ) {
  331. model = gltf.scene;
  332. model.scale.setScalar( 50 );
  333. model.position.set( 200, - 40, - 200 );
  334. scene1.add( model );
  335. } );
  336. // ---------------------------------------------------------------------
  337. // 2nd THREE.Scene
  338. // ---------------------------------------------------------------------
  339. scene2 = new THREE.Scene();
  340. object = new THREE.Mesh( new THREE.BoxGeometry( 100, 100, 100 ), material );
  341. object.position.set( 0, 0, 0 );
  342. object.name = 'Cube2ndScene';
  343. scene2.name = 'Scene2';
  344. scene2.add( object );
  345. //
  346. renderer = new THREE.WebGLRenderer( { antialias: true } );
  347. renderer.setPixelRatio( window.devicePixelRatio );
  348. renderer.setSize( window.innerWidth, window.innerHeight );
  349. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  350. renderer.toneMappingExposure = 1;
  351. container.appendChild( renderer.domElement );
  352. //
  353. window.addEventListener( 'resize', onWindowResize );
  354. // ---------------------------------------------------------------------
  355. // Exporting compressed textures and meshes (KTX2 / Draco / Meshopt)
  356. // ---------------------------------------------------------------------
  357. const ktx2Loader = new KTX2Loader()
  358. .setTranscoderPath( 'jsm/libs/basis/' )
  359. .detectSupport( renderer );
  360. const gltfLoader = new GLTFLoader().setPath( 'models/gltf/' );
  361. gltfLoader.setKTX2Loader( ktx2Loader );
  362. gltfLoader.setMeshoptDecoder( MeshoptDecoder );
  363. gltfLoader.load( 'coffeemat.glb', function ( gltf ) {
  364. gltf.scene.position.x = 400;
  365. gltf.scene.position.z = - 200;
  366. scene1.add( gltf.scene );
  367. coffeemat = gltf.scene;
  368. } );
  369. //
  370. const gui = new GUI();
  371. let h = gui.addFolder( 'Settings' );
  372. h.add( params, 'trs' ).name( 'Use TRS' );
  373. h.add( params, 'onlyVisible' ).name( 'Only Visible Objects' );
  374. h.add( params, 'binary' ).name( 'Binary (GLB)' );
  375. h.add( params, 'maxTextureSize', 2, 8192 ).name( 'Max Texture Size' ).step( 1 );
  376. h = gui.addFolder( 'Export' );
  377. h.add( params, 'exportScene1' ).name( 'Export Scene 1' );
  378. h.add( params, 'exportScenes' ).name( 'Export Scene 1 and 2' );
  379. h.add( params, 'exportSphere' ).name( 'Export Sphere' );
  380. h.add( params, 'exportModel' ).name( 'Export Model' );
  381. h.add( params, 'exportObjects' ).name( 'Export Sphere With Grid' );
  382. h.add( params, 'exportSceneObject' ).name( 'Export Scene 1 and Object' );
  383. h.add( params, 'exportCompressedObject' ).name( 'Export Coffeemat (from compressed data)' );
  384. gui.open();
  385. }
  386. function exportScene1() {
  387. exportGLTF( scene1 );
  388. }
  389. function exportScenes() {
  390. exportGLTF( [ scene1, scene2 ] );
  391. }
  392. function exportSphere() {
  393. exportGLTF( sphere );
  394. }
  395. function exportModel() {
  396. exportGLTF( model );
  397. }
  398. function exportObjects() {
  399. exportGLTF( [ sphere, gridHelper ] );
  400. }
  401. function exportSceneObject() {
  402. exportGLTF( [ scene1, gridHelper ] );
  403. }
  404. function exportCompressedObject() {
  405. exportGLTF( [ coffeemat ] );
  406. }
  407. function onWindowResize() {
  408. camera.aspect = window.innerWidth / window.innerHeight;
  409. camera.updateProjectionMatrix();
  410. renderer.setSize( window.innerWidth, window.innerHeight );
  411. }
  412. //
  413. function animate() {
  414. requestAnimationFrame( animate );
  415. render();
  416. }
  417. function render() {
  418. const timer = Date.now() * 0.0001;
  419. camera.position.x = Math.cos( timer ) * 800;
  420. camera.position.z = Math.sin( timer ) * 800;
  421. camera.lookAt( scene1.position );
  422. renderer.render( scene1, camera );
  423. }
  424. </script>
  425. </body>
  426. </html>