misc_exporter_gltf.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #info {
  15. color: #ccc;
  16. text-align: center;
  17. position: absolute;
  18. top: 0px; width: 100%;
  19. padding: 5px;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="info">
  25. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - exporter - gltf<br/><br/>
  26. <button id="export_scene">Export Scene1</button>
  27. <button id="export_scenes">Export Scene1 and Scene 2</button>
  28. <button id="export_object">Export Sphere</button>
  29. <button id="export_obj">Export WaltHead</button>
  30. <button id="export_objects">Export Sphere and Grid</button>
  31. <button id="export_scene_object">Export Scene1 and Sphere</button>
  32. <br/><br/>
  33. <label><input id="option_trs" name="trs" type="checkbox"/>TRS</label>
  34. <label><input id="option_visible" name="visible" type="checkbox" checked="checked"/>Only Visible</label>
  35. <label><input id="option_drawrange" name="visible" type="checkbox" checked="checked"/>Truncate drawRange</label>
  36. <label><input id="option_binary" name="visible" type="checkbox">Binary (<code>.glb</code>)</label>
  37. <label><input id="option_forceindices" name="visible" type="checkbox">Force indices</label>
  38. <label><input id="option_forcepot" name="visible" type="checkbox">Force POT textures</label>
  39. </div>
  40. <script src="../build/three.js"></script>
  41. <script src="js/WebGL.js"></script>
  42. <script src="js/loaders/OBJLoader.js"></script>
  43. <script src="js/exporters/GLTFExporter.js"></script>
  44. <script>
  45. function exportGLTF( input ) {
  46. var gltfExporter = new THREE.GLTFExporter();
  47. var options = {
  48. trs: document.getElementById( 'option_trs' ).checked,
  49. onlyVisible: document.getElementById( 'option_visible' ).checked,
  50. truncateDrawRange: document.getElementById( 'option_drawrange' ).checked,
  51. binary: document.getElementById( 'option_binary' ).checked,
  52. forceIndices: document.getElementById( 'option_forceindices' ).checked,
  53. forcePowerOfTwoTextures: document.getElementById( 'option_forcepot' ).checked
  54. };
  55. gltfExporter.parse( input, function ( result ) {
  56. if ( result instanceof ArrayBuffer ) {
  57. saveArrayBuffer( result, 'scene.glb' );
  58. } else {
  59. var output = JSON.stringify( result, null, 2 );
  60. console.log( output );
  61. saveString( output, 'scene.gltf' );
  62. }
  63. }, options );
  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. var 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. if ( WEBGL.isWebGLAvailable() === false ) {
  99. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  100. }
  101. var container;
  102. var camera, object, scene1, scene2, renderer;
  103. var gridHelper, sphere, waltHead;
  104. init();
  105. animate();
  106. function init() {
  107. container = document.createElement( 'div' );
  108. document.body.appendChild( container );
  109. scene1 = new THREE.Scene();
  110. scene1.name = 'Scene1';
  111. // ---------------------------------------------------------------------
  112. // Perspective Camera
  113. // ---------------------------------------------------------------------
  114. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  115. camera.position.set( 600, 400, 0 );
  116. camera.name = "PerspectiveCamera";
  117. scene1.add( camera );
  118. // ---------------------------------------------------------------------
  119. // Ambient light
  120. // ---------------------------------------------------------------------
  121. var light = new THREE.AmbientLight( 0xffffff, 0.2 );
  122. light.name = 'AmbientLight';
  123. scene1.add( light );
  124. // ---------------------------------------------------------------------
  125. // DirectLight
  126. // ---------------------------------------------------------------------
  127. light = new THREE.DirectionalLight( 0xffffff, 1 );
  128. light.target.position.set( 0, 0, -1 );
  129. light.add( light.target );
  130. light.lookAt( -1, -1, 0 );
  131. light.name = 'DirectionalLight';
  132. scene1.add( light );
  133. // ---------------------------------------------------------------------
  134. // Grid
  135. // ---------------------------------------------------------------------
  136. gridHelper = new THREE.GridHelper( 2000, 20 );
  137. gridHelper.position.y = - 50;
  138. gridHelper.name = "Grid";
  139. scene1.add( gridHelper );
  140. // ---------------------------------------------------------------------
  141. // Axes
  142. // ---------------------------------------------------------------------
  143. var axes = new THREE.AxesHelper( 500 );
  144. axes.name = "AxesHelper";
  145. scene1.add( axes );
  146. // ---------------------------------------------------------------------
  147. // Simple geometry with basic material
  148. // ---------------------------------------------------------------------
  149. // Icosahedron
  150. var mapGrid = new THREE.TextureLoader().load( 'textures/UV_Grid_Sm.jpg' );
  151. mapGrid.wrapS = mapGrid.wrapT = THREE.RepeatWrapping;
  152. var material = new THREE.MeshBasicMaterial( {
  153. color: 0xffffff,
  154. map: mapGrid
  155. } );
  156. object = new THREE.Mesh( new THREE.IcosahedronBufferGeometry( 75, 0 ), material );
  157. object.position.set( - 200, 0, 200 );
  158. object.name = 'Icosahedron';
  159. scene1.add( object );
  160. // Octahedron
  161. material = new THREE.MeshBasicMaterial( {
  162. color: 0x0000ff,
  163. wireframe: true
  164. } );
  165. object = new THREE.Mesh( new THREE.OctahedronBufferGeometry( 75, 1 ), material );
  166. object.position.set( 0, 0, 200 );
  167. object.name = 'Octahedron';
  168. scene1.add( object );
  169. // Tetrahedron
  170. material = new THREE.MeshBasicMaterial( {
  171. color: 0xff0000,
  172. transparent: true,
  173. opacity: 0.5
  174. } );
  175. object = new THREE.Mesh( new THREE.TetrahedronBufferGeometry( 75, 0 ), material );
  176. object.position.set( 200, 0, 200 );
  177. object.name = 'Tetrahedron';
  178. scene1.add( object );
  179. // ---------------------------------------------------------------------
  180. // Buffered geometry primitives
  181. // ---------------------------------------------------------------------
  182. // Sphere
  183. material = new THREE.MeshStandardMaterial( {
  184. color: 0xffff00,
  185. metalness: 0.5,
  186. roughness: 1.0,
  187. flatShading: true
  188. } );
  189. sphere = new THREE.Mesh( new THREE.SphereBufferGeometry( 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.CylinderBufferGeometry( 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.TorusKnotBufferGeometry( 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. var 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.BoxBufferGeometry( 40, 100, 100 ), material );
  217. object.position.set( - 200, 0, 400 );
  218. object.name = "Cube";
  219. scene1.add( object );
  220. var object2 = new THREE.Mesh( new THREE.BoxBufferGeometry( 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. var group1 = new THREE.Group();
  229. group1.name = "Group";
  230. scene1.add( group1 );
  231. var 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.BoxBufferGeometry( 30, 30, 30 ), material );
  236. object2.name = "Cube in group";
  237. object2.position.set( 0, 0, 400 );
  238. group2.add( object2 );
  239. // ---------------------------------------------------------------------
  240. // Triangle Strip
  241. // ---------------------------------------------------------------------
  242. var geometry = new THREE.BufferGeometry();
  243. var positions = new Float32Array( [
  244. 0, 0, 0,
  245. 0, 80, 0,
  246. 80, 0, 0,
  247. 80, 80, 0,
  248. 80, 0, 80,
  249. 80, 80, 80,
  250. ] );
  251. var colors = new Float32Array( [
  252. 1, 0, 0,
  253. 1, 0, 0,
  254. 1, 1, 0,
  255. 1, 1, 0,
  256. 0, 0, 1,
  257. 0, 0, 1,
  258. ] );
  259. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  260. geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  261. object = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, vertexColors: THREE.VertexColors } ) );
  262. object.position.set( 140, - 40, - 250 );
  263. object.setDrawMode( THREE.TriangleStripDrawMode );
  264. object.name = 'Custom buffered';
  265. object.userData = { data: 'customdata', list: [ 1, 2, 3, 4 ] };
  266. scene1.add( object );
  267. // ---------------------------------------------------------------------
  268. // Line Strip
  269. // ---------------------------------------------------------------------
  270. var geometry = new THREE.BufferGeometry();
  271. var numPoints = 100;
  272. var positions = new Float32Array( numPoints * 3 );
  273. for ( var i = 0; i < numPoints; i ++ ) {
  274. positions[ i * 3 ] = i;
  275. positions[ i * 3 + 1 ] = Math.sin( i / 2 ) * 20;
  276. positions[ i * 3 + 2 ] = 0;
  277. }
  278. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  279. object = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
  280. object.position.set( - 50, 0, - 200 );
  281. scene1.add( object );
  282. // ---------------------------------------------------------------------
  283. // Line Loop
  284. // ---------------------------------------------------------------------
  285. var geometry = new THREE.BufferGeometry();
  286. var numPoints = 5;
  287. var radius = 70;
  288. var positions = new Float32Array( numPoints * 3 );
  289. for ( var i = 0; i < numPoints; i ++ ) {
  290. var s = i * Math.PI * 2 / numPoints;
  291. positions[ i * 3 ] = radius * Math.sin( s );
  292. positions[ i * 3 + 1 ] = radius * Math.cos( s );
  293. positions[ i * 3 + 2 ] = 0;
  294. }
  295. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  296. object = new THREE.LineLoop( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
  297. object.position.set( 0, 0, - 200 );
  298. scene1.add( object );
  299. // ---------------------------------------------------------------------
  300. // Buffer geometry truncated (DrawRange)
  301. // ---------------------------------------------------------------------
  302. var geometry = new THREE.BufferGeometry();
  303. var numElements = 6;
  304. var outOfRange = 3;
  305. var positions = new Float32Array( ( numElements + outOfRange ) * 3 );
  306. var colors = new Float32Array( ( numElements + outOfRange ) * 3 );
  307. positions.set( [
  308. 0, 0, 0,
  309. 0, 80, 0,
  310. 80, 0, 0,
  311. 80, 0, 0,
  312. 0, 80, 0,
  313. 80, 80, 0
  314. ] );
  315. colors.set( [
  316. 1, 0, 0,
  317. 1, 0, 0,
  318. 1, 1, 0,
  319. 1, 1, 0,
  320. 0, 0, 1,
  321. 0, 0, 1,
  322. ] );
  323. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  324. geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  325. geometry.setDrawRange( 0, numElements );
  326. object = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, vertexColors: THREE.VertexColors } ) );
  327. object.name = 'Custom buffered truncated';
  328. object.position.set( 340, - 40, - 200 );
  329. scene1.add( object );
  330. // ---------------------------------------------------------------------
  331. // Points
  332. // ---------------------------------------------------------------------
  333. var numPoints = 100;
  334. var pointsArray = new Float32Array( numPoints * 3 );
  335. for ( var i = 0; i < numPoints; i ++ ) {
  336. pointsArray[ 3 * i ] = - 50 + Math.random() * 100;
  337. pointsArray[ 3 * i + 1 ] = Math.random() * 100;
  338. pointsArray[ 3 * i + 2 ] = - 50 + Math.random() * 100;
  339. }
  340. var pointsGeo = new THREE.BufferGeometry();
  341. pointsGeo.addAttribute( 'position', new THREE.BufferAttribute( pointsArray, 3 ) );
  342. var pointsMaterial = new THREE.PointsMaterial( { color: 0xffff00, size: 5 } );
  343. var points = new THREE.Points( pointsGeo, pointsMaterial );
  344. points.name = "Points";
  345. points.position.set( - 200, 0, - 200 );
  346. scene1.add( points );
  347. // ---------------------------------------------------------------------
  348. // Ortho camera
  349. // ---------------------------------------------------------------------
  350. var cameraOrtho = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, 0.1, 10 );
  351. scene1.add( cameraOrtho );
  352. cameraOrtho.name = 'OrthographicCamera';
  353. material = new THREE.MeshLambertMaterial( {
  354. color: 0xffff00,
  355. side: THREE.DoubleSide
  356. } );
  357. object = new THREE.Mesh( new THREE.CircleBufferGeometry( 50, 20, 0, Math.PI * 2 ), material );
  358. object.position.set( 200, 0, - 400 );
  359. scene1.add( object );
  360. object = new THREE.Mesh( new THREE.RingBufferGeometry( 10, 50, 20, 5, 0, Math.PI * 2 ), material );
  361. object.position.set( 0, 0, - 400 );
  362. scene1.add( object );
  363. object = new THREE.Mesh( new THREE.CylinderBufferGeometry( 25, 75, 100, 40, 5 ), material );
  364. object.position.set( - 200, 0, - 400 );
  365. scene1.add( object );
  366. //
  367. var points = [];
  368. for ( var i = 0; i < 50; i ++ ) {
  369. points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * Math.sin( i * 0.1 ) * 15 + 50, ( i - 5 ) * 2 ) );
  370. }
  371. object = new THREE.Mesh( new THREE.LatheBufferGeometry( points, 20 ), material );
  372. object.position.set( 200, 0, 400 );
  373. scene1.add( object );
  374. // ---------------------------------------------------------------------
  375. // Big red box hidden just for testing `onlyVisible` option
  376. // ---------------------------------------------------------------------
  377. material = new THREE.MeshBasicMaterial( {
  378. color: 0xff0000
  379. } );
  380. object = new THREE.Mesh( new THREE.BoxBufferGeometry( 200, 200, 200 ), material );
  381. object.position.set( 0, 0, 0 );
  382. object.name = "CubeHidden";
  383. object.visible = false;
  384. scene1.add( object );
  385. // ---------------------------------------------------------------------
  386. //
  387. //
  388. var loader = new THREE.OBJLoader();
  389. loader.load( 'models/obj/walt/WaltHead.obj', function ( obj ) {
  390. waltHead = obj;
  391. waltHead.scale.multiplyScalar( 1.5 );
  392. waltHead.position.set( 400, 0, 0 );
  393. scene1.add( waltHead );
  394. } );
  395. // ---------------------------------------------------------------------
  396. // 2nd Scene
  397. // ---------------------------------------------------------------------
  398. scene2 = new THREE.Scene();
  399. object = new THREE.Mesh( new THREE.BoxBufferGeometry( 100, 100, 100 ), material );
  400. object.position.set( 0, 0, 0 );
  401. object.name = "Cube2ndScene";
  402. scene2.name = 'Scene2';
  403. scene2.add( object );
  404. //
  405. renderer = new THREE.WebGLRenderer( { antialias: true } );
  406. renderer.setPixelRatio( window.devicePixelRatio );
  407. renderer.setSize( window.innerWidth, window.innerHeight );
  408. container.appendChild( renderer.domElement );
  409. //
  410. window.addEventListener( 'resize', onWindowResize, false );
  411. }
  412. function onWindowResize() {
  413. camera.aspect = window.innerWidth / window.innerHeight;
  414. camera.updateProjectionMatrix();
  415. renderer.setSize( window.innerWidth, window.innerHeight );
  416. }
  417. //
  418. function animate() {
  419. requestAnimationFrame( animate );
  420. render();
  421. }
  422. function render() {
  423. var timer = Date.now() * 0.0001;
  424. camera.position.x = Math.cos( timer ) * 800;
  425. camera.position.z = Math.sin( timer ) * 800;
  426. camera.lookAt( scene1.position );
  427. renderer.render( scene1, camera );
  428. }
  429. </script>
  430. </body>
  431. </html>