var FlattenIsDraw = false;
|
var FlattenWallArr = [];
|
var FlattenEntity = [];
|
var Flatten3DTileSetAnModels = [];
|
|
//地形压平
|
function CreateTerra_Flatten() {
|
if (FlattenIsDraw) return;
|
FlattenIsDraw = true;
|
ClearAllClippingPlanes();
|
var tooltip = sgworld.Core.CreateTooltip();
|
var positions = [];
|
var hierarchy = [];
|
var polygon;
|
var tempPosition;
|
var bottomH = null;
|
var clickNum = 0;
|
|
var handle = new Cesium.ScreenSpaceEventHandler(Viewer.scene.canvas);
|
|
setDepthTest('open');
|
//鼠标左键单击
|
handle.setInputAction(function (movement) {
|
tempPosition = getPointFromWindowPoint(movement.position);
|
var cartographic = Cesium.Cartographic.fromCartesian(tempPosition);
|
var lng = Cesium.Math.toDegrees(cartographic.longitude);//经度值
|
var lat = Cesium.Math.toDegrees(cartographic.latitude);//纬度值
|
positions.push(tempPosition);
|
hierarchy.push(lng, lat, cartographic.height);
|
if (clickNum === 0) {
|
positions.push(tempPosition);
|
hierarchy.push(lng, lat, cartographic.height);
|
} else if (clickNum === 1) {
|
polygon = Viewer.entities.add({
|
name: null,
|
polygon: {
|
hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(hierarchy)),
|
material: Cesium.Color.YELLOW.withAlpha(0.3)
|
}
|
});
|
polygon.polygon.hierarchy = new Cesium.CallbackProperty(getHierarchy, false);
|
window.sgworld.ProjectTree.pushtemporaryItem(polygon);
|
}
|
clickNum++;
|
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
|
|
//鼠标移动
|
handle.setInputAction(function (movement) {
|
if (clickNum === 0) {
|
tooltip.showAt(movement.endPosition, "左键点击开始绘制点");
|
return;
|
} else if (clickNum === 1) {
|
tooltip.showAt(movement.endPosition, "点击添加第二个点");
|
} else {
|
tooltip.showAt(movement.endPosition, "右键结束绘制");
|
}
|
tempPosition = getPointFromWindowPoint(movement.endPosition);
|
var cartographic = Cesium.Cartographic.fromCartesian(tempPosition);
|
var lng = Cesium.Math.toDegrees(cartographic.longitude);//经度值
|
var lat = Cesium.Math.toDegrees(cartographic.latitude);//纬度值
|
hierarchy.splice(hierarchy.length - 3, 3);
|
positions.splice(positions.length - 1, 1);
|
positions.push(tempPosition);
|
hierarchy.push(lng, lat, cartographic.height);
|
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
|
|
//鼠标右键单击结束绘制
|
handle.setInputAction(function (movement) {
|
tooltip.show(false);
|
handle.destroy();
|
if (clickNum < 2) {
|
clickNum = 0;
|
return;
|
}
|
clickNum = 0;
|
Viewer.entities.remove(polygon);
|
polygon = Viewer.entities.add({
|
name: null,
|
polygon: {
|
hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(hierarchy)),
|
material: new Cesium.ImageMaterialProperty({
|
image: SmartEarthRootUrl + 'Workers/image/bottomplane.jpg',
|
repeat: new Cesium.Cartesian2(1, 1)
|
})
|
}
|
});
|
//计算底部高度
|
for (var i = 2; i < hierarchy.length; i += 3) {
|
if (bottomH) {
|
bottomH = bottomH > hierarchy[i] ? hierarchy[i] : bottomH;
|
} else {
|
bottomH = hierarchy[i];
|
}
|
}
|
positions.push(positions[0]);
|
var cartographic1, cartographic2, lng1, lng2, lat1, lat2;
|
for (var i = 0; i < positions.length - 1; i++) {
|
cartographic1 = Cesium.Cartographic.fromCartesian(positions[i]);
|
cartographic2 = Cesium.Cartographic.fromCartesian(positions[i + 1]);
|
lng1 = Cesium.Math.toDegrees(cartographic1.longitude);//经度值
|
lat1 = Cesium.Math.toDegrees(cartographic1.latitude);//纬度值
|
|
lng2 = Cesium.Math.toDegrees(cartographic2.longitude);//经度值
|
lat2 = Cesium.Math.toDegrees(cartographic2.latitude);//纬度值
|
getPoint(lng1, lat1, lng2, lat2, cartographic1.height, cartographic2.height, bottomH);
|
}
|
polygon.polygon.height = bottomH;
|
FlattenEntity.push(polygon);
|
groundClipping_Flatten(positions);
|
FlattenIsDraw = false;
|
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
|
|
//动态绘制多边形
|
function getHierarchy() {
|
return new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(hierarchy));
|
}
|
|
function getPointFromWindowPoint(point) {
|
var position;
|
if (Viewer.scene.terrainProvider.constructor.name === "EllipsoidTerrainProvider") {
|
position = Viewer.camera.pickEllipsoid(point, Viewer.scene.globe.ellipsoid);
|
} else {
|
var ray = Viewer.scene.camera.getPickRay(point);
|
position = Viewer.scene.globe.pick(ray, Viewer.scene);
|
}
|
var feature = Viewer.scene.pick(point);
|
if (feature) {
|
var _cartesian = Viewer.scene.pickPosition(point);
|
if (_cartesian) {
|
position = _cartesian;
|
var cartographic = Cesium.Cartographic.fromCartesian(position);
|
var height = Viewer.scene.globe.getHeight(cartographic);
|
cartographic.height = height;
|
position = Cesium.Cartographic.toCartesian(cartographic);
|
}
|
}
|
|
return position;
|
}
|
|
function groundClipping_Flatten(positions) {
|
// Create clipping planes for polygon around area to be clipped.
|
var globe = Viewer.scene.globe;
|
var points = positions.concat();
|
points.splice(points.length - 1, 1);
|
//去除相同点
|
for (var i = 0; i < points.length - 1; i++) {
|
if (points[i].x === points[i + 1].x && points[i].y === points[i + 1].y) {
|
points.splice(i, 1);
|
i--;
|
}
|
}
|
var pointsLength = points.length;
|
//判断是否为顺时针
|
if (PolygonIsClockwise(points)) {
|
points.reverse();
|
}
|
// Create center points for each clipping plane
|
var clippingPlanes = [];
|
for (var i = 0; i < pointsLength; ++i) {
|
var nextIndex = (i + 1) % pointsLength;
|
var midpoint = Cesium.Cartesian3.add(points[i], points[nextIndex], new Cesium.Cartesian3());
|
midpoint = Cesium.Cartesian3.multiplyByScalar(midpoint, 0.5, midpoint);
|
|
var up = Cesium.Cartesian3.normalize(midpoint, new Cesium.Cartesian3());
|
var right = Cesium.Cartesian3.subtract(points[nextIndex], midpoint, new Cesium.Cartesian3());
|
right = Cesium.Cartesian3.normalize(right, right);
|
|
var normal = Cesium.Cartesian3.cross(right, up, new Cesium.Cartesian3());
|
normal = Cesium.Cartesian3.normalize(normal, normal);
|
|
// 起始点为坐标原点
|
var originCenteredPlane = new Cesium.Plane(normal, 0.0);
|
var distance = Cesium.Plane.getPointDistance(originCenteredPlane, midpoint);
|
clippingPlanes.push(new Cesium.ClippingPlane(normal, distance));
|
}
|
globe.clippingPlanes = new Cesium.ClippingPlaneCollection({
|
planes: clippingPlanes
|
});
|
}
|
|
//根据两点经纬度测算距离
|
var PI = Math.PI;
|
var EARTH_RADIUS = 6378137.0;
|
|
function getGreatCircleDistance(lat1, lng1, lat2, lng2) {
|
var radLat1 = getRad(lat1);
|
var radLat2 = getRad(lat2);
|
|
var a = radLat1 - radLat2;
|
var b = getRad(lng1) - getRad(lng2);
|
|
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
|
s = s * EARTH_RADIUS;
|
s = Math.round(s * 10000) / 10000.0;
|
return s;
|
}
|
|
function getRad(d) {
|
return d * PI / 180.0;
|
}
|
|
function drawFlattenWall(wallJwd, heightA, heightB) {
|
var blueWall = Viewer.entities.add({
|
name: null,
|
wall: {
|
//坐标点,不指定高度
|
positions: Cesium.Cartesian3.fromDegreesArray(
|
wallJwd),
|
// material : Cesium.Color.BLUE,
|
maximumHeights: heightA,
|
minimumHeights: heightB,
|
}
|
|
});
|
var ellipse = blueWall.wall;
|
ellipse.material = new Cesium.ImageMaterialProperty({
|
image: SmartEarthRootUrl + 'Workers/image/ggy.jpg',
|
repeat: new Cesium.Cartesian2(1, 1)
|
});
|
FlattenWallArr.push(blueWall);
|
}
|
|
function getPoint(lng1, lat1, lng2, lat2, height1, height2, bottomHeight) {
|
//坐标转换
|
// var ellipsoid = Viewer.scene.globe.ellipsoid;
|
// var cartographic1 = Cesium.Cartographic.fromDegrees(lng1, lat1, height1);
|
// var cartesian1 = ellipsoid.cartographicToCartesian(cartographic1);
|
// var scene1 = Cesium.SceneTransforms.wgs84ToWindowCoordinates(Viewer.scene, cartesian1);
|
// var cartographic2 = Cesium.Cartographic.fromDegrees(lng2, lat2, height2);
|
// var cartesian2 = ellipsoid.cartographicToCartesian(cartographic2);
|
// var scene2 = Cesium.SceneTransforms.wgs84ToWindowCoordinates(Viewer.scene, cartesian2);
|
// var leftX = Math.round(scene1.x);
|
// var leftY = Math.round(scene1.y);
|
// var rightX = Math.round(scene2.x);
|
// var rightY = Math.round(scene2.y);
|
var pointSum = 160; //取样点个数
|
var addXTT = Cesium.Math.lerp(lng1, lng2, 1.0 / pointSum) - lng1;
|
var addYTT = Cesium.Math.lerp(lat1, lat2, 1.0 / pointSum) - lat1;
|
// var addX = Cesium.Math.lerp(leftX, rightX, 1.0 / pointSum) - leftX;
|
// var addY = Cesium.Math.lerp(leftY, rightY, 1.0 / pointSum) - leftY;
|
//经纬度高程数组
|
// var longitudeArr = [];
|
// var latitudeArr = [];
|
// var heightArr = [];
|
var wallJwd = [];
|
var positions = [];
|
var heightA = [];
|
var Cartesian;
|
wallJwd.push(lng1, lat1);
|
Cartesian = Cesium.Cartesian3.fromDegrees(lng1, lat1);
|
positions.push(Cesium.Cartographic.fromCartesian(Cartesian));
|
for (var i = 0; i < pointSum; i++) {
|
var longitude = lng1 + (i + 1) * addXTT;
|
var latitude = lat1 + (i + 1) * addYTT;
|
wallJwd.push(longitude, latitude);
|
Cartesian = Cesium.Cartesian3.fromDegrees(longitude, latitude);
|
positions.push(Cesium.Cartographic.fromCartesian(Cartesian));
|
// var x = leftX + (i + 1) * addX;
|
// var y = leftY + (i + 1) * addY;
|
// var eventPosition = {x: x, y: y};
|
//var ray = Viewer.camera.getPickRay(eventPosition);
|
//var position = Viewer.scene.globe.pick(ray, Viewer.scene);
|
// if (Cesium.defined(position)) {
|
// var cartographic = Cesium.Cartographic.fromCartesian(position);
|
// heightArr[i] = cartographic.height.toFixed(6); //保留两位小数
|
// latitudeArr[i] = Cesium.Math.toDegrees(cartographic.latitude);
|
// longitudeArr[i] = Cesium.Math.toDegrees(cartographic.longitude);
|
// if (i == 0) {
|
// heightA.push(heightArr[0]);
|
// }
|
// wallJwd.push(longitudeArr[i]);
|
// wallJwd.push(latitudeArr[i]);
|
// heightA.push(heightArr[i]);
|
// if (i == pointSum - 1) {
|
// heightA.push(heightArr[i]);
|
// }
|
// }
|
|
}
|
wallJwd.push(lng2, lat2);
|
Cartesian = Cesium.Cartesian3.fromDegrees(lng2, lat2);
|
positions.push(Cesium.Cartographic.fromCartesian(Cartesian));
|
var heightB = [];
|
//获取高度
|
getHeightsFromLonLat(positions, function (data) {
|
if (data) {
|
heightA = data;
|
heightA.forEach(function () {
|
heightB.push(bottomHeight);
|
});
|
drawFlattenWall(wallJwd, heightA, heightB);
|
}
|
});
|
}
|
|
//判断多边形是否为顺时针绘制
|
function PolygonIsClockwise(points) {
|
if (points.length >= 3) {
|
//求向量积
|
var type = (points[1].x - points[0].x) * (points[2].y - points[1].y) - (points[1].y - points[0].y) * (points[2].x - points[1].x);
|
if (type > 0) {
|
return false;
|
}
|
return true;
|
}
|
}
|
|
//根据经纬度获取高度
|
function getHeightsFromLonLat(positions, callback) {
|
var camera = Viewer.camera;
|
if (isHaveTerrain(Viewer)) {
|
//根据经纬度计算出地形高度。
|
var promise = Cesium.sampleTerrainMostDetailed(Viewer.terrainProvider, positions);
|
var cameraHeight = camera.positionCartographic.height;
|
Cesium.when(promise, function (updatedPositions) {
|
var heights = [];
|
updatedPositions.forEach(function (item) {
|
heights.push(item.height);
|
});
|
if (typeof callback === "function") {
|
callback(heights);
|
}
|
});
|
} else {
|
layer.msg('请添加地形数据!');
|
}
|
}
|
|
//是否有地形
|
function isHaveTerrain() {
|
if (Viewer.scene && Viewer.scene.terrainProvider && (Viewer.scene.terrainProvider._layers || Viewer.scene.terrainProvider._urlTemplate)) {
|
return true;
|
}
|
return false;
|
}
|
}
|
|
//模型压平
|
function CreateModel_Flatten() {
|
if (FlattenIsDraw) return;
|
if (Flatten3DTileSetAnModels.length === 0) {
|
get3DTileSetAnModels_Flatten();
|
}
|
if (Flatten3DTileSetAnModels.length === 0) {
|
layer.msg('无模型数据');
|
return;
|
}
|
FlattenIsDraw = true;
|
ClearAllClippingPlanes();
|
var tooltip = sgworld.Core.CreateTooltip();
|
var positions = [];
|
var hierarchy = [];
|
var polygon;
|
var tempPosition;
|
var bottomH = null;
|
var clickNum = 0;
|
|
var handle = new Cesium.ScreenSpaceEventHandler(Viewer.scene.canvas);
|
|
setDepthTest('open');
|
|
//修改
|
//Flatten3DTileSetAnModels.forEach(function (tileset) {
|
// debugger
|
// //set3DtilesBug();
|
|
// //sgworld.Creator.createModifyMesh("aaaa", positions, 100000, tileset, {});
|
//})
|
//鼠标左键单击
|
handle.setInputAction(function (movement) {
|
tempPosition = getPointFromWindowPoint(movement.position);
|
var cartographic = Cesium.Cartographic.fromCartesian(tempPosition);
|
var lng = Cesium.Math.toDegrees(cartographic.longitude);//经度值
|
var lat = Cesium.Math.toDegrees(cartographic.latitude);//纬度值
|
positions.push(tempPosition);
|
hierarchy.push(lng, lat, cartographic.height);
|
if (clickNum === 0) {
|
positions.push(tempPosition);
|
hierarchy.push(lng, lat, cartographic.height);
|
} else if (clickNum === 1) {
|
polygon = Viewer.entities.add({
|
name: null,
|
polygon: {
|
hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(hierarchy)),
|
material: Cesium.Color.YELLOW.withAlpha(0.3)
|
}
|
});
|
polygon.polygon.hierarchy = new Cesium.CallbackProperty(getHierarchy, false);
|
window.sgworld.ProjectTree.pushtemporaryItem(polygon);
|
}
|
clickNum++;
|
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
|
|
//鼠标移动
|
handle.setInputAction(function (movement) {
|
if (clickNum === 0) {
|
tooltip.showAt(movement.endPosition, "左键点击开始绘制点");
|
return;
|
} else if (clickNum === 1) {
|
tooltip.showAt(movement.endPosition, "点击添加第二个点");
|
} else {
|
tooltip.showAt(movement.endPosition, "右键结束绘制");
|
}
|
tempPosition = getPointFromWindowPoint(movement.endPosition);
|
var cartographic = Cesium.Cartographic.fromCartesian(tempPosition);
|
var lng = Cesium.Math.toDegrees(cartographic.longitude);//经度值
|
var lat = Cesium.Math.toDegrees(cartographic.latitude);//纬度值
|
hierarchy.splice(hierarchy.length - 3, 3);
|
positions.splice(positions.length - 1, 1);
|
positions.push(tempPosition);
|
hierarchy.push(lng, lat, cartographic.height);
|
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
|
|
//鼠标右键单击结束绘制
|
handle.setInputAction(function (movement) {
|
tooltip.show(false);
|
handle.destroy();
|
if (clickNum < 2) {
|
clickNum = 0;
|
return;
|
}
|
clickNum = 0;
|
Viewer.entities.remove(polygon);
|
|
// polygon = Viewer.entities.add({
|
// name: null,
|
// polygon: {
|
// hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(hierarchy)),
|
// material: new Cesium.ImageMaterialProperty({
|
// image: 'static/img/tt/bottomplane.jpg',
|
// repeat: new Cesium.Cartesian2(1, 1)
|
// })
|
// }
|
// });
|
//计算底部高度
|
for (var i = 2; i < hierarchy.length; i += 3) {
|
if (bottomH) {
|
bottomH = bottomH > hierarchy[i] ? hierarchy[i] : bottomH;
|
} else {
|
bottomH = hierarchy[i];
|
}
|
}
|
var pp = polygon;
|
bottomH = -15;
|
Flatten3DTileSetAnModels.forEach(function (tileset) {
|
//set3DtilesBug();
|
CreateModifyMesh("aaaa", positions, -bottomH, tileset, {});
|
//CreateModifyMesh("aaaa", [{ x: -2614051.124732636, y: 4736955.336069073, z: 3366375.2614991767 }, { x: -2614075.364686353, y: 4736905.985810969, z: 3366424.333971074 }, { x: -2614050.302550419, y: 4736917.680342396, z: 3366428.1807405315 }, { x: -2614050.302550419, y: 4736917.680342396, z: 3366428.1807405315 }], 50, tileset, {});
|
//sgworld.Creator.createModifyMesh("aaaa", positions, 100000, tileset, {});
|
})
|
//positions.push(positions[0]);
|
// var cartographic1, cartographic2, lng1, lng2, lat1, lat2;
|
// for (var i = 0; i < positions.length - 1; i++) {
|
// cartographic1 = Cesium.Cartographic.fromCartesian(positions[i]);
|
// cartographic2 = Cesium.Cartographic.fromCartesian(positions[i + 1]);
|
// lng1 = Cesium.Math.toDegrees(cartographic1.longitude);//经度值
|
// lat1 = Cesium.Math.toDegrees(cartographic1.latitude);//纬度值
|
|
// lng2 = Cesium.Math.toDegrees(cartographic2.longitude);//经度值
|
// lat2 = Cesium.Math.toDegrees(cartographic2.latitude);//纬度值
|
// getPoint(lng1, lat1, lng2, lat2, cartographic1.height, cartographic2.height, bottomH);
|
// }
|
// polygon.polygon.height = bottomH;
|
// FlattenEntity.push(polygon);
|
// groundClipping_Flatten(positions);
|
window.ModelFlattenData = {
|
height: bottomH,
|
Flatten3DTileSetAnModels: Flatten3DTileSetAnModels,
|
positions: positions
|
};
|
FlattenIsDraw = false;
|
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
|
|
//动态绘制多边形
|
function getHierarchy() {
|
return new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(hierarchy));
|
}
|
|
function getPointFromWindowPoint(point) {
|
var position;
|
if (Viewer.scene.terrainProvider.constructor.name === "EllipsoidTerrainProvider") {
|
position = Viewer.camera.pickEllipsoid(point, Viewer.scene.globe.ellipsoid);
|
} else {
|
var ray = Viewer.scene.camera.getPickRay(point);
|
position = Viewer.scene.globe.pick(ray, Viewer.scene);
|
}
|
var feature = Viewer.scene.pick(point);
|
if (feature) {
|
var _cartesian = Viewer.scene.pickPosition(point);
|
if (_cartesian) {
|
position = _cartesian;
|
var cartographic = Cesium.Cartographic.fromCartesian(position);
|
var height = Viewer.scene.globe.getHeight(cartographic);
|
cartographic.height = height;
|
position = Cesium.Cartographic.toCartesian(cartographic);
|
}
|
}
|
|
return position;
|
}
|
|
//获取模型
|
function get3DTileSetAnModels_Flatten() {
|
Flatten3DTileSetAnModels = sgworld.Creator._D3Tilesets || [];
|
}
|
}
|
|
function ClearAllClippingPlanes() {
|
if (FlattenWallArr.length > 0) {
|
for (var i = 0; i < FlattenWallArr.length; i++) {
|
Viewer.entities.remove(FlattenWallArr[i])
|
}
|
FlattenWallArr = [];
|
}
|
if (Viewer.scene.globe.clippingPlanes) {
|
Viewer.scene.globe.clippingPlanes.removeAll();
|
}
|
if (FlattenEntity && FlattenEntity.length > 0) {
|
FlattenEntity.forEach(function (entity) {
|
Viewer.entities.remove(entity);
|
});
|
FlattenEntity = [];
|
}
|
if (Flatten3DTileSetAnModels.length > 0) {
|
Flatten3DTileSetAnModels.forEach(function (model) {
|
for (; model.modifyB3dmObjects && model.modifyB3dmObjects.length;) {
|
model.modifyB3dmObjects.pop()
|
model.modifyMeshObject = undefined;
|
}
|
});
|
}
|
FlattenIsDraw = false;
|
}
|
|
|
|
var CreateModifyMesh = function (description, polygon, altitude, meshObject, options) {
|
var len = polygon.length;
|
var guid = Cesium.createGuid();
|
var heights = new Array(len);
|
heights.fill(altitude);
|
var nowTime = Date.now();
|
var rect = Cesium.Rectangle.fromCartesianArray(polygon);
|
var west = rect.west * 180 / Math.PI;
|
var south = rect.south * 180 / Math.PI;
|
var east = rect.east * 180 / Math.PI;
|
var north = rect.north * 180 / Math.PI;
|
|
var minPoint = Cesium.Cartesian3.fromDegrees(east, south);
|
var maxPoint = Cesium.Cartesian3.fromDegrees(west, north);
|
|
var extrudedPolygon = new Cesium.PolygonGeometry({
|
polygonHierarchy: new Cesium.PolygonHierarchy(polygon)
|
});
|
//var nowRect = new Cesium.Rectangle(west, south, east, north);
|
|
if (meshObject.modifyB3dmObjects === undefined) {
|
meshObject.modifyB3dmObjects = [];
|
}
|
var modifyMeshObject = {
|
id: guid,
|
show: true,
|
name: description,
|
rectangle: rect,
|
polygonBounds: [minPoint.x, minPoint.y, minPoint.z, maxPoint.x, maxPoint.y, maxPoint.z],
|
modelBounds: null,
|
polygonGeometry: extrudedPolygon,
|
vertices: polygon,
|
polygon: polygon,
|
height: altitude,
|
meshObject: meshObject,
|
heights: heights,
|
timeStamp: nowTime,
|
image: createTexture(polygon),
|
texture: null
|
};
|
meshObject.modifyB3dmObjects.push(modifyMeshObject);
|
meshObject.modifyMeshObject = modifyMeshObject;
|
};
|
|
|
|
|
var texturePixel = 1024;
|
function createTexture(polygon) {
|
var rect = Cesium.Rectangle.fromCartesianArray(polygon);
|
var west = rect.west * 180 / Math.PI;
|
var south = rect.south * 180 / Math.PI;
|
var east = rect.east * 180 / Math.PI;
|
var north = rect.north * 180 / Math.PI;
|
var nowRect = new Cesium.Rectangle(west, south, east, north);
|
var canvasPolygon = [];
|
polygon.forEach(function (position) {
|
var point = position.longitude ? position : Cesium.Ellipsoid.WGS84.cartesianToCartographic(position);
|
point = getPointRelativePosition(point, nowRect, texturePixel, texturePixel);
|
canvasPolygon.push([point.normalizedLongitude, point.normalizedLatitude]);
|
});
|
var canvas = createCanvas(canvasPolygon, "#FF0000", texturePixel, texturePixel);
|
//debug
|
//document.body.appendChild(canvas);
|
var image = new Image();
|
image.width = texturePixel;
|
image.height = texturePixel;
|
image.src = canvas.toDataURL("image/png");
|
return image;
|
|
//return canvas;
|
|
}
|
function createCanvas(canvasPolygon, color, pixelX, pixelY) {
|
var canvas = document.createElement("canvas");
|
canvas.width = pixelX;
|
canvas.height = pixelY;
|
var ctx = canvas.getContext("2d");
|
ctx.fillStyle = "#000";
|
ctx.fillRect(0, 0, pixelX, pixelX);
|
if (canvasPolygon.length <= 0) return;
|
ctx.moveTo(canvasPolygon[0][0], canvasPolygon[0][1]);
|
for (var i = 0; i < canvasPolygon.length; i++) {
|
ctx.lineTo(canvasPolygon[i][0], canvasPolygon[i][1]);
|
}
|
ctx.fillStyle = color;
|
ctx.fill();
|
return canvas;
|
}
|
function getPointRelativePosition(point, rect, pixelX, pixelY) {
|
var lonDelta = rect.east - rect.west;
|
//var latDelta = rect.north - rect.south;
|
var latDelta = rect.south - rect.north;
|
var radianLon = point.longitude * 180 / Math.PI;
|
var radianLat = point.latitude * 180 / Math.PI;
|
point.normalizedLongitude = (radianLon - rect.west) / lonDelta * pixelX;
|
point.normalizedLatitude = (radianLat - rect.north) / latDelta * pixelY;
|
// point.normalizedLatitude = (rect.south-radianLat) / latDelta * pixelY;
|
return point;
|
}
|
|
|
|
|
|
|
function set3DtilesBug() {
|
|
var fixGltf = function (gltf) {
|
if (!gltf.extensionsUsed) {
|
return;
|
}
|
if (!gltf.extensionsRequired) {
|
return;
|
}
|
var v = gltf.extensionsUsed.indexOf('KHR_technique_webgl');
|
var t = gltf.extensionsRequired.indexOf('KHR_technique_webgl');
|
|
if (v !== -1) {
|
gltf.extensionsRequired.splice(t, 1, 'KHR_techniques_webgl');
|
gltf.extensionsUsed.splice(v, 1, 'KHR_techniques_webgl');
|
gltf.extensions = gltf.extensions || {};
|
gltf.extensions['KHR_techniques_webgl'] = {};
|
gltf.extensions['KHR_techniques_webgl'].programs = gltf.programs;
|
gltf.extensions['KHR_techniques_webgl'].shaders = gltf.shaders;
|
gltf.extensions['KHR_techniques_webgl'].techniques = gltf.techniques;
|
var techniques = gltf.extensions['KHR_techniques_webgl'].techniques;
|
|
gltf.materials.forEach(function (mat, index) {
|
gltf.materials[index].extensions['KHR_technique_webgl'].values = gltf.materials[
|
index].values;
|
gltf.materials[index].extensions['KHR_techniques_webgl'] = gltf.materials[index]
|
.extensions['KHR_technique_webgl'];
|
|
var vtxfMaterialExtension = gltf.materials[index].extensions[
|
'KHR_techniques_webgl'];
|
|
for (var value in vtxfMaterialExtension.values) {
|
var us = techniques[vtxfMaterialExtension.technique].uniforms;
|
for (var key in us) {
|
if (us[key] === value) {
|
vtxfMaterialExtension.values[key] = vtxfMaterialExtension.values[value];
|
delete vtxfMaterialExtension.values[value];
|
break;
|
}
|
}
|
};
|
});
|
|
techniques.forEach(function (t) {
|
for (var attribute in t.attributes) {
|
var name = t.attributes[attribute];
|
t.attributes[attribute] = t.parameters[name];
|
};
|
|
for (var uniform in t.uniforms) {
|
var name = t.uniforms[uniform];
|
t.uniforms[uniform] = t.parameters[name];
|
};
|
});
|
}
|
}
|
|
Object.defineProperties(Cesium.Model.prototype, {
|
_cachedGltf: {
|
set: function (value) {
|
this._vtxf_cachedGltf = value;
|
if (this._vtxf_cachedGltf && this._vtxf_cachedGltf._gltf) {
|
fixGltf(this._vtxf_cachedGltf._gltf);
|
}
|
},
|
get: function () {
|
return this._vtxf_cachedGltf;
|
}
|
}
|
});
|
|
|
}
|