-
Notifications
You must be signed in to change notification settings - Fork 0
/
lowPoly.html
349 lines (300 loc) · 13.8 KB
/
lowPoly.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<title>Low poly terrain using mesh geometry - 4.14</title>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>
<link rel="stylesheet" href="https://js.arcgis.com/4.14/esri/themes/light/main.css" />
<script src="https://js.arcgis.com/4.14/"></script>
<script>
require([
"esri/Map",
"esri/views/SceneView",
"esri/Graphic",
"esri/geometry/Extent",
"esri/geometry/Point",
"esri/geometry/Mesh",
"esri/geometry/SpatialReference",
"esri/geometry/support/MeshComponent",
"esri/geometry/support/meshUtils",
"esri/widgets/Search",
"esri/geometry/support/webMercatorUtils",
"esri/widgets/Sketch/SketchViewModel",
"esri/layers/GraphicsLayer"
], function (
Map,
SceneView,
Graphic,
Extent,
Point,
Mesh,
SpatialReference,
MeshComponent,
meshUtils,
Search,
webMercatorUtils,
SketchViewModel,
GraphicsLayer
) {
/**************************************************
* This sample shows how to create a low-poly terrain
* from the ground elevation and color it based on the
* height.
* It also adds random trees with low-poly tree 3D models
* that were previously published in ArcGIS Pro. It uses
* the elevationSampler to synchronously get the height
* values at which the trees should be placed.
***************************************************/
// The resolution at which the terrain is sampled to create the mesh
const demResolution = 125;
// The factor to exaggerate height values
const exaggeration = 20;
let coordinates = webMercatorUtils.geographicToWebMercator(
new Point({ y: 41.391256, x: -70.632368 })
);
// console.log("coordinates", coordinates);
const extent = new Extent({
xmax: coordinates.x + 25000,
xmin: coordinates.x - 25000,
ymax: coordinates.y + 15000,
ymin: coordinates.y - 15000,
spatialReference: SpatialReference.WebMercator
});
const map = new Map({
ground: "world-elevation"
});
const view = new SceneView({
container: "viewDiv",
map: map,
viewingMode: "local",
clippingArea: extent
});
// view.watch("camera", (c) => {
// console.log(JSON.stringify(c));
// });
const searchWidget = new Search({
view: view
});
// Adds the search widget below other elements in
// the top left corner of the view
view.ui.add(searchWidget, {
position: "top-right",
index: 0
});
// Enable shadows so that the terrain mesh and trees cast shadows
view.environment.lighting.directShadowsEnabled = true;
const tentBtn = document.getElementById("tent");
view
.when((e) => {
map.ground.navigationConstraint = {
type: "none"
};
view.goTo({ "position": { "spatialReference": { "latestWkid": 3857, "wkid": 102100 }, "x": -7864198.086898301, "y": 5060191.294430446, "z": 1275.8877099690042 }, "heading": 7.161972439051331, "tilt": 82.35111359019972 }, { animate: false });
// view.goTo(coordinates, { animate: false });
// console.log("going to", coordinates);
view.watch("camera", (c) => {
console.log(JSON.stringify(c));
})
const graphicsLayer = new GraphicsLayer({
if: "modelLayer"
});
view.map.add(graphicsLayer);
const sketchVM = new SketchViewModel({
layer: graphicsLayer,
view: view
});
tentBtn.addEventListener("click", function (event) {
// reference the relative path to the glTF model
// in the resource of an ObjectSymbol3DLayer
sketchVM.pointSymbol = {
type: "point-3d",
symbolLayers: [
{
type: "object",
resource: {
href:
"./tent.glb"
}
}
]
};
// Search for graphics at the clicked location. View events can be used
// as screen locations as they expose an x,y coordinate that conforms
// to the ScreenPoint definition.
// view.hitTest(event).then(function(response) {
console.log("click", event);
sketchVM.create("point");
});
sketchVM.on("create", function (event) {
if (event.state === "complete") {
sketchVM.update(event.graphic);
}
});
view.ui.add("paneDiv", "top-right");
})
.then(createTerrain)
//.then(createTrees)
.catch(console.error);
function createTerrain() {
// Use createFromElevation to generate a mesh geometry
// from a given elevation layer, for a certain extent
// and at a custom resolution
return meshUtils
.createFromElevation(map.ground.layers.getItemAt(0), extent, {
demResolution: demResolution
})
.then(function (mesh) {
const vPositions = mesh.vertexAttributes.position;
// Every third element in the positions array represents the height value
// [x1, y1, z1, x2, y2, z2, ...]
for (let index = 0; index < vPositions.length; index += 3) {
// Modify the height position by applying the exaggeration
vPositions[index + 2] = vPositions[index + 2] * exaggeration;
if (vPositions[index + 2] < 100) {
vPositions[index + 2] = -300;
}
}
// Set the shading of the component to flat, to see the generated triangles
// and generate the low-poly effect.
// Change shading to "smooth" for the terrain to be continuously interpolated
mesh.components[0].shading = "flat";
// Generate the texture with the colors for the terrain mesh
// based on the height values
mesh.components[0].material = {
colorTexture: createTerrainTexture(mesh)
};
// Create the graphic using the mesh geometry and a MeshSymbol3D
const graphic = new Graphic({
geometry: mesh,
symbol: {
type: "mesh-3d",
symbolLayers: [
{
type: "fill"
}
]
}
});
view.graphics.add(graphic);
return mesh;
});
}
// Create a texture to be mapped on the mesh geometry
function createTerrainTexture(mesh) {
const vPositions = mesh.vertexAttributes.position;
// Calculate the width and height of the canvas
// based on the number of vertices and the extent aspect ratio
const vertexCount = vPositions.length / 3;
const w = Math.round(
Math.sqrt((vertexCount * extent.width) / extent.height)
);
const h = Math.round(vertexCount / w);
// Create a 2D canvas and assign the colors for each pixel
// based on the height value of the mesh vertices
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
canvas.width = w;
canvas.height = h;
// Set height based color values in the
// ImageData(https://developer.mozilla.org/en-US/docs/Web/API/ImageData)
// of the canvas
const imgData = ctx.createImageData(w, h);
let j = 0;
for (let i = 0; i < vPositions.length; i += 3) {
const color = getColor(vPositions[i + 2]);
imgData.data[j + 0] = color[0]; // r
imgData.data[j + 1] = color[1]; // g
imgData.data[j + 2] = color[2]; // b
imgData.data[j + 3] = 255; // alpha
j += 4;
}
ctx.putImageData(imgData, 0, 0);
return canvas;
}
// Returns a color based on height
function getColor(height) {
if (height < 10 * exaggeration) {
return [71, 103, 61];
// return [149, 195, 50]; // green
} else {
if (height < 100 * exaggeration) {
return [219, 200, 163];
// return [209, 148, 81]; // brown
}
}
return [255, 255, 255]; // white
}
function createTrees(mesh) {
const treeXYCoordinates = [
[949511.0269866717, 5996737.260347401],
[948349.0915666509, 5995392.784148848],
[948134.9365087302, 5995257.3964663735],
[947677.0638892008, 5995549.099657962],
[948533.132906933, 5996579.031805388],
[948164.69040468, 5998378.064842778],
[947679.8323087967, 5996458.904069111],
[949905.6697365673, 5995740.200877652],
[949291.5109791199, 5997380.737017862],
[948419.5816452013, 5994747.508043459],
[949421.7202507699, 5998165.359250989]
];
// Create an elevationSampler to synchronously query the elevation
// of the mesh at the tree locations
return meshUtils
.createElevationSampler(mesh)
.then(function (elevationSampler) {
for (let i = 0; i < treeXYCoordinates.length; i++) {
// Generate a 2D point within the extent of the SceneView
const tree = new Point({
x: treeXYCoordinates[i][0],
y: treeXYCoordinates[i][1],
spatialReference: extent.spatialReference
});
// Query elevation and apply exaggeration
const tree3D = elevationSampler.queryElevation(tree);
const symbol = {
type: "point-3d",
symbolLayers: [
{
type: "object",
resource: {
// The tree model was imported to Pro and published as a WebStyle
// See details on how to do this here: http://pro.arcgis.com/en/pro-app/help/sharing/overview/share-a-web-style.htm
href:
"https://jsapi.maps.arcgis.com/sharing/rest/content/items/4418035fa87d44f490d5bf27a579e118/resources/styles/web/resource/tree.json"
},
height: 200,
heading: Math.random() * 360,
anchor: "bottom"
}
]
};
view.graphics.add(
new Graphic({
geometry: tree3D,
symbol: symbol
})
);
}
});
}
});
</script>
</head>
<body>
<div id="viewDiv"></div>
<div id="paneDiv" class="esri-widget">
<button id="tent" class="esri-button">Tent</button><br />
</div>
</body>
</html>