Deferred and projected decals
Stamps textures such as bullet holes, grime and road markings onto existing surfaces by rendering a box volume and reprojecting scene depth into the box's local space, without generating extra mesh.
Also known as box decals, screen-space decals, decal volumes
In the family Materials, BRDFs & texturing, detail overlay
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | Official add-on | import { DecalGeometry } from 'three/addons/geometries/DecalGeometry.js' docs Projects a box onto mesh geometry; not a deferred/screen-space decal. |
| Babylon.js 9.38.0 | Built in | MeshBuilder.CreateDecal(); material.decalMap docs Mesh-projected decals; not deferred volumes. |
| Godot 4.7 | Built in | Add a Decal node (Decal.texture_albedo, normal, ORM) docs Projected box decals; works in Forward+ and Mobile. |
| Unity | Built in | HDRP: Decal Projector; URP: Renderer > Add Renderer Feature > Decal docs |
| Unreal 5.7 | Built in | Place a Decal Actor; decal material with Material Domain = Deferred Decal, Blend Mode Translucent or Alpha Composite docs |
| Blender 5.2 | None found | No decal projection; use UV Project modifier or object texture coordinates. |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
A decal is an oriented box with a transform, a texture set and a blend mode. The box is rasterised (back faces or front faces with depth test off) so every covered screen pixel runs a shader. That shader reads the depth buffer, reconstructs the world position, transforms it into decal space and uses the xy of the result as UVs; pixels whose local position falls outside the box are discarded. Albedo, normal, roughness or emissive are then blended into the G-buffer (deferred) or accumulated into a decal buffer applied in the forward pass. Tangent frames can come from the decal or from screen-space derivatives of the reconstructed position. An angle fade using the surface normal against the projection axis prevents stretching on perpendicular surfaces, and a layer or stencil mask restricts which meshes receive decals.
When to choose it
Reach for it when
- need bullet holes or dirt on arbitrary surfaces
- stamp textures onto geometry at runtime without new meshes
- decal stretches on surfaces at steep angles
- road markings and graffiti on uneven ground
- placing grime on level geometry from the editor
Choose it for dynamic details placed by gameplay or tooling: impacts, blood, tyre tracks, dirt and signage on arbitrary geometry. Prefer baked texture detail or Lightmap Baking for static, authored detail. Prefer mesh decals when a surface needs real depth or the platform cannot afford a depth read. Use Triplanar mapping for terrain-style blending without a box. Works naturally with Tiled deferred shading, and in forward renderers with Clustered forward shading through a decal buffer.
Field marks
- Stretching on surfaces nearly parallel to the projection direction unless an angle fade is used
- Decals bleed onto objects behind or in front of the intended surface because the box depth range is too thick
- Projected normals blend incorrectly with the underlying normal map, producing flat or inverted lighting
- Moving or skinned meshes smear the stamp because the projection is in world space
- Transparent surfaces and particles have no depth entry, so decals appear on the surface behind them
- Texture mips sample wrong levels when derivatives are reconstructed from a projected UV on depth discontinuities
Presets
Deferred & Projected Decals — OBB projection volumes & local UVs (left) vs normal-blended stamped surface decals (right)
{
"P_DECAL_SCALE": 1,
"P_ANGLE_FADE": 0.45,
"P_WEAR": 0.35,
"P_SPLIT": 0.48
}Show GLSL (255 lines)
const float P_DECAL_SCALE = 1.00; // P Decal Scale [0.5, 1.8, 0.05]
const float P_ANGLE_FADE = 0.45; // P Normal Cutoff [0.1, 0.9, 0.05]
const float P_WEAR = 0.35; // P Grime Wear [0.0, 1.0, 0.05]
const float P_SPLIT = 0.48; // P OBB / Stamped Split [0.0, 1.0, 0.02]
float hash21(vec2 p) {
p = fract(p * vec2(127.1, 311.7));
p += dot(p, p + 34.5);
return fract(p.x * p.y);
}
float vnoise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i);
float b = hash21(i + vec2(1.0, 0.0));
float c = hash21(i + vec2(0.0, 1.0));
float d = hash21(i + vec2(1.0, 1.0));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
float fbm(vec2 p) {
float s = 0.0;
float a = 0.5;
for (int i = 0; i < 4; i++) {
s += a * vnoise(p);
p *= 2.07;
a *= 0.5;
}
return s;
}
float sdBox(vec3 p, vec3 b) {
vec3 q = abs(p) - b;
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}
vec2 mapScene(vec3 p) {
float dFloor = p.y + 0.65;
float dWall = p.z + 0.85;
vec2 res = vec2(dFloor, 1.0);
if (dWall < res.x) res = vec2(dWall, 2.0);
// Curved architectural concrete pillar meeting the wall and floor
float dCyl = length(p.xz - vec2(-0.25, -0.45)) - 0.36;
dCyl = max(dCyl, abs(p.y - 0.15) - 0.85);
if (dCyl < res.x) res = vec2(dCyl, 3.0);
// Stepped concrete curb block
float dCurb = sdBox(p - vec3(0.48, -0.42, -0.32), vec3(0.38, 0.24, 0.32));
if (dCurb < res.x) res = vec2(dCurb, 4.0);
return res;
}
vec3 calcNormal(vec3 p) {
vec2 e = vec2(0.002, 0.0);
return normalize(vec3(
mapScene(p + e.xyy).x - mapScene(p - e.xyy).x,
mapScene(p + e.yxy).x - mapScene(p - e.yxy).x,
mapScene(p + e.yyx).x - mapScene(p - e.yyx).x
));
}
mat3 rotY(float a) {
float c = cos(a), s = sin(a);
return mat3(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
}
mat3 rotX(float a) {
float c = cos(a), s = sin(a);
return mat3(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
}
// Analytical ray-OBB intersection for wireframe volume overlay
vec2 intersectBox(vec3 ro, vec3 rd, vec3 rad) {
vec3 m = 1.0 / (rd + vec3(1e-6));
vec3 n = m * ro;
vec3 k = abs(m) * rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
float tN = max(max(t1.x, t1.y), t1.z);
float tF = min(min(t2.x, t2.y), t2.z);
if (tN > tF || tF < 0.0) return vec2(-1.0);
return vec2(tN, tF);
}
float boxWire(vec3 lp, vec3 rad) {
vec3 d = abs(abs(lp) - rad);
float e1 = max(d.x, d.y);
float e2 = max(d.y, d.z);
float e3 = max(d.z, d.x);
return smoothstep(0.025, 0.006, min(e1, min(e2, e3)));
}
vec3 aces(vec3 x) {
x = max(x, 0.0);
return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
float sx = fragCoord.x / max(iResolution.x, 1.0);
float split = clamp(P_SPLIT, 0.0, 1.0);
bool showStamped = sx >= split;
float dScale = clamp(P_DECAL_SCALE, 0.4, 2.0);
float angCut = clamp(P_ANGLE_FADE, 0.05, 0.95);
float wear = clamp(P_WEAR, 0.0, 1.0);
vec3 ro = vec3(0.32 * sin(iTime * 0.28), 0.34, 2.18);
vec3 ta = vec3(0.05, -0.18, -0.42);
vec3 fw = normalize(ta - ro);
vec3 ri = normalize(cross(fw, vec3(0.0, 1.0, 0.0)));
vec3 up = cross(ri, fw);
vec3 rd = normalize(fw * 1.42 + ri * uv.x + up * uv.y);
vec3 col = mix(vec3(0.09, 0.11, 0.14), vec3(0.22, 0.27, 0.34), smoothstep(-0.3, 0.6, rd.y));
float t = 0.05;
float hitId = 0.0;
for (int i = 0; i < 64; i++) {
vec2 h = mapScene(ro + rd * t);
if (h.x < 0.002) { hitId = h.y; break; }
t += h.x;
if (t > 9.0) break;
}
// Two oriented decal projector boxes:
// Box A: emblem + hazard stencil projected onto curved pillar & back wall
vec3 boxACenter = vec3(-0.14 + 0.12 * sin(iTime * 0.55), -0.02, -0.45);
mat3 boxARot = rotY(0.24) * rotX(0.18);
vec3 boxARad = vec3(0.56, 0.56, 0.52) / dScale;
// Box B: top-down road/floor hazard stripe decal projected over curb & floor
vec3 boxBCenter = vec3(0.42, -0.42, -0.22);
mat3 boxBRot = rotY(-0.32);
vec3 boxBRad = vec3(0.55, 0.45, 0.48) / dScale;
if (hitId > 0.5) {
vec3 p = ro + rd * t;
vec3 n = calcNormal(p);
vec3 v = -rd;
vec3 sunDir = normalize(vec3(0.62, 0.78, 0.52));
// Base procedural concrete G-buffer
float grain = fbm(p.xy * 6.5 + p.zy * 5.0);
vec3 baseAlb = mix(vec3(0.36, 0.38, 0.40), vec3(0.54, 0.55, 0.53), grain);
float baseRough = mix(0.62, 0.85, grain);
vec3 surfN = n;
// Reproject world position p into Decal Box A local space
vec3 lpA = transpose(boxARot) * (p - boxACenter);
vec3 boxAProjDir = boxARot * vec3(0.0, 0.0, 1.0);
float inA = step(max(abs(lpA.x) / boxARad.x, max(abs(lpA.y) / boxARad.y, abs(lpA.z) / boxARad.z)), 1.0);
float facingA = smoothstep(angCut, min(angCut + 0.25, 0.99), dot(n, boxAProjDir));
vec2 uvA = lpA.xy / (2.0 * boxARad.xy) + 0.5;
// Reproject world position p into Decal Box B local space (top-down along +Y)
vec3 lpB = transpose(boxBRot) * (p - boxBCenter);
vec3 boxBProjDir = boxBRot * vec3(0.0, 1.0, 0.0);
float inB = step(max(abs(lpB.x) / boxBRad.x, max(abs(lpB.y) / boxBRad.y, abs(lpB.z) / boxBRad.z)), 1.0);
float facingB = smoothstep(angCut, min(angCut + 0.25, 0.99), dot(n, boxBProjDir));
vec2 uvB = lpB.xz / (2.0 * boxBRad.xz) + 0.5;
if (showStamped) {
// Decal A: Stenciled emblem ring + radial ticks + weathered paint mask
if (inA > 0.5) {
vec2 c = uvA - 0.5;
float r = length(c);
float ang = atan(c.y, c.x);
float ring = smoothstep(0.045, 0.028, abs(r - 0.34));
float innerRing = smoothstep(0.032, 0.018, abs(r - 0.21));
float crossBar = smoothstep(0.03, 0.015, abs(c.y)) * step(0.11, abs(c.x)) * step(abs(c.x), 0.42);
float ticks = step(0.55, cos(ang * 12.0)) * smoothstep(0.04, 0.015, abs(r - 0.28));
float mask = clamp(ring + innerRing + crossBar + ticks, 0.0, 1.0);
float chip = smoothstep(wear * 0.72, wear * 0.72 + 0.16, fbm(uvA * 14.0 + 3.1));
float alphaA = mask * chip * facingA;
vec3 decalColA = mix(vec3(0.95, 0.34, 0.12), vec3(0.98, 0.78, 0.18), innerRing);
baseAlb = mix(baseAlb, decalColA, alphaA);
baseRough = mix(baseRough, 0.28, alphaA);
// Subtle paint thickness edge bump
vec3 dN = normalize(n + vec3(cos(ang), sin(ang), 0.0) * (ring * 0.22 * alphaA));
surfN = normalize(mix(surfN, dN, alphaA));
}
// Decal B: Diagonal safety stripes & oil grime
if (inB > 0.5) {
vec2 e = abs(uvB - 0.5);
float frame = step(max(e.x, e.y), 0.46) * (1.0 - step(max(e.x, e.y), 0.34));
float stripes = smoothstep(0.05, -0.05, sin((uvB.x + uvB.y) * 28.0));
float chipB = smoothstep(wear * 0.65, wear * 0.65 + 0.18, fbm(uvB * 12.0 + 9.4));
float alphaB = frame * chipB * facingB;
vec3 stripeCol = mix(vec3(0.08, 0.09, 0.10), vec3(0.96, 0.76, 0.12), stripes);
baseAlb = mix(baseAlb, stripeCol, alphaB);
baseRough = mix(baseRough, 0.34, alphaB);
}
} else {
// Left: Show local projected UV coordinates inside each box volume
if (inA > 0.5) {
vec2 grid = abs(fract(uvA * 6.0) - 0.5);
float gl = smoothstep(0.06, 0.02, min(0.5 - grid.x, 0.5 - grid.y));
vec3 uvVis = vec3(uvA.x, uvA.y, 0.25) * (0.4 + 0.6 * facingA);
baseAlb = mix(baseAlb, uvVis + vec3(0.95, 0.5, 0.15) * gl * 0.45, 0.75 * facingA + 0.15);
}
if (inB > 0.5) {
vec2 grid = abs(fract(uvB * 6.0) - 0.5);
float gl = smoothstep(0.06, 0.02, min(0.5 - grid.x, 0.5 - grid.y));
vec3 uvVis = vec3(0.18, uvB.x, uvB.y) * (0.4 + 0.6 * facingB);
baseAlb = mix(baseAlb, uvVis + vec3(0.2, 0.92, 0.98) * gl * 0.45, 0.75 * facingB + 0.15);
}
}
// Soft shadow ray
float sh = 1.0;
float ts = 0.03;
for (int s = 0; s < 18; s++) {
float ds = mapScene(p + sunDir * ts).x;
if (ds < 0.002) { sh = 0.0; break; }
sh = min(sh, 8.0 * ds / max(ts, 1e-4));
ts += max(ds, 0.03);
if (ts > 3.5) break;
}
sh = clamp(sh, 0.0, 1.0);
vec3 h = normalize(sunDir + v);
float ndl = max(dot(surfN, sunDir), 0.0);
float ndh = max(dot(surfN, h), 0.0);
float a2 = max(baseRough * baseRough, 0.04);
float spec = pow(ndh, mix(8.0, 96.0, 1.0 - baseRough)) * (1.0 - baseRough * 0.75);
vec3 skyFill = mix(vec3(0.16, 0.20, 0.26), vec3(0.36, 0.48, 0.68), 0.5 + 0.5 * surfN.y);
col = baseAlb * (vec3(1.15, 1.08, 0.96) * ndl * sh + skyFill * 0.48) + vec3(1.0, 0.95, 0.88) * spec * sh * 0.55;
}
// Left side: draw crisp translucent OBB projector box wireframes in 3D space
if (!showStamped) {
vec3 roA = transpose(boxARot) * (ro - boxACenter);
vec3 rdA = transpose(boxARot) * rd;
vec2 hitA = intersectBox(roA, rdA, boxARad);
if (hitA.y > 0.0) {
float wNear = (hitA.x > 0.0 && hitA.x < t) ? boxWire(roA + rdA * hitA.x, boxARad) : 0.0;
float wFar = (hitA.y < t) ? boxWire(roA + rdA * hitA.y, boxARad) : 0.0;
col += vec3(1.0, 0.52, 0.16) * (wNear * 0.95 + wFar * 0.35 + 0.05);
}
vec3 roB = transpose(boxBRot) * (ro - boxBCenter);
vec3 rdB = transpose(boxBRot) * rd;
vec2 hitB = intersectBox(roB, rdB, boxBRad);
if (hitB.y > 0.0) {
float wNear = (hitB.x > 0.0 && hitB.x < t) ? boxWire(roB + rdB * hitB.x, boxBRad) : 0.0;
float wFar = (hitB.y < t) ? boxWire(roB + rdB * hitB.y, boxBRad) : 0.0;
col += vec3(0.22, 0.88, 1.0) * (wNear * 0.95 + wFar * 0.35 + 0.05);
}
}
float sep = smoothstep(0.0035, 0.0008, abs(sx - split));
col = mix(col, vec3(0.95, 0.97, 1.0), sep);
fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}Attribution: MIT — written for this index.
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| deferred-and-projected-decals-live | Tesla T4 1920x1080 | 24.5× baseline (heavy) · 0.807 ms / 0.861 ms | opengl-timer-query · 32 frames · GPU Weight Atlas · Colab T4 sweep |
| deferred-and-projected-decals-live | NVIDIA L4 1920x1080 | 24.6× baseline (heavy) · 0.391 ms / 0.399 ms · 2.1× faster on L4 | opengl-timer-query · Cross-architecture validation |
| deferred-and-projected-decals-live | NVIDIA A100-SXM4-40GB 1920x1080 | 17.2× baseline (moderate) · 0.496 ms / 0.501 ms · 1.6× faster on A100 | opengl-timer-query · Cross-architecture validation |
Crosses
- Reoriented normal map blending (detail normals): Decal normals are blended over the existing surface normal with reoriented blending to keep detail intact.
- Clustered forward shading: Clusters let a forward renderer look up only the decals overlapping each pixel.