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
1
Question by PsyDev · Nov 01, 2016 at 03:41 PM · uvuv mappingsurface shader

How to get texture UV tiling and offset in a Surface Shader?

I am trying to obtain the UV tiling and offset in a surface shader. I'm referring to the standard variables you see in the material inspector when you add a texture property to your shader: _MainTex("Base (RGB) Gloss (A)", 2D) = "white" {}

I have seen plenty of examples and discussion about how to use _MainTex_ST and the TRANSFORM_TEX macro, but that seems to only be useful in vertex and fragment shaders, not Unity's magic Surface Shaders. You can declare UV coordinates as input to the Surface Shader, but that only seems to include UV, not tiling and offset. I looked at the complied code from the surface shader, and it seems to pack the UVs into the xy and the bump map UVs into the zw of a "pack" variable, with no tiling or offset.

I have also seen suggestions to create your own uv adjustment variable, and modify the UVs via that. I'm sure that works, but it bypasses the tiling and offset variables that are visible in the inspector.

Any suggestions as to how I can obtain the tiling and offset variables from the material inspector in a surface shader?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Glurth · Nov 01, 2016 at 04:25 PM

You mentioned you know about this:

"Materials often have Tiling and Offset fields for their texture properties. This information is passed into shaders in a float4 {TextureName}_ST property

....

For example, if a shader contains texture named _MainTex, the tiling information will be in a _MainTex_ST vector."

from: https://docs.unity3d.com/Manual/SL-PropertiesInPrograms.html

But I 'm not sure I understand what you mean about passing this in as a parameter to a surface shader. Though the UV coordinates WILL change from texel to texel, the offsets and tiling values should remain constant: Therefore I see no need to pass them as a parameters. Accessing them from the builtin variable mentioned above should be sufficient.

Here is an example using those variables in a surface shader. My usual method when figuring stuff out, or debugging shaders, is to use the output colors to represent the data I want to look at. Once this sample shader is attached to a material, and that to an object; you can change the material titling and offset, and watch color of objects follow the change, to show your data. (clearly, it does not use the actual texture to render.):

 Shader "Custom/NewSurfaceShader" {
     Properties {
         _tex0 ("Texture1", 2D) = "white" {}
     }
     SubShader {
         Tags { "RenderType"="Transparent" }
         LOD 200
         
         CGPROGRAM
         #pragma surface surf Standard alpha
         #pragma target 3.0
 
         float4 _tex0_ST;
 
         struct Input {
          float3 viewDir;
        };
            
         void surf (Input IN, inout SurfaceOutputStandard o) {
             // Albedo comes from a texture tinted by color
             o.Albedo.r = _tex0_ST.x;
             o.Albedo.g = _tex0_ST.y;
             o.Albedo.b = _tex0_ST.z;
             o.Alpha = _tex0_ST.w;
         }
         ENDCG
     }
     FallBack "Diffuse"
 }

Comment
Add comment · Show 8 · 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 PsyDev · Nov 01, 2016 at 04:56 PM 0
Share

Note that the shader properties you linked above are under the "Writing vertex and fragment shaders" section of the documentation, not the "Writing surface shaders" section. I have not been able to find any examples nor succeeded through trial and error of accessing the _xxx_ST variable in the surface function of a surface shader. If you could provide a code snippet of how that would work it would be greatly appreciated.

I do have a need to know the tiling and offset in the surface shader where I do a texture lookup.

avatar image Glurth PsyDev · Nov 01, 2016 at 05:08 PM 0
Share

"nor succeeded through trial and error of accessing the _xxx_ST variable in the surface function of a surface shader."

Oh! $$anonymous$$y bad then, I don't write many surface shaders. Still, I'll poke around the couple I've done to see if I used any ST variables.

avatar image PsyDev Glurth · Nov 01, 2016 at 05:10 PM 0
Share

Thanks, I appreciate you taking the time anyway.

Show more comments
avatar image PsyDev · Nov 01, 2016 at 06:23 PM 0
Share

Well that sample code definitely works. The problem is that when I add in code to sample the texture, I get a compile error. There seems to be some conflict in the auto-generated code that results from adding a sampler2D and tex2D call.

Here is your original code, plus the addition of a sampler2D, float2 texture coordinates in the Input structure, and the texture lookup. This results in a compilation error:

 Shader "Custom/SurfTiling" {
     Properties{
         _tex0("Texture1", 2D) = "white" {}
     }
         SubShader{
         Tags{"RenderType" = "Transparent"}
         LOD 200
 
         CGPROGRA$$anonymous$$
 #pragma surface surf Standard alpha
 #pragma target 3.0
 
         sampler2D _tex0;
         float4 _tex0_ST;
 
     struct Input {
         float2 uv_tex0;
     };
 
     fixed4 _Color;
 
     void surf(Input IN, inout SurfaceOutputStandard o) {
         // Albedo comes from a texture tinted by color
         //o.Albedo.r = _tex0_ST.x;
         //o.Albedo.g = _tex0_ST.y;
         //o.Albedo.b = _tex0_ST.z;
         //o.Alpha = _tex0_ST.w;
 
         fixed4 tex = tex2D(_tex0, IN.uv_tex0);
         o.Albedo = tex.rgb;
     }
     ENDCG
     }
         FallBack "Diffuse"
 }

Shader compiler error: Shader error in 'Custom/SurfTiling': redefinition of '_tex0_ST' at line 67 (on d3d11)

avatar image PsyDev PsyDev · Nov 01, 2016 at 06:27 PM 0
Share

Edit: I originally pasted the wrong compiler error, just updated to the redefinition error.

avatar image Glurth PsyDev · Nov 02, 2016 at 12:49 AM 0
Share

I remarked out the float4 _tex0_ST; and references to it, and got it to compile. I then looked at the generated compiler code, and sure enough, it stuck it's own version of float4 _tex0_ST; somewhere in there with the generated stuff.
I have no idea how to deal with that. I tried saving and modifying the generate code, to no avail.

avatar image PsyDev Glurth · Nov 02, 2016 at 11:48 AM 0
Share

Well I sure do appreciate you taking the time to look into it. I am going to leave it for now, and add my own property in the shader for tiling. That workaround works fine, but now there is redundancy, and if someone uses the tiling variable they are used to using, it won't work as expected.

Unity is The Pareto Principle in full effect.

avatar image
0

Answer by oukenshu · Apr 15, 2019 at 12:22 PM

I tried to fix it. @PsyDev

 Shader "Custom/NewSurfaceShader" {
          Properties {
              _tex0 ("Texture1", 2D) = "white" {}
          }
          SubShader {
              Tags { "RenderType"="Transparent" }
              LOD 200
              
              CGPROGRAM
              #pragma surface surf Standard alpha
              #pragma target 3.0
      
              
              struct Input {
               float3 viewDir;
               float4 _tex0_ST;
             };
                 
              void surf (Input IN, inout SurfaceOutputStandard o) {
                  // Albedo comes from a texture tinted by color
                  o.Albedo.r = IN._tex0_ST.x;
                  o.Albedo.g = IN._tex0_ST.y;
                  o.Albedo.b = IN._tex0_ST.z;
                  o.Alpha = IN._tex0_ST.w;
              }
              ENDCG
          }
          FallBack "Diffuse"
      }

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

60 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

Related Questions

Normalizing mesh UVS? 1 Answer

Sprite animation on quad with UV-coordinates 1 Answer

How do I generate a Sphere with proper UVs? 1 Answer

Split a mesh while using same UVs and texture position. 0 Answers

Distorted/Warped Textures On Character Model After Bundling For Tabletop Simulator/In A Build 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