以前调试过cesium for unreal,再调试时一惊,发现api变了。 静下心来思考流程 1,样本条要放在actor里 2,包含样本条的actor坐标放在原点 3,样本条坐标和法向量都要从经纬高到ue空间转换
变的只是api,所以深入了代码看了下,传递的经纬度从度数到了弧度。
废话不多说,上代码
//按照datatable进行获取数据 UCLASS() class CESIUM_FLY_API APlaneTrack : public AActor { GENERATED_BODY()
public: // Sets default values for this actor’s properties APlaneTrack();
protected: // Called when the game starts or when spawned virtual void BeginPlay() override;
public: // Called every frame virtual void Tick(float DeltaTime) override;
public: //用于保存航班数据的虚幻引擎数据表 UPROPERTY(EditAnywhere, Category = “FlightTracker”) UDataTable* aircraftsRawDataTable;
//从两点间插值创建样条线轨迹
UFUNCTION(BlueprintCallable)
void LoadSplineTrackPoints();
UFUNCTION(BlueprintCallable)
USplineComponent* GetSplineTrack()
{
return splineTrack;
}
//Cesium工具类,包含很多有用的坐标转换相关的函数
UPROPERTY(EditAnywhere, Category = "FlightTracker")
ACesiumGeoreference* cesiumGeoreference;
private: //根组件 class USceneComponent* RootScene; //代表航班轨迹的样条线成员变量 USplineComponent* splineTrack;
};
// Called when the game starts or when spawned void APlaneTrack::BeginPlay() { Super::BeginPlay();
}
// Called every frame void APlaneTrack::Tick(float DeltaTime) { Super::Tick(DeltaTime); }
void APlaneTrack::LoadSplineTrackPoints() { if (this->aircraftsRawDataTable == nullptr) { return; } if (nullptr == this->cesiumGeoreference) { return; }
//初始化轨迹
splineTrack = NewObject<USplineComponent>(this,TEXT("SplineTrack"));
splineTrack->RegisterComponent();
//让样本线在运行模式下可见
splineTrack->SetDrawDebug(true);
//设置样本条的颜色
splineTrack->SetUnselectedSplineSegmentColor(FLinearColor(1.0f, 0.0f, 0.0f));
//设置样条线的粗细或者宽度
splineTrack->ScaleVisualizationWidth = 70.0f;
splineTrack->AttachToComponent(RootScene, FAttachmentTransformRules::KeepRelativeTransform);
int32 pointIndex = 0;
for (auto& row : this->aircraftsRawDataTable->GetRowMap())
{
FAircraftRawData* point = (FAircraftRawData*)row.Value;
//获取经纬高,转为Ue4坐标
double pointLatitude = point->latitude;
double pointLongitude = point->longitude;
double pointHeight = point->height;
#if 1 //先统一加1000 pointHeight += 100; #endif //计算ue里的坐标 glm::dvec3 posXYZ = this->cesiumGeoreference->TransformLongitudeLatitudeHeightToUnreal(glm::dvec3( FMath::DegreesToRadians(pointLongitude), FMath::DegreesToRadians(pointLatitude), FMath::DegreesToRadians(pointHeight))); FVector splinePointPosition = FVector(posXYZ.x, posXYZ.y, posXYZ.z); this->splineTrack->AddSplinePointAtIndex(splinePointPosition, pointIndex, ESplineCoordinateSpace::World, false);
//获取飞机的向上方向
const CesiumGeospatial::Ellipsoid& ell = CesiumGeospatial::Ellipsoid::WGS84;
glm::dvec3 upVector = ell.geodeticSurfaceNormal(CesiumGeospatial::Cartographic(
FMath::DegreesToRadians(pointLongitude),
FMath::DegreesToRadians(pointLatitude),
FMath::DegreesToRadians(pointHeight)));
//计算飞机上的UE向上方向
glm::dvec3 unrealUp = this->cesiumGeoreference->TransformLongitudeLatitudeHeightToUnreal(upVector);
this->splineTrack->SetUpVectorAtSplinePoint(pointIndex, FVector(unrealUp.x, unrealUp.y, unrealUp.z), ESplineCoordinateSpace::World, false);
pointIndex++;
}
this->splineTrack->UpdateSpline();
//this->AttachToComponent(this->cesiumGeoreference->GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);
} 另外数据库里的高度都是负值,钻到地底下了。所以都加个100 运行Ok
|