On this page
Tri-Planar UVs
Every projection picks one plane to read the surface from, and every single plane is wrong somewhere. Tri-planar projection samples all three planes at once and blends them by how much the surface actually faces each one, which deletes the biggest weakness of position based UVs: the hard seams where one projection stops and another begins.
float3 p = positionWS * _Scale;
float2 uvY = p.xz; // from above
float2 uvX = p.zy; // from the side
float2 uvZ = p.xy; // from the front
Three samples, one surface¶
A position based UV, like the one in Object-Scaled UVs, drops one coordinate to get a 2D point: xz for the top view, zy for the side, xy for the front. Each one is perfect on the face that looks straight at its plane and badly stretched on the faces that look edge on, because a 45 degree face is a squashed rectangle in all three projections at once.
The fix is to stop choosing. Sample all three textures at their three projections, and weight each sample by how aligned the surface normal is with that plane’s axis:
float3 w = abs(normalWS); // how much we face each axis
w /= (w.x + w.y + w.z); // normalize to a sum of 1
half3 top = SAMPLE_TEXTURE2D(_MainTex, s, p.xz).rgb;
half3 side = SAMPLE_TEXTURE2D(_MainTex, s, p.zy).rgb;
half3 front = SAMPLE_TEXTURE2D(_MainTex, s, p.xy).rgb;
half3 color = top * w.y + side * w.x + front * w.z;
A face pointing straight up gets weight 1 on the top projection and 0 on the other two, so it is exactly the single projection you would have written by hand. A face at 45 degrees splits the weight, and the blend across the corner is smooth, because the weights vary continuously with the normal. That continuity is the entire value of the technique: no seams, no decisions, no per mesh UVs, one texture works on any shape.
The cost and the fix¶
Three samples where one would do, and the stretch is only reduced, not removed: at 45 degrees each projection is still mildly foreshortened, so the blended result is a little softer than a perfect UV would be. Artists sharpen the blend by raising the weights to a power before normalizing, which pushes weight toward the dominant projection:
float3 w = pow(abs(normalWS), half3(2, 2, 2)); // sharper falloff
w /= (w.x + w.y + w.z);
Power 1 is the softest blend, and larger powers approach a clean single projection with a narrower transition. The power is the one knob of the technique, and the same trade applies as everywhere in this library: smoothness against sharpness, bought with a small amount of exponent.
Where you see it in the wild: terrain and environment shaders that apply one material to arbitrary rock shapes, and any system where UVs are not an option because the geometry is not an asset, it is a simulation.
What the shader does¶
The shader projects the same texture through tri-planar blending with a blend power property. At low power the corners melt smoothly; at high power the faces snap to clean single projections, and the sphere never shows a seam.
Illustrative schematic: Top, front, side: one texture, blended by normal.
Three projections, normal weighted, with the blend power on the material.
Shader "DecompiledArt/Textures/Projections/TriPlanar/TriPlanar"
{
Properties
{
_MainTex("Texture (Repeat wrap)", 2D) = "white" {}
[MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
_Scale("Scale", Float) = 1
_Power("Blend Power", Range(1, 8)) = 2
}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" }
Pass
{
Name "ForwardOnly"
Tags { "LightMode" = "UniversalForward" }
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex);
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float3 posWS : TEXCOORD0;
half3 normalWS : TEXCOORD1;
};
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
float _Scale;
half _Power;
CBUFFER_END
Varyings Vert(Attributes IN)
{
Varyings OUT;
OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.posWS = TransformObjectToWorld(IN.positionOS.xyz);
OUT.normalWS = TransformObjectToWorldNormal(IN.normalOS);
return OUT;
}
half4 Frag(Varyings IN) : SV_Target
{
float3 p = IN.posWS * _Scale;
half3 n = normalize(IN.normalWS);
// Weight each projection by how much the face points at its plane.
float3 w = pow(abs(n), half3(_Power, _Power, _Power));
w /= (w.x + w.y + w.z);
half3 top = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, p.xz).rgb;
half3 side = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, p.zy).rgb;
half3 front = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, p.xy).rgb;
half3 color = top * w.y + side * w.x + front * w.z;
return half4(_BaseColor.rgb * color, _BaseColor.a);
}
ENDHLSL
}
}
FallBack Off
}
🔗 Related Chapters¶
Object-Scaled UVs • Screen-Space UVs
Related functions: Normalize • Power
❤️ Support Shader Story¶
If this article helped you, consider supporting the project on Patreon: you’ll get access to the related source files and reference cheat-sheets.
Your support helps keep this library open and free for everyone.