So I’m pretty new to shaders however I’m beginning to understand enough to make something basic. I’ve been trying to create a World Space shader that allows for a normal texture to be projected in world space as well as Diffuse, and Spec map. I’ve got it mostly working, however I’m running into a snag where when I rotate the object either 90 or 270 degrees the textures on the z and x axis faces stretch almost as if the shader is remembering which faces originally faced z and x. I’ve taken a couple images to show what I mean, and I’m hoping some one here can show me what I’m missing.
- Before Rotating
- After Rotating
Shader "Diffuse - Worldspace" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_Emission ("Emission (RGB)", Color) = (0,0,0,1)
_Bump ("Normal (RGB)", 2D) = "blue" {}
_SpecMap ("Specular (G)", 2D) = "grey" {}
_Rotation ("Rotation", Range(0,1)) = 0
_Scale ("Texture Scale", Float) = 1.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf StandardSpecular vertex:vert
sampler2D _MainTex;
sampler2D _Bump;
sampler2D _SpecMap;
fixed4 _Emission;
fixed4 _Color;
float _Scale;
float _Rotation;
struct Input
{
float3 sworldNormal;
float3 worldPos;
float2 uv_Bump;
};
void vert (inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input,o);
o.sworldNormal = abs(v.normal);
}
void surf (Input IN, inout SurfaceOutputStandardSpecular o)
{
float2 UV;
fixed4 c;
float r = _Rotation;
float3 uv = IN.sworldNormal.xyz;
half4 x;
half4 y;
half4 z;
half4 n = float4(1,1,1,1);
if(r<0.5)
{
if(abs(IN.sworldNormal.x)>0.5)
{
UV = IN.worldPos.zy; // side
c = tex2D(_MainTex, UV* _Scale);
o.Specular = tex2D(_SpecMap, UV* _Scale).a;
half4 x = tex2D (_MainTex, uv.zy);
x = tex2D(_Bump, UV* _Scale);
n = lerp(n,x,IN.sworldNormal.r);
}
else if(abs(IN.sworldNormal.z)>0.5)
{
UV = IN.worldPos.xy; // front
c = tex2D(_MainTex, UV* _Scale);
o.Specular = tex2D(_SpecMap, UV* _Scale).a;
half4 z = tex2D (_MainTex, uv.xy);
z = tex2D(_Bump, UV* _Scale);
n = lerp(n,z,IN.sworldNormal.b);
}
else
{
UV = IN.worldPos.zx; // top
c = tex2D(_MainTex, UV* _Scale);
o.Specular = tex2D(_SpecMap, UV* _Scale).a;
half4 y = tex2D (_MainTex, uv.zx);
y = tex2D(_Bump, UV* _Scale);
n = lerp(n,y,IN.sworldNormal.g);
}
}
else
{
if(abs(IN.sworldNormal.x)>0.5)
{
UV = IN.worldPos.zy; // side
c = tex2D(_MainTex, UV* _Scale);
o.Specular = tex2D(_SpecMap, UV* _Scale).a;
half4 x = tex2D (_MainTex, uv.zy);
x = tex2D(_Bump, UV* _Scale);
n = lerp(n,x,IN.sworldNormal.r);
}
else if(abs(IN.sworldNormal.z)>0.5)
{
UV = IN.worldPos.xy; // front
c = tex2D(_MainTex, UV* _Scale);
o.Specular = tex2D(_SpecMap, UV* _Scale).a;
half4 z = tex2D (_MainTex, uv.xy);
z = tex2D(_Bump, UV* _Scale);
n = lerp(n,z,IN.sworldNormal.b);
}
else
{
UV = IN.worldPos.xz; // top
c = tex2D(_MainTex, UV* _Scale);
o.Specular = tex2D(_SpecMap, UV* _Scale).a;
half4 y = tex2D (_MainTex, uv.xz);
y = tex2D(_Bump, UV* _Scale);
n = lerp(n,y,IN.sworldNormal.g);
}
}
o.Normal = UnpackNormal(n);
o.Albedo = c.rgb * _Color;
o.Emission = _Emission;
}
ENDCG
}
Fallback "VertexLit"
}