misc_exporter_gltf.html 17 KB

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