misc_exporter_gltf.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { GLTFExporter } from 'three/addons/exporters/GLTFExporter.js';
  24. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  25. import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
  26. import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. import * as TextureUtils from 'three/addons/utils/WebGLTextureUtils.js';
  29. function exportGLTF( input ) {
  30. const gltfExporter = new GLTFExporter().setTextureUtils( TextureUtils );
  31. const options = {
  32. trs: params.trs,
  33. onlyVisible: params.onlyVisible,
  34. binary: params.binary,
  35. maxTextureSize: params.maxTextureSize
  36. };
  37. gltfExporter.parse(
  38. input,
  39. function ( result ) {
  40. if ( result instanceof ArrayBuffer ) {
  41. saveArrayBuffer( result, 'scene.glb' );
  42. } else {
  43. const output = JSON.stringify( result, null, 2 );
  44. console.log( output );
  45. saveString( output, 'scene.gltf' );
  46. }
  47. },
  48. function ( error ) {
  49. console.log( 'An error happened during parsing', error );
  50. },
  51. options
  52. );
  53. }
  54. const link = document.createElement( 'a' );
  55. link.style.display = 'none';
  56. document.body.appendChild( link ); // Firefox workaround, see #6594
  57. function save( blob, filename ) {
  58. link.href = URL.createObjectURL( blob );
  59. link.download = filename;
  60. link.click();
  61. // URL.revokeObjectURL( url ); breaks Firefox...
  62. }
  63. function saveString( text, filename ) {
  64. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  65. }
  66. function saveArrayBuffer( buffer, filename ) {
  67. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  68. }
  69. let container;
  70. let camera, object, object2, material, geometry, scene1, scene2, renderer;
  71. let gridHelper, sphere, model, coffeemat, webpBox;
  72. const params = {
  73. trs: false,
  74. onlyVisible: true,
  75. binary: false,
  76. maxTextureSize: 4096,
  77. exportScene1: exportScene1,
  78. exportScenes: exportScenes,
  79. exportSphere: exportSphere,
  80. exportModel: exportModel,
  81. exportObjects: exportObjects,
  82. exportSceneObject: exportSceneObject,
  83. exportCompressedObject: exportCompressedObject,
  84. exportWebPModel: exportWebPModel,
  85. };
  86. init();
  87. function init() {
  88. container = document.createElement( 'div' );
  89. document.body.appendChild( container );
  90. // Make linear gradient texture
  91. const data = new Uint8ClampedArray( 100 * 100 * 4 );
  92. for ( let y = 0; y < 100; y ++ ) {
  93. for ( let x = 0; x < 100; x ++ ) {
  94. const stride = 4 * ( 100 * y + x );
  95. data[ stride ] = Math.round( 255 * y / 99 );
  96. data[ stride + 1 ] = Math.round( 255 - 255 * y / 99 );
  97. data[ stride + 2 ] = 0;
  98. data[ stride + 3 ] = 255;
  99. }
  100. }
  101. const gradientTexture = new THREE.DataTexture( data, 100, 100, THREE.RGBAFormat );
  102. gradientTexture.minFilter = THREE.LinearFilter;
  103. gradientTexture.magFilter = THREE.LinearFilter;
  104. gradientTexture.needsUpdate = true;
  105. scene1 = new THREE.Scene();
  106. scene1.name = 'Scene1';
  107. // ---------------------------------------------------------------------
  108. // Perspective Camera
  109. // ---------------------------------------------------------------------
  110. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  111. camera.position.set( 600, 400, 0 );
  112. camera.name = 'PerspectiveCamera';
  113. scene1.add( camera );
  114. // ---------------------------------------------------------------------
  115. // Ambient light
  116. // ---------------------------------------------------------------------
  117. const ambientLight = new THREE.AmbientLight( 0xcccccc );
  118. ambientLight.name = 'AmbientLight';
  119. scene1.add( ambientLight );
  120. // ---------------------------------------------------------------------
  121. // DirectLight
  122. // ---------------------------------------------------------------------
  123. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  124. dirLight.target.position.set( 0, 0, - 1 );
  125. dirLight.add( dirLight.target );
  126. dirLight.lookAt( - 1, - 1, 0 );
  127. dirLight.name = 'DirectionalLight';
  128. scene1.add( dirLight );
  129. // ---------------------------------------------------------------------
  130. // Grid
  131. // ---------------------------------------------------------------------
  132. gridHelper = new THREE.GridHelper( 2000, 20, 0xc1c1c1, 0x8d8d8d );
  133. gridHelper.position.y = - 50;
  134. gridHelper.name = 'Grid';
  135. scene1.add( gridHelper );
  136. // ---------------------------------------------------------------------
  137. // Axes
  138. // ---------------------------------------------------------------------
  139. const axes = new THREE.AxesHelper( 500 );
  140. axes.name = 'AxesHelper';
  141. scene1.add( axes );
  142. // ---------------------------------------------------------------------
  143. // Simple geometry with basic material
  144. // ---------------------------------------------------------------------
  145. // Icosahedron
  146. const mapGrid = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
  147. mapGrid.wrapS = mapGrid.wrapT = THREE.RepeatWrapping;
  148. mapGrid.colorSpace = THREE.SRGBColorSpace;
  149. material = new THREE.MeshBasicMaterial( {
  150. color: 0xffffff,
  151. map: mapGrid
  152. } );
  153. object = new THREE.Mesh( new THREE.IcosahedronGeometry( 75, 0 ), material );
  154. object.position.set( - 200, 0, 200 );
  155. object.name = 'Icosahedron';
  156. scene1.add( object );
  157. // Octahedron
  158. material = new THREE.MeshBasicMaterial( {
  159. color: 0x0000ff,
  160. wireframe: true
  161. } );
  162. object = new THREE.Mesh( new THREE.OctahedronGeometry( 75, 1 ), material );
  163. object.position.set( 0, 0, 200 );
  164. object.name = 'Octahedron';
  165. scene1.add( object );
  166. // Tetrahedron
  167. material = new THREE.MeshBasicMaterial( {
  168. color: 0xff0000,
  169. transparent: true,
  170. opacity: 0.5
  171. } );
  172. object = new THREE.Mesh( new THREE.TetrahedronGeometry( 75, 0 ), material );
  173. object.position.set( 200, 0, 200 );
  174. object.name = 'Tetrahedron';
  175. scene1.add( object );
  176. // ---------------------------------------------------------------------
  177. // Buffered geometry primitives
  178. // ---------------------------------------------------------------------
  179. // Sphere
  180. material = new THREE.MeshStandardMaterial( {
  181. color: 0xffff00,
  182. metalness: 0.5,
  183. roughness: 1.0,
  184. flatShading: true,
  185. } );
  186. material.map = gradientTexture;
  187. material.bumpMap = mapGrid;
  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. // THREE.Points
  272. // ---------------------------------------------------------------------
  273. numPoints = 100;
  274. const pointsArray = new Float32Array( numPoints * 3 );
  275. for ( let i = 0; i < numPoints; i ++ ) {
  276. pointsArray[ 3 * i ] = - 50 + Math.random() * 100;
  277. pointsArray[ 3 * i + 1 ] = Math.random() * 100;
  278. pointsArray[ 3 * i + 2 ] = - 50 + Math.random() * 100;
  279. }
  280. const pointsGeo = new THREE.BufferGeometry();
  281. pointsGeo.setAttribute( 'position', new THREE.BufferAttribute( pointsArray, 3 ) );
  282. const pointsMaterial = new THREE.PointsMaterial( { color: 0xffff00, size: 5 } );
  283. const pointCloud = new THREE.Points( pointsGeo, pointsMaterial );
  284. pointCloud.name = 'Points';
  285. pointCloud.position.set( - 200, 0, - 200 );
  286. scene1.add( pointCloud );
  287. // ---------------------------------------------------------------------
  288. // Ortho camera
  289. // ---------------------------------------------------------------------
  290. const height = 1000; // frustum height
  291. const aspect = window.innerWidth / window.innerHeight;
  292. const cameraOrtho = new THREE.OrthographicCamera( - height * aspect, height * aspect, height, - height, 0, 2000 );
  293. cameraOrtho.position.set( 600, 400, 0 );
  294. cameraOrtho.lookAt( 0, 0, 0 );
  295. scene1.add( cameraOrtho );
  296. cameraOrtho.name = 'OrthographicCamera';
  297. material = new THREE.MeshLambertMaterial( {
  298. color: 0xffff00,
  299. side: THREE.DoubleSide
  300. } );
  301. object = new THREE.Mesh( new THREE.CircleGeometry( 50, 20, 0, Math.PI * 2 ), material );
  302. object.position.set( 200, 0, - 400 );
  303. scene1.add( object );
  304. object = new THREE.Mesh( new THREE.RingGeometry( 10, 50, 20, 5, 0, Math.PI * 2 ), material );
  305. object.position.set( 0, 0, - 400 );
  306. scene1.add( object );
  307. object = new THREE.Mesh( new THREE.CylinderGeometry( 25, 75, 100, 40, 5 ), material );
  308. object.position.set( - 200, 0, - 400 );
  309. scene1.add( object );
  310. //
  311. const points = [];
  312. for ( let i = 0; i < 50; i ++ ) {
  313. points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * Math.sin( i * 0.1 ) * 15 + 50, ( i - 5 ) * 2 ) );
  314. }
  315. object = new THREE.Mesh( new THREE.LatheGeometry( points, 20 ), material );
  316. object.position.set( 200, 0, 400 );
  317. scene1.add( object );
  318. // ---------------------------------------------------------------------
  319. // Big red box hidden just for testing `onlyVisible` option
  320. // ---------------------------------------------------------------------
  321. material = new THREE.MeshBasicMaterial( {
  322. color: 0xff0000
  323. } );
  324. object = new THREE.Mesh( new THREE.BoxGeometry( 200, 200, 200 ), material );
  325. object.position.set( 0, 0, 0 );
  326. object.name = 'CubeHidden';
  327. object.visible = false;
  328. scene1.add( object );
  329. // ---------------------------------------------------------------------
  330. // Model requiring KHR_mesh_quantization
  331. // ---------------------------------------------------------------------
  332. const loader = new GLTFLoader();
  333. loader.load( 'models/gltf/ShaderBall.glb', function ( gltf ) {
  334. model = gltf.scene;
  335. model.scale.setScalar( 50 );
  336. model.position.set( 200, - 40, - 200 );
  337. scene1.add( model );
  338. } );
  339. // ---------------------------------------------------------------------
  340. // Model requiring KHR_mesh_quantization
  341. // ---------------------------------------------------------------------
  342. material = new THREE.MeshBasicMaterial( {
  343. color: 0xffffff,
  344. } );
  345. object = new THREE.InstancedMesh( new THREE.BoxGeometry( 10, 10, 10, 2, 2, 2 ), material, 50 );
  346. const matrix = new THREE.Matrix4();
  347. const color = new THREE.Color();
  348. for ( let i = 0; i < 50; i ++ ) {
  349. matrix.setPosition( Math.random() * 100 - 50, Math.random() * 100 - 50, Math.random() * 100 - 50 );
  350. object.setMatrixAt( i, matrix );
  351. object.setColorAt( i, color.setHSL( i / 50, 1, 0.5 ) );
  352. }
  353. object.position.set( 400, 0, 200 );
  354. scene1.add( object );
  355. // ---------------------------------------------------------------------
  356. // 2nd THREE.Scene
  357. // ---------------------------------------------------------------------
  358. scene2 = new THREE.Scene();
  359. object = new THREE.Mesh( new THREE.BoxGeometry( 100, 100, 100 ), material );
  360. object.position.set( 0, 0, 0 );
  361. object.name = 'Cube2ndScene';
  362. scene2.name = 'Scene2';
  363. scene2.add( object );
  364. //
  365. renderer = new THREE.WebGLRenderer( { antialias: true } );
  366. renderer.setPixelRatio( window.devicePixelRatio );
  367. renderer.setSize( window.innerWidth, window.innerHeight );
  368. renderer.setAnimationLoop( animate );
  369. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  370. renderer.toneMappingExposure = 1;
  371. container.appendChild( renderer.domElement );
  372. //
  373. window.addEventListener( 'resize', onWindowResize );
  374. // ---------------------------------------------------------------------
  375. // Exporting compressed textures and meshes (KTX2 / Draco / Meshopt)
  376. // ---------------------------------------------------------------------
  377. const ktx2Loader = new KTX2Loader()
  378. .setTranscoderPath( 'jsm/libs/basis/' )
  379. .detectSupport( renderer );
  380. const gltfLoader = new GLTFLoader().setPath( 'models/gltf/' );
  381. gltfLoader.setKTX2Loader( ktx2Loader );
  382. gltfLoader.setMeshoptDecoder( MeshoptDecoder );
  383. gltfLoader.load( 'coffeemat.glb', function ( gltf ) {
  384. gltf.scene.position.x = 400;
  385. gltf.scene.position.z = - 200;
  386. scene1.add( gltf.scene );
  387. coffeemat = gltf.scene;
  388. } );
  389. // ---------------------------------------------------------------------
  390. // Box with WebP texture (EXT_texture_webp)
  391. // ---------------------------------------------------------------------
  392. const canvas = document.createElement( 'canvas' );
  393. canvas.width = 64;
  394. canvas.height = 64;
  395. const ctx = canvas.getContext( '2d' );
  396. ctx.fillStyle = '#005BBB';
  397. ctx.fillRect( 0, 0, 64, 64 );
  398. ctx.fillStyle = '#FFD500';
  399. ctx.fillRect( 16, 16, 32, 32 );
  400. const webpTexture = new THREE.CanvasTexture( canvas );
  401. webpTexture.userData.mimeType = 'image/webp';
  402. webpTexture.colorSpace = THREE.SRGBColorSpace;
  403. webpBox = new THREE.Mesh(
  404. new THREE.BoxGeometry( 100, 100, 100 ),
  405. new THREE.MeshBasicMaterial( { map: webpTexture } )
  406. );
  407. webpBox.position.set( 400, 0, 0 );
  408. webpBox.name = 'WebPBox';
  409. scene1.add( webpBox );
  410. //
  411. const gui = new GUI();
  412. let h = gui.addFolder( 'Settings' );
  413. h.add( params, 'trs' ).name( 'Use TRS' );
  414. h.add( params, 'onlyVisible' ).name( 'Only Visible Objects' );
  415. h.add( params, 'binary' ).name( 'Binary (GLB)' );
  416. h.add( params, 'maxTextureSize', 2, 8192 ).name( 'Max Texture Size' ).step( 1 );
  417. h = gui.addFolder( 'Export' );
  418. h.add( params, 'exportScene1' ).name( 'Export Scene 1' );
  419. h.add( params, 'exportScenes' ).name( 'Export Scene 1 and 2' );
  420. h.add( params, 'exportSphere' ).name( 'Export Sphere' );
  421. h.add( params, 'exportModel' ).name( 'Export Model' );
  422. h.add( params, 'exportObjects' ).name( 'Export Sphere With Grid' );
  423. h.add( params, 'exportSceneObject' ).name( 'Export Scene 1 and Object' );
  424. h.add( params, 'exportCompressedObject' ).name( 'Export Coffeemat (from compressed data)' );
  425. h.add( params, 'exportWebPModel' ).name( 'Export WebP Model (EXT_texture_webp)' );
  426. gui.open();
  427. }
  428. function exportScene1() {
  429. exportGLTF( scene1 );
  430. }
  431. function exportScenes() {
  432. exportGLTF( [ scene1, scene2 ] );
  433. }
  434. function exportSphere() {
  435. exportGLTF( sphere );
  436. }
  437. function exportModel() {
  438. exportGLTF( model );
  439. }
  440. function exportObjects() {
  441. exportGLTF( [ sphere, gridHelper ] );
  442. }
  443. function exportSceneObject() {
  444. exportGLTF( [ scene1, gridHelper ] );
  445. }
  446. function exportCompressedObject() {
  447. exportGLTF( [ coffeemat ] );
  448. }
  449. function exportWebPModel() {
  450. exportGLTF( webpBox );
  451. }
  452. function onWindowResize() {
  453. camera.aspect = window.innerWidth / window.innerHeight;
  454. camera.updateProjectionMatrix();
  455. renderer.setSize( window.innerWidth, window.innerHeight );
  456. }
  457. //
  458. function animate() {
  459. const timer = Date.now() * 0.0001;
  460. camera.position.x = Math.cos( timer ) * 800;
  461. camera.position.z = Math.sin( timer ) * 800;
  462. camera.lookAt( scene1.position );
  463. renderer.render( scene1, camera );
  464. }
  465. </script>
  466. </body>
  467. </html>
粤ICP备19079148号