Untitled
unknown
plain_text
a year ago
2.7 kB
10
Indexable
using System;
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
namespace Game.World.Views.Spreads.Jobs
{
[BurstCompile]
public struct CreateGradationMapJob : IJob
{
public NativeArray<int> GradationMap;
public int Gradations;
public int Width;
public int Height;
public NativeArray<int2> InitialPoints;
public NativeArray<bool> Filled;
private int _pixelsCount;
private int _pixelsPerColor;
public void Execute()
{
_pixelsCount = Width * Height;
_pixelsPerColor = _pixelsCount / Gradations;
for (int colorId = 0; colorId < Gradations; colorId++)
{
int2 startPoint = InitialPoints[colorId];
int startX = startPoint.x;
int startY = startPoint.y;
if (startX >= 0 && startX < Width && startY >= 0 && startY < Height)
{
NativeList<int2> queue = new NativeList<int2>(Allocator.Temp);
queue.Add(new int2(startX, startY));
int filledCount = 0;
while (queue.Length > 0 && filledCount < _pixelsPerColor)
{
int2 point = queue[queue.Length - 1];
queue.RemoveAt(queue.Length - 1);
int x = point.x;
int y = point.y;
if (Fill(x, y, colorId))
{
filledCount++;
if (x + 1 < Width) queue.Add(new int2(x + 1, y));
if (x - 1 >= 0) queue.Add(new int2(x - 1, y));
if (y + 1 < Height) queue.Add(new int2(x, y + 1));
if (y - 1 >= 0) queue.Add(new int2(x, y - 1));
}
}
queue.Dispose();
}
}
}
private bool Fill(int x, int y, int colorId)
{
if (x < 0 || x >= Width || y < 0 || y >= Height || GetFilledValue(x, y))
{
return false;
}
int index = x + y * Width;
SetFilledValue(x, y, true);
GradationMap[index] = colorId;
return true;
}
private bool GetFilledValue(int x, int y)
{
int index = y * Width + x;
return Filled[index];
}
private void SetFilledValue(int x, int y, bool value)
{
int index = y * Width + x;
Filled[index] = value;
}
}
}
Editor is loading...
Leave a Comment