misc_exporter_gltf.html 16 KB

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