gltf_exporter.html 13 KB

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