- Home /
I solved it
Change Colour Map colours using struct
Hi. I'm doing a Procedural Generation project that takes a user sketch. The user uses simple colours, in this case, unity's "blue", "magenta", "yellow", etc to draw his sketch, each colour representing a biome. I then take this sketch and build a colormap from it, with the colours exactly as they were painted. I have a TerrainType struct for "biome", with name, colour, humidity, etc system serialized, so i can change in editor the colours (and later use textures). What i want is to change the original colourmap and for each pixel, check its colour and apply the respective region colour.
Color[] colorMapSketch = copy.GetPixels();
int mapWidth = image.width;
int mapHeight = image.height;
Vector3[] spawnPositions = new Vector3[colorMapSketch.Length];
Vector3 startSpawnPos = new Vector3(-Mathf.Round(mapWidth / 2), 0, -Mathf.Round(mapHeight / 2));
Vector3 currentSpawnPos = startSpawnPos;
int counter = 0;
for (int z = 0; z < mapHeight; z++)
{
for (int x = 0; x < mapWidth; x++)
{
spawnPositions[counter] = currentSpawnPos;
counter++;
currentSpawnPos.x++;
}
currentSpawnPos.x = startSpawnPos.x;
currentSpawnPos.z++;
}
counter = 0;
List<Vector3> bluePos = new List<Vector3>();
List<Vector3> yellowPos = new List<Vector3>();
List<Vector3> magentaPos = new List<Vector3>();
List<Vector3> greenPos = new List<Vector3>();
foreach (Vector3 pos in spawnPositions)
{
Color c = colorMapSketch[counter];
if (c.Equals(Color.blue))
{
bluePos.Add(pos);
}
else if (c.Equals(Color.yellow))
{
yellowPos.Add(pos);
}
else if (c.Equals(Color.magenta))
{
magentaPos.Add(pos);
}
else if (c.Equals(Color.green))
{
greenPos.Add(pos);
}
counter++;
}
MapDisplay display = FindObjectOfType<MapDisplay>();
display.DrawTexture(TextureSketchGen.TxtFromSketchClrMap(colorMapSketch, mapWidth, mapHeight));
Here I'm simply saving positions and just displaying the map the user sketched.
I want to use the struct:
public struct TerrainType
{
public string name;
[Range(0, 100)]
public int humidity;
public Color colour;
}
I'm trying to approach the problem this way, using the foreach spawnPositions loop mentioned above:
foreach (Vector3 pos in spawnPositions)
{
Color c = colorMapSketch[counter];
if (c.Equals(Color.blue))
{
bluePos.Add(pos);
for (int i = 0; i < regions.Length; i++)
{
currentHumidity = 100;
if (currentHumidity == regions[i].humidity)
{
foreach(Vector3 bpos in bluePos)
{
colorMapSketch[bluePos.Count] = regions[i].colour;
}
}
}
}
else if (c.Equals(Color.yellow))
{
yellowPos.Add(pos);
}
else if (c.Equals(Color.magenta))
{
magentaPos.Add(pos);
}
else if (c.Equals(Color.green))
{
greenPos.Add(pos);
}
counter++;
}
And this code produces the following output:
As it is seen, the blue pixels the user sketched are not being changed for the struct color, instead the code is painting the number of blue pixels top to bottom on top of the sketch. Anyone has any idea on how i can change this loop to change colours in the correct pixel positions?
Follow this Question
Related Questions
Color.lerp not working properly 2 Answers
UI Text not changing color [C#] 2 Answers
Convert RGB to RYB color scheme? 0 Answers
Why does changing this object's color using Material.color work at all? (C#) 1 Answer
UI Triangle Color picker 0 Answers