I posted all the code, please help me find the problem, how to improve to produce results.
var points = [
{x: 114.4864783553646, y: 30.604215790554694},
{x: 114.48382898892015, y: 30.60431560806962},
{x: 114.48353342631442, y: 30.601700422371984},
{x: 114.48683580671532, y: 30.60167662465819},
{x: 114.49045381640737, y: 30.603004562925776}
]
function geometry2(points, height) {
var pointsBottom = [], // 底面世界坐标数组
pointsTop = [], // 顶面世界坐标数组
lons = [], // 底面坐标点的经度(弧度)集合
lats = [], // 底面坐标点的纬度(弧度)集合
pointsChic = [], // 底面坐标(经纬度弧度)集合
ps = []; // 几何体的所有点(世界坐标)
for (var i = 0; i < points.length; i++) {
var chicBottom = Cesium.Cartographic.fromDegrees(points[i].x, points[i].y, points[i].z ? points[i].z : 0);
lons.push(chicBottom.longitude);
lats.push(chicBottom.latitude);
pointsChic.push(chicBottom);
var chicTop = Cesium.Cartographic.fromDegrees(points[i].x, points[i].y, chicBottom.height + height);
var cat3Bottom = Cesium.Cartographic.toCartesian(chicBottom);
var cat3Top = Cesium.Cartographic.toCartesian(chicTop);
pointsBottom.push(cat3Bottom);
pointsTop.push(cat3Top);
ps.push(cat3Bottom, cat3Top);
}
// 以立方体为例
/*
3'
*************************** 2'
** **
* * * *
* * * *
* * * *
0' *************************** 1' *
* * * *
* * * *
* * * *
* * 3 * *
* * * * * * * * * * * * * * * 2
* * * *
* * * *
***************************
0 1
*/
// 底面坐标个数
var planePsNumber = points.length;
// 顶点个数(点不复用,每个三角形3个点)
var num = planePsNumber * 2 * 3 + (planePsNumber - 2) * 3 * 2;
// 计算顶面和底面的纹理贴图的点的坐标
var minlon = Math.min.apply(null, lons);
var minLat = Math.min.apply(null, lats);
var maxlon = Math.max.apply(null, lons);
var maxlat = Math.max.apply(null, lats);
// 顶面和底面纹理坐标数组
var stsTopAndBottom = [];
for (var i = 0; i < planePsNumber; i++) {
var x = (pointsChic[i].latitude - minLat) / (maxlat - minLat);
var y = (pointsChic[i].longitude - minlon) / (maxlon - minlon);
stsTopAndBottom.push({x: Number(x.toFixed(2)), y: Number(y.toFixed(2))});
}
// attributes
var positions = new Float64Array(num * 3);
var colors = new Uint8Array(num * 4);
// 三角形
var sans = []; // 三角形世界坐标数组(每一个代表一个三角形)
// 侧面
for (var i = 0; i < planePsNumber; i++){
sans.push([pointsBottom[i], pointsBottom[(i + 1) > (planePsNumber - 1) ? 0 : (i + 1)], pointsTop[(i + 1) > (planePsNumber - 1) ? 0 : (i + 1)]]);
sans.push([pointsBottom[i], pointsTop[(i + 1) > (planePsNumber - 1) ? 0 : (i + 1)], pointsTop[i]]);
}
// 顶面和底面
for (var i = 1; i <= planePsNumber - 2; i++) {
sans.push([pointsBottom[0], pointsBottom[i], pointsBottom[i + 1]]);
sans.push([pointsTop[0], pointsTop[i], pointsTop[i + 1]]);
}
// sans数组拆分成[x, y, z, x, y, z.....]
// sans数组拆分成[x, y, x, y.....]
var k = 0;
var h = 0;
for (var i = 0; i < sans.length; i++) {
var san = sans[i];
for (var j = 0; j < san.length; j++) {
positions[k++] = san[j].x;
positions[k++] = san[j].y;
positions[k++] = san[j].z;
var r = Cesium.Color.floatToByte(Math.random());
var g = Cesium.Color.floatToByte(Math.random());
var b = Cesium.Color.floatToByte(Math.random());
var a = Cesium.Color.floatToByte(Math.random());
colors[h++] = r;
colors[h++] = g;
colors[h++] = b;
colors[h++] = a;
}
}
console.log(colors)
// attributes
var indices = new Uint32Array(positions.length / 3);
for (var i = 0; i < positions.length / 3; i++) {
indices[i] = i;
}
var attributes = new Cesium.GeometryAttributes({
position: new Cesium.GeometryAttribute({
componentDatatype: Cesium.ComponentDatatype.DOUBLE,
componentsPerAttribute: 3,
values: positions
}),
color: new Cesium.GeometryAttribute({
componentDatatype: Cesium.ComponentDatatype.UNSIGNED_BYTE,
componentsPerAttribute: 4,
values: colors
})
});
return new Cesium.Geometry({
attributes: attributes,
indices: indices,
primitiveType: Cesium.PrimitiveType.TRIANGLES,
vertexFormat: new Cesium.VertexFormat({
position: true,
color: true
}),
boundingSphere: Cesium.BoundingSphere.fromPoints(ps)
});
}
var geometry = geometry2(points, 100);
var primitive = new Cesium.Primitive({
geometryInstances: new Cesium.GeometryInstance({
geometry: geometry
}),
asynchronous: false,
appearance: new Cesium.PerInstanceColorAppearance()
});
viewer.scene.primitives.add(primitive);
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(114.4864783553646, 30.604215790554694, 500),
duration: 2
})