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 MD_Reptile · Aug 26, 2014 at 04:49 AM · arrayvector2pixelpixels

After placing a texture2D's pixel alpha value into an int[] array, later change that value without knowing xy coordinates?

I am trying to figure out something I have had problems with in the past, but unfortunately I cannot find the answer I had found before...

Anyway, I take a Texture2D with some transparent pixels, and create an int array (which I init by doing int[someTexture2D.GetPixels32().Length] & then assign the int value to match the alpha value) and with that array I have every pixels alpha value, which is either completely transparent or completely solid. This seems to work no problem for me and I can tell which are solid, which are not...

Now the problem is that later, I need to take specific pixels, and change them to transparent or solid, and then have the values in this array match. I just need to find the same individual value in the int array to change, based on a Vector 2 I need to change.

So I would like to utilize some poor developer testing art to help describe my problems:

alt text

Here you can see that the top left of the purple sprite (0, 64 in unity, 0, 0 in photoshop) and surrounding it is transparent, which I take this sprites texture2d, and assign all the alpha values to the int array (which again is either transparent or solid) and later I am planning on removing certain pixels somewhere from the sprite, so how can I then take the 1 dimensional int array (same length as there are pixels) and find that one exact pixel?... lets say 30, 34 - which is near the center. I can't just take x y because, well that would be wrong! Whats the math magic that is slipping my mind? Is there something as simple as counting how many times its gone over the width/height of the images pixels, maybe like x y / width, take remainder as x, or y...? I have a feeling this is one of those questions I will refer to later... many many times...

ADDITIONAL INFO:

-to assign alpha to the ints, I try to iterate through x, and then y within that iteration, and take x, y values a, assign to the int... hope that makes sense to you. Problem is I don't know which int[number] to assign, based on an x, y value!

  • wish I knew a better way to store the alpha value without referring to the texture2d itself (for speed reasons), and thought an int array would be faster, if I could just compare the int array with the vector2's of the texture2d...

  • come up with incredibly complex ways to ask simple questions.

  • found this page about a similar issue, does this comment apply to my problem:

    SniperED007 said... Looks confusing, but its pretty simple:

    Say you have an array of 5 wide and 3 down, this would be:

    When creating the arry [width * height] = [5*3] = 15.

    So now we want to compare the very first pixel which is [x=0,y=0]

    This would be [x+y*width] = [0+0*5] = 0

    The next pixel would be [x=1, y=0] This would then be [x+y*width] = [1+0*width] = 1 (remember BODMAS, so the Multiplication happens before the addition)

    Hope that makes more sense? Basically you getting rid of the Y co-ordinate and making one array with the total number of pixels and then just finding the position in that array by using x + (y * width) July 5, 2008 11:51 AM

unityhelpthing.png (9.4 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

1 Reply

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

Answer by robertbu · Aug 26, 2014 at 05:38 AM

The 2D array starts in the lower left corner and walks up the image row by row ending in the upper right corner. So given and x,y coordinate, you can find the index into the 1D array by:

  var index = x + y * textureWidth;

Going the other way, given an index into your 1D array you an do:

 y = index / width;
 x = index % width;

This is essentially what the quote you pulled says.

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 MD_Reptile · Aug 26, 2014 at 05:45 AM 0
Share

I was just testing that out now, thanks for confir$$anonymous$$g that link @robertbu!

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

22 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

Related Questions

How to get/pass (Vector2) arrays in a shader? 1 Answer

Issues with Pixel Distortion 0 Answers

Use world space for EdgeCollider2D in Script ? 0 Answers

how to get pixel colors in the GUI brush area 0 Answers

Check if values exists in an int[,] array 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