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 Professor Snake · Jul 09, 2013 at 07:33 PM · shaderuvoffsetblursurface

UV offset in surface shader.

Hello there, I am working on a simple surface shader that blurs its texture, mainly as an experiment for uv coordinate offsets. Here is the code that i am using.

 half4 p;
 for(i=0;i<3;i++)
    for(j=0;j<3;j++){
      p.rgb+=tex2D (_MainTex, float2(IN.uv_MainTex.x+(i-1),IN.uv_MainTex.y+(j-1)))/9;
    }
  o.Albedo=p.rgb;

Unfortunately, this makes the object's surface completely black. Removing the +(i-1) and +(j-1) parts makes the shader behave normally again, but obviously without the desired result.

What am i doing wrong?

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 CHPedersen · Jul 10, 2013 at 11:00 AM

It's hard to say why the lookup becomes black without knowing what the texture looks like and whether its wrap mode is set to Repeat or Clamp. But I think part of your problem stems from a slight misunderstanding about UVs: They are not discrete, integer pixel coordinates. You can't "move to the next pixel" by adding or subtracting 1. Instead, they are normalized ranges across the texture. 0,0 is the lower left corner of the texture and 1,1 is the upper right corner. So when you add whole integers to the UV coordinates before your lookup, you're causing the UVs to leave the 0-1 range, becoming < 0 and > 1 as the loops iterate.

To grab the next pixel, you have to include a division by the texture's height and width and then add that to the pixel's interpolated UV. Try that, and see if it has an effect on the calculated color.

On another note, try to limit divisions or avoiding them entirely in shaders where possible. A division is a lot more computationally expensive than a multiplication. E.g. here, instead of dividing by 9, multiply by 0.111111f. :)

Comment
Add comment · Show 5 · 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 Professor Snake · Jul 10, 2013 at 01:51 PM 0
Share

I forgot to mention that i also tried IN.uv_$$anonymous$$ainTex.x+((i-1)*0.005) , as well as replacing 0.005 with a multitude of other <1 floats. Tried

float2(IN.uv_$$anonymous$$ainTex.x+((i-1)*0.00005f),IN.uv_$$anonymous$$ainTex.y+((j-1)*0.00005f))

with various textures and import settings, but the result is still the same.

avatar image CHPedersen · Jul 10, 2013 at 02:18 PM 0
Share

Well, 0.00005 isn't gonna work because if the next pixel is that close in normalized UVs, it means your texture's resolution is 1/0.00005 = 20000x20000. ;) You should be multiplying by 1 / textureWidth on the x and 1 / textureHeight on the y.

But that's not the issue, yet. First, we gotta figure out why you're getting all blacks. Can you try to simplify the shader a little to see if the look-ups are giving you what you expect? $$anonymous$$aybe something is wrong in the calculation of the color average in that loop. What happens if you unroll it manually and do the 9 lookups side by side? Can you get a sensible color out of them outside the loops?

avatar image CHPedersen · Jul 10, 2013 at 02:20 PM 0
Share

Oh, and one more thing: Beware that Cg/HLSL does not have loop count registers until Shader$$anonymous$$odel 3 and above. See this list:

http://en.wikipedia.org/wiki/HLSL#Shader_model_comparison

I don't think that's the issue because your shader does compile, but for good measure, make sure it compiles to profile 3 or above.

avatar image Professor Snake · Jul 10, 2013 at 02:45 PM 0
Share

I used that small a number purely for testing, as it should produce similar results to +0. Strangely enogh, doing everything manually ins$$anonymous$$d seemed to work, even though i added #pragma target 3.0 at the top of my shader.

avatar image CHPedersen · Jul 10, 2013 at 02:47 PM 0
Share

I think the compiler had a problem parsing those for-loops, though I have no idea why, and if so, why it didn't warn you. $$anonymous$$aybe it had to do with the loops being nested. This is kind of an anti-solution since you ended up with working, but uglier code. ;) But I'm glad you got it working nonetheless.

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

16 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

Related Questions

Adding a UV offset to tex2dproj. 1 Answer

uv not being passed through from vertex to surface shader 1 Answer

Material offset relative to other game object 0 Answers

Second UV set as coordinates for a different texture? 3 Answers

TexGen'ed UVs - how to access in surface shader? 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