misc_exporter_gltf.html 18 KB

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