misc_exporter_obj.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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><br /><br />
  33. <button id="export">export to obj</button>
  34. </div>
  35. <script type="module">
  36. import * as THREE from '../build/three.module.js';
  37. import { OBJExporter } from './jsm/exporters/OBJExporter.js';
  38. var camera, scene, light, renderer;
  39. var exportButton, floatingDiv;
  40. var mouseX = 0, mouseY = 0;
  41. function exportToObj() {
  42. var exporter = new OBJExporter();
  43. var result = exporter.parse( scene );
  44. floatingDiv.style.display = 'block';
  45. floatingDiv.innerHTML = result.split( '\n' ).join( '<br />' );
  46. }
  47. function addGeometry( type ) {
  48. for ( var i = 0; i < scene.children.length; i ++ ) {
  49. var child = scene.children[ i ];
  50. if ( child.isMesh ) {
  51. child.geometry.dispose();
  52. scene.remove( child );
  53. i --;
  54. }
  55. }
  56. if ( type === 1 ) {
  57. var material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  58. var geometry = generateTriangleGeometry();
  59. scene.add( new THREE.Mesh( geometry, material ) );
  60. } else if ( type === 2 ) {
  61. var material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  62. var geometry = new THREE.BoxBufferGeometry( 100, 100, 100 );
  63. scene.add( new THREE.Mesh( geometry, material ) );
  64. } else if ( type === 3 ) {
  65. var material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  66. var geometry = new THREE.CylinderBufferGeometry( 50, 50, 100, 30, 1 );
  67. scene.add( new THREE.Mesh( geometry, material ) );
  68. } else if ( type === 4 || type === 5 ) {
  69. var material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  70. var geometry = generateTriangleGeometry();
  71. var mesh = new THREE.Mesh( geometry, material );
  72. mesh.position.x = - 200;
  73. scene.add( mesh );
  74. var geometry2 = new THREE.BoxBufferGeometry( 100, 100, 100 );
  75. var mesh2 = new THREE.Mesh( geometry2, material );
  76. scene.add( mesh2 );
  77. var geometry3 = new THREE.CylinderBufferGeometry( 50, 50, 100, 30, 1 );
  78. var mesh3 = new THREE.Mesh( geometry3, material );
  79. mesh3.position.x = 200;
  80. scene.add( mesh3 );
  81. if ( type === 5 ) {
  82. mesh.rotation.y = Math.PI / 4.0;
  83. mesh2.rotation.y = Math.PI / 4.0;
  84. mesh3.rotation.y = Math.PI / 4.0;
  85. }
  86. }
  87. }
  88. function init() {
  89. renderer = new THREE.WebGLRenderer();
  90. renderer.setPixelRatio( window.devicePixelRatio );
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. document.body.appendChild( renderer.domElement );
  93. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  94. camera.position.set( 0, 0, 400 );
  95. scene = new THREE.Scene();
  96. light = new THREE.DirectionalLight( 0xffffff );
  97. scene.add( light );
  98. addGeometry( 1 );
  99. window.addEventListener( 'click', onWindowClick, false );
  100. window.addEventListener( 'resize', onWindowResize, false );
  101. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  102. document.addEventListener( 'mouseover', onDocumentMouseMove, false );
  103. document.getElementById( 'triangle' ).addEventListener( 'click', function () {
  104. addGeometry( 1 );
  105. } );
  106. document.getElementById( 'cube' ).addEventListener( 'click', function () {
  107. addGeometry( 2 );
  108. } );
  109. document.getElementById( 'cylinder' ).addEventListener( 'click', function () {
  110. addGeometry( 3 );
  111. } );
  112. document.getElementById( 'multiple' ).addEventListener( 'click', function () {
  113. addGeometry( 4 );
  114. } );
  115. document.getElementById( 'transformed' ).addEventListener( 'click', function () {
  116. addGeometry( 5 );
  117. } );
  118. exportButton = document.getElementById( 'export' );
  119. exportButton.addEventListener( 'click', function () {
  120. exportToObj();
  121. } );
  122. floatingDiv = document.createElement( 'div' );
  123. floatingDiv.className = 'floating';
  124. document.body.appendChild( floatingDiv );
  125. }
  126. function onWindowClick( event ) {
  127. var needToClose = true;
  128. var target = event.target;
  129. while ( target !== null ) {
  130. if ( target === floatingDiv || target === exportButton ) {
  131. needToClose = false;
  132. break;
  133. }
  134. target = target.parentElement;
  135. }
  136. if ( needToClose ) {
  137. floatingDiv.style.display = 'none';
  138. }
  139. }
  140. function onWindowResize() {
  141. camera.aspect = window.innerWidth / window.innerHeight;
  142. camera.updateProjectionMatrix();
  143. renderer.setSize( window.innerWidth, window.innerHeight );
  144. }
  145. function onDocumentMouseMove( event ) {
  146. var windowHalfX = window.innerWidth / 2;
  147. var windowHalfY = window.innerHeight / 2;
  148. mouseX = ( event.clientX - windowHalfX ) / 2;
  149. mouseY = ( event.clientY - windowHalfY ) / 2;
  150. }
  151. function animate() {
  152. requestAnimationFrame( animate );
  153. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  154. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  155. camera.lookAt( scene.position );
  156. light.position.set( camera.position.x, camera.position.y, camera.position.z ).normalize();
  157. renderer.render( scene, camera );
  158. }
  159. function generateTriangleGeometry() {
  160. var geometry = new THREE.BufferGeometry();
  161. var vertices = [];
  162. vertices.push( - 50, - 50, 0 );
  163. vertices.push( 50, - 50, 0 );
  164. vertices.push( 50, 50, 0 );
  165. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  166. geometry.computeVertexNormals();
  167. return geometry;
  168. }
  169. init();
  170. animate();
  171. </script>
  172. </body>
  173. </html>