misc_exporter_gltf.html 17 KB

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