misc_exporter_gltf.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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.position.set( 1, 1, 0 );
  129. light.name = 'DirectionalLight';
  130. scene1.add( light );
  131. // ---------------------------------------------------------------------
  132. // Grid
  133. // ---------------------------------------------------------------------
  134. gridHelper = new THREE.GridHelper( 2000, 20 );
  135. gridHelper.position.y = - 50;
  136. gridHelper.name = "Grid";
  137. scene1.add( gridHelper );
  138. // ---------------------------------------------------------------------
  139. // Axes
  140. // ---------------------------------------------------------------------
  141. var axes = new THREE.AxesHelper( 500 );
  142. axes.name = "AxesHelper";
  143. scene1.add( axes );
  144. // ---------------------------------------------------------------------
  145. // Simple geometry with basic material
  146. // ---------------------------------------------------------------------
  147. // Icosahedron
  148. var mapGrid = new THREE.TextureLoader().load( 'textures/UV_Grid_Sm.jpg' );
  149. mapGrid.wrapS = mapGrid.wrapT = THREE.RepeatWrapping;
  150. var material = new THREE.MeshBasicMaterial( {
  151. color: 0xffffff,
  152. map: mapGrid
  153. } );
  154. object = new THREE.Mesh( new THREE.IcosahedronBufferGeometry( 75, 0 ), material );
  155. object.position.set( - 200, 0, 200 );
  156. object.name = 'Icosahedron';
  157. scene1.add( object );
  158. // Octahedron
  159. material = new THREE.MeshBasicMaterial( {
  160. color: 0x0000ff,
  161. wireframe: true
  162. } );
  163. object = new THREE.Mesh( new THREE.OctahedronBufferGeometry( 75, 1 ), material );
  164. object.position.set( 0, 0, 200 );
  165. object.name = 'Octahedron';
  166. scene1.add( object );
  167. // Tetrahedron
  168. material = new THREE.MeshBasicMaterial( {
  169. color: 0xff0000,
  170. transparent: true,
  171. opacity: 0.5
  172. } );
  173. object = new THREE.Mesh( new THREE.TetrahedronBufferGeometry( 75, 0 ), material );
  174. object.position.set( 200, 0, 200 );
  175. object.name = 'Tetrahedron';
  176. scene1.add( object );
  177. // ---------------------------------------------------------------------
  178. // Buffered geometry primitives
  179. // ---------------------------------------------------------------------
  180. // Sphere
  181. material = new THREE.MeshStandardMaterial( {
  182. color: 0xffff00,
  183. metalness: 0.5,
  184. roughness: 1.0,
  185. flatShading: true
  186. } );
  187. sphere = new THREE.Mesh( new THREE.SphereBufferGeometry( 70, 10, 10 ), material );
  188. sphere.position.set( 0, 0, 0 );
  189. sphere.name = "Sphere";
  190. scene1.add( sphere );
  191. // Cylinder
  192. material = new THREE.MeshStandardMaterial( {
  193. color: 0xff00ff,
  194. flatShading: true
  195. } );
  196. object = new THREE.Mesh( new THREE.CylinderBufferGeometry( 10, 80, 100 ), material );
  197. object.position.set( 200, 0, 0 );
  198. object.name = "Cylinder";
  199. scene1.add( object );
  200. // TorusKnot
  201. material = new THREE.MeshStandardMaterial( {
  202. color: 0xff0000,
  203. roughness: 1
  204. } );
  205. object = new THREE.Mesh( new THREE.TorusKnotBufferGeometry( 50, 15, 40, 10 ), material );
  206. object.position.set( - 200, 0, 0 );
  207. object.name = "Cylinder";
  208. scene1.add( object );
  209. // ---------------------------------------------------------------------
  210. // Hierarchy
  211. // ---------------------------------------------------------------------
  212. var mapWood = new THREE.TextureLoader().load( 'textures/hardwood2_diffuse.jpg' );
  213. material = new THREE.MeshStandardMaterial( { map: mapWood, side: THREE.DoubleSide } );
  214. object = new THREE.Mesh( new THREE.BoxBufferGeometry( 40, 100, 100 ), material );
  215. object.position.set( - 200, 0, 400 );
  216. object.name = "Cube";
  217. scene1.add( object );
  218. var object2 = new THREE.Mesh( new THREE.BoxBufferGeometry( 40, 40, 40, 2, 2, 2 ), material );
  219. object2.position.set( 0, 0, 50 );
  220. object2.rotation.set( 0, 45, 0 );
  221. object2.name = "SubCube";
  222. object.add( object2 );
  223. // ---------------------------------------------------------------------
  224. // Groups
  225. // ---------------------------------------------------------------------
  226. var group1 = new THREE.Group();
  227. group1.name = "Group";
  228. scene1.add( group1 );
  229. var group2 = new THREE.Group();
  230. group2.name = "subGroup";
  231. group2.position.set( 0, 50, 0 );
  232. group1.add( group2 );
  233. object2 = new THREE.Mesh( new THREE.BoxBufferGeometry( 30, 30, 30 ), material );
  234. object2.name = "Cube in group";
  235. object2.position.set( 0, 0, 400 );
  236. group2.add( object2 );
  237. // ---------------------------------------------------------------------
  238. // Triangle Strip
  239. // ---------------------------------------------------------------------
  240. var geometry = new THREE.BufferGeometry();
  241. var positions = new Float32Array( [
  242. 0, 0, 0,
  243. 0, 80, 0,
  244. 80, 0, 0,
  245. 80, 80, 0,
  246. 80, 0, 80,
  247. 80, 80, 80,
  248. ] );
  249. var colors = new Float32Array( [
  250. 1, 0, 0,
  251. 1, 0, 0,
  252. 1, 1, 0,
  253. 1, 1, 0,
  254. 0, 0, 1,
  255. 0, 0, 1,
  256. ] );
  257. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  258. geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  259. object = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, vertexColors: THREE.VertexColors } ) );
  260. object.position.set( 140, - 40, - 250 );
  261. object.setDrawMode( THREE.TriangleStripDrawMode );
  262. object.name = 'Custom buffered';
  263. object.userData = { data: 'customdata', list: [ 1, 2, 3, 4 ] };
  264. scene1.add( object );
  265. // ---------------------------------------------------------------------
  266. // Line Strip
  267. // ---------------------------------------------------------------------
  268. var geometry = new THREE.BufferGeometry();
  269. var numPoints = 100;
  270. var positions = new Float32Array( numPoints * 3 );
  271. for ( var i = 0; i < numPoints; i ++ ) {
  272. positions[ i * 3 ] = i;
  273. positions[ i * 3 + 1 ] = Math.sin( i / 2 ) * 20;
  274. positions[ i * 3 + 2 ] = 0;
  275. }
  276. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  277. object = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
  278. object.position.set( - 50, 0, - 200 );
  279. scene1.add( object );
  280. // ---------------------------------------------------------------------
  281. // Line Loop
  282. // ---------------------------------------------------------------------
  283. var geometry = new THREE.BufferGeometry();
  284. var numPoints = 5;
  285. var radius = 70;
  286. var positions = new Float32Array( numPoints * 3 );
  287. for ( var i = 0; i < numPoints; i ++ ) {
  288. var s = i * Math.PI * 2 / numPoints;
  289. positions[ i * 3 ] = radius * Math.sin( s );
  290. positions[ i * 3 + 1 ] = radius * Math.cos( s );
  291. positions[ i * 3 + 2 ] = 0;
  292. }
  293. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  294. object = new THREE.LineLoop( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
  295. object.position.set( 0, 0, - 200 );
  296. scene1.add( object );
  297. // ---------------------------------------------------------------------
  298. // Buffer geometry truncated (DrawRange)
  299. // ---------------------------------------------------------------------
  300. var geometry = new THREE.BufferGeometry();
  301. var numElements = 6;
  302. var outOfRange = 3;
  303. var positions = new Float32Array( ( numElements + outOfRange ) * 3 );
  304. var colors = new Float32Array( ( numElements + outOfRange ) * 3 );
  305. positions.set( [
  306. 0, 0, 0,
  307. 0, 80, 0,
  308. 80, 0, 0,
  309. 80, 0, 0,
  310. 0, 80, 0,
  311. 80, 80, 0
  312. ] );
  313. colors.set( [
  314. 1, 0, 0,
  315. 1, 0, 0,
  316. 1, 1, 0,
  317. 1, 1, 0,
  318. 0, 0, 1,
  319. 0, 0, 1,
  320. ] );
  321. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  322. geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  323. geometry.setDrawRange( 0, numElements );
  324. object = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, vertexColors: THREE.VertexColors } ) );
  325. object.name = 'Custom buffered truncated';
  326. object.position.set( 340, - 40, - 200 );
  327. scene1.add( object );
  328. // ---------------------------------------------------------------------
  329. // Points
  330. // ---------------------------------------------------------------------
  331. var numPoints = 100;
  332. var pointsArray = new Float32Array( numPoints * 3 );
  333. for ( var i = 0; i < numPoints; i ++ ) {
  334. pointsArray[ 3 * i ] = - 50 + Math.random() * 100;
  335. pointsArray[ 3 * i + 1 ] = Math.random() * 100;
  336. pointsArray[ 3 * i + 2 ] = - 50 + Math.random() * 100;
  337. }
  338. var pointsGeo = new THREE.BufferGeometry();
  339. pointsGeo.addAttribute( 'position', new THREE.BufferAttribute( pointsArray, 3 ) );
  340. var pointsMaterial = new THREE.PointsMaterial( { color: 0xffff00, size: 5 } );
  341. var points = new THREE.Points( pointsGeo, pointsMaterial );
  342. points.name = "Points";
  343. points.position.set( - 200, 0, - 200 );
  344. scene1.add( points );
  345. // ---------------------------------------------------------------------
  346. // Ortho camera
  347. // ---------------------------------------------------------------------
  348. var cameraOrtho = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, 0.1, 10 );
  349. scene1.add( cameraOrtho );
  350. cameraOrtho.name = 'OrthographicCamera';
  351. material = new THREE.MeshLambertMaterial( {
  352. color: 0xffff00,
  353. side: THREE.DoubleSide
  354. } );
  355. object = new THREE.Mesh( new THREE.CircleBufferGeometry( 50, 20, 0, Math.PI * 2 ), material );
  356. object.position.set( 200, 0, - 400 );
  357. scene1.add( object );
  358. object = new THREE.Mesh( new THREE.RingBufferGeometry( 10, 50, 20, 5, 0, Math.PI * 2 ), material );
  359. object.position.set( 0, 0, - 400 );
  360. scene1.add( object );
  361. object = new THREE.Mesh( new THREE.CylinderBufferGeometry( 25, 75, 100, 40, 5 ), material );
  362. object.position.set( - 200, 0, - 400 );
  363. scene1.add( object );
  364. //
  365. var points = [];
  366. for ( var i = 0; i < 50; i ++ ) {
  367. points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * Math.sin( i * 0.1 ) * 15 + 50, ( i - 5 ) * 2 ) );
  368. }
  369. object = new THREE.Mesh( new THREE.LatheBufferGeometry( points, 20 ), material );
  370. object.position.set( 200, 0, 400 );
  371. scene1.add( object );
  372. // ---------------------------------------------------------------------
  373. // Big red box hidden just for testing `onlyVisible` option
  374. // ---------------------------------------------------------------------
  375. material = new THREE.MeshBasicMaterial( {
  376. color: 0xff0000
  377. } );
  378. object = new THREE.Mesh( new THREE.BoxBufferGeometry( 200, 200, 200 ), material );
  379. object.position.set( 0, 0, 0 );
  380. object.name = "CubeHidden";
  381. object.visible = false;
  382. scene1.add( object );
  383. // ---------------------------------------------------------------------
  384. //
  385. //
  386. var loader = new THREE.OBJLoader();
  387. loader.load( 'models/obj/walt/WaltHead.obj', function ( obj ) {
  388. waltHead = obj;
  389. waltHead.scale.multiplyScalar( 1.5 );
  390. waltHead.position.set( 400, 0, 0 );
  391. scene1.add( waltHead );
  392. } );
  393. // ---------------------------------------------------------------------
  394. // 2nd Scene
  395. // ---------------------------------------------------------------------
  396. scene2 = new THREE.Scene();
  397. object = new THREE.Mesh( new THREE.BoxBufferGeometry( 100, 100, 100 ), material );
  398. object.position.set( 0, 0, 0 );
  399. object.name = "Cube2ndScene";
  400. scene2.name = 'Scene2';
  401. scene2.add( object );
  402. //
  403. renderer = new THREE.WebGLRenderer( { antialias: true } );
  404. renderer.setPixelRatio( window.devicePixelRatio );
  405. renderer.setSize( window.innerWidth, window.innerHeight );
  406. container.appendChild( renderer.domElement );
  407. //
  408. window.addEventListener( 'resize', onWindowResize, false );
  409. }
  410. function onWindowResize() {
  411. camera.aspect = window.innerWidth / window.innerHeight;
  412. camera.updateProjectionMatrix();
  413. renderer.setSize( window.innerWidth, window.innerHeight );
  414. }
  415. //
  416. function animate() {
  417. requestAnimationFrame( animate );
  418. render();
  419. }
  420. function render() {
  421. var timer = Date.now() * 0.0001;
  422. camera.position.x = Math.cos( timer ) * 800;
  423. camera.position.z = Math.sin( timer ) * 800;
  424. camera.lookAt( scene1.position );
  425. renderer.render( scene1, camera );
  426. }
  427. </script>
  428. </body>
  429. </html>