gltf_exporter.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  43. camera.position.y = 400;
  44. camera.name = "PerspectiveCamera";
  45. scene1 = new THREE.Scene();
  46. scene1.name = 'Scene1';
  47. var light, object;
  48. scene1.add( new THREE.AmbientLight( 0xffffff ) );
  49. light = new THREE.DirectionalLight( 0xffffff );
  50. light.position.set( 0, 1, 0 );
  51. scene1.add( light );
  52. var map = new THREE.TextureLoader().load( 'textures/UV_Grid_Sm.jpg' );
  53. map.wrapS = map.wrapT = THREE.RepeatWrapping;
  54. map.anisotropy = 16;
  55. // var material = new THREE.MeshLambertMaterial( { map: map, side: THREE.DoubleSide } );
  56. //
  57. /*
  58. object = new THREE.Mesh( new THREE.IcosahedronGeometry( 75, 1 ), material );
  59. object.position.set( -200, 0, 200 );
  60. scene1.add( object );
  61. object = new THREE.Mesh( new THREE.OctahedronGeometry( 75, 2 ), material );
  62. object.position.set( 0, 0, 200 );
  63. scene1.add( object );
  64. object = new THREE.Mesh( new THREE.TetrahedronGeometry( 75, 0 ), material );
  65. object.position.set( 200, 0, 200 );
  66. scene1.add( object );
  67. //
  68. object = new THREE.Mesh( new THREE.PlaneGeometry( 100, 100, 4, 4 ), material );
  69. object.position.set( -400, 0, 0 );
  70. scene1.add( object );
  71. */
  72. var map2 = new THREE.TextureLoader().load( 'textures/hardwood2_diffuse.jpg' );
  73. var material = new THREE.MeshStandardMaterial( {
  74. map: map2,
  75. metalness: 0.5,
  76. roughness: 1.0
  77. } );
  78. /*
  79. object = new THREE.Mesh( new THREE.SphereBufferGeometry( 100, 32, 32 ), material );
  80. object.position.set( 0, 0, 0 );
  81. object.name = "Sphere";
  82. scene1.add(object);
  83. /*
  84. var material = new THREE.MeshStandardMaterial( {
  85. color: 0xff0000,
  86. metalness: 0.1,
  87. roughness: 0.5
  88. } );
  89. */
  90. var material = new THREE.MeshStandardMaterial( { map: map, side: THREE.DoubleSide } );
  91. /*
  92. object3 = new THREE.Mesh( new THREE.CylinderBufferGeometry( 100, 100, 100 ), material );
  93. object3.position.set( 200, 0, 0 );
  94. object3.name = "Cube";
  95. scene1.add(object3);
  96. */
  97. /*
  98. object = new THREE.Mesh( new THREE.BoxBufferGeometry( 100, 100, 100 ), material );
  99. object.position.set( -200, 0, 0 );
  100. object.name = "Cube";
  101. scene1.add( object );
  102. object2 = new THREE.Mesh( new THREE.BoxBufferGeometry( 40, 10, 80, 2, 2, 2 ), material );
  103. object2.position.set( 0, 0, 50 );
  104. object2.rotation.set( 0, 45, 0 );
  105. object2.name = "SubCube";
  106. object.add( object2 );
  107. */
  108. group1 = new THREE.Group();
  109. group1.name = "Group";
  110. scene1.add( group1 );
  111. group2 = new THREE.Group();
  112. group2.name = "subGroup";
  113. group1.add( group2 );
  114. object2 = new THREE.Mesh( new THREE.BoxBufferGeometry( 30, 30, 30 ), material );
  115. object2.name = "Cube in group";
  116. object2.position.set( 0, 100, 100 );
  117. group2.add( object2 );
  118. scene1.add( camera );
  119. var cameraOrtho = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 10, 10 );
  120. scene1.add( cameraOrtho );
  121. cameraOrtho.name = 'OrthographicCamera';
  122. /*
  123. object = new THREE.Mesh( new THREE.CircleGeometry( 50, 20, 0, Math.PI * 2 ), material );
  124. object.position.set( 0, 0, 0 );
  125. scene1.add( object );
  126. object = new THREE.Mesh( new THREE.RingGeometry( 10, 50, 20, 5, 0, Math.PI * 2 ), material );
  127. object.position.set( 200, 0, 0 );
  128. scene1.add( object );
  129. object = new THREE.Mesh( new THREE.CylinderGeometry( 25, 75, 100, 40, 5 ), material );
  130. object.position.set( 400, 0, 0 );
  131. scene1.add( object );
  132. */
  133. //
  134. /*
  135. var points = [];
  136. for ( var i = 0; i < 50; i ++ ) {
  137. points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * Math.sin( i * 0.1 ) * 15 + 50, ( i - 5 ) * 2 ) );
  138. }
  139. object = new THREE.Mesh( new THREE.LatheGeometry( points, 20 ), material );
  140. object.position.set( -400, 0, -200 );
  141. scene1.add( object );
  142. object = new THREE.Mesh( new THREE.TorusGeometry( 50, 20, 20, 20 ), material );
  143. object.position.set( -200, 0, -200 );
  144. scene1.add( object );
  145. object = new THREE.Mesh( new THREE.TorusKnotGeometry( 50, 10, 50, 20 ), material );
  146. object.position.set( 0, 0, -200 );
  147. scene1.add( object );
  148. object = new THREE.AxisHelper( 50 );
  149. object.position.set( 200, 0, -200 );
  150. scene1.add( object );
  151. object = new THREE.ArrowHelper( new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, 0, 0 ), 50 );
  152. object.position.set( 400, 0, -200 );
  153. scene1.add( object );
  154. */
  155. var scene2 = new THREE.Scene();
  156. object = new THREE.Mesh( new THREE.BoxBufferGeometry( 100, 100, 100 ), material );
  157. object.position.set( 0, 0, 0 );
  158. object.name = "Cube2ndScene";
  159. scene2.name = 'Scene2';
  160. scene2.add(object);
  161. //
  162. renderer = new THREE.WebGLRenderer( { antialias: true } );
  163. renderer.setPixelRatio( window.devicePixelRatio );
  164. renderer.setSize( window.innerWidth, window.innerHeight );
  165. container.appendChild( renderer.domElement );
  166. stats = new Stats();
  167. container.appendChild( stats.dom );
  168. //
  169. window.addEventListener( 'resize', onWindowResize, false );
  170. var gltfExporter = new THREE.GLTFExporter( renderer );
  171. gltfExporter.parse( [ scene1 ], function( result ) {
  172. console.log( JSON.stringify( result, null, 2 ) );
  173. } );
  174. }
  175. function onWindowResize() {
  176. camera.aspect = window.innerWidth / window.innerHeight;
  177. camera.updateProjectionMatrix();
  178. renderer.setSize( window.innerWidth, window.innerHeight );
  179. }
  180. //
  181. function animate() {
  182. requestAnimationFrame( animate );
  183. render();
  184. stats.update();
  185. }
  186. function render() {
  187. var timer = Date.now() * 0.001;
  188. camera.position.x = Math.cos( timer ) * 800;
  189. camera.position.z = Math.sin( timer ) * 800;
  190. camera.lookAt( scene1.position );
  191. renderer.render( scene1, camera );
  192. }
  193. </script>
  194. </body>
  195. </html>
粤ICP备19079148号