- Home /
World Space material Stretching when object is rotated.
I've pieced together a world space shader including a few extra textures like Normal/Bump, Specular, Emissions and a rotation variable for the top facing sides of our cubes. I'm running into an issue where when I rotate the mesh 90 and 270 degrees the faces on the x and z axis stretch the texture as if it is holding normals for those faces and not updating to the new axis for the world space. when I rotate to 180 it corrects the texture again, however I need it to work correctly on 90 and 270 as well. Any help would be appreciative, I'm sure I'm missing something I just can't see it as I've been staring at it too long.
https://imgur.com/pBBXhfy - Before Rotation
https://imgur.com/WkK0seu - After Rotation
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"
}
[1]: https://imgur.com/pBBXhfy
Comment