misc_exporter_gltf.html 17 KB

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