misc_exporter_gltf.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { OBJLoader } from './jsm/loaders/OBJLoader.js';
  26. import { GLTFExporter } from './jsm/exporters/GLTFExporter.js';
  27. import { GUI } from './jsm/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. truncateDrawRange: params.truncateDrawRange,
  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, waltHead;
  72. const params = {
  73. trs: false,
  74. onlyVisible: true,
  75. truncateDrawRange: true,
  76. binary: false,
  77. maxTextureSize: 4096,
  78. exportScene1: exportScene1,
  79. exportScenes: exportScenes,
  80. exportSphere: exportSphere,
  81. exportHead: exportHead,
  82. exportObjects: exportObjects,
  83. exportSceneObject: exportSceneObject
  84. };
  85. init();
  86. animate();
  87. function init() {
  88. container = document.createElement( 'div' );
  89. document.body.appendChild( container );
  90. // Make linear gradient texture
  91. const data = new Uint8ClampedArray( 100 * 100 * 4 );
  92. for ( let y = 0; y < 100; y ++ ) {
  93. for ( let x = 0; x < 100; x ++ ) {
  94. const stride = 4 * ( 100 * y + x );
  95. data[ stride ] = Math.round( 255 * y / 99 );
  96. data[ stride + 1 ] = Math.round( 255 - 255 * y / 99 );
  97. data[ stride + 2 ] = 0;
  98. data[ stride + 3 ] = 255;
  99. }
  100. }
  101. const gradientTexture = new THREE.DataTexture( data, 100, 100, THREE.RGBAFormat );
  102. gradientTexture.minFilter = THREE.LinearFilter;
  103. gradientTexture.magFilter = THREE.LinearFilter;
  104. gradientTexture.needsUpdate = true;
  105. scene1 = new THREE.Scene();
  106. scene1.name = 'Scene1';
  107. // ---------------------------------------------------------------------
  108. // Perspective Camera
  109. // ---------------------------------------------------------------------
  110. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  111. camera.position.set( 600, 400, 0 );
  112. camera.name = 'PerspectiveCamera';
  113. scene1.add( camera );
  114. // ---------------------------------------------------------------------
  115. // Ambient light
  116. // ---------------------------------------------------------------------
  117. const ambientLight = new THREE.AmbientLight( 0xffffff, 0.2 );
  118. ambientLight.name = 'AmbientLight';
  119. scene1.add( ambientLight );
  120. // ---------------------------------------------------------------------
  121. // DirectLight
  122. // ---------------------------------------------------------------------
  123. const dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
  124. dirLight.target.position.set( 0, 0, - 1 );
  125. dirLight.add( dirLight.target );
  126. dirLight.lookAt( - 1, - 1, 0 );
  127. dirLight.name = 'DirectionalLight';
  128. scene1.add( dirLight );
  129. // ---------------------------------------------------------------------
  130. // Grid
  131. // ---------------------------------------------------------------------
  132. gridHelper = new THREE.GridHelper( 2000, 20, 0x888888, 0x444444 );
  133. gridHelper.position.y = - 50;
  134. gridHelper.name = 'Grid';
  135. scene1.add( gridHelper );
  136. // ---------------------------------------------------------------------
  137. // Axes
  138. // ---------------------------------------------------------------------
  139. const axes = new THREE.AxesHelper( 500 );
  140. axes.name = 'AxesHelper';
  141. scene1.add( axes );
  142. // ---------------------------------------------------------------------
  143. // Simple geometry with basic material
  144. // ---------------------------------------------------------------------
  145. // Icosahedron
  146. const mapGrid = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
  147. mapGrid.wrapS = mapGrid.wrapT = THREE.RepeatWrapping;
  148. material = new THREE.MeshBasicMaterial( {
  149. color: 0xffffff,
  150. map: mapGrid
  151. } );
  152. object = new THREE.Mesh( new THREE.IcosahedronGeometry( 75, 0 ), material );
  153. object.position.set( - 200, 0, 200 );
  154. object.name = 'Icosahedron';
  155. scene1.add( object );
  156. // Octahedron
  157. material = new THREE.MeshBasicMaterial( {
  158. color: 0x0000ff,
  159. wireframe: true
  160. } );
  161. object = new THREE.Mesh( new THREE.OctahedronGeometry( 75, 1 ), material );
  162. object.position.set( 0, 0, 200 );
  163. object.name = 'Octahedron';
  164. scene1.add( object );
  165. // Tetrahedron
  166. material = new THREE.MeshBasicMaterial( {
  167. color: 0xff0000,
  168. transparent: true,
  169. opacity: 0.5
  170. } );
  171. object = new THREE.Mesh( new THREE.TetrahedronGeometry( 75, 0 ), material );
  172. object.position.set( 200, 0, 200 );
  173. object.name = 'Tetrahedron';
  174. scene1.add( object );
  175. // ---------------------------------------------------------------------
  176. // Buffered geometry primitives
  177. // ---------------------------------------------------------------------
  178. // Sphere
  179. material = new THREE.MeshStandardMaterial( {
  180. color: 0xffff00,
  181. metalness: 0.5,
  182. roughness: 1.0,
  183. flatShading: true
  184. } );
  185. material.map = gradientTexture;
  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. // Buffer geometry truncated (DrawRange)
  270. // ---------------------------------------------------------------------
  271. geometry = new THREE.BufferGeometry();
  272. const numElements = 6;
  273. const outOfRange = 3;
  274. positions = new Float32Array( ( numElements + outOfRange ) * 3 );
  275. const colors = new Float32Array( ( numElements + outOfRange ) * 3 );
  276. positions.set( [
  277. 0, 0, 0,
  278. 0, 80, 0,
  279. 80, 0, 0,
  280. 80, 0, 0,
  281. 0, 80, 0,
  282. 80, 80, 0
  283. ] );
  284. colors.set( [
  285. 1, 0, 0,
  286. 1, 0, 0,
  287. 1, 1, 0,
  288. 1, 1, 0,
  289. 0, 0, 1,
  290. 0, 0, 1,
  291. ] );
  292. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  293. geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  294. geometry.setDrawRange( 0, numElements );
  295. object = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, vertexColors: true } ) );
  296. object.name = 'Custom buffered truncated';
  297. object.position.set( 140, - 40, - 200 );
  298. scene1.add( object );
  299. // ---------------------------------------------------------------------
  300. // THREE.Points
  301. // ---------------------------------------------------------------------
  302. numPoints = 100;
  303. const pointsArray = new Float32Array( numPoints * 3 );
  304. for ( let i = 0; i < numPoints; i ++ ) {
  305. pointsArray[ 3 * i ] = - 50 + Math.random() * 100;
  306. pointsArray[ 3 * i + 1 ] = Math.random() * 100;
  307. pointsArray[ 3 * i + 2 ] = - 50 + Math.random() * 100;
  308. }
  309. const pointsGeo = new THREE.BufferGeometry();
  310. pointsGeo.setAttribute( 'position', new THREE.BufferAttribute( pointsArray, 3 ) );
  311. const pointsMaterial = new THREE.PointsMaterial( { color: 0xffff00, size: 5 } );
  312. const pointCloud = new THREE.Points( pointsGeo, pointsMaterial );
  313. pointCloud.name = 'Points';
  314. pointCloud.position.set( - 200, 0, - 200 );
  315. scene1.add( pointCloud );
  316. // ---------------------------------------------------------------------
  317. // Ortho camera
  318. // ---------------------------------------------------------------------
  319. const cameraOrtho = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, 0.1, 10 );
  320. scene1.add( cameraOrtho );
  321. cameraOrtho.name = 'OrthographicCamera';
  322. material = new THREE.MeshLambertMaterial( {
  323. color: 0xffff00,
  324. side: THREE.DoubleSide
  325. } );
  326. object = new THREE.Mesh( new THREE.CircleGeometry( 50, 20, 0, Math.PI * 2 ), material );
  327. object.position.set( 200, 0, - 400 );
  328. scene1.add( object );
  329. object = new THREE.Mesh( new THREE.RingGeometry( 10, 50, 20, 5, 0, Math.PI * 2 ), material );
  330. object.position.set( 0, 0, - 400 );
  331. scene1.add( object );
  332. object = new THREE.Mesh( new THREE.CylinderGeometry( 25, 75, 100, 40, 5 ), material );
  333. object.position.set( - 200, 0, - 400 );
  334. scene1.add( object );
  335. //
  336. const points = [];
  337. for ( let i = 0; i < 50; i ++ ) {
  338. points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * Math.sin( i * 0.1 ) * 15 + 50, ( i - 5 ) * 2 ) );
  339. }
  340. object = new THREE.Mesh( new THREE.LatheGeometry( points, 20 ), material );
  341. object.position.set( 200, 0, 400 );
  342. scene1.add( object );
  343. // ---------------------------------------------------------------------
  344. // Big red box hidden just for testing `onlyVisible` option
  345. // ---------------------------------------------------------------------
  346. material = new THREE.MeshBasicMaterial( {
  347. color: 0xff0000
  348. } );
  349. object = new THREE.Mesh( new THREE.BoxGeometry( 200, 200, 200 ), material );
  350. object.position.set( 0, 0, 0 );
  351. object.name = 'CubeHidden';
  352. object.visible = false;
  353. scene1.add( object );
  354. // ---------------------------------------------------------------------
  355. //
  356. //
  357. const loader = new OBJLoader();
  358. loader.load( 'models/obj/walt/WaltHead.obj', function ( obj ) {
  359. waltHead = obj;
  360. waltHead.scale.multiplyScalar( 1.5 );
  361. waltHead.position.set( 400, 0, 0 );
  362. scene1.add( waltHead );
  363. } );
  364. // ---------------------------------------------------------------------
  365. // 2nd THREE.Scene
  366. // ---------------------------------------------------------------------
  367. scene2 = new THREE.Scene();
  368. object = new THREE.Mesh( new THREE.BoxGeometry( 100, 100, 100 ), material );
  369. object.position.set( 0, 0, 0 );
  370. object.name = 'Cube2ndScene';
  371. scene2.name = 'Scene2';
  372. scene2.add( object );
  373. //
  374. renderer = new THREE.WebGLRenderer( { antialias: true } );
  375. renderer.setPixelRatio( window.devicePixelRatio );
  376. renderer.setSize( window.innerWidth, window.innerHeight );
  377. container.appendChild( renderer.domElement );
  378. //
  379. window.addEventListener( 'resize', onWindowResize );
  380. const gui = new GUI();
  381. let h = gui.addFolder( 'Settings' );
  382. h.add( params, 'trs' ).name( 'Use TRS' );
  383. h.add( params, 'onlyVisible' ).name( 'Only Visible Objects' );
  384. h.add( params, 'truncateDrawRange' ).name( 'Truncate Draw Range' );
  385. h.add( params, 'binary' ).name( 'Binary (GLB)' );
  386. h.add( params, 'maxTextureSize', 2, 8192 ).name( 'Max Texture Size' ).step( 1 );
  387. h = gui.addFolder( 'Export' );
  388. h.add( params, 'exportScene1' ).name( 'Export Scene 1' );
  389. h.add( params, 'exportScenes' ).name( 'Export Scene 1 and 2' );
  390. h.add( params, 'exportSphere' ).name( 'Export Sphere' );
  391. h.add( params, 'exportHead' ).name( 'Export Head' );
  392. h.add( params, 'exportObjects' ).name( 'Export Sphere With Grid' );
  393. h.add( params, 'exportSceneObject' ).name( 'Export Scene 1 and Object' );
  394. gui.open();
  395. }
  396. function exportScene1() {
  397. exportGLTF( scene1 );
  398. }
  399. function exportScenes() {
  400. exportGLTF( [ scene1, scene2 ] );
  401. }
  402. function exportSphere() {
  403. exportGLTF( sphere );
  404. }
  405. function exportHead() {
  406. exportGLTF( waltHead );
  407. }
  408. function exportObjects() {
  409. exportGLTF( [ sphere, gridHelper ] );
  410. }
  411. function exportSceneObject() {
  412. exportGLTF( [ scene1, gridHelper ] );
  413. }
  414. function onWindowResize() {
  415. camera.aspect = window.innerWidth / window.innerHeight;
  416. camera.updateProjectionMatrix();
  417. renderer.setSize( window.innerWidth, window.innerHeight );
  418. }
  419. //
  420. function animate() {
  421. requestAnimationFrame( animate );
  422. render();
  423. }
  424. function render() {
  425. const timer = Date.now() * 0.0001;
  426. camera.position.x = Math.cos( timer ) * 800;
  427. camera.position.z = Math.sin( timer ) * 800;
  428. camera.lookAt( scene1.position );
  429. renderer.render( scene1, camera );
  430. }
  431. </script>
  432. </body>
  433. </html>