misc_exporter_gltf.html 17 KB

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