misc_exporter_gltf.html 17 KB

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