On this page
Interior Mapping
Look through a window and you see a room, with a floor, a back wall, a side wall, all of which cost geometry that a city of a thousand windows could never afford. Interior mapping replaces those six surfaces with a shader that guesses, per pixel, which wall you are actually looking at and samples a picture of it. One quad per window, one cubemap per room, and the city gets its interiors for free.
// Recenter the UV, add the view direction as depth, sample the inside of a cube.
float3 dir = normalize(float3(uv * 2.0 - 1.0, -viewDirT.z));
half3 color = SAMPLE_TEXTURECUBE(_RoomCube, sampler_RoomCube, dir).rgb;
The problem of a thousand rooms¶
The technique, published by Joost van Dongen in 2008 and used since in everything from SimCity to the interiors of Spider-Man’s city, attacks the cost head on. Model a room and you pay six surfaces, multiplied by every window, multiplied by every building. The alternative is to render a static image behind the window, which is cheap and fails the moment you look at it closely, because rooms have depth and a flat image does not. Interior mapping takes the third path: fake the depth with the view.
The key observation is geometric. When you look through a window at a normal angle, you can only ever see three of the room’s six sides: the back wall, and one of the floor, ceiling, or two side walls, depending on exactly where the ray lands. So the shader does not need to model the room. It needs to answer one question per pixel: which of those three is the ray hitting first? and then show a picture of that wall at the spot where the ray meets it.
The cubemap trick¶
The classic implementation answers that question with a cubemap, sampled from the inside. Here is why that works. Take the window’s UVs, recenter them to minus 1 to 1 so the center of the window is the origin, and you have two axes of a direction. The view direction, brought into the window’s tangent space, gives you the third: how much the ray is diving into the room. Combine the three, normalize, and you have a direction that points from the window into the room, straight along your line of sight:
// uv is the window's 0..1 coordinate, recentered:
float2 c = frac(IN.uv) * 2.0 - 1.0;
// The view direction in the window's tangent frame.
float3 viewDirT = TBN * normalize(GetWorldSpaceViewDir(positionWS));
// Dive into the room along the view's z, and out through the window in xy.
float3 dir = normalize(float3(c, -viewDirT.z));
half3 color = SAMPLE_TEXTURECUBE(_RoomCube, sampler_RoomCube, dir).rgb;
The cubemap is the room, drawn from its center toward its six inner walls. A direction pointing mostly along z, your view direction nearly parallel to the window, hits the back wall of the cube and shows it. Tilt the view down and the direction gains a minus y component and the sample slides across onto the floor face. The cube does the “which of the three sides” selection for you, because the six faces are the six sides, and the ray can only exit through the one it is most aligned with.
Two authoring notes. The cube must be the inside of the room, so you sample with the direction as is, or you flip the direction if your cube is authored outward. And the UVs must be recentered per window pane, which is why the frac is there: a multi window facade has the same texture repeated per window, and each repeat is one room.
Where it breaks, and how people patch it¶
The effect is a projection, and projections have their usual failure at grazing angles. Look at the window nearly edge on and the “room” compresses into a sliver, and the hard edges of the cube faces can show as a faint seam inside the room, a line where the back wall meets the side wall. Games patch these with a painted gradient in the cube’s own artwork, with a mesh trim around the window opening, or by fading the whole effect out as the view angle approaches edge on. The Cubemaps chapter is the reference for the asset itself, and Parallax Mapping is the same “fake depth with the view” idea applied to a single surface instead of a room.
What the shader does¶
The shader samples a single cubemap authored as the inside of a small furnished room, so the rooms behind the windows shift plausibly with the camera angle, with no room geometry anywhere in the scene.
Illustrative schematic: Windows, no room geometry; the room is a cubemap.
The window quad with the full recenter, dive, sample chain, plus a fade to the facade at grazing angles to hide the projection’s edge.
Shader "DecompiledArt/Textures/Projections/InteriorMapping/InteriorMapping"
{
Properties
{
_FacadeTex("Facade Texture", 2D) = "white" {}
_RoomCube("Room Interior (inside of cube)", Cube) = "" {}
[MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
_RoomDepth("Room Depth (fake)", Range(0, 2)) = 1
_FadeAngle("Grading Fade (dot)", Range(0, 1)) = 0.7
}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" }
Cull Off
Pass
{
Name "ForwardOnly"
Tags { "LightMode" = "UniversalForward" }
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
TEXTURE2D(_FacadeTex); SAMPLER(sampler_FacadeTex);
TEXTURECUBE(_RoomCube); SAMPLER(sampler_RoomCube);
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
float4 tangentOS : TANGENT;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float3 positionWS : TEXCOORD0;
half3 normalWS : TEXCOORD1;
half3 tangentWS : TEXCOORD2;
half3 bitangentWS: TEXCOORD3;
float2 uv : TEXCOORD4;
};
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
float _RoomDepth;
half _FadeAngle;
CBUFFER_END
Varyings Vert(Attributes IN)
{
Varyings OUT;
OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.positionWS = TransformObjectToWorld(IN.positionOS.xyz);
OUT.uv = IN.uv;
VertexNormalInputs n = GetVertexNormalInputs(IN.normalOS, IN.tangentOS);
OUT.normalWS = n.normalWS;
OUT.tangentWS = n.tangentWS;
OUT.bitangentWS = n.bitangentWS;
return OUT;
}
half4 Frag(Varyings IN) : SV_Target
{
// The facade, for the parts of the mesh that are not windows.
half3 facade = SAMPLE_TEXTURE2D(_FacadeTex, sampler_FacadeTex, IN.uv).rgb;
// The room: recenter the window, dive along the view, sample the cube.
float2 c = frac(IN.uv) * 2.0 - 1.0;
half3 viewDirT = half3x3(IN.tangentWS, IN.bitangentWS, IN.normalWS)
* normalize(GetWorldSpaceViewDir(IN.positionWS));
float3 dir = normalize(float3(c, -viewDirT.z * _RoomDepth));
half3 room = SAMPLE_TEXTURECUBE(_RoomCube, sampler_RoomCube, dir).rgb;
// Fade to the facade when the window is viewed nearly edge on.
half facing = saturate(dot(normalize(IN.normalWS), -normalize(GetWorldSpaceViewDir(IN.positionWS))));
half showRoom = smoothstep(_FadeAngle * 0.5, _FadeAngle, facing);
half3 color = lerp(facade, room, showRoom);
return half4(_BaseColor.rgb * color, _BaseColor.a);
}
ENDHLSL
}
}
FallBack Off
}
Author the facade texture with dark window holes aligned to the quad UVs, and the room cube as a small room viewed from its center. The two then compose themselves.
🔗 Related Chapters¶
Cubemaps • Parallax Mapping • Foundations: Normal Maps
Related functions: Dot • Smoothstep
❤️ 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.