- Home /
Question by
avoorhees · Jul 16, 2014 at 09:32 PM ·
shadersnormal map
Shader has uv seams
Hey there, I'm working on a vertex fragment shader which has a diffuse, normal and specular texture. Everything is working except that the normal map doesn't look correct in the shader. There are seams at all the uv edges and the rest doesn't look right either. I believe the issue is in how I'm doing the normal mapping in the fragment program. It almost looks as if on of the channels in the normal map is flipped, but I tried flipping channels in Photoshop to no avail. Also, I know the problem is not the normal map because I tested it on a builtin Unity shader and it looks fine.
I'm stumped and need to figure this out so any help would really be appreciated.
Shader "Simple" {
Properties {
_Color ("Lit Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (1,1,1,1)
_Shininess ("Specular Polish", Range(0.1,10)) = 1
_MainTex ("Diffuse Map", 2D) = "white" {}
_BumpMap ("Normal Map", 2D) = "Bump" {}
}
SubShader {
Pass {
Tags {"LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma target 3.0
//User defined variables
uniform fixed4 _SpecColor;
uniform float _Shininess;
uniform sampler _MainTex;
uniform float4 _MainTex_ST;
uniform sampler2D _BumpMap;
uniform float4 _BumpMap_ST;
//Structs
struct vertexInput{
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
float3 normal : NORMAL;
float4 tangent : TANGENT;
};
struct vertexOutput{
float4 pos : SV_POSITION;
float2 uvs : TEXCOORD0;
float4 posWorld : TEXCOORD1;
float3 normalWorld : TEXCOORD2;
float3 tangentWorld : TEXCOORD3;
float3 binormalWorld : TEXCOORD4;
};
//Vertex function
vertexOutput vert(vertexInput v){
vertexOutput o;
//Adding normal stuff
o.normalWorld = normalize(mul(float4(v.normal, 0.0), _World2Object).xyz);
o.tangentWorld = normalize(mul(_Object2World, v.tangent).xyz);
o.binormalWorld = normalize(cross(o.normalWorld, o.tangentWorld) * v.tangent.w);
//Unity transform position
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
//World position
o.posWorld = mul(_Object2World, v.vertex);
//UV coordinates
o.uvs = v.texcoord;
return o;
}
//Fragment function
fixed4 frag(vertexOutput i) : COLOR {
//Basic parameters
fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
//Textures
float4 difTex = tex2D(_MainTex, i.uvs.xy * _MainTex_ST.xy + _MainTex_ST.zw);
float4 norTex = tex2D(_BumpMap, _BumpMap_ST.xy * i.uvs.xy + _BumpMap_ST.zw);
//Normal map
float3 localCoords = float3(2.0 * norTex.ag - float2(1.0), 0.0);
localCoords.z = sqrt(1.0 - dot(localCoords, localCoords));
//Normal matrix
float3x3 local2WorldTranspose = float3x3(i.tangentWorld, i.binormalWorld, i.normalWorld);
float3 normalDir = normalize(mul(localCoords, local2WorldTranspose));
//Specular shading
float4 specularShading = _SpecColor * pow(saturate(dot(reflect(-lightDir, normalDir), viewDir)), _Shininess);
//Output
return difTex + specularShading;
}
ENDCG
}
}
}
Comment