- Home /
Uncorrect normals tri-planer shader
I'm making a tri-planer shader that can accept a texture atlases for images and their normal maps. I got it working for showing the textures, but the normals are being set incorrectly: screenshot
The cube to the left is my shader, the one to the right is the legacy BumpedDiffuse.
The shader is heavily based on example 1-3 from section 1.5 of https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch01.html
Shader "Custom/TriPlaner" {
Properties {
_Atlas ("Texture Atlas", 2D) = "white" {}
_NormalAtlas("Normal Altas", 2D) = "bump" {}
}
SubShader {
Tags {
"Queue"="Geometry"
"IgnoreProjector"="False"
"RenderType"="Opaque"
}
Cull Back
ZWrite On
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma exclude_renderers flash
sampler2D _Atlas;
sampler2D _NormalAtlas;
struct Input {
float3 worldPos;
float2 uv_Atlas;
float2 uv_NormalAtlas;
float3 WorldNormal;
INTERNAL_DATA
};
void surf (Input IN, inout SurfaceOutputStandard o) {
float3 blend_weights = abs(IN.WorldNormal);
blend_weights = (blend_weights - 0.2) * 7;
blend_weights = max(blend_weights, 0);
blend_weights /= (blend_weights.x + blend_weights.y + blend_weights.z).xxx;
float4 colFetch = tex2D(_Atlas, IN.uv_Atlas);
float2 bumpFetch = UnpackNormal(tex2D(_NormalAtlas, IN.uv_NormalAtlas)).xy - 0.5;
float3 bump1 = float3(0, bumpFetch.x, bumpFetch.y);
float3 bump2 = float3(bumpFetch.y, 0, bumpFetch.x);
float3 bump3 = float3(bumpFetch.x, bumpFetch.y, 0);
float4 blend_color = colFetch.xyzw * blend_weights.xxxx +
colFetch.xyzw * blend_weights.yyyy +
colFetch.xyzw * blend_weights.zzzz;
float3 blended_bump_vec = bump1.xyz * blend_weights.xxx +
bump2.xyz * blend_weights.yyy +
bump3.xyz * blend_weights.zzz;
o.Albedo = blend_color.xyz;
o.Alpha = blend_color.w;
o.Normal = blended_bump_vec;
}
ENDCG
}
Fallback "Diffuse"
}
Comment