• 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
Question by wibble82 · Sep 22, 2014 at 03:14 PM · shadertextureuvcgsurface

uv not being passed through from vertex to surface shader

Hi

I'm getting up to scratch writing shaders in unity, and am trying to get the uv from the verts through to the surface shader. However for some reason when I declare a variable for the surface shader input named 'uv_x', it isn't filled in with the uv from the mesh. To verify the uvs weren't wrong, I tried just manually passing it through in a variable called 'hello' and get the expected result. This is my code (or a snippet of it)

     SubShader 
     {
 
 
 
         
         CGPROGRAM
         #pragma surface surf Lambert vertex:vert
         #pragma glsl
         #pragma target 3.0
 
         sampler2D _SDF_Texture;
         
         struct Input 
         {
             float3 pos;
             float2 uv_sdf;
             float2 hello;
         };
 
         void vert (inout appdata_full v, out Input OUT)
         {
             UNITY_INITIALIZE_OUTPUT(Input, OUT);
 
             //manually forcing through the uv value has no effect
             //OUT.uv_sdf = v.texcoord;
 
             //try passing uv value through just as a general parameter
             OUT.hello = v.texcoord;
         }
 
         void surf (Input IN, inout SurfaceOutput o) 
         {
             //always black (i.e. uv_sdf is always 0s)
             //float4 color = float4(IN.uv_sdf.x,IN.uv_sdf.y,0,1);
 
             //shows the colour gradient correctly across a quad (i.e. bl=black, tl=green, br=red, tr=yellow)
             float4 color = float4(IN.uv_sdf.x,IN.uv_sdf.y,0,1);
 
             o.Albedo = color.rgb;
             o.Alpha = 1.0;
         }
         ENDCG
     }
     

I'm a little baffled. Manually forcing uv_sdf to be the right value has no effect, which suggests its being automatically overwritten with something, but not the uvs from the mesh. I tried explicitly marking uv_sdf as TEXCOORD0 but still no luck.

Anyone any thoughts on this one?

cheers

-Chris

Comment
sosh

People who like this

1 Show 1
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 tanoshimi · Sep 22, 2014 at 05:52 PM 0
Share

I'm a bit confused by the comments for the following lines in your surf() function:

 //always black (i.e. uv_sdf is always 0s)
 float4 color = float4(IN.uv_sdf.x,IN.uv_sdf.y,0,1);
 
 //shows the colour gradient correctly across a quad
 float4 color = float4(IN.uv_sdf.x,IN.uv_sdf.y,0,1);

My eyes might be failing me, but I couldn't see any difference between those lines - how does one create all black and the other colour correctly?

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by moosefetcher · Jan 21, 2016 at 06:36 PM

I've just spent the best part of a day figuring this out - I couldn't find a version of this question with an answer, so I'll attempt to answer it here: (Sorry this is 4 months late - I'm new to Unity)

First up you need to name your texture in your Input struct, with the format uv followed by TextureName:

 float2 uv_MainTex;

You'll then be able to access the UV coordinates in the surf struct and add an offset to sample MainTex in an alternate location, like this:

 void surf (Input IN, inout SurfaceOutput o) {
        float2 offsetUVs = IN.uv_MainTex;
        offsetUVs.y += _adjustmentAmount;
        _surfaceColour = saturate(tex2D (_MainTex, offsetUVs).rgba * _offsetColour.rgba);
 }

The tex2D method takes a sampler2D and a float2 as parameters, so you can make a change to the float2 from the input struct (IN.uv_MainTex) and pass the altered float2 into the tex2D method instead. I hope that works for you.

Comment
sosh

People who like this

1 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

25 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

Related Questions

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

Assigning UV Map to model at runtime 0 Answers

TexGen'ed UVs - how to access in surface shader? 1 Answer

uv.xyz in a surface shader? 2 Answers

Shifting Texture Coordinates via CG 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