misc_exporter_gltf.html 16 KB

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