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 xgono · Apr 28, 2017 at 05:59 AM · textureshadersshader programming

Shader: texture sample exact pixel value

Is it possible to sample a pixel during shading and getting the exact color values as are stored in the texture image? For example if my first pixel has a red byte value of for example 212 (thats 11010100 in binary), is it possible to get exactly this value during the shading process for a point corresponding to that pixels location?

My problem is, that when I tried the following code and debugged the red value, it was slightly off, as if some rounding or interpolation with nearby pixels occured

 CGPROGRAM
     #pragma vertex vert
     #pragma fragment frag
   
     #include "UnityCG.cginc"
 
     fixed4 _Color;
     sampler2D _MainTex;
 
     struct v2f {
         float4 pos : SV_POSITION;
         float2 uv : TEXCOORD0;
     };
 
     float4 _MainTex_ST;
 
     v2f vert(appdata_base v)
     {
         v2f o;
         o.pos = UnityObjectToClipPos(v.vertex);
         o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
         return o;
     }
 
     fixed4 frag(v2f i) : SV_Target
     {
         // resolution of the texture is 1024*1024
         float texSize = 1024;
         // sample the texture but only at positions corresponding to whole pixels
         // multiplying the position by the texture width and then flooring it should
         // bring it into the [0, 1023] interval with only integer values
         // dividing it by the texture width again should return the interval to [0, 1]
         float2 myUV = float2(floor(i.uv.x * texSize) / texSize, floor(i.uv.y * texSize) / texSize);
         fixed4 texcol = tex2D(_MainTex, myUV);
         // I want to get exactly the value that was in the pixel at the sampled location
         fixed redPixelValue = floor(texcol.r * 256)
         return texcol * _Color;
     }
     ENDCG


Is there something I am missing? Thank you for any answers!

Comment
Add comment · Show 4
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 Namey5 · May 01, 2017 at 11:00 AM 1
Share

$$anonymous$$y suggestion would be to use a higher precision format, although there's no guarantee that would work. I've also changed some things just to make it more usable (texSize for any texture, etc.);

  CGPROGRA$$anonymous$$
      #pragma vertex vert
      #pragma fragment frag
    
      #include "UnityCG.cginc"
  
      //fixed4 _Color;
      sampler2D _$$anonymous$$ainTex;
  
      struct v2f {
          float4 pos : SV_POSITION;
          float2 uv : TEXCOORD0;
      };
  
      float4 _$$anonymous$$ainTex_ST;
      float4 _$$anonymous$$ainTex_TexelSize;
  
      v2f vert(appdata_base v)
      {
          v2f o;
          o.pos = UnityObjectToClipPos(v.vertex);
          o.uv = TRANSFOR$$anonymous$$_TEX(v.texcoord, _$$anonymous$$ainTex);
          return o;
      }
  
      float4 frag(v2f i) : SV_Target
      {
          float2 texSize = 1/_$$anonymous$$ainTex_TexelSize.xy;
          // sample the texture but only at positions corresponding to whole pixels
          // multiplying the position by the texture width and then flooring it should
          // bring it into the [0, 1023] interval with only integer values
          // dividing it by the texture width again should return the interval to [0, 1]
          float2 myUV = float2(floor(i.uv.x * texSize.x) / texSize.x, floor(i.uv.y * texSize.y) / texSize.y);
          float4 texcol = tex2D(_$$anonymous$$ainTex, myUV);
          float redPixelValue = texcol.r;
          return float4 (texcol, 0, 0, 1);    // * _Color;
      }
      ENDCG

$$anonymous$$ight help, might not.

avatar image xgono Namey5 · May 03, 2017 at 07:28 PM 0
Share

I already found the culprit - the texture was being compressed on import.

Though your answer did help me to get a cleaner code. Thank you!

avatar image _dns_ · May 01, 2017 at 03:04 PM 2
Share

Hi, I'm not sure i understand what you want to do: doesn't using "point" filtering on the texture settings does what you want ? (bilinear or trilinear will always sample an interpolation of multiple texels)

avatar image xgono _dns_ · May 03, 2017 at 07:20 PM 0
Share

I was already using point filtering, I must be overlooking something else. Thank you for you time, though!

Edit: I finally found it! The texture was compressed on import, I found the setting when I double checked the point filtering.

Setting it to uncompressed ARGB 32 bit did the trick!

Thank you!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Steamc0re · Aug 10, 2018 at 03:29 AM

/ 255

not /256

@xgono values are 0-255 right?

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 Steamc0re · Aug 10, 2018 at 03:35 AM 0
Share

oh and you need the center of the pixel, so your values are actually .5 to 1023.5

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

Shader: 1 Base Texture Overlaid by 1 Transparent Texture. 0 Answers

Default Uniy Shader variables are not working. 1 Answer

How to move alpha mask along the floor plane as the user (camera) moves 1 Answer

Shader: Modify XY Position of Texture 0 Answers

How to make homogenous lighting with point lights? 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