-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.html
190 lines (187 loc) · 6.73 KB
/
exporter.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!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">
<style>
body {
font-family: Monospace;
background-color: #000000;
margin: 0px;
overflow: hidden;
}
#info {
color: #fff;
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 100;
display: block;
}
div.floating {
color : #ffffff;
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;
}
span.link {
color: skyblue;
cursor: pointer;
text-decoration : underline;
}
a {
color: skyblue
}
</style>
</head>
<body>
<div id="info">
<a href="http://threejs.org" target="_blank">three.js</a>
- geometries:
<span class="link" id="triangle">triangle</span>,
<span class="link" id="cube">cube</span>,
<span class="link" id="cylinder">cylinder</span>,
<span class="link" id="both">both</span>,
<span class="link" id="transformed">transformed</span>
- <span class="link" id="export">export to obj</span>
</div>
<script src="./index_files/THREE/three.js"></script>
<script src="./OBJExporter.js"></script>
<script>
var camera, scene, light, renderer;
var exportButton, floatingDiv;
var mouseX = 0, mouseY = 0;
function exportToObj() {
var exporter = new THREE.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 current = scene.children[ i ];
if( current instanceof THREE.Mesh ) {
current.geometry.dispose();
scene.remove( current );
i--;
}
}
if ( type === 1 ) {
var material = new THREE.MeshLambertMaterial( { color : 0x00cc00 } );
var geometry = new THREE.Geometry();
geometry.vertices.push( new THREE.Vector3( -50, -50, 0 ) );
geometry.vertices.push( new THREE.Vector3( 50, -50, 0 ) );
geometry.vertices.push( new THREE.Vector3( 50, 50, 0 ) );
var face = new THREE.Face3( 0, 1, 2 );
geometry.faces.push( face );
geometry.computeFaceNormals();
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 = new THREE.Geometry();
geometry.vertices.push( new THREE.Vector3( -50, -50, 0 ) );
geometry.vertices.push( new THREE.Vector3( 50, -50, 0 ) );
geometry.vertices.push( new THREE.Vector3( 50, 50, 0 ) );
var face = new THREE.Face3( 0, 1, 2 );
geometry.faces.push( face );
geometry.computeFaceNormals();
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( 'both' ).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 ) * .05;
camera.position.y += ( -mouseY - camera.position.y ) * .05;
camera.lookAt( scene.position );
light.position.set( camera.position.x, camera.position.y, camera.position.z ).normalize();
renderer.render( scene, camera );
}
init();
animate();
</script>
</body>
</html>