gltf_exporter.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. text-align: center;
  16. position: absolute;
  17. top: 0px; width: 100%;
  18. padding: 5px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="info">
  24. <button id="export">Export scene to .GLTF</button>
  25. </div>
  26. <script src="../build/three.js"></script>
  27. <script src="js/Detector.js"></script>
  28. <script src="js/exporters/GLTFExporter.js"></script>
  29. <script>
  30. document.getElementById( 'export' ).addEventListener( 'click', function () {
  31. var gltfExporter = new THREE.GLTFExporter( renderer );
  32. gltfExporter.parse( [ scene1 ], function( result ) {
  33. var output = JSON.stringify( result, null, 2 );
  34. console.log( output );
  35. saveString( output, 'scene.gltf' );
  36. } );
  37. } );
  38. var link = document.createElement( 'a' );
  39. link.style.display = 'none';
  40. document.body.appendChild( link ); // Firefox workaround, see #6594
  41. function save( blob, filename ) {
  42. link.href = URL.createObjectURL( blob );
  43. link.download = filename || 'data.json';
  44. link.click();
  45. // URL.revokeObjectURL( url ); breaks Firefox...
  46. }
  47. function saveString( text, filename ) {
  48. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  49. }
  50. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  51. var container;
  52. var camera, scene1, renderer;
  53. init();
  54. animate();
  55. function init() {
  56. var object;
  57. container = document.createElement( 'div' );
  58. document.body.appendChild( container );
  59. scene1 = new THREE.Scene();
  60. scene1.name = 'Scene1';
  61. // ---------------------------------------------------------------------
  62. // Perspective Camera
  63. // ---------------------------------------------------------------------
  64. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  65. camera.position.set(600, 400, 0);
  66. camera.name = "PerspectiveCamera";
  67. scene1.add( camera );
  68. // ---------------------------------------------------------------------
  69. // Ambient light
  70. // ---------------------------------------------------------------------
  71. scene1.add( new THREE.AmbientLight( 0xffffff, 0.2 ) );
  72. // ---------------------------------------------------------------------
  73. // DirectLight
  74. // ---------------------------------------------------------------------
  75. var light = new THREE.DirectionalLight( 0xffffff, 1 );
  76. light.position.set( 1, 1, 0 );
  77. scene1.add( light );
  78. // ---------------------------------------------------------------------
  79. // Grid
  80. // ---------------------------------------------------------------------
  81. var gridHelper = new THREE.GridHelper( 2000, 20 );
  82. gridHelper.position.y = -50;
  83. gridHelper.name = "Grid";
  84. scene1.add( gridHelper );
  85. // ---------------------------------------------------------------------
  86. // Axis
  87. // ---------------------------------------------------------------------
  88. var axis = new THREE.AxisHelper(500);
  89. axis.name = "AxisHelper";
  90. scene1.add( axis );
  91. // ---------------------------------------------------------------------
  92. // Simple geometry with basic material
  93. // ---------------------------------------------------------------------
  94. // Icosahedron
  95. var mapGrid = new THREE.TextureLoader().load( 'textures/UV_Grid_Sm.jpg' );
  96. mapGrid.wrapS = mapGrid.wrapT = THREE.RepeatWrapping;
  97. var material = new THREE.MeshBasicMaterial( {
  98. color: 0xffffff,
  99. map: mapGrid
  100. } );
  101. object = new THREE.Mesh( new THREE.IcosahedronGeometry( 75, 0 ), material );
  102. object.position.set( -200, 0, 200 );
  103. object.name = 'Icosahedron';
  104. scene1.add( object );
  105. // Octahedron
  106. material = new THREE.MeshBasicMaterial( {
  107. color: 0x0000ff,
  108. wireframe: true
  109. } );
  110. object = new THREE.Mesh( new THREE.OctahedronGeometry( 75, 1 ), material );
  111. object.position.set( 0, 0, 200 );
  112. object.name = 'Octahedron';
  113. scene1.add( object );
  114. // Tetrahedron
  115. material = new THREE.MeshBasicMaterial( {
  116. color: 0xff0000,
  117. transparent: true,
  118. opacity: 0.5
  119. } );
  120. object = new THREE.Mesh( new THREE.TetrahedronGeometry( 75, 0 ), material );
  121. object.position.set( 200, 0, 200 );
  122. object.name = 'Tetrahedron';
  123. scene1.add( object );
  124. // ---------------------------------------------------------------------
  125. // Buffered geometry primitives
  126. // ---------------------------------------------------------------------
  127. // Sphere
  128. material = new THREE.MeshStandardMaterial( {
  129. color: 0xffff00,
  130. metalness: 0.5,
  131. roughness: 1.0,
  132. flatShading: true
  133. } );
  134. object = new THREE.Mesh( new THREE.SphereBufferGeometry( 70, 10, 10 ), material );
  135. object.position.set( 0, 0, 0 );
  136. object.name = "Sphere";
  137. scene1.add( object );
  138. // Cylinder
  139. material = new THREE.MeshStandardMaterial( {
  140. color: 0xff00ff,
  141. flatShading: true
  142. } );
  143. object = new THREE.Mesh( new THREE.CylinderBufferGeometry( 10, 80, 100 ), material );
  144. object.position.set( 200, 0, 0 );
  145. object.name = "Cylinder";
  146. scene1.add( object );
  147. // TorusKnot
  148. material = new THREE.MeshStandardMaterial( {
  149. color: 0xff0000,
  150. roughness: 1
  151. } );
  152. object = new THREE.Mesh( new THREE.TorusKnotGeometry( 50, 15, 40, 10 ), material );
  153. object.position.set( -200, 0, 0 );
  154. object.name = "Cylinder";
  155. scene1.add( object );
  156. // ---------------------------------------------------------------------
  157. // Hierarchy
  158. // ---------------------------------------------------------------------
  159. var mapWood = new THREE.TextureLoader().load( 'textures/hardwood2_diffuse.jpg' );
  160. material = new THREE.MeshStandardMaterial( { map: mapWood, side: THREE.DoubleSide } );
  161. object = new THREE.Mesh( new THREE.BoxBufferGeometry( 40, 100, 100 ), material );
  162. object.position.set( -200, 0, 400 );
  163. object.name = "Cube";
  164. scene1.add( object );
  165. object2 = new THREE.Mesh( new THREE.BoxBufferGeometry( 40, 40, 40, 2, 2, 2 ), material );
  166. object2.position.set( 0, 0, 50 );
  167. object2.rotation.set( 0, 45, 0 );
  168. object2.name = "SubCube";
  169. object.add( object2 );
  170. // ---------------------------------------------------------------------
  171. // Groups
  172. // ---------------------------------------------------------------------
  173. group1 = new THREE.Group();
  174. group1.name = "Group";
  175. scene1.add( group1 );
  176. group2 = new THREE.Group();
  177. group2.name = "subGroup";
  178. group2.position.set( 0, 50, 0);
  179. group1.add( group2 );
  180. object2 = new THREE.Mesh( new THREE.BoxBufferGeometry( 30, 30, 30 ), material );
  181. object2.name = "Cube in group";
  182. object2.position.set( 0, 0, 400 );
  183. group2.add( object2 );
  184. // ---------------------------------------------------------------------
  185. // Triangle Strip
  186. // ---------------------------------------------------------------------
  187. var geometry = new THREE.BufferGeometry();
  188. var positions = new Float32Array([
  189. 0, 0, 0,
  190. 0, 80, 0,
  191. 80, 0, 0,
  192. 80, 80, 0,
  193. 80, 0, 80,
  194. 80, 80, 80,
  195. ]);
  196. var colors = new Float32Array([
  197. 1, 0, 0,
  198. 1, 0, 0,
  199. 1, 1, 0,
  200. 1, 1, 0,
  201. 0, 0, 1,
  202. 0, 0, 1,
  203. ]);
  204. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  205. geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  206. object = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, vertexColors: THREE.VertexColors } ) );
  207. object.position.set( 140, -40, -250);
  208. object.setDrawMode( THREE.TriangleStripDrawMode );
  209. object.name = 'Custom buffered';
  210. object.userData = { data: 'customdata', list: [ 1,2,3,4 ] };
  211. scene1.add( object );
  212. // ---------------------------------------------------------------------
  213. // Line Strip
  214. // ---------------------------------------------------------------------
  215. var geometry = new THREE.BufferGeometry();
  216. var numPoints = 100;
  217. var positions = new Float32Array( numPoints * 3 );
  218. for (var i = 0; i < numPoints; i++ ) {
  219. positions[ i * 3 ] = i;
  220. positions[ i * 3 + 1 ] = Math.sin( i / 2 ) * 20;
  221. positions[ i * 3 + 2 ] = 0;
  222. }
  223. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  224. object = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
  225. object.position.set(-50, 0, -200);
  226. scene1.add( object );
  227. // ---------------------------------------------------------------------
  228. // Line Loop
  229. // ---------------------------------------------------------------------
  230. var geometry = new THREE.BufferGeometry();
  231. var numPoints = 5;
  232. var radius = 70;
  233. var positions = new Float32Array( numPoints * 3 );
  234. for (var i = 0; i < numPoints; i++ ) {
  235. var s = i * Math.PI * 2 / numPoints;
  236. positions[ i * 3 ] = radius * Math.sin ( s );
  237. positions[ i * 3 + 1 ] = radius * Math.cos ( s );
  238. positions[ i * 3 + 2 ] = 0;
  239. }
  240. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  241. object = new THREE.LineLoop( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
  242. object.position.set(0, 0, -200);
  243. scene1.add( object );
  244. // ---------------------------------------------------------------------
  245. // Points
  246. // ---------------------------------------------------------------------
  247. var numPoints = 100;
  248. var pointsArray = new Float32Array( numPoints * 3 );
  249. for ( var i = 0; i < numPoints; i++ ) {
  250. pointsArray[ 3 * i ] = -50 + Math.random() * 100;
  251. pointsArray[ 3 * i + 1 ] = Math.random() * 100;
  252. pointsArray[ 3 * i + 2 ] = -50 + Math.random() * 100;
  253. }
  254. pointsGeo = new THREE.BufferGeometry();
  255. pointsGeo.addAttribute( 'position', new THREE.BufferAttribute( pointsArray, 3 ) );
  256. var pointsMaterial = new THREE.PointsMaterial( { color: 0xffff00, size: 5 } );
  257. var points = new THREE.Points( pointsGeo, pointsMaterial );
  258. points.name = "Points";
  259. points.position.set( -200, 0, -200);
  260. scene1.add( points );
  261. // ---------------------------------------------------------------------
  262. // Ortho camera
  263. // ---------------------------------------------------------------------
  264. var cameraOrtho = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 10, 10 );
  265. scene1.add( cameraOrtho );
  266. cameraOrtho.name = 'OrthographicCamera';
  267. material = new THREE.MeshLambertMaterial( {
  268. color: 0xffff00,
  269. side: THREE.DoubleSide
  270. } );
  271. object = new THREE.Mesh( new THREE.CircleGeometry( 50, 20, 0, Math.PI * 2 ), material );
  272. object.position.set( 200, 0, -400 );
  273. scene1.add( object );
  274. object = new THREE.Mesh( new THREE.RingGeometry( 10, 50, 20, 5, 0, Math.PI * 2 ), material );
  275. object.position.set( 0, 0, -400 );
  276. scene1.add( object );
  277. object = new THREE.Mesh( new THREE.CylinderGeometry( 25, 75, 100, 40, 5 ), material );
  278. object.position.set( -200, 0, -400 );
  279. scene1.add( object );
  280. //
  281. var points = [];
  282. for ( var i = 0; i < 50; i ++ ) {
  283. points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * Math.sin( i * 0.1 ) * 15 + 50, ( i - 5 ) * 2 ) );
  284. }
  285. object = new THREE.Mesh( new THREE.LatheGeometry( points, 20 ), material );
  286. object.position.set( 200, 0, 400 );
  287. scene1.add( object );
  288. // ---------------------------------------------------------------------
  289. // 2nd Scene
  290. // ---------------------------------------------------------------------
  291. var scene2 = new THREE.Scene();
  292. object = new THREE.Mesh( new THREE.BoxBufferGeometry( 100, 100, 100 ), material );
  293. object.position.set( 0, 0, 0 );
  294. object.name = "Cube2ndScene";
  295. scene2.name = 'Scene2';
  296. scene2.add(object);
  297. //
  298. renderer = new THREE.WebGLRenderer( { antialias: true } );
  299. renderer.setPixelRatio( window.devicePixelRatio );
  300. renderer.setSize( window.innerWidth, window.innerHeight );
  301. container.appendChild( renderer.domElement );
  302. //
  303. window.addEventListener( 'resize', onWindowResize, false );
  304. }
  305. function onWindowResize() {
  306. camera.aspect = window.innerWidth / window.innerHeight;
  307. camera.updateProjectionMatrix();
  308. renderer.setSize( window.innerWidth, window.innerHeight );
  309. }
  310. //
  311. function animate() {
  312. requestAnimationFrame( animate );
  313. render();
  314. }
  315. function render() {
  316. var timer = Date.now() * 0.0001;
  317. camera.position.x = Math.cos( timer ) * 800;
  318. camera.position.z = Math.sin( timer ) * 800;
  319. camera.lookAt( scene1.position );
  320. renderer.render( scene1, camera );
  321. }
  322. </script>
  323. </body>
  324. </html>
粤ICP备19079148号