misc_exporter_obj.html 7.3 KB

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