-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.html
275 lines (229 loc) · 8.86 KB
/
index.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<!DOCTYPE html>
<!--
Fork of three.js interactive voxel painter
Added WebGL and Download to STL support
-->
<html lang="en">
<head>
<title>Voxel Builder</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: #f0f0f0;
margin: 0px;
overflow: hidden;
}
</style>
<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
</head>
<body>
<script src="three.js"></script>
<script src="Projector.js"></script>
<script src="SelectionControls.js"></script>
<script src="csg/csg.js"></script>
<script src="FileSaverjs/FileSaver.min.js"></script>
<script>
var container;
var camera, scene, renderer,controls;
var plane;
var mouse, raycaster, isShiftDown = false;
var BOX_SIZE = 50;
var cubeGeometry = new THREE.BoxGeometry( BOX_SIZE,BOX_SIZE, BOX_SIZE );
var cubeMaterial = new THREE.MeshLambertMaterial( { color: 0x00ff80 } );
var objects = [];
init();
render();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
var info = document.createElement( 'div' );
info.style.position = 'absolute';
info.style.top = '10px';
info.style.width = '100%';
info.style.textAlign = 'center';
info.innerHTML = '<strong>click</strong>: add voxel, <strong>shift + click</strong>: remove voxel';
container.appendChild( info );
var menu = document.createElement( 'div' );
menu.style.position = 'absolute';
menu.style.right = '10px';
menu.style.margin = '10px';
menu.innerHTML = '<button class="btn btn-primary" onclick="save()"><span class="glyphicon glyphicon-download"></span> Download STL</button>';
container.appendChild( menu );
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set( 500, 800, 1300 );
camera.up.set( 0, 0, 1 );
camera.lookAt( new THREE.Vector3() );
scene = new THREE.Scene();
// Grid
var size = 500, step = 50;
var geometry = new THREE.Geometry();
for ( var i = - size; i <= size; i += step ) {
geometry.vertices.push( new THREE.Vector3( - size, 0, i ) );
geometry.vertices.push( new THREE.Vector3( size, 0, i ) );
geometry.vertices.push( new THREE.Vector3( i, 0, - size ) );
geometry.vertices.push( new THREE.Vector3( i, 0, size ) );
}
var material = new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2 } );
var line = new THREE.LineSegments( geometry, material );
line.rotation.set(Math.PI/2,0,0);
scene.add( line );
//
raycaster = new THREE.Raycaster();
mouse = new THREE.Vector2();
var geometry = new THREE.PlaneBufferGeometry( 1000, 1000 );
geometry.rotateX( - Math.PI / 2 );
plane = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { visible: false } ) );
plane.rotation.set(Math.PI/2,0,0);
scene.add( plane );
objects.push( plane );
var material = new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } );
// Lights
var ambientLight = new THREE.AmbientLight( 0x606060 );
scene.add( ambientLight );
var directionalLight = new THREE.DirectionalLight( 0xffffff );
directionalLight.position.x = Math.random() - 0.5;
directionalLight.position.y = Math.random() - 0.5;
directionalLight.position.z = Math.random() - 0.5;
directionalLight.position.normalize();
scene.add( directionalLight );
var directionalLight = new THREE.DirectionalLight( 0x808080 );
directionalLight.position.x = Math.random() - 0.5;
directionalLight.position.y = Math.random() - 0.5;
directionalLight.position.z = Math.random() - 0.5;
directionalLight.position.normalize();
scene.add( directionalLight );
renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setClearColor( 0xf0f0f0 );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
controls = new THREE.SelectionControls( camera, renderer.domElement );
container.appendChild(renderer.domElement);
controls.addEventListener('change',render);
controls.addEventListener('change',render);
controls.addEventListener('select-start',onDocumentMouseLeftDown);
//document.addEventListener( 'mousedown', onDocumentMouseDown, false );
document.addEventListener( 'keydown', onDocumentKeyDown, false );
document.addEventListener( 'keyup', onDocumentKeyUp, false );
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
render();
}
function addVoxel(point,normal){
var voxel = new THREE.Mesh( cubeGeometry, cubeMaterial );
point.z = Math.round(point.z);
normal.z = Math.round(normal.z);
voxel.position.copy( point ).add( normal );
voxel.position.divideScalar( BOX_SIZE ).floor().multiplyScalar( BOX_SIZE ).addScalar( BOX_SIZE/2 );
scene.add( voxel );
var edges = new THREE.EdgesHelper( voxel, 0x002200 );
edges.name = "voxel-helper-" + voxel.id;
scene.add (edges);
objects.push( voxel );
}
function removeObject(object){
var edges = scene.getObjectByName("voxel-helper-" +object.id);
scene.remove( object );
objects.splice( objects.indexOf( object ), 1 );
scene.remove(edges);
}
function mergeVoxels(voxels){
var firstPosition = [voxels[0].position.x,voxels[0].position.y,voxels[0].position.z];
var mergedModel = CSG.cube({center:firstPosition,
radius: BOX_SIZE/2});
for (var i=1;i<voxels.length;i++){
var pos = [voxels[i].position.x,voxels[i].position.y,voxels[i].position.z];
var voxel = CSG.cube({center: pos,
radius: BOX_SIZE/2});
mergedModel = mergedModel.union(voxel);
}
var polygons = mergedModel.toPolygons();
console.log(polygons);
//Map on simpler structure
var vertices = [];
var faces = [];
var indexOffset = 0;
polygons.forEach(function(ply){
for (var i=0;i<ply.vertices.length;i++){
vertices.push(new THREE.Vector3(ply.vertices[i].pos.x,ply.vertices[i].pos.y,ply.vertices[i].pos.z));
}
faces.push(new THREE.Face3(indexOffset,indexOffset+1,indexOffset+2));
faces.push(new THREE.Face3(indexOffset,indexOffset+2,indexOffset+3));
indexOffset +=ply.vertices.length;
});
return {faces:faces,vertices:vertices};
}
function onDocumentMouseLeftDown( event ) {
var content = event.content;
//event.preventDefault();
mouse.x = ( content.clientX / renderer.domElement.clientWidth ) * 2 - 1;
mouse.y = - ( content.clientY / renderer.domElement.clientHeight ) * 2 + 1;
raycaster.setFromCamera( mouse, camera );
var intersects = raycaster.intersectObjects( objects );
if ( intersects.length > 0 ) {
var intersect = intersects[ 0 ];
if ( isShiftDown ) {
if ( intersect.object != plane ) {
removeObject(intersect.object);
}
} else {
addVoxel(intersect.point,intersect.face.normal);
}
render();
}
}
function onDocumentKeyDown( event ) {
switch( event.keyCode ) {
case 16: isShiftDown = true; break;
}
}
function onDocumentKeyUp( event ) {
switch( event.keyCode ) {
case 16: isShiftDown = false; break;
}
}
function generateStlString(faces,vertices){
var modelName = "voxels";
var stl = new Array();
stl.push("solid "+modelName);
faces.forEach(function(face){
stl.push("facet normal 0 0 0");
stl.push("outer loop");
var v1 = vertices[face.a];
var v2 = vertices[face.b];
var v3 = vertices[face.c];
stl.push("vertex "+v1.x+ " "+ v1.y+" " + v1.z);
stl.push("vertex "+v2.x+ " "+ v2.y+" " + v2.z);
stl.push("vertex "+v3.x+ " "+ v3.y+" " + v3.z);
stl.push("endloop");
stl.push("endfacet");
});
stl.push('endsolid ' + modelName);
var str = stl.join("\n");
return str;
}
function save() {
if (objects.length > 1){
var geom = mergeVoxels(objects.slice(1));
console.log(geom);
var str = generateStlString(geom.faces,geom.vertices);
var blob = new Blob([str]);
saveAs(blob, "voxels.stl");
//Merge all cubes and download
//var myStlString = stlFromMeshList( objects.slice(1), {download:true} )
}
//window.open( renderer.domElement.toDataURL('image/png'), 'mywindow' );
return false;
}
function render() {
controls.update();
renderer.render( scene, camera );
}
</script>
</body>
</html>