{
 "id": "contact-shadows",
 "title": "Screen-Space Contact Shadows",
 "aliases": [],
 "domain": "visibility",
 "subdomain": "screen-space shadows",
 "stage": "visibility",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "mobile-gpu",
  "unity",
  "unreal",
  "godot"
 ],
 "year_introduced": 2016,
 "inputs": [
  "depth",
  "normals"
 ],
 "outputs": [
  "visibility-mask"
 ],
 "style": "both",
 "dynamic": "dynamic",
 "summary": "A short depth-buffer ray march toward the light that adds crisp occlusion in the small gaps shadow maps miss, such as feet on ground or small props.",
 "how_it_works": "For each pixel, step a ray from its view-space position toward the light for a short distance (typically centimetres to a metre) and compare each step's depth with the depth buffer; a hit with a thickness test means occlusion. The result is combined with the regular shadow term with a min or multiply, and the ray length is faded out at screen edges. The march is stepped uniformly with jitter, or by a hierarchical scheme, and works on any opaque rendered object including alpha-tested and skinned meshes whose shadow-map bias would otherwise leave a gap. Because it samples only what is on screen it ignores off-screen and back-facing occluders.",
 "when_to_use": "Choose it as a supplement to `shadow-mapping` or `cascaded-shadow-maps` when large biases detach shadows from contact points, especially on characters and foliage. Prefer `ray-traced-shadows` when you can trace actual geometry, `sdf-soft-shadows` for cheap soft world-space occlusion, and `shadow-volumes` for exact hard edges.",
 "cost": {
  "class": "cheap",
  "notes": "A small fixed number of depth taps per pixel along the light direction; fast, sometimes tiled for efficiency."
 },
 "failure_modes": [
  "Screen-space limits: missing occluders that are off-screen or hidden behind other geometry",
  "Thickness ambiguity causes false shadows behind thin objects",
  "Banding or noise from low step counts",
  "Halos at depth discontinuities if the ray length is too long"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1201/9781351208352-13",
   "type": "chapter",
   "title": "Practical Screen-Space Soft Shadows",
   "authors": [
    "Márton Tamás",
    "Viktor Heisenberger"
   ],
   "year": 2018,
   "venue": "GPU Pro 360",
   "url": "https://doi.org/10.1201/9781351208352-13",
   "ids": {
    "doi": "10.1201/9781351208352-13"
   },
   "license": null,
   "extra": {
    "csl_type": "book-chapter"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1201/9781351208352-13",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1201/9781351208352-13",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1201/9781351208352-13",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1201/9781351208352-13",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1201/9781351208352-13",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "tams2018practical"
  }
 ],
 "presets": [
  {
   "id": "contact-shadows-specimen",
   "title": "Left: coarse shadow map (gap at contact). Right: plus a short depth-buffer ray march toward the light",
   "runtime": "glsl-fragment",
   "params": {
    "RES": 32,
    "BIAS": 1.1,
    "NC": 24,
    "CL": 1.3,
    "THICK": 0.5
   },
   "code": "float hash(vec2 c){uvec2 u=uvec2(ivec2(floor(c)));uint h=u.x*1597334673u^u.y*3812015801u;h^=h>>15;h*=2246822519u;h^=h>>13;h*=3266489917u;h^=h>>16;return float(h)*(1./4294967296.);}\nfloat lum(vec3 c){return dot(c,vec3(.2126,.7152,.0722));}\nvec3 gam(vec3 c){return pow(clamp(c,0.,1.),vec3(1./2.2));}\nconst vec3 RO=vec3(0.,0.15,-5.0);\nconst vec2 SH=vec2(0.,.17);\nvec2 proj(vec3 v){return v.xy/v.z*1.7+SH;}\nvec3 unproj(vec2 u,float z){return RO+vec3((u-SH)*z/1.7,z);}\nconst vec3 LD=vec3(-0.75,0.55,0.37);\nconst vec4 SP[3]=vec4[3](vec4(-0.32,-0.25,0.3,0.45),vec4(0.38,-0.47,-0.15,0.23),vec4(0.02,0.38,0.75,0.17));\nfloat sdf(vec3 p){float d=p.y+0.7;for(int i=0;i<3;i++)d=min(d,length(p-SP[i].xyz)-SP[i].w);return d;}\nfloat isect(vec3 ro,vec3 rd,out vec3 n){\n  float t=1e9;n=vec3(0.);\n  if(rd.y<-1e-4){float tp=(-0.7-ro.y)/rd.y;if(tp>1e-4){t=tp;n=vec3(0.,1.,0.);}}\n  for(int i=0;i<3;i++){vec3 oc=ro-SP[i].xyz;float b=dot(oc,rd),c=dot(oc,oc)-SP[i].w*SP[i].w,h=b*b-c;\n    if(h>0.){float ts=-b-sqrt(h);if(ts>1e-4&&ts<t){t=ts;n=normalize(ro+rd*ts-SP[i].xyz);}}}\n  return t;}\nvec3 alb(vec3 p){\n  if(abs(p.y+.7)<.003){float c=mod(floor(p.x*1.5)+floor(p.z*1.5),2.);return mix(vec3(.55),vec3(.85),c);}\n  float d0=abs(length(p-SP[0].xyz)-SP[0].w),d1=abs(length(p-SP[1].xyz)-SP[1].w);\n  return d0<.01?vec3(.85,.35,.25):(d1<.01?vec3(.3,.55,.85):vec3(.92,.8,.3));}\nvec3 sky(vec3 d){return mix(vec3(.8,.85,.9),vec3(.25,.45,.85),clamp(d.y*1.6,0.,1.));}\nvec3 rayDir(vec2 uv){return normalize(vec3(uv-SH,1.7));}\nfloat zbuf(vec2 uv){vec3 n;vec3 rd=rayDir(uv);float t=isect(RO,rd,n);return t>1e8?1e9:t*rd.z;}\nvec3 lit(vec3 P,vec3 N){vec3 q;float sh=isect(P+N*2e-3,LD,q)>1e8?1.:0.;\n  return alb(P)*(vec3(.55,.62,.75)*(.45+.35*N.y-.25*N.z)*.45+vec3(1.,.95,.85)*max(dot(N,LD),0.)*sh*1.25);}\nvec3 shadeBasic(vec2 uv){vec3 rd=rayDir(uv),n;float t=isect(RO,rd,n);if(t>1e8)return sky(rd);\n  return mix(lit(RO+rd*t,n),sky(rd),smoothstep(6.,25.,t));}\nconst float EXT=3.2;\nvec3 lU(){return normalize(cross(vec3(0.,1.,0.),LD));}\nvec3 lV(){return cross(LD,lU());}\nvec2 toL(vec3 P){return vec2(dot(P,lU()),dot(P,lV()));}\nfloat recv(vec3 P){return 10.-dot(P,LD);}\nfloat smTex(vec2 st,float res){float tx=2.*EXT/res;vec2 c=(floor(st/tx)+.5)*tx;vec3 n;\n  return isect(LD*10.+lU()*c.x+lV()*c.y,-LD,n);}\nfloat vis1(vec3 P,vec3 N,vec2 st,vec2 o,float res,float b){float tx=2.*EXT/res;vec2 cc=(floor((st+o)/tx)+.5)*tx;vec3 q;\n  float d=isect(LD*10.+lU()*cc.x+lV()*cc.y,-LD,q);vec2 e=cc-st;\n  float zr=recv(P)+(e.x*dot(N,lU())+e.y*dot(N,lV()))/max(dot(N,LD),.15);\n  return zr-b>d?0.:1.;}\nvec3 shadeV(vec3 P,vec3 N,float vis){return alb(P)*(vec3(.55,.62,.75)*(.45+.35*N.y-.25*N.z)*.45+vec3(1.,.95,.85)*max(dot(N,LD),0.)*vis*1.3);}\n\nconst float RES=32.0; // P\nconst float BIAS=1.1; // P\nconst int NC=24; // P\nconst float CL=1.3; // P\nconst float THICK=0.5; // P\nvoid mainImage(out vec4 fragColor,in vec2 fragCoord){\n  vec2 u0=(fragCoord-.5*iResolution.xy)/iResolution.y;bool R=u0.x>0.;vec2 uv=vec2(u0.x+(R?-.25:.25),u0.y);\n  vec3 rd=rayDir(uv),n;float t=isect(RO,rd,n);vec3 col=sky(rd);\n  if(t<1e8){\n    vec3 P=RO+rd*t;\n    float vis=vis1(P,n,toL(P),vec2(0.),RES,BIAS);\n    if(R){\n      vec3 o=P+n*.01;\n      for(int i=1;i<=NC;i++){vec3 v=o+LD*(float(i)/float(NC)*CL)-RO;float zs=zbuf(proj(v));\n        if(zs<v.z-.01&&v.z-zs<THICK){vis=0.;break;}}\n    }\n    col=mix(shadeV(P,n,vis),sky(rd),smoothstep(6.,25.,t));\n  }\n  if(abs(u0.x)<.0015)col=vec3(1.);fragColor=vec4(gam(col),1.);\n}",
   "basis": "original",
   "license_note": "written for this index",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-29",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "contact-shadows",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-29T15:51:50.471Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.7454,
      "std": 0.2123,
      "unique_colors": 192
     },
     "image": "/specimens/contact-shadows-specimen.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "shadow-mapping",
  "ray-traced-shadows",
  "sdf-soft-shadows"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/visibility/contact-shadows.json"
 },
 "solves": [
  "character's feet float above the ground in shadows",
  "small props lose their shadow contact with surfaces",
  "shadow map bias detaches shadows from objects",
  "need crisp tiny-gap occlusion near the light",
  "shadows miss small details like fingers or foliage"
 ],
 "authority": "Tamás & Heisenberger, 2018",
 "url": "https://texelatlas.vercel.app/t/contact-shadows/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 10.73,
  "tier": "moderate",
  "p50_ms": 0.353,
  "p95_ms": 0.3691,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "contact-shadows-specimen",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 8.28,
   "tier": "moderate",
   "p50_ms": 0.1316,
   "p95_ms": 0.1336,
   "speedup_x": 2.68,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 6.62,
   "tier": "moderate",
   "p50_ms": 0.1907,
   "p95_ms": 0.1914,
   "speedup_x": 1.85,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}