webgl_exporter_gltf2.html 14 KB

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