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
0
Question by slingchilders · Jan 17, 2017 at 10:45 PM · shadershadersglsl

Storing information in a texture and sending it to a shader - problem in retreiving values

I store some information in a dynamically created texture. It holds a number (0-255) in the rgb channels for each cell I have. I checked in photoshop and the values are the way they should be for each pixel.

The way i store information in is as follows in Unity:

 byte r = (myMesh.info0);
 byte g = (myMesh.info1);
 byte b = (myMesh.info2);
 
 meshInfoTexture.SetPixel(iTextureX, iTextureY, new Color32(r,g,b, 255));

In my shader i get the color of the pixel by tex2D, and then I try to get that value back to the range of 0-255:

 float4 myPixelColor = tex2D(_InfoTexture, pixelPos / 64);
 int iInfoValue = (int)(myPixelColor.b*255.0f);

However these never end up in the value ranges they were supposed to. How can I get the values back? I know the position in the texture is calculated correctly 100%, and also the values in photoshop are the way they should be (rgb numbers). I tried flooring the value, experimenting with some math, but it never works, it goes back to 0 or gets these numbers all over the place. Does this have to do with the precision of floats? Or am i missing something really simple?

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

Answer by Bunny83 · Jan 17, 2017 at 11:20 PM

Some possible problems:

  • first make sure your texture is not in a compressed format that might cause data loss.

  • Make sure you disable MipMaps (or not generating them).

  • Make sure that it is either a power of two texture or set it as non-power-of-two.

  • Make sure you set the texture filtering to point and not bilinear or trilinear.

  • casting a float to int might result in a value one off. That's because 3.0 might come out as 2.99999. The usual solution is to simply add "0.5f" before you cast it to int. This does only work for positive numbers. So 2.9999 + 0.5 --> 3.4999 --> 3

Also keep in mind that textures contain discrete values while the UV space is a continuous space. Pixels have a width and height. For example a 2x2 texture a U value of "0" would reference the left edge of the left pixel column while "1" would reference the right edge of the right pixel. 0.50001 is the left edge of the right pixel and 0.49999 is the right edge of the left pixel.

So usually when you have an image with a resolution of 64x64 you would add 0.5 to your desired pixel position and divide by "64" to sample the center of each pixel. Of course your pixel position is in range "0-63"

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

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

Unity GLSL shader - getting screen coordinates 1 Answer

Shader - What is float3.xy? 1 Answer

Shadow artifacts on vertex animation shader 1 Answer

Fake Volumetrics via custom bloom 0 Answers

Using custom tree shaders? 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