Performance Increase
Project Journey » Devlog
This is an update to my infinite roads and terrain project.
Using the Unity Job system Bursts, performance has been drastically increased.
This is a code snippet of the new method of finding the distance to a road spline from a vertex on the terrain mesh:
[BurstCompile]
struct GetNearestJob : IJob
{
public NativeArray<float3> vertices;
[NativeDisableContainerSafetyRestriction]
public NativeArray<NativeSpline> splines;
public float distanceCutoff;
public Matrix4x4 ChunkMatrix4X4;
public void Execute()
{
// Get the inverse of the ChunkMatrix4X4 to convert vertices back to local space later
Matrix4x4 inverseMatrix = ChunkMatrix4X4.inverse;
for (int i = 0; i < vertices.Length; i++)
{
float closestDistance = float.MaxValue;
float3 closestPoint = new();
// Convert the vertex to world space
float3 worldSpaceVertex = ChunkMatrix4X4.MultiplyPoint3x4(vertices[i]);
for (int j = 0; j < splines.Length; j++)
{
float tempClosestDistance = SplineUtility.GetNearestPoint(splines[j], worldSpaceVertex, out float3 tempClosestPoint, out float _);
if (tempClosestDistance < closestDistance)
{
closestPoint = tempClosestPoint;
closestDistance = tempClosestDistance;
}
if (closestDistance < distanceCutoff)
{
float normalizedDistance = Mathf.Clamp01(closestDistance / distanceCutoff);
float outValue = 1f / (1f + Mathf.Exp(10f * (normalizedDistance - 0.5f)));
worldSpaceVertex.y = Mathf.Lerp(worldSpaceVertex.y, closestPoint.y, outValue);
// Convert modified vertex back to localSpace
float3 localSpaceVertex = inverseMatrix.MultiplyPoint3x4(worldSpaceVertex);
vertices[i] = localSpaceVertex;
}
}
}
}
}Files
Project Journey Bursts Performance Version 35 MB
Dec 07, 2024
Leave a comment
Log in with itch.io to leave a comment.