Reading Tile3d Attributes - jgoffeney/Cesium4Unreal GitHub Wiki

Back

Description

Reading JSON File

.Build.cs

To add JSON support add the Json and JsonUtilities modules to the project build file.

using UnrealBuildTool;

public class CesiumCppStarter : ModuleRules
{
	public CesiumCppStarter(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
	
		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", 
			"CesiumRuntime", "Json", "JsonUtilities", "HTTP" });

		PrivateDependencyModuleNames.AddRange(new string[] {  });

		CppStandard = CppStandardVersion.Cpp17;
	}
}

Getting Base Tile Bounds

The code using the Unreal JSON functions to directly open and read from the base tileset.json file. The variable tileIndexPath is an FString containing the full file path.

After after the file's contents are read, it is deserialized and then the array of root->boundingVolume->region contains the values of minimum longitude and latitude, maximum longitude and latitude and minimum and maximum heights. Note the longitude and latitude coordinate values are in radians and the height values are in meters.

For my use to fly between points I save the center point of the top of the bounding volume in both geospatial and Unreal grid coordinates. To convert the geospatial coordinates into Unreal coordinates you can use the CesiumGeoreference function TransformLongitudeLatitudeHeightToUnreal.

FString jsonContent;
		
FFileHelper::LoadFileToString(jsonContent, *tileIndexPath);
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
TSharedRef<TJsonReader<>> JsonReader = TJsonReaderFactory<>::Create(jsonContent);

if (FJsonSerializer::Deserialize(JsonReader, JsonObject) && JsonObject.IsValid())
{
	TSharedPtr<FJsonObject> rootObject = JsonObject->GetObjectField("root");
	TSharedPtr<FJsonObject> bvObject = rootObject->GetObjectField("boundingVolume");
	TArray<TSharedPtr<FJsonValue>> regionArray = bvObject->GetArrayField("region");
	double rad2Deg = 57.29577951;
	_geospatialBounds.Min.X = regionArray[0]->AsNumber() * rad2Deg;
	_geospatialBounds.Min.Y = regionArray[1]->AsNumber() * rad2Deg;
	_geospatialBounds.Max.X = regionArray[2]->AsNumber() * rad2Deg;
	_geospatialBounds.Max.Y = regionArray[3]->AsNumber() * rad2Deg;
	_geospatialBounds.Min.Z = regionArray[4]->AsNumber();
	_geospatialBounds.Max.Z = regionArray[5]->AsNumber();
	

	double centerLat = (_geospatialBounds.Max.Y + _geospatialBounds.Min.Y) / 2.0;
	double centerLon = (_geospatialBounds.Max.X + _geospatialBounds.Min.X) / 2.0;

	
	_topGridPosition = geoReference->TransformLongitudeLatitudeHeightToUnreal(
		glm::dvec3(centerLon, centerLat, _geospatialBounds.Max.Z));

	_topGeoPosition.x = centerLon;
	_topGeoPosition.y = centerLat;
	_topGeoPosition.z = _geospatialBounds.Max.Z;
}
else
{
	UE_LOG(LogTemp, Error, TEXT("TileInfo::TileInfo: The file %s JSON can not be serialized."),
		*tileIndexPath);
}
⚠️ **GitHub.com Fallback** ⚠️