So I was trying to jobify my map generation code, I figured it was simple enough, but my result is coming back kinda right but like the pixels are going into not exactly the right places they’re supposed to go to.
Left side of the image is what my regular code produces, right what the jobified code produced:
One thing that I wasn’t so sure how to accomplish and might be the center of my problem is that I didn’t know how to connect the position of the world coordinates to the NativeArray of colors, so I created a NativeArray and did a nested for loop with x and y to input the coordinates into the nativearray, I figured that if both indexes are the same in the colors native array and the coordinates native array it should work. But I guess I’m wrong? How should I approach this?
If anyone has any ideas I’d be happy to hear them, not sure where to go from here ;( thanks
The job code:
public struct MapTexturePreviewJob : IJobParallelFor
{
public NativeArray<float2> coordinates;
public NativeArray<Color> colors;
public void Execute(int i)
{
var coords = coordinates[i];
float xWorld = coords.x;
float zWorld = coords.y;
colors[i] = TerrainMaker.FindColor(xWorld, zWorld, true,1));
}
}
public static MapTexturePreviewJob CreateMapTexturePreviewJob(int width, int height, int midPosX,int midPosZ, float zoomOut)
{
var _coords = new NativeArray<float2>(width * height, Allocator.TempJob);
var _cols = new NativeArray<Color>(width * height, Allocator.TempJob);
float xOffset = ((midPosX) - (width * 0.5f));
float yOffset = ((midPosZ) - (height * 0.5f));
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
float xWorld = (xOffset + x) * zoomOut;
float zWorld = (yOffset + y) * zoomOut;
_coords[x + y * width] = new float2(xWorld, zWorld);
}
}
MapTexturePreviewJob m_PreviewMapJob = new MapTexturePreviewJob()
{
coordinates = _coords,
colors = _cols
};
return m_PreviewMapJob;
}
Without the job essentially the same thing, but produces correct result:
var _coords = new NativeArray<float2>(width * height, Allocator.TempJob);
var _cols = new NativeArray<Color>(width * height, Allocator.TempJob);
gen = TerraSettings.GetGeneratorForPosition(midPosX,midPosZ);
float xOffset = ((midPosX) - (width * 0.5f));
float yOffset = ((midPosZ) - (height * 0.5f));
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
float xWorld = (xOffset + x) * zoomOut;
float zWorld = (yOffset + y) * zoomOut;
_coords[x + y * width] = new float2(xWorld, zWorld);
}
}
for (int i = 0; i < _coords.Length; i++)
{
var coords = _coords[i];
float xWorld = coords.x;
float zWorld = coords.y;
colors[i] = TerrainMaker.FindColor(xWorld, zWorld, true,1));
}
var res = new Texture2D(width, height, format, false);
Color[] colors = new Color[width * height];
_cols.CopyTo(colors);
res.SetPixels(colors, 0);
res.Apply();
_coords.Dispose();
_cols.Dispose();