Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Apr 17, 2021 at 02:39 PM by andreministro for the following reason:

I solved it

avatar image
0
Question by andreministro · Apr 15, 2021 at 09:09 PM · colorcolor changeprocedural generationstructsketch

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. alt text

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: alt text

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?

7057939e7c9c0e14c2d7755a29d363d5.png (95.2 kB)
8a01955ca1e35ca4fdf65c65b59acb76.png (46.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

118 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I change Particle System color via script in Unity 5.6.1f1? 1 Answer

Specific colors change gradually as I keep playing on Android! 0 Answers

How to toggle a box color on & Off? 1 Answer

SpriteRender.color not changing the color 1 Answer

Change startcolor of particle system through script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges