· 3 min read

A walkable corridor in plain CSS 3D

The Chhaya studio site is built around a concrete corridor in plain CSS 3D: short wall segments, a camera that turns toward each project, and a draw distance that keeps phones alive.

  • CSS
  • 3D
  • scroll

Chhaya is a concept website for an architecture studio. You walk down a corridor and each project hangs on a wall; tap one and it opens as a normal page with a plan and a section. There is no canvas and no 3D library. The corridor is ordinary elements placed in 3D space with CSS transforms.

Short segments, not long planes

The first version used five divs: two walls, a floor and a ceiling as long as the whole corridor, plus an end wall. It looked right on a laptop and crashed iPhones. A phone gives every 3D plane its own GPU texture, and a plane a hundred metres long is a very large texture. So the corridor is now cut into 480 px segments, each with its own walls, floor and ceiling.

js
for(let k=0;k<n;k++){
  const zc=-(k*SEG+SEG/2);
  plane('wall',SEG,Hc,`translate3d(${-Wc}px,0,${zc}px) rotateY(90deg)`);
  plane('wall',SEG,Hc,`translate3d(${Wc}px,0,${zc}px) rotateY(-90deg)`);
  plane('floor',Wc*2,SEG,`translate3d(0,${Hc/2}px,${zc}px) rotateX(90deg)`);
  plane('ceil',Wc*2,SEG,`translate3d(0,${-Hc/2}px,${zc}px) rotateX(-90deg)`);
}

Here Wc is half the corridor width and Hc its height, both computed from the window size. The scene has perspective set and the wrappers use transform-style: preserve-3d, so children keep their 3D position instead of being flattened.

Only draw what is near

Each frame, segments behind the viewer or further ahead than the fog lets you see get display: none. Phones get a shorter draw distance than laptops. That keeps only a handful of planes alive at once, and it is what stopped the crashes.

js
const vis=cam+pre-seg.far<persp*.98&&seg.near-pre-cam<VIEW;
if(vis!==seg.vis){seg.vis=vis;seg.els.forEach(e=>e.style.display=vis?'':'none')}

Here Wc is half the corridor width, Hc its height and L its length, all computed from the window size. For this to look like a room, the scene has perspective set and the wrappers use transform-style: preserve-3d, so children keep their 3D position instead of being flattened. Without that line you get a pile of flat rectangles.

Walking is moving the world

The camera never moves. The page has a tall empty track, and scroll progress is turned into a distance. Each frame the distance eases toward its target, and the whole world container is pushed toward you along Z. A small pointer-driven sway and a tiny bob are added on top, and the bob is turned off for people who ask for reduced motion. As you walk past a project, the view turns toward it a little, so the frame in front of you sits in the middle of the screen.

js
target=sc()*camMax;cam+=(target-cam)*(reduce?1:.075);
world.style.transform=`translate3d(${-smx*10}px,${bob}px,${cam+pre}px)`;

Fog

A real corridor has a far end you can see, and that makes the illusion fall apart: you notice the vanishing point is just a rectangle. So I hide it. A fixed overlay with a radial gradient in the page colour sits over the centre of the screen. Then each frame, every framed project fades in as it gets closer, using a smoothstep on its distance ahead of the camera. Near the end of the walk the overlay fades out so you can read the last wall.

What I would still watch: frame rates on older Android phones, which I have not measured. The 3D scene is marked aria-hidden; the projects are also in a plain, keyboard-friendly index, and every project opens as an ordinary dialog, so nothing is trapped inside the 3D.