-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.js
More file actions
166 lines (142 loc) · 4.99 KB
/
Copy pathmain.js
File metadata and controls
166 lines (142 loc) · 4.99 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
/*jslint browser: true*/
/*global Tangram, gui */
(function () {
'use strict';
var map_start_location = [40.70531887544228, -74.00976419448853, 16]; // NYC
/*** URL parsing ***/
// leaflet-style URL hash pattern:
// #[zoom],[lat],[lng]
var url_hash = window.location.hash.slice(1, window.location.hash.length).split('/');
if (url_hash.length == 3) {
map_start_location = [url_hash[1],url_hash[2], url_hash[0]];
// convert from strings
map_start_location = map_start_location.map(Number);
}
/*** Map ***/
var map = L.map('map', {
maxZoom: 20,
minZoom: 1,
inertia: false,
keyboard: true,
keyboardZoomOffset: .05
});
var layer = Tangram.leafletLayer({
scene: 'scene.yaml',
attribution: '<a href="https://github.com/tangrams/tangram" target="_blank">Tangram</a> | © OSM contributors'
});
window.layer = layer;
var scene = layer.scene;
window.scene = scene;
// setView expects format ([lat, long], zoom)
map.setView(map_start_location.slice(0, 3), map_start_location[2]);
var hash = new L.Hash(map);
// Resize map to window
function resizeMap() {
document.getElementById('map').style.width = window.innerWidth + 'px';
document.getElementById('map').style.height = window.innerHeight + 'px';
map.invalidateSize(false);
}
window.addEventListener('resize', resizeMap);
/***** GUI/debug controls *****/
// Create dat GUI
var gui = new dat.GUI({ autoPlace: true });
function addGUI () {
gui.domElement.parentNode.style.zIndex = 500;
window.gui = gui;
// Camera
var camera_types = {
'Flat': 'flat',
'Perspective': 'perspective',
'Isometric': 'isometric'
};
gui.camera = scene.config.camera.type;
gui.add(gui, 'camera', camera_types).onChange(function(value) {
scene.config.camera.type = value;
scene.updateConfig();
});
// Layers
var layer_gui = gui.addFolder('Layers');
var layer_colors = {};
var layer_controls = {};
Object.keys(scene.config.layers).forEach(function(l) {
if (!scene.config.layers[l]) {
return;
}
layer_controls[l] = !(scene.config.layers[l].visible == false);
layer_gui.
add(layer_controls, l).
onChange(function(value) {
scene.config.layers[l].visible = value;
scene.rebuild();
});
try {
var c = scene.config.layers[l].draw.polygons.color;
}
catch(e) {
var c = scene.config.layers[l].draw.lines.color;
}
layer_colors[l] = [c[0]*255, c[1]*255, c[2]*255];
layer_gui.
addColor(layer_colors, l).
onChange(function(value) {
try {
scene.config.layers[l].draw.polygons.color = [value[0]/255, value[1]/255, value[2]/255];
}
catch(e) {
scene.config.layers[l].draw.lines.color = [value[0]/255, value[1]/255, value[2]/255];
}
scene.rebuild();
});
});
layer_gui.open();
// Lighting
var light_gui = gui.addFolder('Light');
var light_controls = {
"x position": .3,
"y position": .5,
"z position": .5,
"diffuse": 1,
"ambient": .5
};
light_gui.
add(light_controls, "x position", -1, 1).
onChange(function(value) {
scene.lights.light1.direction[0] = -value;
scene.requestRedraw();
});
light_gui.
add(light_controls, "y position", -1, 1).
onChange(function(value) {
scene.lights.light1.direction[1] = -value;
scene.requestRedraw();
});
light_gui.
add(light_controls, "z position", 0, 1).
onChange(function(value) {
scene.lights.light1.direction[2] = -value;
scene.requestRedraw();
});
light_gui.
add(light_controls, "diffuse", 0, 2).
onChange(function(value) {
scene.lights.light1.diffuse = [value, value, value];
scene.requestRedraw();
});
light_gui.
add(light_controls, "ambient", 0, 1).
onChange(function(value) {
scene.lights.light1.ambient = [value, value, value];
scene.requestRedraw();
});
light_gui.open();
}
/***** Render loop *****/
window.addEventListener('load', function () {
// Scene initialized
layer.on('init', function() {
addGUI();
resizeMap();
});
layer.addTo(map);
});
}());