misc_exporter_gltf.html 17 KB

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