misc_exporter_gltf.html 16 KB

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