misc_exporter_gltf.html 17 KB

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