best way to handle UVs

I’ve been working on a skin shader for a while now and I’m just noticing that my uvs don’t look right in my custom lighting shader.
I only noticed when I tried someone else’s shader. On the right is bumped diffuse and on the left is my shader. I’m using the sam normals but they looked inverted in places on mine.

The shader is a half matcap, half lambert spec shader using vet/frag hack job. I suspect that I’m unwrapping the uvs wrong but this is how I learned to do it from a tutorial:
half4 tans = tex2D(_Normals, i.sktex.xy + (_Normals_ST.xy * _Normals_ST.zw));

I know there is another way to unwrap uvs but I don’t know how.

Shader"custome/Skinwrap"

{


properties

{

//_Ramp("BlendingEffects", 2D) = "white" {}

//_Range("Gloss",range(0,20))= 5

//_AmbGlo("AmbiantLighting",range(0,50))= 10

//_Occl("Oclusion",range(0,50))= 10

_MainTex("Texture", 2D) = "white" {}


_Smap("SpecMap", 2D) = "white" {}

_Shininess ("Specularity",range(0.005,5))= 2

_MatCap ("MatCap (RGB)", 2D) = "white" {}

_CapPower("matPower",range(-0.1,3))= 1

_Normals ("Normal", 2D)="white" {}

_NorRange ("Dephs",range(1,5))= 3




}

SubShader

{

Pass{

Tags{"LightMode" = "ForwardBase"}


CGPROGRAM

#pragma vertex vertsA

#pragma fragment fragsA  

#pragma target 3.0

#pragma only_renderers d3d9  debug


// no simcolens on the pragmas

//user variables and unity veriables

uniform half4 _Color;

uniform half _Shininess;

uniform half4 _LightColor0;

uniform sampler2D _MainTex;

uniform half4 _MainTex_ST;

uniform sampler2D _Normals;

uniform half4 _Normals_ST;

uniform half _NorRange;

uniform half _CapPower;

uniform sampler2D _Smap;

uniform half4 _Smap_ST;

uniform sampler2D _MatCap;

uniform float2 matcapUV;

half4x4 _Object2World;

half4x4 _World2Object;

half4 _WorldSpaceLightPos0; 

half3 _WorldSpaceCameraPos;

//input structs

struct VertInput1 {   // vertex imput info

half4 vertex : POSITION;

half3 normal : NORMAL;

half4 texcoord : TEXCOORD0; 

half4 tangent : TANGENT;

};

//output structs

struct VertOutput1 { // vertex output info

half4 pos : SV_POSITION;

half4 colur: COLOR;

half4 sktex : TEXCOORD0; 

half4 lightDir : TEXCOORD1;

half3 posNormal : TEXCOORD2;

half3 tans : TEXCOORD3;

half3 binoms : TEXCOORD4;

half3 cameraView :TEXCOORD5;

half4 spTex : TEXCOORD6;

float2 cap  : TEXCOORD7;

};

// vertex

VertOutput1 vertsA(VertInput1 g)

{ 

VertOutput1 h;

//matcap

  half2 capCoord;

 capCoord.x = dot(UNITY_MATRIX_IT_MV[0].xyz,g.normal);

 capCoord.y = dot(UNITY_MATRIX_IT_MV[1].xyz,g.normal);

 h.cap = capCoord * 0.5 + 0.5;

// direction variables

h.posNormal =  normalize(mul(half4(g.normal,0.0),_World2Object).xyz);

h.tans = normalize( mul(_Object2World, g.tangent).xyz);

h.binoms = normalize(cross(h.posNormal,h.tans)* g.tangent.w);

half4 posMain = mul(_Object2World, g.vertex);

h.pos = mul(UNITY_MATRIX_MVP, g.vertex);

h.sktex = g.texcoord;

h.cameraView = normalize (_WorldSpaceCameraPos.xyz -posMain.xyz); //tweakable

//multilight and light physics

half3 facetolite= _WorldSpaceLightPos0.xyz -posMain.xyz;

half distance = length(facetolite);

h.lightDir = half4(normalize(lerp(_WorldSpaceLightPos0.xyz,facetolite,_WorldSpaceLightPos0.w)),

lerp(1.0,(1.0/(distance*distance)),_WorldSpaceLightPos0.w));

return h;

}

// fragment

half4 fragsA(VertOutput1 i) : COLOR

{


//maps


half4 sktexf = tex2D(_MainTex, i.sktex.xy + (_MainTex_ST.xy * _MainTex_ST.zw));

half4 tans = tex2D(_Normals, i.sktex.xy + (_Normals_ST.xy * _Normals_ST.zw));

half4 spTex = tex2D(_Smap, i.sktex.xy + (_Smap_ST.xy *_Smap_ST.zw));


//normals

half3 localCoords = half3(2.0 * tans.ag - half2(1.0,1.0),_NorRange);


half3x3 local2WorldTranspose = float3x3( i.tans, i.binoms, i.posNormal);

half3 normalDirection =  normalize(mul(localCoords, local2WorldTranspose));


//matcap 

fixed4 mc = tex2D(_MatCap,i.cap);


//lambert lighting calc do not use half lambert. use a point light instead.


half3 SNdotL=  saturate( dot( normalDirection,i.lightDir.xyz)*0.5+0.5);

half3 thelighting = i.lightDir.w * _LightColor0.xyz *2* SNdotL;


//Specularty calc (0.7*normalDirection,0.7*mainlightDir for wetness, specularity and shine,

half3 theshine = i.lightDir.w * 0.8*spTex.rgb* max( 0.0,dot(0.7*normalDirection,0.7* i.lightDir.xyz)) 

* pow(max(0.0, dot(reflect(-i.lightDir.xyz, normalDirection),i.cameraView )),_Shininess);

half3 lights = (mc*_CapPower)*(thelighting)+(theshine*0.6);// + (2*UNITY_LIGHTMODEL_AMBIENT.xyz);


//final lighting

return half4 (sktexf.rgb*lights,1.0);

//( lerp((1-2*(1-sktex.rgb)),(2*sktex.rgb* matcaplight),step(matcaplight, 0.2 ))+(mtheshine),1.0)  overlay

// float4((((matcaplight)*(sktex.rgb*sktex.rgb))*(2*sktex.rgb* matcaplight)+sktex.rgb*0.5)+(theshine),1.0) soft light


}

ENDCG


}

This:

half4 tans = tex2D(_Normals, i.sktex.xy + (_Normals_ST.xy * _Normals_ST.zw));

Should be:

half4 tans = tex2D(_Normals, i.sktex.xy * _Normals_ST.xy + _Normals_ST.zw);

Since xy contains the scale and zw the offset. (I wouldn’t call it unwrapping the uv’s, that’s a whole different thing.)

I assume you know that your code expects the normal map to have the red channel moved into the alpha channel. (To enable DXT5 compression on the texture most likely.)

Why are you using a half4 for 2 component uv coordinates?

maybe I should say unpacking normals
“Why are you using a half4 for 2 component uv coordinates?”
because I don’t know any better; it came from the tutorial. When I change it to half3 the shader goes pink.
I fixed the syntax: half4 tans = tex2D(_Normals, i.sktex.xy * _Normals_ST.xy + _Normals_ST.zw);
and it still looks the same.

EDIT: I figured it out, I did something else stupid with the shader that was messing things up.
but thank you anyway since it should have been multiplied instead of added.