misc_exporter_gltf.html 17 KB

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