misc_exporter_gltf.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - gltf exporter</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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #info {
  15. color: #ccc;
  16. text-align: center;
  17. position: absolute;
  18. top: 0px; width: 100%;
  19. padding: 5px;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="info">
  25. GLTF Exporter<br/>
  26. <button id="export_scene">Export Scene1</button>
  27. <button id="export_scenes">Export Scene1 and Scene 2</button>
  28. <button id="export_object">Export Sphere</button>
  29. <button id="export_empty">Export empty geometry</button>
  30. <button id="export_obj">Export WaltHead</button>
  31. <button id="export_objects">Export Sphere and Grid</button>
  32. <button id="export_scene_object">Export Scene1 and Sphere</button>
  33. <br/>
  34. <label><input id="option_trs" name="trs" type="checkbox"/>TRS</label>
  35. <label><input id="option_visible" name="visible" type="checkbox" checked="checked"/>Only Visible</label>
  36. <label><input id="option_drawrange" name="visible" type="checkbox" checked="checked"/>Truncate drawRange</label>
  37. <label><input id="option_binary" name="visible" type="checkbox">Binary (<code>.glb</code>)</label>
  38. <label><input id="option_forceindices" name="visible" type="checkbox">Force indices</label>
  39. <label><input id="option_forcepot" name="visible" type="checkbox">Force POT textures</label>
  40. </div>
  41. <script src="../build/three.js"></script>
  42. <script src="js/Detector.js"></script>
  43. <script src="js/loaders/OBJLoader.js"></script>
  44. <script src="js/exporters/GLTFExporter.js"></script>
  45. <script>
  46. function exportGLTF( input ) {
  47. var gltfExporter = new THREE.GLTFExporter();
  48. var options = {
  49. trs: document.getElementById('option_trs').checked,
  50. onlyVisible: document.getElementById('option_visible').checked,
  51. truncateDrawRange: document.getElementById('option_drawrange').checked,
  52. binary: document.getElementById('option_binary').checked,
  53. forceIndices: document.getElementById('option_forceindices').checked,
  54. forcePowerOfTwoTextures: document.getElementById('option_forcepot').checked
  55. };
  56. gltfExporter.parse( input, function( result ) {
  57. if ( result instanceof ArrayBuffer ) {
  58. saveArrayBuffer( result, 'scene.glb' );
  59. } else {
  60. var output = JSON.stringify( result, null, 2 );
  61. console.log( output );
  62. saveString( output, 'scene.gltf' );
  63. }
  64. }, options );
  65. }
  66. document.getElementById( 'export_scene' ).addEventListener( 'click', function () {
  67. exportGLTF( scene1 );
  68. } );
  69. document.getElementById( 'export_scenes' ).addEventListener( 'click', function () {
  70. exportGLTF( [ scene1, scene2 ] );
  71. } );
  72. document.getElementById( 'export_empty' ).addEventListener( 'click', function () {
  73. exportGLTF( empty );
  74. } );
  75. document.getElementById( 'export_object' ).addEventListener( 'click', function () {
  76. exportGLTF( sphere );
  77. } );
  78. document.getElementById( 'export_obj' ).addEventListener( 'click', function () {
  79. exportGLTF( waltHead );
  80. } );
  81. document.getElementById( 'export_objects' ).addEventListener( 'click', function () {
  82. exportGLTF( [ sphere, gridHelper ] );
  83. } );
  84. document.getElementById( 'export_scene_object' ).addEventListener( 'click', function () {
  85. exportGLTF( [ scene1, gridHelper ] );
  86. } );
  87. var link = document.createElement( 'a' );
  88. link.style.display = 'none';
  89. document.body.appendChild( link ); // Firefox workaround, see #6594
  90. function save( blob, filename ) {
  91. link.href = URL.createObjectURL( blob );
  92. link.download = filename;
  93. link.click();
  94. // URL.revokeObjectURL( url ); breaks Firefox...
  95. }
  96. function saveString( text, filename ) {
  97. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  98. }
  99. function saveArrayBuffer( buffer, filename ) {
  100. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  101. }
  102. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  103. var container;
  104. var camera, object, scene1, scene2, renderer;
  105. var gridHelper, sphere, waltHead, empty;
  106. init();
  107. animate();
  108. function init() {
  109. container = document.createElement( 'div' );
  110. document.body.appendChild( container );
  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. var light = new THREE.AmbientLight( 0xffffff, 0.2 );
  124. light.name = 'AmbientLight';
  125. scene1.add( light );
  126. // ---------------------------------------------------------------------
  127. // DirectLight
  128. // ---------------------------------------------------------------------
  129. light = new THREE.DirectionalLight( 0xffffff, 1 );
  130. light.position.set( 1, 1, 0 );
  131. light.name = 'DirectionalLight';
  132. scene1.add( light );
  133. // ---------------------------------------------------------------------
  134. // Grid
  135. // ---------------------------------------------------------------------
  136. gridHelper = new THREE.GridHelper( 2000, 20 );
  137. gridHelper.position.y = -50;
  138. gridHelper.name = "Grid";
  139. scene1.add( gridHelper );
  140. // ---------------------------------------------------------------------
  141. // Axes
  142. // ---------------------------------------------------------------------
  143. var axes = new THREE.AxesHelper( 500 );
  144. axes.name = "AxesHelper";
  145. scene1.add( axes );
  146. // ---------------------------------------------------------------------
  147. // Simple geometry with basic material
  148. // ---------------------------------------------------------------------
  149. // Icosahedron
  150. var mapGrid = new THREE.TextureLoader().load( 'textures/UV_Grid_Sm.jpg' );
  151. mapGrid.wrapS = mapGrid.wrapT = THREE.RepeatWrapping;
  152. var material = new THREE.MeshBasicMaterial( {
  153. color: 0xffffff,
  154. map: mapGrid
  155. } );
  156. object = new THREE.Mesh( new THREE.IcosahedronGeometry( 75, 0 ), material );
  157. object.position.set( -200, 0, 200 );
  158. object.name = 'Icosahedron';
  159. scene1.add( object );
  160. // Octahedron
  161. material = new THREE.MeshBasicMaterial( {
  162. color: 0x0000ff,
  163. wireframe: true
  164. } );
  165. object = new THREE.Mesh( new THREE.OctahedronGeometry( 75, 1 ), material );
  166. object.position.set( 0, 0, 200 );
  167. object.name = 'Octahedron';
  168. scene1.add( object );
  169. // Tetrahedron
  170. material = new THREE.MeshBasicMaterial( {
  171. color: 0xff0000,
  172. transparent: true,
  173. opacity: 0.5
  174. } );
  175. object = new THREE.Mesh( new THREE.TetrahedronGeometry( 75, 0 ), material );
  176. object.position.set( 200, 0, 200 );
  177. object.name = 'Tetrahedron';
  178. scene1.add( object );
  179. // ---------------------------------------------------------------------
  180. // Buffered geometry primitives
  181. // ---------------------------------------------------------------------
  182. // Sphere
  183. material = new THREE.MeshStandardMaterial( {
  184. color: 0xffff00,
  185. metalness: 0.5,
  186. roughness: 1.0,
  187. flatShading: true
  188. } );
  189. sphere = new THREE.Mesh( new THREE.SphereBufferGeometry( 70, 10, 10 ), material );
  190. sphere.position.set( 0, 0, 0 );
  191. sphere.name = "Sphere";
  192. scene1.add( sphere );
  193. // Cylinder
  194. material = new THREE.MeshStandardMaterial( {
  195. color: 0xff00ff,
  196. flatShading: true
  197. } );
  198. object = new THREE.Mesh( new THREE.CylinderBufferGeometry( 10, 80, 100 ), material );
  199. object.position.set( 200, 0, 0 );
  200. object.name = "Cylinder";
  201. scene1.add( object );
  202. // TorusKnot
  203. material = new THREE.MeshStandardMaterial( {
  204. color: 0xff0000,
  205. roughness: 1
  206. } );
  207. object = new THREE.Mesh( new THREE.TorusKnotGeometry( 50, 15, 40, 10 ), material );
  208. object.position.set( -200, 0, 0 );
  209. object.name = "Cylinder";
  210. scene1.add( object );
  211. // ---------------------------------------------------------------------
  212. // Hierarchy
  213. // ---------------------------------------------------------------------
  214. var mapWood = new THREE.TextureLoader().load( 'textures/hardwood2_diffuse.jpg' );
  215. material = new THREE.MeshStandardMaterial( { map: mapWood, side: THREE.DoubleSide } );
  216. object = new THREE.Mesh( new THREE.BoxBufferGeometry( 40, 100, 100 ), material );
  217. object.position.set( -200, 0, 400 );
  218. object.name = "Cube";
  219. scene1.add( object );
  220. var object2 = new THREE.Mesh( new THREE.BoxBufferGeometry( 40, 40, 40, 2, 2, 2 ), material );
  221. object2.position.set( 0, 0, 50 );
  222. object2.rotation.set( 0, 45, 0 );
  223. object2.name = "SubCube";
  224. object.add( object2 );
  225. // ---------------------------------------------------------------------
  226. // Groups
  227. // ---------------------------------------------------------------------
  228. var group1 = new THREE.Group();
  229. group1.name = "Group";
  230. scene1.add( group1 );
  231. var group2 = new THREE.Group();
  232. group2.name = "subGroup";
  233. group2.position.set( 0, 50, 0);
  234. group1.add( group2 );
  235. object2 = new THREE.Mesh( new THREE.BoxBufferGeometry( 30, 30, 30 ), material );
  236. object2.name = "Cube in group";
  237. object2.position.set( 0, 0, 400 );
  238. group2.add( object2 );
  239. // ---------------------------------------------------------------------
  240. // Triangle Strip
  241. // ---------------------------------------------------------------------
  242. var geometry = new THREE.BufferGeometry();
  243. var positions = new Float32Array([
  244. 0, 0, 0,
  245. 0, 80, 0,
  246. 80, 0, 0,
  247. 80, 80, 0,
  248. 80, 0, 80,
  249. 80, 80, 80,
  250. ]);
  251. var colors = new Float32Array([
  252. 1, 0, 0,
  253. 1, 0, 0,
  254. 1, 1, 0,
  255. 1, 1, 0,
  256. 0, 0, 1,
  257. 0, 0, 1,
  258. ]);
  259. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  260. geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  261. object = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, vertexColors: THREE.VertexColors } ) );
  262. object.position.set( 140, -40, -250);
  263. object.setDrawMode( THREE.TriangleStripDrawMode );
  264. object.name = 'Custom buffered';
  265. object.userData = { data: 'customdata', list: [ 1,2,3,4 ] };
  266. scene1.add( object );
  267. // ---------------------------------------------------------------------
  268. // Line Strip
  269. // ---------------------------------------------------------------------
  270. var geometry = new THREE.BufferGeometry();
  271. var numPoints = 100;
  272. var positions = new Float32Array( numPoints * 3 );
  273. for (var i = 0; i < numPoints; i++ ) {
  274. positions[ i * 3 ] = i;
  275. positions[ i * 3 + 1 ] = Math.sin( i / 2 ) * 20;
  276. positions[ i * 3 + 2 ] = 0;
  277. }
  278. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  279. object = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
  280. object.position.set(-50, 0, -200);
  281. scene1.add( object );
  282. // ---------------------------------------------------------------------
  283. // Line Loop
  284. // ---------------------------------------------------------------------
  285. var geometry = new THREE.BufferGeometry();
  286. var numPoints = 5;
  287. var radius = 70;
  288. var positions = new Float32Array( numPoints * 3 );
  289. for (var i = 0; i < numPoints; i++ ) {
  290. var s = i * Math.PI * 2 / numPoints;
  291. positions[ i * 3 ] = radius * Math.sin ( s );
  292. positions[ i * 3 + 1 ] = radius * Math.cos ( s );
  293. positions[ i * 3 + 2 ] = 0;
  294. }
  295. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  296. object = new THREE.LineLoop( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
  297. object.position.set(0, 0, -200);
  298. scene1.add( object );
  299. // ---------------------------------------------------------------------
  300. // Buffer geometry truncated (DrawRange)
  301. // ---------------------------------------------------------------------
  302. var geometry = new THREE.BufferGeometry();
  303. var numElements = 6;
  304. var outOfRange = 3;
  305. var positions = new Float32Array( ( numElements + outOfRange ) * 3 );
  306. var colors = new Float32Array( ( numElements + outOfRange ) * 3 );
  307. positions.set([
  308. 0, 0, 0,
  309. 0, 80, 0,
  310. 80, 0, 0,
  311. 80, 0, 0,
  312. 0, 80, 0,
  313. 80, 80, 0
  314. ]);
  315. colors.set([
  316. 1, 0, 0,
  317. 1, 0, 0,
  318. 1, 1, 0,
  319. 1, 1, 0,
  320. 0, 0, 1,
  321. 0, 0, 1,
  322. ]);
  323. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  324. geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  325. geometry.setDrawRange( 0, numElements );
  326. object = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, vertexColors: THREE.VertexColors } ) );
  327. object.name = 'Custom buffered truncated';
  328. object.position.set( 340, -40, -200 );
  329. scene1.add( object );
  330. // ---------------------------------------------------------------------
  331. // Empty buffer geometry
  332. // ---------------------------------------------------------------------
  333. var geometry = new THREE.BufferGeometry();
  334. var numElements = 6;
  335. var positions = new Float32Array( ( numElements ) * 3 );
  336. var colors = new Float32Array( ( numElements ) * 3 );
  337. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  338. geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  339. geometry.setDrawRange( 0, 0 );
  340. empty = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, vertexColors: THREE.VertexColors } ) );
  341. empty.name = 'Custom buffered empty (drawrange)';
  342. scene1.add( empty );
  343. // ---------------------------------------------------------------------
  344. // Points
  345. // ---------------------------------------------------------------------
  346. var numPoints = 100;
  347. var pointsArray = new Float32Array( numPoints * 3 );
  348. for ( var i = 0; i < numPoints; i++ ) {
  349. pointsArray[ 3 * i ] = -50 + Math.random() * 100;
  350. pointsArray[ 3 * i + 1 ] = Math.random() * 100;
  351. pointsArray[ 3 * i + 2 ] = -50 + Math.random() * 100;
  352. }
  353. var pointsGeo = new THREE.BufferGeometry();
  354. pointsGeo.addAttribute( 'position', new THREE.BufferAttribute( pointsArray, 3 ) );
  355. var pointsMaterial = new THREE.PointsMaterial( { color: 0xffff00, size: 5 } );
  356. var points = new THREE.Points( pointsGeo, pointsMaterial );
  357. points.name = "Points";
  358. points.position.set( -200, 0, -200);
  359. scene1.add( points );
  360. // ---------------------------------------------------------------------
  361. // Ortho camera
  362. // ---------------------------------------------------------------------
  363. var cameraOrtho = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, 0.1, 10 );
  364. scene1.add( cameraOrtho );
  365. cameraOrtho.name = 'OrthographicCamera';
  366. material = new THREE.MeshLambertMaterial( {
  367. color: 0xffff00,
  368. side: THREE.DoubleSide
  369. } );
  370. object = new THREE.Mesh( new THREE.CircleGeometry( 50, 20, 0, Math.PI * 2 ), material );
  371. object.position.set( 200, 0, -400 );
  372. scene1.add( object );
  373. object = new THREE.Mesh( new THREE.RingGeometry( 10, 50, 20, 5, 0, Math.PI * 2 ), material );
  374. object.position.set( 0, 0, -400 );
  375. scene1.add( object );
  376. object = new THREE.Mesh( new THREE.CylinderGeometry( 25, 75, 100, 40, 5 ), material );
  377. object.position.set( -200, 0, -400 );
  378. scene1.add( object );
  379. //
  380. var points = [];
  381. for ( var i = 0; i < 50; i ++ ) {
  382. points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * Math.sin( i * 0.1 ) * 15 + 50, ( i - 5 ) * 2 ) );
  383. }
  384. object = new THREE.Mesh( new THREE.LatheGeometry( points, 20 ), material );
  385. object.position.set( 200, 0, 400 );
  386. scene1.add( object );
  387. // ---------------------------------------------------------------------
  388. // Big red box hidden just for testing `onlyVisible` option
  389. // ---------------------------------------------------------------------
  390. material = new THREE.MeshBasicMaterial( {
  391. color: 0xff0000
  392. } );
  393. object = new THREE.Mesh( new THREE.BoxBufferGeometry( 200, 200, 200 ), material );
  394. object.position.set( 0, 0, 0 );
  395. object.name = "CubeHidden";
  396. object.visible = false;
  397. scene1.add( object );
  398. // ---------------------------------------------------------------------
  399. //
  400. //
  401. var loader = new THREE.OBJLoader();
  402. loader.load( 'models/obj/walt/WaltHead.obj', function ( obj ) {
  403. waltHead = obj;
  404. waltHead.scale.multiplyScalar( 1.5 );
  405. waltHead.position.set(400, 0, 0);
  406. scene1.add( waltHead );
  407. } );
  408. // ---------------------------------------------------------------------
  409. // 2nd Scene
  410. // ---------------------------------------------------------------------
  411. scene2 = new THREE.Scene();
  412. object = new THREE.Mesh( new THREE.BoxBufferGeometry( 100, 100, 100 ), material );
  413. object.position.set( 0, 0, 0 );
  414. object.name = "Cube2ndScene";
  415. scene2.name = 'Scene2';
  416. scene2.add(object);
  417. //
  418. renderer = new THREE.WebGLRenderer( { antialias: true } );
  419. renderer.setPixelRatio( window.devicePixelRatio );
  420. renderer.setSize( window.innerWidth, window.innerHeight );
  421. container.appendChild( renderer.domElement );
  422. //
  423. window.addEventListener( 'resize', onWindowResize, false );
  424. }
  425. function onWindowResize() {
  426. camera.aspect = window.innerWidth / window.innerHeight;
  427. camera.updateProjectionMatrix();
  428. renderer.setSize( window.innerWidth, window.innerHeight );
  429. }
  430. //
  431. function animate() {
  432. requestAnimationFrame( animate );
  433. render();
  434. }
  435. function render() {
  436. var timer = Date.now() * 0.0001;
  437. camera.position.x = Math.cos( timer ) * 800;
  438. camera.position.z = Math.sin( timer ) * 800;
  439. camera.lookAt( scene1.position );
  440. renderer.render( scene1, camera );
  441. }
  442. </script>
  443. </body>
  444. </html>
粤ICP备19079148号