|
@@ -5,33 +5,10 @@
|
|
|
<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>
|
|
|
- <button id="points">points</button><br /><br />
|
|
|
- <button id="export">export to obj</button>
|
|
|
+ <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - exporter - obj
|
|
|
</div>
|
|
|
|
|
|
<!-- Import maps polyfill -->
|
|
@@ -50,18 +27,74 @@
|
|
|
|
|
|
import * as THREE from 'three';
|
|
|
|
|
|
+ import { OrbitControls } from './jsm/controls/OrbitControls.js';
|
|
|
import { OBJExporter } from './jsm/exporters/OBJExporter.js';
|
|
|
+ import { GUI } from './jsm/libs/lil-gui.module.min.js';
|
|
|
+
|
|
|
+ let camera, scene, renderer;
|
|
|
+
|
|
|
+ const params = {
|
|
|
+ addTriangle: addTriangle,
|
|
|
+ addCube: addCube,
|
|
|
+ addCylinder: addCylinder,
|
|
|
+ addMultiple: addMultiple,
|
|
|
+ addTransformed: addTransformed,
|
|
|
+ addPoints: addPoints,
|
|
|
+ exportToObj: exportToObj
|
|
|
+ };
|
|
|
+
|
|
|
+ init();
|
|
|
+ animate();
|
|
|
+
|
|
|
+ function init() {
|
|
|
+
|
|
|
+ renderer = new THREE.WebGLRenderer();
|
|
|
+ renderer.outputEncoding = THREE.sRGBEncoding;
|
|
|
+ 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();
|
|
|
+
|
|
|
+ const ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
|
|
|
+ scene.add( ambientLight );
|
|
|
+
|
|
|
+ const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.8 );
|
|
|
+ directionalLight.position.set( 0, 1, 1 );
|
|
|
+ scene.add( directionalLight );
|
|
|
+
|
|
|
+ const gui = new GUI();
|
|
|
+
|
|
|
+ let h = gui.addFolder( 'Geometry Selection' );
|
|
|
+ h.add( params, 'addTriangle' ).name( 'Triangle' );
|
|
|
+ h.add( params, 'addCube' ).name( 'Cube' );
|
|
|
+ h.add( params, 'addCylinder' ).name( 'Cylinder' );
|
|
|
+ h.add( params, 'addMultiple' ).name( 'Multiple objects' );
|
|
|
+ h.add( params, 'addTransformed' ).name( 'Transformed objects' );
|
|
|
+ h.add( params, 'addPoints' ).name( 'Point Cloud' );
|
|
|
+
|
|
|
+ h = gui.addFolder( 'Export' );
|
|
|
+ h.add( params, 'exportToObj' ).name( 'Export OBJ' );
|
|
|
+
|
|
|
+ gui.open();
|
|
|
+
|
|
|
+ addGeometry( 1 );
|
|
|
|
|
|
- let camera, scene, light, renderer;
|
|
|
- let exportButton, floatingDiv;
|
|
|
- let mouseX = 0, mouseY = 0;
|
|
|
+ window.addEventListener( 'resize', onWindowResize );
|
|
|
+
|
|
|
+ const controls = new OrbitControls( camera, renderer.domElement );
|
|
|
+ controls.enablePan = false;
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
function exportToObj() {
|
|
|
|
|
|
const exporter = new OBJExporter();
|
|
|
const result = exporter.parse( scene );
|
|
|
- floatingDiv.style.display = 'block';
|
|
|
- floatingDiv.innerHTML = result.split( '\n' ).join( '<br />' );
|
|
|
+ saveString( result, 'object.obj' );
|
|
|
|
|
|
}
|
|
|
|
|
@@ -146,96 +179,57 @@
|
|
|
|
|
|
}
|
|
|
|
|
|
- function init() {
|
|
|
-
|
|
|
- renderer = new THREE.WebGLRenderer();
|
|
|
- renderer.outputEncoding = THREE.sRGBEncoding;
|
|
|
- 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 );
|
|
|
+ function addTriangle() {
|
|
|
|
|
|
addGeometry( 1 );
|
|
|
|
|
|
- window.addEventListener( 'click', onWindowClick );
|
|
|
- window.addEventListener( 'resize', onWindowResize );
|
|
|
- document.addEventListener( 'mousemove', onDocumentMouseMove );
|
|
|
- document.addEventListener( 'mouseover', onDocumentMouseMove );
|
|
|
-
|
|
|
- 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 () {
|
|
|
+ function addCube() {
|
|
|
|
|
|
- addGeometry( 4 );
|
|
|
+ addGeometry( 2 );
|
|
|
|
|
|
- } );
|
|
|
- document.getElementById( 'transformed' ).addEventListener( 'click', function () {
|
|
|
+ }
|
|
|
|
|
|
- addGeometry( 5 );
|
|
|
+ function addCylinder() {
|
|
|
|
|
|
- } );
|
|
|
- document.getElementById( 'points' ).addEventListener( 'click', function () {
|
|
|
+ addGeometry( 3 );
|
|
|
|
|
|
- addGeometry( 6 );
|
|
|
+ }
|
|
|
|
|
|
- } );
|
|
|
+ function addMultiple() {
|
|
|
|
|
|
- exportButton = document.getElementById( 'export' );
|
|
|
- exportButton.addEventListener( 'click', function () {
|
|
|
+ addGeometry( 4 );
|
|
|
|
|
|
- exportToObj();
|
|
|
+ }
|
|
|
|
|
|
- } );
|
|
|
+ function addTransformed() {
|
|
|
|
|
|
- floatingDiv = document.createElement( 'div' );
|
|
|
- floatingDiv.className = 'floating';
|
|
|
- document.body.appendChild( floatingDiv );
|
|
|
+ addGeometry( 5 );
|
|
|
|
|
|
}
|
|
|
|
|
|
- function onWindowClick( event ) {
|
|
|
+ function addPoints() {
|
|
|
|
|
|
- let needToClose = true;
|
|
|
- let target = event.target;
|
|
|
+ addGeometry( 6 );
|
|
|
|
|
|
- while ( target !== null ) {
|
|
|
+ }
|
|
|
|
|
|
- if ( target === floatingDiv || target === exportButton ) {
|
|
|
+ const link = document.createElement( 'a' );
|
|
|
+ link.style.display = 'none';
|
|
|
+ document.body.appendChild( link );
|
|
|
|
|
|
- needToClose = false;
|
|
|
- break;
|
|
|
+ function save( blob, filename ) {
|
|
|
|
|
|
- }
|
|
|
+ link.href = URL.createObjectURL( blob );
|
|
|
+ link.download = filename;
|
|
|
+ link.click();
|
|
|
|
|
|
- target = target.parentElement;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- if ( needToClose ) {
|
|
|
+ }
|
|
|
|
|
|
- floatingDiv.style.display = 'none';
|
|
|
+ function saveString( text, filename ) {
|
|
|
|
|
|
- }
|
|
|
+ save( new Blob( [ text ], { type: 'text/plain' } ), filename );
|
|
|
|
|
|
}
|
|
|
|
|
@@ -248,24 +242,10 @@
|
|
|
|
|
|
}
|
|
|
|
|
|
- function onDocumentMouseMove( event ) {
|
|
|
-
|
|
|
- const windowHalfX = window.innerWidth / 2;
|
|
|
- const 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 );
|
|
|
|
|
|
}
|
|
@@ -286,10 +266,6 @@
|
|
|
|
|
|
}
|
|
|
|
|
|
- init();
|
|
|
- animate();
|
|
|
-
|
|
|
-
|
|
|
</script>
|
|
|
|
|
|
</body>
|