• 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 Sprakle · Feb 19, 2015 at 06:29 PM · shadertexture

Shader trouble when using TRANSFORM_TEX built-in macro

I'm trying to include a texture's Tiling and Offset values in my shader, and understand that the TRANSFORM_TEX macro must be used for that to work. Following this guide, I wrote a shader.

 Shader "Custom/Displacement Ramp Debug"
 {
     Properties
     {
         _DispTex ("Displacement Texture", 2D) = "gray" {}
     }
  
     SubShader
     {
         Tags { "RenderType"="Opaque" }
         Cull Off
         LOD 300
  
         CGPROGRAM
         #pragma surface surf Lambert vertex:disp nolightmap
         #pragma target 3.0
         #pragma glsl
         #include "UnityCG.cginc"
  
         sampler2D _DispTex;
         uniform float4 _DispTex_ST; // Needed for TRANSFORM_TEX(v.texcoord, _DispTex)
  
         struct Input
         {
             float2 uv_DispTex;
         };
  
         void disp (inout appdata_full v)
         {
             float2 newUV = TRANSFORM_TEX(v.texcoord.xy, _DispTex);
             v.vertex.xyz += newUV.x;
         }
  
         void surf (Input IN, inout SurfaceOutput o)
         {
             half4 c = tex2D(_DispTex, IN.uv_DispTex);
             o.Albedo = c.rgb;
         }
         ENDCG
     }
     FallBack "Diffuse"
 }

When compiling, I get this error on a non-existent line:

 Shader error in 'Custom/Displacement Ramp Debug': declaration of "_DispTex_ST" conflicts with previous declaration at (21) at line 62

But if I try to remove _DispTex_ST, I get this error:

 Shader error in 'Custom/Displacement Ramp Debug': undefined variable "_DispTex_ST" undefined variable "_DispTex_ST" at line 30

Any ideas on why this is happening?

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
2
Best Answer

Answer by tanoshimi · Feb 19, 2015 at 06:51 PM

From memory, I had a surface shader problem like this in the past caused by using variables that began with an underscore. Try this instead:

 Shader "Custom/Displacement Ramp Debug"
 {
     Properties
     {
       DispTex ("Displacement Texture", 2D) = "gray" {}
     }
     SubShader
     {
       Tags { "RenderType"="Opaque" }
       Cull Off
       LOD 300
       CGPROGRAM
       #pragma surface surf Lambert vertex:disp nolightmap
       #pragma target 3.0
       #pragma glsl
       #include "UnityCG.cginc"
       sampler2D DispTex;
       uniform float4 DispTex_ST; // Needed for TRANSFORM_TEX(v.texcoord, _DispTex)
       struct Input
       {
         float2 uv_DispTex;
       };
       void disp (inout appdata_full v)
       {
         float2 newUV = TRANSFORM_TEX(v.texcoord.xy, DispTex);
         v.vertex.xyz += newUV.x;
       }
       void surf (Input IN, inout SurfaceOutput o)
       {
         half4 c = tex2D(DispTex, IN.uv_DispTex);
         o.Albedo = c.rgb;
       }
       ENDCG
       }
     FallBack "Diffuse"
 }
Comment
Add comment · Show 6 · 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 Sprakle · Feb 19, 2015 at 09:26 PM 0
Share

Unfortunately that doesn't solve the problem because _{TextureName}_ST is a special variable name that Unity uses. The tiling/offset data is put into any variable with that name.

So if I rename it to not use the underscore, I don't get the required data.

avatar image tanoshimi · Feb 19, 2015 at 10:30 PM 0
Share

Did you try my code? Works for me, including the scaling and offset parameters of the texture slot (well, the x- offset and scaling, which is all you use), in 4.6.2

avatar image Sprakle · Feb 19, 2015 at 10:59 PM 0
Share

@tanoshimi Oh sorry, I messed up copy/pasting your code. It works great now, thanks!

avatar image Sprakle · Feb 19, 2015 at 11:11 PM 0
Share

No wait, that breaks uv_DispTex (line 21) because the name no longer matches, so Unity doesn't add the UV data.

And if I rename it to uvDispTex, the original conflict error comes up.

avatar image tanoshimi · Feb 20, 2015 at 02:29 PM 1
Share

Ok, I think you're right - my half-remembering a bug with variables beginning with an underscore is unrelated (something to do with the fact that Unity macros auto-prefix your variables with another underscore, and variables beginning with two underscores are reserved according to the GLSL spec (p13)). But anyway...

Let's try another solution to your problem - from your original shader, in the vertex modifier function try changing:

 float2 newUV = TRANSFOR$$anonymous$$_TEX(v.texcoord.xy, _DispTex);

to:

 float2 newUV = TRANSFOR$$anonymous$$_UV(0);

Does that work?

Show more comments

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 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

Related Questions

Textures missing after extended gameplay on Playstation game,Missing Textures after extended play on Playstation 4 game 1 Answer

How do you return to the default rendering mode after installing the lightweight-scriptable render pipeline? 1 Answer

Is it possible to create a shader that emits light into the world? 0 Answers

Cutout Material is not showing texture transparency? 0 Answers

Noob Question - Custom trees and aliasing 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