misc_exporter_obj.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - exporter - obj</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 - obj
  12. </div>
  13. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. import { OBJExporter } from 'three/addons/exporters/OBJExporter.js';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. let camera, scene, renderer;
  30. const params = {
  31. addTriangle: addTriangle,
  32. addCube: addCube,
  33. addCylinder: addCylinder,
  34. addMultiple: addMultiple,
  35. addTransformed: addTransformed,
  36. addPoints: addPoints,
  37. exportToObj: exportToObj
  38. };
  39. init();
  40. animate();
  41. function init() {
  42. renderer = new THREE.WebGLRenderer();
  43. renderer.setPixelRatio( window.devicePixelRatio );
  44. renderer.setSize( window.innerWidth, window.innerHeight );
  45. document.body.appendChild( renderer.domElement );
  46. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  47. camera.position.set( 0, 0, 400 );
  48. scene = new THREE.Scene();
  49. const ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
  50. scene.add( ambientLight );
  51. const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.8 );
  52. directionalLight.position.set( 0, 1, 1 );
  53. scene.add( directionalLight );
  54. const gui = new GUI();
  55. let h = gui.addFolder( 'Geometry Selection' );
  56. h.add( params, 'addTriangle' ).name( 'Triangle' );
  57. h.add( params, 'addCube' ).name( 'Cube' );
  58. h.add( params, 'addCylinder' ).name( 'Cylinder' );
  59. h.add( params, 'addMultiple' ).name( 'Multiple objects' );
  60. h.add( params, 'addTransformed' ).name( 'Transformed objects' );
  61. h.add( params, 'addPoints' ).name( 'Point Cloud' );
  62. h = gui.addFolder( 'Export' );
  63. h.add( params, 'exportToObj' ).name( 'Export OBJ' );
  64. gui.open();
  65. addGeometry( 1 );
  66. window.addEventListener( 'resize', onWindowResize );
  67. const controls = new OrbitControls( camera, renderer.domElement );
  68. controls.enablePan = false;
  69. }
  70. function exportToObj() {
  71. const exporter = new OBJExporter();
  72. const result = exporter.parse( scene );
  73. saveString( result, 'object.obj' );
  74. }
  75. function addGeometry( type ) {
  76. for ( let i = 0; i < scene.children.length; i ++ ) {
  77. const child = scene.children[ i ];
  78. if ( child.isMesh || child.isPoints ) {
  79. child.geometry.dispose();
  80. scene.remove( child );
  81. i --;
  82. }
  83. }
  84. if ( type === 1 ) {
  85. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  86. const geometry = generateTriangleGeometry();
  87. scene.add( new THREE.Mesh( geometry, material ) );
  88. } else if ( type === 2 ) {
  89. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  90. const geometry = new THREE.BoxGeometry( 100, 100, 100 );
  91. scene.add( new THREE.Mesh( geometry, material ) );
  92. } else if ( type === 3 ) {
  93. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  94. const geometry = new THREE.CylinderGeometry( 50, 50, 100, 30, 1 );
  95. scene.add( new THREE.Mesh( geometry, material ) );
  96. } else if ( type === 4 || type === 5 ) {
  97. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  98. const geometry = generateTriangleGeometry();
  99. const mesh = new THREE.Mesh( geometry, material );
  100. mesh.position.x = - 200;
  101. scene.add( mesh );
  102. const geometry2 = new THREE.BoxGeometry( 100, 100, 100 );
  103. const mesh2 = new THREE.Mesh( geometry2, material );
  104. scene.add( mesh2 );
  105. const geometry3 = new THREE.CylinderGeometry( 50, 50, 100, 30, 1 );
  106. const mesh3 = new THREE.Mesh( geometry3, material );
  107. mesh3.position.x = 200;
  108. scene.add( mesh3 );
  109. if ( type === 5 ) {
  110. mesh.rotation.y = Math.PI / 4.0;
  111. mesh2.rotation.y = Math.PI / 4.0;
  112. mesh3.rotation.y = Math.PI / 4.0;
  113. }
  114. } else if ( type === 6 ) {
  115. const points = [ 0, 0, 0, 100, 0, 0, 100, 100, 0, 0, 100, 0 ];
  116. const colors = [ 0.5, 0, 0, 0.5, 0, 0, 0, 0.5, 0, 0, 0.5, 0 ];
  117. const geometry = new THREE.BufferGeometry();
  118. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( points, 3 ) );
  119. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  120. const material = new THREE.PointsMaterial( { size: 10, vertexColors: true } );
  121. const pointCloud = new THREE.Points( geometry, material );
  122. pointCloud.name = 'point cloud';
  123. scene.add( pointCloud );
  124. }
  125. }
  126. function addTriangle() {
  127. addGeometry( 1 );
  128. }
  129. function addCube() {
  130. addGeometry( 2 );
  131. }
  132. function addCylinder() {
  133. addGeometry( 3 );
  134. }
  135. function addMultiple() {
  136. addGeometry( 4 );
  137. }
  138. function addTransformed() {
  139. addGeometry( 5 );
  140. }
  141. function addPoints() {
  142. addGeometry( 6 );
  143. }
  144. const link = document.createElement( 'a' );
  145. link.style.display = 'none';
  146. document.body.appendChild( link );
  147. function save( blob, filename ) {
  148. link.href = URL.createObjectURL( blob );
  149. link.download = filename;
  150. link.click();
  151. }
  152. function saveString( text, filename ) {
  153. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  154. }
  155. function onWindowResize() {
  156. camera.aspect = window.innerWidth / window.innerHeight;
  157. camera.updateProjectionMatrix();
  158. renderer.setSize( window.innerWidth, window.innerHeight );
  159. }
  160. function animate() {
  161. requestAnimationFrame( animate );
  162. renderer.render( scene, camera );
  163. }
  164. function generateTriangleGeometry() {
  165. const geometry = new THREE.BufferGeometry();
  166. const vertices = [];
  167. vertices.push( - 50, - 50, 0 );
  168. vertices.push( 50, - 50, 0 );
  169. vertices.push( 50, 50, 0 );
  170. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  171. geometry.computeVertexNormals();
  172. return geometry;
  173. }
  174. </script>
  175. </body>
  176. </html>