X-Ray Style Outline via Normals

A lightweight X-ray / outline effect can be achieved purely from the normal and the camera view direction—no post-processing required.
Principle
- Convert the camera position to object space in the vertex shader to get
viewDir - Pass the object-space normal to the fragment shader
- Sample
rim = 1 - saturate(dot(viewDir, normal)) - Use
_RimColorand_RimPowerto control color and falloff
Normals facing the camera produce a dot product near 1 ⇒ rim near 0 (dark). Normals pointing sideways give smaller dot ⇒ rim brightens the silhouette.
Key shader code
Vertex
v2f vert (appdata_full v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.viewDir = normalize(TransformWorldToObject(GetCameraPositionWS()));
o.normal = v.normal;
return o;
}
Fragment
float4 frag (v2f i) : COLOR {
float rim = 1 - saturate(dot(i.viewDir, i.normal));
float4 color = _RimColor * pow(rim, _RimPower);
return color;
}
Expose _RimColor and _RimPower on the material. Blend this with the original albedo if you want a hybrid look, or render only the rim for an X-ray overlay.
Possible extensions:
- Combine with regular shading for a hologram vibe
- Apply only when the object is occluded (depth comparison)
- Move the logic into a URP/HDRP full-screen pass for scene-wide outlines
Original article (Chinese) on CSDN “uniGame”, CC BY-SA 4.0.
https://blog.csdn.net/alla_Candy/article/details/122918655