var FlattenIsDraw = false; var Flatten3DTileSetAnModels = []; //模型压平 function CreateModel_Flatten() { if (FlattenIsDraw) return; if (Flatten3DTileSetAnModels.length === 0) { get3DTileSetAnModels_Flatten(); } if (Flatten3DTileSetAnModels.length === 0) { layer.msg('无模型数据'); return; } FlattenIsDraw = true; 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); // SmartEarthPopupData.layerContainer && SmartEarthPopupData.layerContainer.hide(); //修改 //Flatten3DTileSetAnModels.forEach(function (tileset) { // //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); //计算底部高度 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) { CreateModifyMesh("aaaa", positions, -bottomH, tileset, {}); }) window.ModelFlattenData = { height: bottomH, Flatten3DTileSetAnModels: Flatten3DTileSetAnModels, positions: positions }; FlattenIsDraw = false; // SmartEarthPopupData.layerContainer && SmartEarthPopupData.layerContainer.show(); }, 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 (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; } } }); }