I can’t seem to get a working shader that takes in 3 textures, 3 bump maps and successfully shades an object with triplanar shading. Anytime I write to o.Normal, my objects simply go black. To simplify things, I’m trying to just apply 1 bump map as follows to otherwise standard triplanar shader:
Shader "Tri-Planar World" {
Properties {
_Side("Side", 2D) = "white" {}
_Top("Top", 2D) = "white" {}
_Bottom("Bottom", 2D) = "white" {}
_BumpMap("BumpMap", 2D) = "bump" {}
}
SubShader {
Tags {
"Queue" = "Geometry"
"IgnoreProjector" = "False"
"RenderType" = "Opaque"
}
Cull Back
ZWrite On
CGPROGRAM
#pragma surface surf Standard
#pragma exclude_renderers flash
#pragma shader_feature _NORMALMAP_ON
sampler2D _Side, _Top, _Bottom, _BumpMap;
struct Input {
float3 worldPos;
float3 worldNormal;
#if _NORMALMAP_ON
INTERNAL_DATA
#endif
};
void surf(Input IN, inout SurfaceOutputStandard o) {
float3 projNormal = saturate(pow(IN.worldNormal * 1.4, 4));
// SIDE X
float3 x = tex2D(_Side, frac(IN.worldPos.zy * 0.7)) * abs(IN.worldNormal.x);
// TOP / BOTTOM
float3 y = 0;
float absny = abs(IN.worldNormal.y);
float2 fraczx = frac(IN.worldPos.zx * 0.7);
if (IN.worldNormal.y > 0) {
y = tex2D(_Top, fraczx) * absny;
}
else {
y = tex2D(_Bottom, fraczx) * absny;
}
// SIDE Z
float3 z = tex2D(_Side, frac(IN.worldPos.xy * 0.7)) * abs(IN.worldNormal.z);
#if _NORMALMAP_ON
o.Normal = UnpackNormal(tex2D(_BumpMap, fraczx) * absny);
#endif
o.Albedo = z;
o.Albedo = lerp(o.Albedo, x, projNormal.x);
o.Albedo = lerp(o.Albedo, y, projNormal.y);
}
ENDCG
}
Fallback "Diffuse"
}
If I comment out the o.Normal line, my stuff looks fine (but no bump mapping, as expected). If I set o.Normal to apparently anything, even (0,1,0) – things still go black.
Here is a shader that supposedly does what I want:
… but the “world normal” it calculated in the vertex shader didn’t seem stable… as I moved around the scene, the normals of my objects would go whack.
If I modify my shader to try calculating the world normal as the linked one above is doing:
Shader "Tri-Planar World" {
Properties {
_Side("Side", 2D) = "white" {}
_Top("Top", 2D) = "white" {}
_Bottom("Bottom", 2D) = "white" {}
_BumpMap("BumpMap", 2D) = "bump" {}
}
SubShader {
Tags {
"Queue" = "Geometry"
"IgnoreProjector" = "False"
"RenderType" = "Opaque"
}
Cull Back
ZWrite On
CGPROGRAM
#pragma surface surf Standard vert:vertWorld
#pragma exclude_renderers flash
#pragma shader_feature _NORMALMAP_ON
sampler2D _Side, _Top, _Bottom, _BumpMap;
struct Input {
float3 worldPos;
float3 thisNormal;
//#if _NORMALMAP_ON
// INTERNAL_DATA
//#endif
};
void vertWorld(inout appdata_full v, out Input o) {
o.thisNormal = mul(unity_ObjectToWorld, float4(v.normal, 0.0f)).xyz;
}
void surf(Input IN, inout SurfaceOutputStandard o) {
float3 projNormal = saturate(pow(IN.thisNormal * 1.4, 4));
// SIDE X
float3 x = tex2D(_Side, frac(IN.worldPos.zy * 0.7)) * abs(IN.thisNormal.x);
// TOP / BOTTOM
float3 y = 0;
float absny = abs(IN.thisNormal.y);
float2 fraczx = frac(IN.worldPos.zx * 0.7);
if (IN.thisNormal.y > 0) {
y = tex2D(_Top, fraczx) * absny;
}
else {
y = tex2D(_Bottom, fraczx) * absny;
}
// SIDE Z
float3 z = tex2D(_Side, frac(IN.worldPos.xy * 0.7)) * abs(IN.thisNormal.z);
#if _NORMALMAP_ON
o.Normal = UnpackNormal(tex2D(_BumpMap, fraczx) * absny);
#endif
o.Albedo = z;
o.Albedo = lerp(o.Albedo, x, projNormal.x);
o.Albedo = lerp(o.Albedo, y, projNormal.y);
}
ENDCG
}
Fallback "Diffuse"
}
… all upward facing textures are smeared in one direction & the normals (as I said before) are not stable (because I notice flickering surfaces in relation to my directional light).
I’m pulling my hair out & I can’t seem to figure out how to write a triplanar shader, that uses the world normal to blend 3 textures, and then apply a bump map.
Any help is greatly appreciated!