misc_exporter_gltf.html 14 KB

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