Why is my UV'd texture displaying wrapped with the wrong scale?

Hey folks. Still getting to grips with shaders here and I’m having an issue where my UV unwrapped texture for my creature is either appearing multiple times on the creature surface or (if I mark the texture clamped) is only showing part of the texture stretched across the creature.

Shader code follows:

Shader "Custom/CaveCreatureShader2"
    {
        Properties
        {
            _MainTex ("C", 2D) = "white" {}
            // How much creature vs cave material.
            _PercentCreature ("Percentage Creature", Range(0,1)) = 0
 
            // Standard shader
            _Color("Color", Color) = (1,1,1,1)
            _CreatureTex("Albedo (RGB)", 2D) = "white" {}
            _Glossiness("Smoothness", Range(0,1)) = 0.5
            _Metallic("Metallic", Range(0,1)) = 0.0
        }
        SubShader
        {
            Tags { "RenderType"="Opaque" }
            LOD 100
 
            //Pass
            //{
                CGPROGRAM
 
                #include "UnityCG.cginc"
 
                #pragma vertex vert
                // Use 'custom stupid' lighting...
                #pragma surface surf CustomStupid vertex:vert
            
                #include "UnityPBSLighting.cginc"
 
 
                //fixed4 _Color;
 
                struct appdata
                {
                    float4 vertex : POSITION;
                    float2 uv : TEXCOORD0;
                };
 
                struct Input
                {
                    float2 uv_MainTex;
 
                    //--- TRIPLANAR STUFF ---//
 
                    float3 worldNormal;
                    float3 worldPos;
 
                    float3 localPos; // Local position of vertex
                };
 
                // Helper funcs!
                float4 tex3D_2D(in float3 pos, in float3 normal, sampler2D tex) {
                    return     tex2Dlod(tex, float4(pos.y, pos.z, 0, 0)) * abs(normal.x)
                          + tex2Dlod(tex, float4(pos.x, pos.z, 0, 0)) * abs(normal.y)
                          + tex2Dlod(tex, float4(pos.x, pos.y, 0, 0)) * abs(normal.z);
                }
 
                sampler2D _MainTex;
                sampler2D _CreatureTex;
 
                // float4 _MainTex_ST;
                float _PercentCreature;
                half _Glossiness;
                half _Metallic;
 
                void vert (inout appdata_full v, out Input o)
                {
                    UNITY_INITIALIZE_OUTPUT(Input, o);
 
                    o.localPos = v.vertex.xyz;
                }
 
                // Apply bump map.
                float3 material_TextureBump(in sampler2D tex, inout float3 normal, float3 p) {
                    // TEMP!!
                    const float scale = 0.04;
                    const float normScale = 0.03;
 
                    normal = normalize(normal);
                    float3 col = tex3D_2D(p*scale, normal, tex).rgb;
 
                    return col;
                }
 
                float3 getLights(in float3 color, in float3 pos, in float3 normal) {
                    float3 lightValue = 0.1 * float3(1, 1, 1);
 
                    const float lightStr = 0.5;
                    const float3 lightDir = float3(1, 1, 0);
 
                    float diff = max(lightStr * dot(-lightDir, normal), 0.0);
                    float3 diffuse = diff * lightValue;
 
                    lightValue = color * diffuse;
 
                    return lightValue;
                }
 
                half4 LightingCustomStupid(SurfaceOutputStandard s, half3 viewDir, UnityGI gi) {
                    float p = 0; // Disable custom lighting for the time being... Doesn't play nicely.
                    return lerp(half4(s.Albedo, 1), LightingStandard(s, viewDir, gi), p);
                }
 
                inline void LightingCustomStupid_GI(
                    SurfaceOutputStandard s,
                    UnityGIInput data,
                    inout UnityGI gi)
                {
                    LightingStandard_GI(s, data, gi);
                }
 
                void surf(Input IN, inout SurfaceOutputStandard o)
                {
                    float3 p = IN.worldPos;
                    float3 lp = IN.localPos * 10;  // TODO: Tweak scaling.
                    float3 n = IN.worldNormal;
 
                    // Then, we get the color of the world at that point, based on our material ids.
                    // Interpolated between cave and wall colours
                    float3 colorBase =
                        material_TextureBump(_MainTex, n, p) * (1-_PercentCreature) + // Cave/Wall texture
                        tex2D(_CreatureTex, IN.uv_MainTex).rgb * _PercentCreature;     // Creature texture
 
 
                    float3 light = getLights(colorBase, p, n);
 
                    // The ambient color is applied.
                    const float3 _AmbientColor = float4(13.0,13.0,13.0,255).rgb / 255;
                    float3 color = colorBase * _AmbientColor;
 
                    // And lights are added.
                    color += light;
 
                    // Dist to camera
                    float dist = distance(p, _WorldSpaceCameraPos) / 35;
 
                    // Fog!
                    const float4 FogColor = float4(169, 190, 191, 255) / 255.0;
                    const float FogDensity = 0.01;
                    color = lerp(color, FogColor, 1.0-exp2(-FogDensity * dist * dist));
 
                    o.Albedo = color.rgb;
                
                    o.Metallic = _Metallic;
                    o.Smoothness = _Glossiness;
                    o.Alpha = 1;
 
                }
                ENDCG
            //}
        }
    }

I’m really confused. Can anybody help me get the creature texture appearing as a normal skinned image? Running out of time here, so a fast response would be appreciated…!

The image you posted doesn’t work. But the shader appears to be doing a basic (and wrong) triplanar texturing using the _MainTex “C”, and using the mesh’s normal UVs for the _CreatureTex “Albedo (RGB)”. But it’s also using the IN.uv_MainTex for its UVs which means it’ll be applying the _MainTex’s texture scale and offset settings. You can rename uv_MainTex to uv_CreatureTex in both the Input struct and the surf function to fix that.

1 Like

@bgolus - Thank you so much for this response. Annoyed I’m doing triplnar incorrectly, but I’m sorta fumbling around in shaders at the moment and have a lot to learn. You identified the exact crux of the problem and provided the solution. Appreciated.