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
1
Question by codeimpossible · Aug 02, 2021 at 01:53 PM · shadershlsl

HLSL convert float to int losing precision

I have the following frag function in my vertex shader: (the rest of the shader is the Sprite-Default shader that ships with Unity).

 float4 SpriteFrag(v2f IN) : SV_Target
 {
     float4 c = tex2D (_MainTex, IN.texcoord);
     int r = int(c.r * 255);
     int g = int(c.g * 255);
     int b = int(c.b * 255);
 
     c.r = r / 255.0f;
     c.g = g / 255.0f;
     c.b = b / 255.0f;
                
     c.rgb *= c.a;
                 
     return c;
 }

and the following color value gets sampled: RGBA(68, 48, 36, 255). I would like to cast the R, G, B values to ints to perform bit manipulation on them, but when I cast the float values they aren't accurate. My values end up as:

 r = 66
 g = 46
 b = 34

The values are not consistently off by 2 either (otherwise i would just increment them and work with that). What am I doing wrong?

note: if I return c unmodified, the color values are correct.

Update:

I feel like I'm going insane here. Returning a hardcoded red value seems to be incorrect.

 fixed4 SpriteFrag(v2f IN) : SV_Target
 {
     float4 c = tex2D (_MainTex, IN.texcoord);

     c.r = float(48.0/255.0);
     c.g = 0;
     c.b = 0;

     c.rgb *= c.a;
             
     return c;
 }

This should set the R channel to 48, or 0.18823529411, but instead it sets it to 120 or 0.4705883. WTAF is going on here?

Comment
Add comment · Show 2
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 viesc123 · Aug 02, 2021 at 03:44 PM 0
Share

Hey there. How are you checking the color values that the shader produces? In code, or with the color picker?

avatar image codeimpossible viesc123 · Aug 02, 2021 at 08:21 PM 0
Share

I was checking it using the color picker which was matching the same sprite drawn by the default sprite shader.

1 Reply

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

Answer by Bunny83 · Aug 02, 2021 at 03:40 PM

and the following color value gets sampled

Where and how do you get that value? Keep in mind that depending on the actual shader you may have to consider lighting or other post processing effects. Apart from that what bites most people are gamma corrections either when textures are imported or the gamma correction of the actual output. See gamma workflow for more information. If you have never heard about gamma / gamma correction, you may have a look at the wikipedia article to get a better understanding what it is or why it's applied.

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 codeimpossible · Aug 02, 2021 at 04:26 PM 0
Share

The shader is an unlit sprite shader, so there's no lighting information and the sprite is written in sRGB so it's being stored gamma corrected already. Also, shouldn't returning a hardcoded red value still work as expected?

avatar image Bunny83 codeimpossible · Aug 02, 2021 at 04:57 PM 0
Share

No, not if your color space is set to linear since Unity would apply gamma correction on the output. When you set your color mode to gamma, the color will be output exactly as you stated since Untiy assumes we are already in gamma space. Keep in $$anonymous$$d that gamma corrections are applied because of the behaviour of monitors and the human eye. So the output gamma correction is the one that impacts you the most.


Colors are not as simple as you may think ^^. Anyways when switching the color space (Project settings --> Player --> Other settings --> color space) to gamma, there will be no gamma correction applied to your output. I bet it's currently set to linear.

avatar image codeimpossible Bunny83 · Aug 02, 2021 at 08:24 PM 0
Share

Thank you for the explanation, it was driving me crazy. I think I understand my mistake now: I was outputting the color value as I expected it to be (gamma corrected) but it was then further gamma corrected by Unity after the fragment shader ran.

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

145 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 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 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 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 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 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 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

Simple Unlit HLSL Shader with Rim Lighting? 1 Answer

Can this shader run in unity? 1 Answer

How to get picked worldposition to clipspace, in fragment shader? (Image effect shader) 0 Answers

How to get Initial texture in Post-Processing Stack v2 1 Answer

2D Water top surface profile 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