-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathextruder.js
127 lines (117 loc) · 5.39 KB
/
extruder.js
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
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {Util} from "./util.js";
class Extruder {
constructor(coords) {
this.coords = coords;
}
/**
* Returns a THREE.Mesh (subclass of THREE.Object3D) instance which represents an extrusion
* of a polygonal geographic feature.
* @param {Object} feature A polygon geographic feature of a footprint.
* @param {number} numberOfLevels
* @param {Object} options
* @returns {Object} a new THREE.Mesh instance, or null if any errors are encountered
*
* The depth of the extrusion can be determined, confusingly, in several ways:
* 1. if options['extrudeDepth'] is present, extrude depth is set to it;
* note that options['extrudeDepth'] should be < 0.
* 2. otherwise, if the feature has a property named 'height', extrude depth
* is set to -feature.properties['height'] (note this property value can
* be a string and will be parsed into a floating point number)
* 3. otherwise, if numberOfLevels > 0, extrude depth is set to
* -numberOfLevels * options['averageStoreyHeightMeters'],
* 4. otherwise, extrude depth is set to -options['minimumExtrusionMeters']
*/
extrudeFeature(feature, numberOfLevels, options) {
try{
options = options || {};
const shape = this.featureToSceneCoordsShape(feature);
if (!('averageStoreyHeightMeters' in options)) {
throw new Error("Extruder.extrudeFeature: options['averageStoreyHeightMeters'] is required");
}
if (!('minimumExtrusionMeters' in options)) {
throw new Error("Extruder.extrudeFeature: options['minimumExtrusionMeters'] is required");
}
if (!('brightnessOfExtrudedModels' in options)) {
throw new Error("Extruder.extrudeFeature: options['brightnessOfExtrudedModels'] is required");
}
if (!('colorVariationOfExtrudedModels' in options)) {
throw new Error("Extruder.extrudeFeature: options['colorVariationOfExtrudedModels'] is required");
}
const MINIMUM_EXTRUSION_METERS = 0.01;
const extrudeSettings = {
depth: numberOfLevels > 0 ? -numberOfLevels * options['averageStoreyHeightMeters'] :
-options['minimumExtrusionMeters'],
bevelEnabled: false
};
if (feature.properties['height']) {
extrudeSettings.depth = -parseFloat(feature.properties['height']);
}
if (options.extrudeDepth) {
extrudeSettings.depth = options.extrudeDepth;
}
const mesh = this.shapeToMesh(shape, extrudeSettings, options);
// NOTE: id of this feature is feature.properties.id; use that later to track this object
mesh.name = feature.properties.id;
// TODO: set visibility based on current year and the values of
// feature.properties.start_date and feature.properties.end_date
mesh.visible = true;
return mesh;
} catch (e) {
console.log('Error while loading feature '+ feature.id + ': ' +e);
}
return null;
}
/**
* Converts a geojson feature to a THREE.Shape instance in scene coords.
* @param feature {Objet} a geojson object
* @return {THREE.Shape} a new THREE.Shape instance
*/
featureToSceneCoordsShape(feature) {
// Convert the coordinate array in the feature to an array of THREE.Vector2 containing lon,lat in degrees.
// Note this only uses the first array (outer ring? first polygon?) in the coordinates array.
// TODO: generalize this to incorporate all coordinates.
const lonLatDegreesArray = feature.geometry.coordinates[0].map(lonLatDegrees => new THREE.Vector2(lonLatDegrees[0], lonLatDegrees[1]));
// Convert to scene coords
const sceneCoordsArray = lonLatDegreesArray.map(lonLatDegrees => this.coords.lonLatDegreesToSceneCoords(lonLatDegrees));
// Create the shape
return new THREE.Shape(sceneCoordsArray);
}
/**
* Extrudes a shape to create a Mesh.
* @param {!THREE.Shape} shape
* @param {!Object} extrudeSettings
* @param {?Object} options
* @return {!THREE.Mesh}
*/
shapeToMesh(shape, extrudeSettings, options) {
const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);
// Nudge extruded objects up off the ground plane just a tad; this prevents objects
// with height 0 from flickering due to being exactly coplanar with the ground.
geometry.vertices.forEach(v => {
v.z -= 0.02;
});
options = options || {};
const mat = new THREE.MeshPhongMaterial({
color: options.color ? options.color : Util.generateRandomGreyColor(options['brightnessOfExtrudedModels'],
options['colorVariationOfExtrudedModels']),
side: options.side == null ? THREE.DoubleSide : options.side
});
const mesh = THREE.SceneUtils.createMultiMaterialObject(geometry, [mat]);
mesh.rotation.x = 90 * (Math.PI / 180);
return mesh;
}
}
export {Extruder};