Skip to content

X-Ray Style Outline via Normals

Effect

A lightweight X-ray / outline effect can be achieved purely from the normal and the camera view direction—no post-processing required.


Principle

  1. Convert the camera position to object space in the vertex shader to get viewDir
  2. Pass the object-space normal to the fragment shader
  3. Sample rim = 1 - saturate(dot(viewDir, normal))
  4. Use _RimColor and _RimPower to 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