HFT.js 5.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14

// specifiy credentials and assets from Cesium ION account
Cesium.Ion.defaultAccessToken =
	"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJiYjY5ZTAxNy03YTc0LTQyZTYtYjJlMC0xYzYwNTAzNDQ0ZjUiLCJpZCI6MjkwNCwiaWF0IjoxNjM1NDEzOTI0fQ.Xhmt0sD4Dda4Q46FBYew4wPbqlJ4T8U9n1nNNwGyH7o";

// setting up Cesium Viewer
var viewer = new Cesium.Viewer("cesiumContainer", {
	imageryProvider: new Cesium.IonImageryProvider({ assetId: 3 }), //3 bing maps with labels // use Sentinel2 imagery as default assetID:3954
	/////////////////////////////////////////
	// Note the next 3 lines specify that the Cesium Terrain should be used. ION account is needed
	// For a 'flat' Earth the z-offsets below for each of the tilesets needs to be adjusted
	// comment the next 3 lines to remove the Cesium terrain (Resulution approx. 30m)
	/////////////////////////////////////////
	terrainProvider: new Cesium.CesiumTerrainProvider({
15
		url: Cesium.IonResource.fromAssetId(1),
16
17
18
19
20
	}),
});


// set home button extend
21
var extent = Cesium.Rectangle.fromDegrees(9.162794728779428, 48.78605872069245, 9.181827683763427, 48.77616123564855);
22
23
24
25
26
27
28
29
30
31
32
Cesium.Camera.DEFAULT_VIEW_RECTANGLE = extent;
Cesium.Camera.DEFAULT_VIEW_FACTOR = 0;




viewer.scene.globe.enableLighting = true; // set lighting to true

// define a function to zoom to the tileset (invoke later)
var zoomAll = function (tileset) {
	return new Promise(function (resolve, reject) {
33
		if (!tileset) {
34
			reject("Tileset is undifined");
35
36
		}
		viewer.camera.viewBoundingSphere(
37
38
			tileset.boundingSphere,
			new Cesium.HeadingPitchRange(0, -0.5, 400)
39
40
41
		);
		viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
		resolve();
42
43
44
45
46
47
48
49
50
51
	});
};

//////////////////////////// load 3d building tiles or OSM 3d Buildings ///////////////////////
// here is the switch to switch between different 3d buildings 
const LOAD_3DTILES = true;

if (LOAD_3DTILES) {
	// load 3d Tile set of SToeckach.
	var tileset = viewer.scene.primitives.add(
52
		new Cesium.Cesium3DTileset({
53
			url: Cesium.IonResource.fromAssetId(656401),
54
		})
55
56
57
58
59
60
	);

	//////// important value. to find the correct value trail and error is needed for a perfect fit
	const BUILDIG_TILESET_HEIGHT_OFFSET = 54;

	tileset.readyPromise.then(function (tileset) {
61
62
		var height = BUILDIG_TILESET_HEIGHT_OFFSET;
		var cartographic = Cesium.Cartographic.fromCartesian(
63
			tileset.boundingSphere.center
64
65
		);
		var surface = Cesium.Cartesian3.fromRadians(
66
67
68
			cartographic.longitude,
			cartographic.latitude,
			0.0
69
70
		);
		var offset = Cesium.Cartesian3.fromRadians(
71
72
73
			cartographic.longitude,
			cartographic.latitude,
			height
74
75
		);
		var translation = Cesium.Cartesian3.subtract(
76
77
78
			offset,
			surface,
			new Cesium.Cartesian3()
79
80
81
		);
		// now shift / translate the tileset by the translation vector defined above
		tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
82

83
		//return zoomAll(tileset); // zoom or rather go to the translated tileset
84
85
86
87
88
89
90
91
92
	});
}
/////////////////////////////////////////////////////////////////////
// Alternatively, instead of using the Stoeckach LOD1 Building 3d Tiles,
//  use OSM Buildings from Cesium ION assets - in this case set LOAD_3DTILES variable in Line 41 to 'false'
////////////////////////////////////////////////////////
else {
	var osmBuildings = Cesium.createOsmBuildings();
	osmBuildings.readyPromise
93
		.then(function (osmBuildings) {
94
			viewer.scene.primitives.add(osmBuildings);
95
96
		})
		.otherwise(function (error) {
97
			console.log(error);
98
		});
99
100
101
102

	// now zoom to target and set camera view angle to some oblique angle: use '-Cesium.Math.PI_OVER_TWO' to look down nadir.
	// if you use '+Cesium.Math.PI_OVER_TWO', you look into space.
	viewer.camera.setView({
103
104
		destination: Cesium.Cartesian3.fromDegrees(
			9.172183958234173,
105
106
			48.78029680030391,
			20000
107
108
		),
		orientation: {
109
110
111
			heading: 0.0,
			pitch: -Cesium.Math.PI_OVER_TWO, // set an oblique viewing angle
			roll: 0.0,
112
		},
113
114
115
116
117
118
119
120
121
122
123
124
125
126
	});
}

tileset.style = new Cesium.Cesium3DTileStyle({
	show: true
});

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
// HFT PointCloud Tileset.
////////////////////////////////////////////////////////////////////////////////////

const PCOFFSET = 0;

127
var hftLargePC = viewer.scene.primitives.add(
128
129
130
131
132
	new Cesium.Cesium3DTileset({
		url: Cesium.IonResource.fromAssetId(655879),
	})
);

133
hftLargePC.readyPromise.then(function (hftLargePC) {
134
135
	var height = PCOFFSET;
	var cartographic = Cesium.Cartographic.fromCartesian(
136
		hftLargePC.boundingSphere.center
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
	);
	var surface = Cesium.Cartesian3.fromRadians(
		cartographic.longitude,
		cartographic.latitude,
		0.0
	);
	var offset = Cesium.Cartesian3.fromRadians(
		cartographic.longitude,
		cartographic.latitude,
		height
	);
	var translation = Cesium.Cartesian3.subtract(
		offset,
		surface,
		new Cesium.Cartesian3()
	);
	// now shift / translate the tileset by the translation vector defined above
154
	hftLargePC.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
155

156
	return zoomAll(hftLargePC); // zoom or rather go to the translated tileset
157
158
});

159
hftLargePC.style = new Cesium.Cesium3DTileStyle({
160
161
162
163
	color: "color('blue',0.3)",
	// color: "color('red')" ,
	pointSize: 3
});
164
165
166
167




168
///////////////////////////// innenhof PC
169

170
var hftInnehofRGB_PC = viewer.scene.primitives.add(
171
	new Cesium.Cesium3DTileset({
172
		url: Cesium.IonResource.fromAssetId(656854),
173
	})
174
175
);

176
hftInnehofRGB_PC.style = new Cesium.Cesium3DTileStyle({
177
178
	// color: "color('blue',0.3)",	
	// color: "color('red')" ,
179
180
181
182
183
184
185
	pointSize: 3
});



////////////////////////// Control Pointclouds and Tileset with switches

186
187
188
189
190
191
192
var toggleLargePC = function () {
	if (hftLargePC.show === true) {
		hftLargePC.show = false
	}
	else {
		hftLargePC.show = true
	}
193
}
194
195
196
// or use tenrary operator as for the other two 
var toggleRGBPC = function () {
	hftInnehofRGB_PC.show = hftInnehofRGB_PC.show ? false : true;
197
198
}

199
200
var toggleBuildingTiles = function () {
	tileset.show = tileset.show ? false : true;
201
202
203
204
}