-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathmodel_generator.ts
More file actions
208 lines (186 loc) · 8.1 KB
/
model_generator.ts
File metadata and controls
208 lines (186 loc) · 8.1 KB
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
import * as log from 'loglevel';
import * as THREE from 'three'
import Vector from './vector';
import { CSG } from 'three-csg-ts';
import {BuildingModel} from './ui/buildings';
enum ModelGeneratorStates {
WAITING,
SUBTRACT_OCEAN,
ADD_COASTLINE,
SUBTRACT_RIVER,
ADD_ROADS,
ADD_BLOCKS,
ADD_BUILDINGS,
CREATE_ZIP,
}
export default class ModelGenerator {
private readonly groundLevel = 20; // Thickness of groundMesh
// Using THREE.extrudeGeometry with depth = 0 flips normals
// Specifying a small minimum extrusion depth prevents issues with Boolean modifiers in Blender
private readonly minExtrudeDepth = 0.01;
private readonly exportSTL = require('threejs-export-stl');
private resolve: (blob: any) => void = b => {};
private zip: any;
private state: ModelGeneratorStates = ModelGeneratorStates.WAITING;
private groundMesh: THREE.Mesh;
private groundBsp: CSG;
private polygonsToProcess: Vector[][] = [];
private roadsGeometry = new THREE.Geometry();
private blocksGeometry = new THREE.Geometry();
private roadsBsp: CSG;
private buildingsGeometry = new THREE.Geometry();
private buildingsToProcess: BuildingModel[];
constructor(private ground: Vector[],
private sea: Vector[],
private coastline: Vector[],
private river: Vector[],
private mainRoads: Vector[][],
private majorRoads: Vector[][],
private minorRoads: Vector[][],
private buildings: BuildingModel[],
private blocks: Vector[][]) {
}
public async getSTL(): Promise<any> {
return new Promise<any>(resolve => {
this.resolve = resolve;
const JSZip = require("jszip");
this.zip = new JSZip();
this.zip.file("model/README.txt", "For a tutorial on putting these models together to create a city, go to https://maps.probabletrain.com/#/stl");
this.groundMesh = this.polygonToMesh(this.ground, this.groundLevel);
this.groundBsp = CSG.fromMesh(this.groundMesh);
this.setState(ModelGeneratorStates.SUBTRACT_OCEAN);
});
}
private setState(s: ModelGeneratorStates): void {
this.state = s;
log.info(ModelGeneratorStates[s]);
}
/**
* Return true if processing a model
* Work done in update loop so main thread isn't swamped
*/
public update(): boolean {
switch(this.state) {
case ModelGeneratorStates.WAITING: {
return false;
}
case ModelGeneratorStates.SUBTRACT_OCEAN: {
const seaLevelMesh = this.polygonToMesh(this.ground, this.minExtrudeDepth);
this.threeToBlender(seaLevelMesh);
const seaLevelSTL = this.exportSTL.fromMesh(seaLevelMesh);
this.zip.file("model/domain.stl", seaLevelSTL);
const seaMesh = this.polygonToMesh(this.sea, this.minExtrudeDepth);
this.threeToBlender(seaMesh);
const seaMeshSTL = this.exportSTL.fromMesh(seaMesh);
this.zip.file("model/sea.stl", seaMeshSTL);
this.setState(ModelGeneratorStates.ADD_COASTLINE);
break;
}
case ModelGeneratorStates.ADD_COASTLINE: {
const coastlineMesh = this.polygonToMesh(this.coastline, this.minExtrudeDepth);
this.threeToBlender(coastlineMesh);
const coastlineSTL = this.exportSTL.fromMesh(coastlineMesh);
this.zip.file("model/coastline.stl", coastlineSTL);
this.setState(ModelGeneratorStates.SUBTRACT_RIVER);
break;
}
case ModelGeneratorStates.SUBTRACT_RIVER: {
const riverMesh = this.polygonToMesh(this.river, this.minExtrudeDepth);
this.threeToBlender(riverMesh);
const riverSTL = this.exportSTL.fromMesh(riverMesh);
this.zip.file("model/river.stl", riverSTL);
this.setState(ModelGeneratorStates.ADD_ROADS);
this.polygonsToProcess = this.minorRoads.concat(this.majorRoads).concat(this.mainRoads);
break;
}
case ModelGeneratorStates.ADD_ROADS: {
if (this.polygonsToProcess.length === 0) {
const mesh = new THREE.Mesh(this.roadsGeometry);
this.threeToBlender(mesh);
const buildingsSTL = this.exportSTL.fromMesh(mesh);
this.zip.file("model/roads.stl", buildingsSTL);
this.setState(ModelGeneratorStates.ADD_BLOCKS);
this.polygonsToProcess = [...this.blocks];
break;
}
const road = this.polygonsToProcess.pop();
const roadsMesh = this.polygonToMesh(road, this.minExtrudeDepth);
this.roadsGeometry.merge(roadsMesh.geometry as THREE.Geometry, this.groundMesh.matrix);
break;
}
case ModelGeneratorStates.ADD_BLOCKS: {
if (this.polygonsToProcess.length === 0) {
const mesh = new THREE.Mesh(this.blocksGeometry);
this.threeToBlender(mesh);
const blocksSTL = this.exportSTL.fromMesh(mesh);
this.zip.file("model/blocks.stl", blocksSTL);
this.setState(ModelGeneratorStates.ADD_BUILDINGS);
this.buildingsToProcess = [...this.buildings];
break;
}
const block = this.polygonsToProcess.pop();
const blockMesh = this.polygonToMesh(block, 1);
this.blocksGeometry.merge(blockMesh.geometry as THREE.Geometry, this.groundMesh.matrix);
break;
}
case ModelGeneratorStates.ADD_BUILDINGS: {
if (this.buildingsToProcess.length === 0) {
const mesh = new THREE.Mesh(this.buildingsGeometry);
this.threeToBlender(mesh);
const buildingsSTL = this.exportSTL.fromMesh(mesh);
this.zip.file("model/buildings.stl", buildingsSTL);
this.setState(ModelGeneratorStates.CREATE_ZIP);
break;
}
const b = this.buildingsToProcess.pop();
const buildingMesh = this.polygonToMesh(b.lotScreen, b.height);
this.buildingsGeometry.merge(buildingMesh.geometry as THREE.Geometry, this.groundMesh.matrix);
break;
}
case ModelGeneratorStates.CREATE_ZIP: {
this.zip.generateAsync({type:"blob"}).then((blob: any) => this.resolve(blob));
this.setState(ModelGeneratorStates.WAITING);
break;
}
default: {
break;
}
}
return true;
}
/**
* Rotate and scale mesh so up is in the right direction
*/
private threeToBlender(mesh: THREE.Object3D): void {
mesh.scale.multiplyScalar(0.02);
mesh.updateMatrixWorld(true);
}
/**
* Extrude a polygon into a THREE.js mesh
*/
private polygonToMesh(polygon: Vector[], height: number): THREE.Mesh {
if (polygon.length < 3) {
log.error("Tried to export empty polygon as OBJ");
return null;
}
const shape = new THREE.Shape();
shape.moveTo(polygon[0].x, polygon[0].y);
for (let i = 1; i < polygon.length; i++) {
shape.lineTo(polygon[i].x, polygon[i].y);
}
shape.lineTo(polygon[0].x, polygon[0].y);
if (height === 0) {
return new THREE.Mesh(new THREE.ShapeGeometry(shape));
}
const extrudeSettings = {
steps: 1,
depth: height,
bevelEnabled: false,
};
const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);
const mesh = new THREE.Mesh(geometry);
// mesh.translateZ(-height);
mesh.updateMatrixWorld(true);
return mesh;
}
}