Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

askthedev.com Logo askthedev.com Logo
Sign InSign Up

askthedev.com

Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Ubuntu
  • Python
  • JavaScript
  • Linux
  • Git
  • Windows
  • HTML
  • SQL
  • AWS
  • Docker
  • Kubernetes
Home/ Questions/Q 39348
In Process

askthedev.com Latest Questions

Asked: April 5, 20252025-04-05T06:14:22+05:30 2025-04-05T06:14:22+05:30

How can I implement tiling and offset for multiple texture slots in my Unity surface shader?

anonymous user

I’m stuck trying to figure out how to implement tiling and offset for multiple texture slots in my Unity surface shader. I’ve made a custom shader based on Unity’s standard surface shader, but I’ve hit a wall. Right now, it seems that only the albedo texture slot (_MainTex) is responding to the tiling and offset properties. I want to get the same functionality for my normal texture slot (_NormalTex), but I can’t seem to get it to work.

Here’s the thing: when I adjust the tiling and offset for the _MainTex in the material properties, everything behaves as expected. The texture scales up or down based on the values I put in. But then I tried to do the same for the _NormalTex and nothing happens; it just stays the same no matter what values I enter.

From what I understand, I should be able to have individual control over each texture’s tiling and offset, right? I checked my shader code and noticed that I’m only passing the _MainTex UVs (IN.uv_MainTex) to the tex2D function for both the albedo and normal textures. Shouldn’t I set up separate UVs for each texture slot to properly support the tiling and offset functionality?

I’m a bit overwhelmed with where to go from here. I mean, I want to maintain the integrity of the surface shading, but I also want full control over the textures. I was thinking of perhaps defining new UV inputs for my normal texture or modifying the input structure, but I’m not really sure how to approach this.

Anyone else run into this issue or have any pointers on how to correctly set up the tiling and offset for multiple texture slots in a surface shader? Any advice or code snippets would be super helpful! Thanks!

  • 0
  • 0
  • 2 2 Answers
  • 0 Followers
  • 0
Share
  • Facebook

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Continue with Google
    or use

    Forgot Password?

    Need An Account, Sign Up Here
    Continue with Google

    2 Answers

    • Voted
    • Oldest
    • Recent
    1. anonymous user
      2025-04-05T06:14:24+05:30Added an answer on April 5, 2025 at 6:14 am

      Texturing Troubles in Unity Surface Shader

      It sounds like you’re dealing with a common issue when it comes to handling multiple texture slots in Unity shaders. You’re right that you should be able to control the tiling and offset for both your albedo (_MainTex) and normal (_NormalTex) textures.

      The key thing here is that you need to use different UVs for each texture. It seems like you’re only using the UVs for _MainTex (IN.uv_MainTex) for both textures, which is why _NormalTex isn’t reacting to the tiling and offset settings you input.

      Here’s a rough idea of what you can do:

      1. Define a new set of UV inputs for your normal texture in your shader’s input structure. For instance, you could add float2 uv_NormalTex;.
      2. Then, when you’re doing the calculations in the shader, make sure to sample _NormalTex using this new UV set. You would write something like:
      3.         fixed4 normalSample = tex2D(_NormalTex, IN.uv_NormalTex);
                
      4. When you set the material properties in the Unity editor, you should be able to control Tiling and Offset for both the albedo and normal textures separately.

      Here’s a tiny code snippet to illustrate:

          struct Input {
              float2 uv_MainTex;
              float2 uv_NormalTex; // New UV for normal texture
          };
          
          void surf(Input IN, inout SurfaceOutputStandard o) {
              fixed4 albedo = tex2D(_MainTex, IN.uv_MainTex);
              fixed4 normal = tex2D(_NormalTex, IN.uv_NormalTex);
              // do something with albedo and normal
          }
          

      Also, don’t forget to define the tiling and offset parameters for your normal texture in the properties section of the shader, like this:

          _NormalTex ("Normal Map", 2D) = "white" {}
          _NormalTex_Tiling ("Normal Map Tiling", Vector) = (1, 1, 0, 0)
          

      Then make sure to read the tiling and offset values in your shader code and apply them to the UVs of _NormalTex. This way, you’ll maintain the integrity of your surface shading while having complete control over your textures!

      Good luck and happy coding!

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. anonymous user
      2025-04-05T06:14:25+05:30Added an answer on April 5, 2025 at 6:14 am

      The issue you’re experiencing arises because right now you’re only using UV coordinates for your _MainTex (e.g., IN.uv_MainTex) for sampling both textures. This means your normal map texture, _NormalTex, is locked to the same tiling and offset parameters as your albedo texture. To independently control tiling and offsets for multiple texture slots, you need to define separate UV inputs for each texture in your shader’s input structure and then apply Unity’s built-in TRANSFORM_TEX macro to these UV coordinates individually.

      To correctly implement this, adjust your input struct in your surface shader by adding a dedicated UV coordinate such as float2 uv_NormalTex;. Then, in your surf function, sample your normal texture using tex2D(_NormalTex, IN.uv_NormalTex). In your shader, ensure you’ve declared the texture scale and offset by adding the line float4 _NormalTex_ST; and apply Unity’s macro o.uv_NormalTex = TRANSFORM_TEX(v.texcoord, _NormalTex); within the vertex function. Following these adjustments will grant you independent control over both the albedo and normal textures’ tiling and offset.

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    Sidebar

    Recent Answers

    1. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    2. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    3. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    4. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    5. anonymous user on How can I update the server about my hotbar changes in a FabricMC mod?
    • Home
    • Learn Something
    • Ask a Question
    • Answer Unanswered Questions
    • Privacy Policy
    • Terms & Conditions

    © askthedev ❤️ All Rights Reserved

    Explore

    • Ubuntu
    • Python
    • JavaScript
    • Linux
    • Git
    • Windows
    • HTML
    • SQL
    • AWS
    • Docker
    • Kubernetes

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.