I have a project that utilizes Cesium and most of my systems have been developed with a scale of 1 in mind. I have started to run up against floating point issues with globe anchors that are very far away so I’ve reduced the Georeference scale however some of my systems have been offset as a result. Most have been fixable but I’m having issues with a trail generator for on of my flying game objects.
At a high level the logic for my trail generator is
->Convert Unity Position of the transform of the gameobject to ECEF using the Georeference
->Store the trail as an array of ECEF positions
->Use a job to convert the array from ECEF back to unity positions every frame so that the world being offset is accounted for
This worked very well when the scale was 1 however now that it is 0.1 it is very offset.
I have tried both multiplying the position by the georeference scale and it’s inverse but seem to be having no luck.
Was wondering if someone might be able to shed some light. Below is the code for my job and how it is scheduled. Where should I be accounting for the georeference scale here? I would have assumed that the EcefToLocalMatrix is already accounting for that?
void ScheduleTrailCreation()
{
double3 currentPosDoubleECEF = cesiumGeoreference.TransformUnityPositionToEarthCenteredEarthFixed(VecToDouble3(transform.position));
float distance = Vector3.Distance(transform.position, lastPosition);
if (distance >= segmentLength)
{
AddTrailSegment(DoubleToVec3(currentPosDoubleECEF));
lastPosition = transform.position;
}
NativeArray<Vector3> trailPointsNative = new NativeArray<Vector3>(trailPoints, Allocator.TempJob);
NativeArray<Vector3> worldSpaceVerticesNative = new NativeArray<Vector3>(trailPointCount, Allocator.TempJob);
var job = new TrailVertexConversionJob
{
ecefTrailPoints = trailPointsNative,
worldSpaceVertices = worldSpaceVerticesNative,
ecefToLocal = cesiumGeoreference.ecefToLocalMatrix
};
JobHandle handle = job.Schedule(trailPointCount, 1);
handle.Complete();
Vector3[] worldSpaceVertices = worldSpaceVerticesNative.ToArray();
lineStripBehavior.UpdateLineVertices(worldSpaceVertices);
trailPointsNative.Dispose();
worldSpaceVerticesNative.Dispose();
}
[BurstCompile(FloatPrecision.Medium, FloatMode.Fast)]
public struct TrailVertexConversionJob : IJobParallelFor
{
[ReadOnly] public NativeArray<Vector3> ecefTrailPoints;
[WriteOnly] public NativeArray<Vector3> worldSpaceVertices;
public double4x4 ecefToLocal; // Assuming this is a double-precision matrix compatible with Burst
public void Execute(int index)
{
double3 unityPosition = VecToDouble3(ecefTrailPoints[index]);
double3 ecefPosition = TransformEarthCenteredEarthFixedPositionToUnity(unityPosition);
worldSpaceVertices[index] = DoubleToVec3(ecefPosition);
}
double3 VecToDouble3(Vector3 pos)
{
return new double3 { x = pos.x, y = pos.y, z = pos.z };
}
Vector3 DoubleToVec3(double3 pos)
{
return new Vector3 { x = (float)pos.x, y = (float)pos.y, z = (float)pos.z };
}
//double3 TransformUnityPositionToEarthCenteredEarthFixed(double3 unityPosition)
//{
// // Assuming Initialize has been called beforehand, and localToEcef is set up correctly
// return math.mul(localToEcef, new double4(unityPosition, 1.0)).xyz;
//}
double3 TransformEarthCenteredEarthFixedPositionToUnity(double3 earthCenteredEarthFixed)
{
return math.mul(ecefToLocal, new double4(earthCenteredEarthFixed, 1)).xyz;
}
}
Edit: figured out the issue in the process of writing this very post out. Multiplying the world position output from the job by the inverse of the scale fixed it