misc_exporter_gltf.html 16 KB

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