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