Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Pancratius · Dec 26, 2013 at 07:30 PM · generatinggeneral programming

GetPixel Not returning the same value after SetPixel.

Hello All,

I'm currently working on a Terraria clone to improve my programming skills. I've come to generating a map and decided to use a Texture2D to hold a map, using color coded pixels to represent the blocks at each position. However when I reach the point of getting the colors to place blocks, Instead of getting the color I have set, I get a slightly different shade of the color. For example. In my code I set the default color of a pixel to black. If I then ask to get the color of the pixel, instead of returning (0, 0, 0, 255) I get (1.000, 0.000, 1.000, 1.000) or (0.000, 1.000, 1.000, 1.000) and even sometimes (1.000, 1.000, 0.000, 1.000) but never (0, 0, 0, 255) which is what I'd expect.

I've don't have my code with me as I'm not currently at home, but it went something like this:

// On Generating the map, I used an algorithm to generate hills etc but thats not needed here: public void Generate() { Texture2D map = new Texture2D(width, height);

 for(int i=0; i<width;i++){
 for(int j=0;j<height;j++){

 if ( j < width/2 ) 
 {
     map.SetPixel(i, j, Color.green);
 }
 else
 {
     map.SetPixel(i, j, Color.Black);
 }

 }}

 map.Apply();
 SetSpawnPoint();

}

// This is where it always went wrong since it would place the spawn right at the top of the map. suggesting that the very top most pixel was not black. public void SetSpawnPoint() { int xPos = Mathf.RoundToInt( Random.Range(width/4, 3*width/4));

 for(int j=0;j<height;j++)
 {
     if( map.GetPixel(xPos, j) != Color.black )
     {
         map.SetPixel(xPos, j-1, Color.cyan);
         map.Apply();
         break;
     }
 }

}

It was almost as if Unity is applying some form of antialiasing to the Texture even though it wasn't being displayed anywhere. I did also try using Debug to have it print out the color it found and what it was comparing it with, but that also didn't work.

From my searches on google, I found that most people with a similar problem were having issue with textures they had imported, but I'm not importing. Their issue was with the compression system used by unity, however Since I'm generating this texture, I shouldn't be having these problems right?

I'd appreciate a quick response and I'll add more detail if needed.

Kind Regards, John

Comment
Add comment · Show 2
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
avatar image robertbu · Dec 26, 2013 at 07:35 PM 0
Share

In the Unity 'Color' struct, values for r, g, b, a are in the range of 0.0 to 1.0. There is a 'Color32' which has more traditional values in the range of 0 to 255. Setting pixels individually using GetPixels() and SetPixels() is expensive. Look at GetPixels()/SetPixels() (or better yet GetPixels32()/SetPixels32()), to get and set all the pixels rather than deal a function call for each pixel.

avatar image Pancratius · Dec 27, 2013 at 07:41 PM 0
Share

Works perfectly now and a lot faster, thanks a lot Robertbu!

I do have one more question though. Now that I have this Texture2D, I need to save it, however the problem of DXT Compression is co$$anonymous$$g up again... Is it really going to cause me a problem by changing the colors of some pixels? or will the pixels keep the correct colors?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by team_eden · Dec 27, 2013 at 07:54 PM

Please post your solutions as an actual answer so others can reference this in the search indexes. Thank you


In the Unity 'Color' struct, values for r, g, b, a are in the range of 0.0 to 1.0. There is a 'Color32' which has more traditional values in the range of 0 to 255. Setting pixels individually using GetPixels() and SetPixels() is expensive. Look at GetPixels()/SetPixels() (or better yet GetPixels32()/SetPixels32()), to get and set all the pixels rather than deal a function call for each pixel.

robertbu 1 day ago

Comment
Add comment · Show 1 · Share
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
avatar image team_eden · Dec 16, 2014 at 07:03 PM 0
Share

$$anonymous$$oderating my answer does not help, if I cannot see the answer which was wrong to begin with. FYI

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Terrain generation problem. 1 Answer

Generating cars in a parking garage? 1 Answer

Generating Many Objects at Once - Alternative Options? 0 Answers

2d minecraft survival game 1 Answer

Generating Lightmaps in Unity vs Using your own UV's 0 Answers


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