I’ve been working on what I started calling an “autotiling shader” (because I came up with the idea independently for a slightly different use case). At first I had the textures working but the normals all messed up. I did some research and found ways to fix it, but it still only really works on some walls. “World”/mesh normals seem to work everywhere, but normal maps are only applied correctly to side facing in the Z direction / parallel to the XY plane. Sides facing in the X direction have no normal map at all, while sides facing in the Y direction (so all my floors) have normal map rotated 90 (or 270?) degrees. I’m not sure what’s wrong – I’ve gone beyond my own understanding at this point.
Shader "Custom/Autotile" {
Properties{
_Color("Color", Color) = (1,1,1,1)
_MainTex("Base Color", 2D) = "white" {}
_Normal("Normal Map", 2D) = "bump" {}
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
_UVs("UV Scale", float) = 1.0
}
SubShader{
Tags { "RenderType" = "Opaque" }
LOD 300
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows vertex:vertWorld
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "Lighting.cginc"
#include "UnityPBSLighting.cginc"
#include "UnityStandardBRDF.cginc"
sampler2D _MainTex;
sampler2D _Normal;
float _Glossiness;
float _Metallic;
fixed4 _Color;
float _UVs;
struct Input {
float3 worldPos;
float3 worldNormal;
float4 worldToTangent0;
float4 worldToTangent1;
float4 worldToTangent2;
INTERNAL_DATA
};
// Vertex program is determined in pragma above
void vertWorld(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
float3 worldNormal = UnityObjectToWorldNormal(v.normal);
TANGENT_SPACE_ROTATION;
float3x3 worldToTangent = mul(rotation, (float3x3)unity_WorldToObject);
o.worldToTangent0 = float4(worldToTangent[0], worldNormal.x);
o.worldToTangent1 = float4(worldToTangent[1], worldNormal.y);
o.worldToTangent2 = float4(worldToTangent[2], worldNormal.z);
}
float3 rnmBlendUnpacked(float3 n1, float3 n2)
{
n1 += float3(0, 0, 1);
n2 *= float3(-1, -1, 1);
return n1 * dot(n1, n2) / n1.z - n2;
}
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_INSTANCING_BUFFER_END(Props)
void surf(Input IN, inout SurfaceOutputStandard o)
{
// Preparation of needed variables
float3 worldNormal = float3(IN.worldToTangent0.w, IN.worldToTangent1.w, IN.worldToTangent2.w);
float alpha21 = abs(worldNormal.x);
float alpha23 = abs(worldNormal.z);
half3 absVertNormal = abs(worldNormal);
half3 axisSign = sign(worldNormal);
float3 Pos = IN.worldPos / (-1.0 * abs(_UVs));
// Regular stuff
// Note, the change in sign and component order is required
// to keep all sides of a wall oriented the same way.
float3 c1 = tex2D(_MainTex, -Pos.zy).rgb;
float3 c2 = tex2D(_MainTex, Pos.xz).rgb;
float3 c3 = tex2D(_MainTex, -Pos.xy).rgb;
float3 c21 = lerp(c2, c1, alpha21).rgb;
float3 c23 = lerp(c21, c3, alpha23).rgb;
// Normals -- ugghhh!
float3 n1 = UnpackNormal(tex2D(_Normal, -Pos.zy));
float3 n2 = UnpackNormal(tex2D(_Normal, Pos.zx));
float3 n3 = UnpackNormal(tex2D(_Normal, -Pos.xy));
float3 n21 = lerp(n2, n1, alpha21).xyz;
float3 n23 = lerp(n21, n3, alpha23).xyz;
n1 = rnmBlendUnpacked(half3(worldNormal.zy, absVertNormal.x), n1);
n2 = rnmBlendUnpacked(half3(worldNormal.xz, absVertNormal.y), n2);
n3 = rnmBlendUnpacked(half3(worldNormal.xy, absVertNormal.z), n3);
n1.z *= axisSign.x;
n2.z *= axisSign.y;
n3.z *= axisSign.z;
float3 blend = pow(n23.xyz, 4);
blend /= dot(blend, float3(1, 1, 1));
half3 wNorm = normalize(
n1.xyz * blend.x +
n2.xyz * blend.y +
n3.xyz * blend.z +
worldNormal
);
// Output Results
o.Albedo = c23;
o.Normal = normalize(
float3(
dot(wNorm, IN.worldToTangent0.xyz),
dot(wNorm, IN.worldToTangent1.xyz),
dot(wNorm, IN.worldToTangent2.xyz)
)
);
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
}
ENDCG
}
FallBack "Standard"
}
Here I’ve replace a brick materials albedo with snow to make the problem more visible.
Can anyone tell me what I’ve done wrong or how to fix it?
