misc_exporter_gltf.html 17 KB

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