Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by DaveA · Aug 29, 2011 at 10:41 PM · colorsetpixelsgreyscale

SetPixels and Alpha8 - most efficient method?

I need to set an Alpha8 texture quickly (I generate it on the fly). I see that SetPixels will work with it, but that takes a Color array (note: the float value kind with RGBA). SetPixels32 works with 8-bit RGBA's. What I was hoping for was something like a SetPixels8, or Color8 or ColorGray type of API (none exist that I know of).

So do I really need to allocate a 32-bit color array for my 8-bit bitmap? Or a full RGBA pixel array that converts to float and back? Is there a better way?

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

2 Replies

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

Answer by Eric5h5 · Aug 29, 2011 at 11:05 PM

SetPixels is the only way. Sounds like a good candidate for a feature request though.

Comment
Add comment · 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
7

Answer by Lumen Digital · Nov 23, 2014 at 08:06 PM

Try creating your texture as a byte array and use an Alpha8 texture...

Create the texture

 texture = new Texture2D(width, height, TextureFormat.Alpha8, false);

Create a buffer

 pixelBuffer = new byte[width * height];

Fill the buffer with the pixel values (0 - 255)....

Load the buffer into the texture

 texture.LoadRawTextureData(buffer);
 texture.Apply();

To get the pixel vale in your shader use the alpha value of the texture

tex2D(_MainTex, i.texcoord).a

This works for us.

Comment
Add comment · Show 2 · 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 ai_enabled · Jul 23, 2015 at 11:29 AM 0
Share

Yes, this works very well for us too. Thank you for this answer!

avatar image Orion_78 · Apr 11, 2018 at 09:57 AM 0
Share

This almost works, but i feel like it copy 1/4 of the picture over and over on the same picture. How could I fill the pixel values with only the alpha of a texture efficiently ?

For now, I have

textureA.LoadRawTextureData(Texture2DRGB.GetRawTextureData());

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I fill a texture with one color? 2 Answers

Texture randomly generated terrain 0 Answers

Convert 2D array to a 1D array 2 Answers

Color a greyscale image using a gradient [SOLVED] 4 Answers

Material doesn't have a color property '_Color' 4 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