misc_exporter_gltf.html 16 KB

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