misc_exporter_gltf.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. const gltfExporter = new GLTFExporter();
  32. const 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. const 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. const 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. let container;
  84. let camera, object, object2, material, geometry, scene1, scene2, renderer;
  85. let gridHelper, sphere, waltHead;
  86. init();
  87. animate();
  88. function init() {
  89. container = document.createElement( 'div' );
  90. document.body.appendChild( container );
  91. // Make linear gradient texture
  92. const data = new Uint8ClampedArray( 100 * 100 * 3 );
  93. for ( let y = 0; y < 100; y ++ ) {
  94. for ( let x = 0; x < 100; x ++ ) {
  95. data[ 3 * ( 100 * y + x ) ] = Math.round( 255 * y / 99 );
  96. data[ 3 * ( 100 * y + x ) + 1 ] = Math.round( 255 - 255 * y / 99 );
  97. }
  98. }
  99. const gradientTexture = new THREE.DataTexture( data, 100, 100, THREE.RGBFormat );
  100. gradientTexture.minFilter = THREE.LinearFilter;
  101. gradientTexture.magFilter = THREE.LinearFilter;
  102. scene1 = new THREE.Scene();
  103. scene1.name = 'Scene1';
  104. // ---------------------------------------------------------------------
  105. // Perspective Camera
  106. // ---------------------------------------------------------------------
  107. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  108. camera.position.set( 600, 400, 0 );
  109. camera.name = "PerspectiveCamera";
  110. scene1.add( camera );
  111. // ---------------------------------------------------------------------
  112. // Ambient light
  113. // ---------------------------------------------------------------------
  114. const ambientLight = new THREE.AmbientLight( 0xffffff, 0.2 );
  115. ambientLight.name = 'AmbientLight';
  116. scene1.add( ambientLight );
  117. // ---------------------------------------------------------------------
  118. // DirectLight
  119. // ---------------------------------------------------------------------
  120. const dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
  121. dirLight.target.position.set( 0, 0, - 1 );
  122. dirLight.add( dirLight.target );
  123. dirLight.lookAt( - 1, - 1, 0 );
  124. dirLight.name = 'DirectionalLight';
  125. scene1.add( dirLight );
  126. // ---------------------------------------------------------------------
  127. // Grid
  128. // ---------------------------------------------------------------------
  129. gridHelper = new THREE.GridHelper( 2000, 20, 0x888888, 0x444444 );
  130. gridHelper.position.y = - 50;
  131. gridHelper.name = "Grid";
  132. scene1.add( gridHelper );
  133. // ---------------------------------------------------------------------
  134. // Axes
  135. // ---------------------------------------------------------------------
  136. const axes = new THREE.AxesHelper( 500 );
  137. axes.name = "AxesHelper";
  138. scene1.add( axes );
  139. // ---------------------------------------------------------------------
  140. // Simple geometry with basic material
  141. // ---------------------------------------------------------------------
  142. // Icosahedron
  143. const mapGrid = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
  144. mapGrid.wrapS = mapGrid.wrapT = THREE.RepeatWrapping;
  145. material = new THREE.MeshBasicMaterial( {
  146. color: 0xffffff,
  147. map: mapGrid
  148. } );
  149. object = new THREE.Mesh( new THREE.IcosahedronBufferGeometry( 75, 0 ), material );
  150. object.position.set( - 200, 0, 200 );
  151. object.name = 'Icosahedron';
  152. scene1.add( object );
  153. // Octahedron
  154. material = new THREE.MeshBasicMaterial( {
  155. color: 0x0000ff,
  156. wireframe: true
  157. } );
  158. object = new THREE.Mesh( new THREE.OctahedronBufferGeometry( 75, 1 ), material );
  159. object.position.set( 0, 0, 200 );
  160. object.name = 'Octahedron';
  161. scene1.add( object );
  162. // Tetrahedron
  163. material = new THREE.MeshBasicMaterial( {
  164. color: 0xff0000,
  165. transparent: true,
  166. opacity: 0.5
  167. } );
  168. object = new THREE.Mesh( new THREE.TetrahedronBufferGeometry( 75, 0 ), material );
  169. object.position.set( 200, 0, 200 );
  170. object.name = 'Tetrahedron';
  171. scene1.add( object );
  172. // ---------------------------------------------------------------------
  173. // Buffered geometry primitives
  174. // ---------------------------------------------------------------------
  175. // Sphere
  176. material = new THREE.MeshStandardMaterial( {
  177. color: 0xffff00,
  178. metalness: 0.5,
  179. roughness: 1.0,
  180. flatShading: true
  181. } );
  182. material.map = gradientTexture;
  183. sphere = new THREE.Mesh( new THREE.SphereBufferGeometry( 70, 10, 10 ), material );
  184. sphere.position.set( 0, 0, 0 );
  185. sphere.name = "Sphere";
  186. scene1.add( sphere );
  187. // Cylinder
  188. material = new THREE.MeshStandardMaterial( {
  189. color: 0xff00ff,
  190. flatShading: true
  191. } );
  192. object = new THREE.Mesh( new THREE.CylinderBufferGeometry( 10, 80, 100 ), material );
  193. object.position.set( 200, 0, 0 );
  194. object.name = "Cylinder";
  195. scene1.add( object );
  196. // TorusKnot
  197. material = new THREE.MeshStandardMaterial( {
  198. color: 0xff0000,
  199. roughness: 1
  200. } );
  201. object = new THREE.Mesh( new THREE.TorusKnotBufferGeometry( 50, 15, 40, 10 ), material );
  202. object.position.set( - 200, 0, 0 );
  203. object.name = "Cylinder";
  204. scene1.add( object );
  205. // ---------------------------------------------------------------------
  206. // Hierarchy
  207. // ---------------------------------------------------------------------
  208. const mapWood = new THREE.TextureLoader().load( 'textures/hardwood2_diffuse.jpg' );
  209. material = new THREE.MeshStandardMaterial( { map: mapWood, side: THREE.DoubleSide } );
  210. object = new THREE.Mesh( new THREE.BoxBufferGeometry( 40, 100, 100 ), material );
  211. object.position.set( - 200, 0, 400 );
  212. object.name = "Cube";
  213. scene1.add( object );
  214. object2 = new THREE.Mesh( new THREE.BoxBufferGeometry( 40, 40, 40, 2, 2, 2 ), material );
  215. object2.position.set( 0, 0, 50 );
  216. object2.rotation.set( 0, 45, 0 );
  217. object2.name = "SubCube";
  218. object.add( object2 );
  219. // ---------------------------------------------------------------------
  220. // Groups
  221. // ---------------------------------------------------------------------
  222. const group1 = new THREE.Group();
  223. group1.name = "Group";
  224. scene1.add( group1 );
  225. const group2 = new THREE.Group();
  226. group2.name = "subGroup";
  227. group2.position.set( 0, 50, 0 );
  228. group1.add( group2 );
  229. object2 = new THREE.Mesh( new THREE.BoxBufferGeometry( 30, 30, 30 ), material );
  230. object2.name = "Cube in group";
  231. object2.position.set( 0, 0, 400 );
  232. group2.add( object2 );
  233. // ---------------------------------------------------------------------
  234. // THREE.Line Strip
  235. // ---------------------------------------------------------------------
  236. geometry = new THREE.BufferGeometry();
  237. let numPoints = 100;
  238. let positions = new Float32Array( numPoints * 3 );
  239. for ( let 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.setAttribute( '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. // THREE.Line Loop
  250. // ---------------------------------------------------------------------
  251. geometry = new THREE.BufferGeometry();
  252. numPoints = 5;
  253. const radius = 70;
  254. positions = new Float32Array( numPoints * 3 );
  255. for ( let i = 0; i < numPoints; i ++ ) {
  256. const 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.setAttribute( '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. // Buffer geometry truncated (DrawRange)
  267. // ---------------------------------------------------------------------
  268. geometry = new THREE.BufferGeometry();
  269. const numElements = 6;
  270. const outOfRange = 3;
  271. positions = new Float32Array( ( numElements + outOfRange ) * 3 );
  272. const colors = new Float32Array( ( numElements + outOfRange ) * 3 );
  273. positions.set( [
  274. 0, 0, 0,
  275. 0, 80, 0,
  276. 80, 0, 0,
  277. 80, 0, 0,
  278. 0, 80, 0,
  279. 80, 80, 0
  280. ] );
  281. colors.set( [
  282. 1, 0, 0,
  283. 1, 0, 0,
  284. 1, 1, 0,
  285. 1, 1, 0,
  286. 0, 0, 1,
  287. 0, 0, 1,
  288. ] );
  289. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  290. geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  291. geometry.setDrawRange( 0, numElements );
  292. object = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, vertexColors: true } ) );
  293. object.name = 'Custom buffered truncated';
  294. object.position.set( 140, - 40, - 200 );
  295. scene1.add( object );
  296. // ---------------------------------------------------------------------
  297. // THREE.Points
  298. // ---------------------------------------------------------------------
  299. numPoints = 100;
  300. const pointsArray = new Float32Array( numPoints * 3 );
  301. for ( let i = 0; i < numPoints; i ++ ) {
  302. pointsArray[ 3 * i ] = - 50 + Math.random() * 100;
  303. pointsArray[ 3 * i + 1 ] = Math.random() * 100;
  304. pointsArray[ 3 * i + 2 ] = - 50 + Math.random() * 100;
  305. }
  306. const pointsGeo = new THREE.BufferGeometry();
  307. pointsGeo.setAttribute( 'position', new THREE.BufferAttribute( pointsArray, 3 ) );
  308. const pointsMaterial = new THREE.PointsMaterial( { color: 0xffff00, size: 5 } );
  309. const pointCloud = new THREE.Points( pointsGeo, pointsMaterial );
  310. pointCloud.name = "Points";
  311. pointCloud.position.set( - 200, 0, - 200 );
  312. scene1.add( pointCloud );
  313. // ---------------------------------------------------------------------
  314. // Ortho camera
  315. // ---------------------------------------------------------------------
  316. const cameraOrtho = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, 0.1, 10 );
  317. scene1.add( cameraOrtho );
  318. cameraOrtho.name = 'OrthographicCamera';
  319. material = new THREE.MeshLambertMaterial( {
  320. color: 0xffff00,
  321. side: THREE.DoubleSide
  322. } );
  323. object = new THREE.Mesh( new THREE.CircleBufferGeometry( 50, 20, 0, Math.PI * 2 ), material );
  324. object.position.set( 200, 0, - 400 );
  325. scene1.add( object );
  326. object = new THREE.Mesh( new THREE.RingBufferGeometry( 10, 50, 20, 5, 0, Math.PI * 2 ), material );
  327. object.position.set( 0, 0, - 400 );
  328. scene1.add( object );
  329. object = new THREE.Mesh( new THREE.CylinderBufferGeometry( 25, 75, 100, 40, 5 ), material );
  330. object.position.set( - 200, 0, - 400 );
  331. scene1.add( object );
  332. //
  333. const points = [];
  334. for ( let i = 0; i < 50; i ++ ) {
  335. points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * Math.sin( i * 0.1 ) * 15 + 50, ( i - 5 ) * 2 ) );
  336. }
  337. object = new THREE.Mesh( new THREE.LatheBufferGeometry( points, 20 ), material );
  338. object.position.set( 200, 0, 400 );
  339. scene1.add( object );
  340. // ---------------------------------------------------------------------
  341. // Big red box hidden just for testing `onlyVisible` option
  342. // ---------------------------------------------------------------------
  343. material = new THREE.MeshBasicMaterial( {
  344. color: 0xff0000
  345. } );
  346. object = new THREE.Mesh( new THREE.BoxBufferGeometry( 200, 200, 200 ), material );
  347. object.position.set( 0, 0, 0 );
  348. object.name = "CubeHidden";
  349. object.visible = false;
  350. scene1.add( object );
  351. // ---------------------------------------------------------------------
  352. //
  353. //
  354. const loader = new OBJLoader();
  355. loader.load( 'models/obj/walt/WaltHead.obj', function ( obj ) {
  356. waltHead = obj;
  357. waltHead.scale.multiplyScalar( 1.5 );
  358. waltHead.position.set( 400, 0, 0 );
  359. scene1.add( waltHead );
  360. } );
  361. // ---------------------------------------------------------------------
  362. // 2nd THREE.Scene
  363. // ---------------------------------------------------------------------
  364. scene2 = new THREE.Scene();
  365. object = new THREE.Mesh( new THREE.BoxBufferGeometry( 100, 100, 100 ), material );
  366. object.position.set( 0, 0, 0 );
  367. object.name = "Cube2ndScene";
  368. scene2.name = 'Scene2';
  369. scene2.add( object );
  370. //
  371. renderer = new THREE.WebGLRenderer( { antialias: true } );
  372. renderer.setPixelRatio( window.devicePixelRatio );
  373. renderer.setSize( window.innerWidth, window.innerHeight );
  374. container.appendChild( renderer.domElement );
  375. //
  376. window.addEventListener( 'resize', onWindowResize, false );
  377. }
  378. function onWindowResize() {
  379. camera.aspect = window.innerWidth / window.innerHeight;
  380. camera.updateProjectionMatrix();
  381. renderer.setSize( window.innerWidth, window.innerHeight );
  382. }
  383. //
  384. function animate() {
  385. requestAnimationFrame( animate );
  386. render();
  387. }
  388. function render() {
  389. const timer = Date.now() * 0.0001;
  390. camera.position.x = Math.cos( timer ) * 800;
  391. camera.position.z = Math.sin( timer ) * 800;
  392. camera.lookAt( scene1.position );
  393. renderer.render( scene1, camera );
  394. }
  395. </script>
  396. </body>
  397. </html>