misc_exporter_gltf.html 16 KB

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