misc_exporter_gltf.html 16 KB

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