misc_exporter_obj.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. <style>
  9. .floating {
  10. background : #000000;
  11. opacity : 0.8;
  12. width : 80%;
  13. height : 80%;
  14. position : absolute;
  15. left : 10%;
  16. top : 10%;
  17. border : 1px solid #555555;
  18. padding : 10px;
  19. display : none;
  20. overflow : auto;
  21. z-index: 100;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="info">
  27. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - exporter - obj<br /><br />
  28. <button id="triangle">triangle</button>
  29. <button id="cube">cube</button>
  30. <button id="cylinder">cylinder</button>
  31. <button id="multiple">multiple</button>
  32. <button id="transformed">transformed</button>
  33. <button id="points">points</button><br /><br />
  34. <button id="export">export to obj</button>
  35. </div>
  36. <script type="module">
  37. import * as THREE from '../build/three.module.js';
  38. import { OBJExporter } from './jsm/exporters/OBJExporter.js';
  39. let camera, scene, light, renderer;
  40. let exportButton, floatingDiv;
  41. let mouseX = 0, mouseY = 0;
  42. function exportToObj() {
  43. const exporter = new OBJExporter();
  44. const result = exporter.parse( scene );
  45. floatingDiv.style.display = 'block';
  46. floatingDiv.innerHTML = result.split( '\n' ).join( '<br />' );
  47. }
  48. function addGeometry( type ) {
  49. for ( let i = 0; i < scene.children.length; i ++ ) {
  50. const child = scene.children[ i ];
  51. if ( child.isMesh || child.isPoints ) {
  52. child.geometry.dispose();
  53. scene.remove( child );
  54. i --;
  55. }
  56. }
  57. if ( type === 1 ) {
  58. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  59. const geometry = generateTriangleGeometry();
  60. scene.add( new THREE.Mesh( geometry, material ) );
  61. } else if ( type === 2 ) {
  62. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  63. const geometry = new THREE.BoxGeometry( 100, 100, 100 );
  64. scene.add( new THREE.Mesh( geometry, material ) );
  65. } else if ( type === 3 ) {
  66. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  67. const geometry = new THREE.CylinderGeometry( 50, 50, 100, 30, 1 );
  68. scene.add( new THREE.Mesh( geometry, material ) );
  69. } else if ( type === 4 || type === 5 ) {
  70. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  71. const geometry = generateTriangleGeometry();
  72. const mesh = new THREE.Mesh( geometry, material );
  73. mesh.position.x = - 200;
  74. scene.add( mesh );
  75. const geometry2 = new THREE.BoxGeometry( 100, 100, 100 );
  76. const mesh2 = new THREE.Mesh( geometry2, material );
  77. scene.add( mesh2 );
  78. const geometry3 = new THREE.CylinderGeometry( 50, 50, 100, 30, 1 );
  79. const mesh3 = new THREE.Mesh( geometry3, material );
  80. mesh3.position.x = 200;
  81. scene.add( mesh3 );
  82. if ( type === 5 ) {
  83. mesh.rotation.y = Math.PI / 4.0;
  84. mesh2.rotation.y = Math.PI / 4.0;
  85. mesh3.rotation.y = Math.PI / 4.0;
  86. }
  87. } else if ( type === 6 ) {
  88. const points = [ 0, 0, 0, 100, 0, 0, 100, 100, 0, 0, 100, 0 ];
  89. const colors = [ 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0 ];
  90. const geometry = new THREE.BufferGeometry();
  91. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( points, 3 ) );
  92. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  93. const material = new THREE.PointsMaterial( { size: 10, vertexColors: true } );
  94. const pointCloud = new THREE.Points( geometry, material );
  95. pointCloud.name = 'point cloud';
  96. scene.add( pointCloud );
  97. }
  98. }
  99. function init() {
  100. renderer = new THREE.WebGLRenderer();
  101. renderer.setPixelRatio( window.devicePixelRatio );
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. document.body.appendChild( renderer.domElement );
  104. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  105. camera.position.set( 0, 0, 400 );
  106. scene = new THREE.Scene();
  107. light = new THREE.DirectionalLight( 0xffffff );
  108. scene.add( light );
  109. addGeometry( 1 );
  110. window.addEventListener( 'click', onWindowClick );
  111. window.addEventListener( 'resize', onWindowResize );
  112. document.addEventListener( 'mousemove', onDocumentMouseMove );
  113. document.addEventListener( 'mouseover', onDocumentMouseMove );
  114. document.getElementById( 'triangle' ).addEventListener( 'click', function () {
  115. addGeometry( 1 );
  116. } );
  117. document.getElementById( 'cube' ).addEventListener( 'click', function () {
  118. addGeometry( 2 );
  119. } );
  120. document.getElementById( 'cylinder' ).addEventListener( 'click', function () {
  121. addGeometry( 3 );
  122. } );
  123. document.getElementById( 'multiple' ).addEventListener( 'click', function () {
  124. addGeometry( 4 );
  125. } );
  126. document.getElementById( 'transformed' ).addEventListener( 'click', function () {
  127. addGeometry( 5 );
  128. } );
  129. document.getElementById( 'points' ).addEventListener( 'click', function () {
  130. addGeometry( 6 );
  131. } );
  132. exportButton = document.getElementById( 'export' );
  133. exportButton.addEventListener( 'click', function () {
  134. exportToObj();
  135. } );
  136. floatingDiv = document.createElement( 'div' );
  137. floatingDiv.className = 'floating';
  138. document.body.appendChild( floatingDiv );
  139. }
  140. function onWindowClick( event ) {
  141. let needToClose = true;
  142. let target = event.target;
  143. while ( target !== null ) {
  144. if ( target === floatingDiv || target === exportButton ) {
  145. needToClose = false;
  146. break;
  147. }
  148. target = target.parentElement;
  149. }
  150. if ( needToClose ) {
  151. floatingDiv.style.display = 'none';
  152. }
  153. }
  154. function onWindowResize() {
  155. camera.aspect = window.innerWidth / window.innerHeight;
  156. camera.updateProjectionMatrix();
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. }
  159. function onDocumentMouseMove( event ) {
  160. const windowHalfX = window.innerWidth / 2;
  161. const windowHalfY = window.innerHeight / 2;
  162. mouseX = ( event.clientX - windowHalfX ) / 2;
  163. mouseY = ( event.clientY - windowHalfY ) / 2;
  164. }
  165. function animate() {
  166. requestAnimationFrame( animate );
  167. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  168. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  169. camera.lookAt( scene.position );
  170. light.position.set( camera.position.x, camera.position.y, camera.position.z ).normalize();
  171. renderer.render( scene, camera );
  172. }
  173. function generateTriangleGeometry() {
  174. const geometry = new THREE.BufferGeometry();
  175. const vertices = [];
  176. vertices.push( - 50, - 50, 0 );
  177. vertices.push( 50, - 50, 0 );
  178. vertices.push( 50, 50, 0 );
  179. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  180. geometry.computeVertexNormals();
  181. return geometry;
  182. }
  183. init();
  184. animate();
  185. </script>
  186. </body>
  187. </html>