misc_exporter_gltf.html 17 KB

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