123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - exporter - obj</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- .floating {
- background : #000000;
- opacity : 0.8;
- width : 80%;
- height : 80%;
- position : absolute;
- left : 10%;
- top : 10%;
- border : 1px solid #555555;
- padding : 10px;
- display : none;
- overflow : auto;
- z-index: 100;
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - exporter - obj<br /><br />
- <button id="triangle">triangle</button>
- <button id="cube">cube</button>
- <button id="cylinder">cylinder</button>
- <button id="multiple">multiple</button>
- <button id="transformed">transformed</button><br /><br />
- <button id="export">export to obj</button>
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { OBJExporter } from './jsm/exporters/OBJExporter.js';
- var camera, scene, light, renderer;
- var exportButton, floatingDiv;
- var mouseX = 0, mouseY = 0;
- function exportToObj() {
- var exporter = new OBJExporter();
- var result = exporter.parse( scene );
- floatingDiv.style.display = 'block';
- floatingDiv.innerHTML = result.split( '\n' ).join( '<br />' );
- }
- function addGeometry( type ) {
- for ( var i = 0; i < scene.children.length; i ++ ) {
- var child = scene.children[ i ];
- if ( child.isMesh ) {
- child.geometry.dispose();
- scene.remove( child );
- i --;
- }
- }
- if ( type === 1 ) {
- var material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
- var geometry = generateTriangleGeometry();
- scene.add( new THREE.Mesh( geometry, material ) );
- } else if ( type === 2 ) {
- var material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
- var geometry = new THREE.BoxBufferGeometry( 100, 100, 100 );
- scene.add( new THREE.Mesh( geometry, material ) );
- } else if ( type === 3 ) {
- var material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
- var geometry = new THREE.CylinderBufferGeometry( 50, 50, 100, 30, 1 );
- scene.add( new THREE.Mesh( geometry, material ) );
- } else if ( type === 4 || type === 5 ) {
- var material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
- var geometry = generateTriangleGeometry();
- var mesh = new THREE.Mesh( geometry, material );
- mesh.position.x = - 200;
- scene.add( mesh );
- var geometry2 = new THREE.BoxBufferGeometry( 100, 100, 100 );
- var mesh2 = new THREE.Mesh( geometry2, material );
- scene.add( mesh2 );
- var geometry3 = new THREE.CylinderBufferGeometry( 50, 50, 100, 30, 1 );
- var mesh3 = new THREE.Mesh( geometry3, material );
- mesh3.position.x = 200;
- scene.add( mesh3 );
- if ( type === 5 ) {
- mesh.rotation.y = Math.PI / 4.0;
- mesh2.rotation.y = Math.PI / 4.0;
- mesh3.rotation.y = Math.PI / 4.0;
- }
- }
- }
- function init() {
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.set( 0, 0, 400 );
- scene = new THREE.Scene();
- light = new THREE.DirectionalLight( 0xffffff );
- scene.add( light );
- addGeometry( 1 );
- window.addEventListener( 'click', onWindowClick, false );
- window.addEventListener( 'resize', onWindowResize, false );
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- document.addEventListener( 'mouseover', onDocumentMouseMove, false );
- document.getElementById( 'triangle' ).addEventListener( 'click', function () {
- addGeometry( 1 );
- } );
- document.getElementById( 'cube' ).addEventListener( 'click', function () {
- addGeometry( 2 );
- } );
- document.getElementById( 'cylinder' ).addEventListener( 'click', function () {
- addGeometry( 3 );
- } );
- document.getElementById( 'multiple' ).addEventListener( 'click', function () {
- addGeometry( 4 );
- } );
- document.getElementById( 'transformed' ).addEventListener( 'click', function () {
- addGeometry( 5 );
- } );
- exportButton = document.getElementById( 'export' );
- exportButton.addEventListener( 'click', function () {
- exportToObj();
- } );
- floatingDiv = document.createElement( 'div' );
- floatingDiv.className = 'floating';
- document.body.appendChild( floatingDiv );
- }
- function onWindowClick( event ) {
- var needToClose = true;
- var target = event.target;
- while ( target !== null ) {
- if ( target === floatingDiv || target === exportButton ) {
- needToClose = false;
- break;
- }
- target = target.parentElement;
- }
- if ( needToClose ) {
- floatingDiv.style.display = 'none';
- }
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function onDocumentMouseMove( event ) {
- var windowHalfX = window.innerWidth / 2;
- var windowHalfY = window.innerHeight / 2;
- mouseX = ( event.clientX - windowHalfX ) / 2;
- mouseY = ( event.clientY - windowHalfY ) / 2;
- }
- function animate() {
- requestAnimationFrame( animate );
- camera.position.x += ( mouseX - camera.position.x ) * 0.05;
- camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
- camera.lookAt( scene.position );
- light.position.set( camera.position.x, camera.position.y, camera.position.z ).normalize();
- renderer.render( scene, camera );
- }
- function generateTriangleGeometry() {
- var geometry = new THREE.BufferGeometry();
- var vertices = [];
- vertices.push( - 50, - 50, 0 );
- vertices.push( 50, - 50, 0 );
- vertices.push( 50, 50, 0 );
- geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
- geometry.computeVertexNormals();
- return geometry;
- }
- init();
- animate();
- </script>
- </body>
- </html>
|