good day everyone , I’m hoping someone can help with my normal mapping using tri planar terrain ,
The normals are correct if I set the offset to be high by adding float3 (2,0,0) + normal
this seems like a hack and doesnt work on cubes ,
Heres my code
float3 rnmBlendUnpacked(float3 n1, float3 n2)
{
n1 += float3(0, 0, 1);
n2 *= float3(-1, -1, 1);
return n1 * dot(n1, n2) / n1.z - n2;
}
half3 TriplanarNormal(int tex, half3 worldPosition, half3 projNormal, half scale)
{
half4 worldpos = 0;
worldpos.xyz = worldPosition.xyz;
worldpos.w = tex;
half3 absVertNormal = abs(projNormal);
float3 blendWeight = pow(saturate(abs(projNormal) - BlendOff), HeightIntensity);
blendWeight /= (blendWeight.x + blendWeight.y + blendWeight.z);
half3 X = 0;
worldpos.zy *= scale;
if(blendWeight.x>0)
X = UnpackNormal(UNITY_SAMPLE_TEX2DARRAY(_MainTexNorm, (worldpos.zyw)));
worldpos.xyz = worldPosition.xyz;
worldpos.xz *= scale;
half3 Y = 0;
if (blendWeight.y > 0)
Y = UnpackNormal(UNITY_SAMPLE_TEX2DARRAY(_MainTexNorm, (worldpos.xzw))) ;
worldpos.xyz = worldPosition.xyz;
worldpos.xy *= scale;
half3 Z = 0;
if (blendWeight.z > 0)
Z = UnpackNormal(UNITY_SAMPLE_TEX2DARRAY(_MainTexNorm, (worldpos.xyw)));
// Swizzle world normals to match tangent space and apply RNM blend
X = rnmBlendUnpacked(half3(projNormal.zy, absVertNormal.x), X);
Y = rnmBlendUnpacked(half3(projNormal.xz, absVertNormal.y), Y);
Z = rnmBlendUnpacked(half3(projNormal.xy, absVertNormal.z), Z);
// Get the sign (-1 or 1) of the surface normal
half3 axisSign = sign(projNormal);
// Reapply sign to Z
X.z *= axisSign.x;
Y.z *= axisSign.y;
Z.z *= axisSign.z;
return ((X * blendWeight.x + Y * blendWeight.y + Z * blendWeight.z ) + projNormal);
}
Adding to o.normal, here I add an offset which helps bring the normal to the correct position kinda
//_OffSet is usually 2 - 8
o.Normal = normalize(half3(0, 0, _OffSet) +
(float3(
dot(colN, IN.worldToTangent0.xyz) ,
dot(colN, IN.worldToTangent1.xyz) ,
dot(colN, IN.worldToTangent2.xyz)
) * normalIntensity)
);
I tried removing swizzling the values but maybe im incorrect in doing so
Thanks for your help much appreciated .