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 screwworkn · Jul 26, 2012 at 05:00 PM · imagegetpixelsetpixel

Changing texture to pixels several problems

There are several issues I have with the following code that all can be wrapped up with "it doesn't work". I'm hoping to use this to convert parts of an image to grayscale.

  • Super slow. I did this similar code in xCode last year and it flew. Texture is 512x256.

  • The texture color doesn't change. It should appear a white but it remains unchanged on the screen

  • I had originally been doing a GetPixel and multiplying out the RGB values by .3/59/.11 to get the grayscale look. it looked something like (color.r .3f). It always returned 0. In the watch window it was appearing as a long instead of a float. Why would a float float return as long?

    Texture2D texture = (Texture2D)Plain.renderer.material.mainTexture;
    for (int iy = 0; iy < texture.height; iy++) {
    for (int ix = 0; ix < texture.width; ix++) {
    texture.SetPixel (ix, iy, new Color (0, 0, 0));
    } }
    texture.Apply ();
    Plain.renderer.material.mainTexture = texture;

Any help would be appreciated.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Eric5h5 · Jul 26, 2012 at 06:01 PM

What you have with the "texture" variable is just a reference, so 1) you don't need to do "Plain.renderer.material.mainTexture = texture" afterward, but 2) you don't actually want it to be a reference anyway, because then it's working on the actual asset...you should use Instantiate or create a new texture using the Texture2D constructor.

As for speed, aside from not working on the original asset, use SetPixels instead of SetPixel (or better yet, SetPixels32). Also try not to allocate new structs inside loops if you don't have to.

There's already a .grayscale value for Color, so just use that. float*float does not return long, it returns float, so I think you must be mistaken about that.

Comment
Add comment · Show 3 · 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 screwworkn · Jul 26, 2012 at 07:39 PM 0
Share

Ok, thanks for the push in the right direction. I still don't think I understand the difference between working on the "actual asset" vs creating a new Texture2D object. Seems to me that they are both Texture2D objects in memory, I should be able to manipulate them the same.

Your suggestion has worked, I am able now to render out the image the way I wanted. I am confused on Color32 vs Color object. Color32 is made up of bytes (which is how I always thought about Color objects. Color is floats. What's the conversion? Why the floats? Is Color just able to handle a many more colors than Color32? Why not just have a Color64?

I ended up with this code in my loop. Where colors is a Color32[];

Color c = texture.GetPixel (ix, iy); colors [ix + (iy * texture.width)] = new Color(c.grayscale, c.grayscale, c.grayscale);

avatar image screwworkn · Jul 26, 2012 at 08:35 PM 0
Share

Stupid me, there is a getpixel32....

avatar image Eric5h5 · Jul 27, 2012 at 01:13 AM 0
Share

If you just reference renderer.material.mainTexture then you will change that texture in your project permanently. (Unless mainTexture already comes from an instantiated or created texture of course.) Color32 uses 1 byte for each rgba. Color uses a float for each rgba. I expect the original intent with Color was to normalize colors so that rgba would always be 0..1 regardless of bit depth, but realistically the hardware just uses 1 byte anyway and this hasn't improved in many years, so using floats in the end just makes memory requirements go up by 4X and adds overhead for the float conversion.

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

6 People are following this question.

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

Related Questions

Finding a pixel in an images texture by raycast 0 Answers

new UI/Sprite scratch/remove using GetPixel 0 Answers

GetPixel Returning Inaccurate Color 1 Answer

SetPixel() for UI Image? 2 Answers

Best Method for Setting Mass Numbers of Pixels (SetPixel, SetPixels) 2 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