Wie kommt ein 3D-Modell auf den Bildschirm? Du schreibst den Kern eines kleinen Renderers – Projektion,
Perspektive, Licht und Schatten – und malst damit den Turm der Sparrenburg in Bielefeld aus unserem Steinmodell:
4.466 Steine, 24 × 24 Noppen, 40 Lagen. Das Programm vergleicht dein Bild Pixel für Pixel mit der Referenz.
How does a 3D model get onto the screen? You write the core of a small renderer – projection,
perspective, light and shadow – and use it to paint the tower of the Sparrenburg in Bielefeld from our brick model:
4,466 bricks, 24 × 24 studs, 40 layers. The program compares your image pixel by pixel with the reference.
Level 1 · Parallelprojektion und Maler-Algorithmus
Jeder Stein hat sechs Seitenflächen; das Programm sammelt alle Flächen, die man sehen könnte. Deine Funktion
project bildet jeden Eckpunkt auf das Bild ab, sortFaces legt die Reihenfolge fest: Wie ein
Maler malst du zuerst das Hinterste und dann alles, was davor liegt.
Parallelprojektion: d = p − cam.target, dann bildX = Mitte + scale · (d · right),
bildY = Mitte − scale · (d · up), tiefe = d · forward. Erklärung unten bei Mathe.
Die Steine sind 1 × 1 Noppe groß und 1,2 Noppen hoch (ein Stein ist 9,6 mm hoch, eine Noppe 8 mm breit).
Level 1 · Orthographic projection and the painter's algorithm
Every brick has six faces; the program collects all faces that could be seen. Your function project maps
every corner onto the image, sortFaces decides the order: like a painter you paint the farthest first and
then everything in front of it.
Orthographic projection: d = p − cam.target, then imageX = centre + scale · (d · right),
imageY = centre − scale · (d · up), depth = d · forward. Explained below under Maths.
The bricks are 1 × 1 stud and 1.2 studs high (a brick is 9.6 mm high, a stud 8 mm wide).
Level 2 · Perspektive
Im echten Leben wird kleiner, was weiter weg ist. Die Kamera sitzt jetzt an einem Punkt cam.eye, die
Bildebene liegt im Abstand cam.focal (in Pixeln) davor. Nach dem Strahlensatz gilt:
Bildabstand ÷ focal = Abstand zur Mitte ÷ Tiefe.
project(p, cam) → [bildX, bildY, tiefe]
Der Startcode bildet schon richtig ab – nur ohne Perspektive. Vergleiche die Bilder: Was ist anders?
Level 2 · Perspective
In real life things get smaller the farther away they are. The camera now sits at a point cam.eye, the
image plane lies cam.focal (in pixels) in front of it. By the intercept theorem:
image offset ÷ focal = offset from the centre ÷ depth.
project(p, cam) → [imageX, imageY, depth]
The starter code already projects correctly – just without perspective. Compare the images: what is different?
Level 3 · Licht nach Lambert
Bisher hat jede Seite eine feste Helligkeit. Jetzt scheint die Sonne aus Richtung l: Eine Fläche ist umso
heller, je direkter das Licht auf sie fällt. Nach Johann Heinrich Lambert ist die Helligkeit der Kosinus des Winkels
zwischen Normale n und Lichtrichtung l – und den liefert das Skalarprodukt.
shade(color, n, l) → [r, g, b] = max(0, n · l) · color
Level 3 · Lambert lighting
So far every side has a fixed brightness. Now the sun shines from direction l: a face is the brighter the
more directly the light hits it. Following Johann Heinrich Lambert, the brightness is the cosine of the angle between the
normal n and the light direction l – and the dot product gives you exactly that.
shade(color, n, l) → [r, g, b] = max(0, n · l) · color
Bonus · Harte Schatten durch das Voxel-Gitter
Der Turm wirft einen Schatten. Für jede Fläche schickt das Programm einen Strahl von der Fläche zur Sonne. Liegt
ein Stein im Weg, bleibt nur das Umgebungslicht. Weil die Szene ein Gitter ist, muss der Strahl nicht in winzigen
Schritten laufen: Mit dem 3D-DDA-Verfahren von Amanatides und Woo springt er von Zellwand zu Zellwand und prüft jede
Zelle genau einmal.
inShadow(p, l, grid) → true / false
Bonus · Hard shadows through the voxel grid
The tower casts a shadow. For every face the program sends a ray from the face towards the sun. If a brick is in the
way, only the ambient light is left. Because the scene is a grid, the ray does not have to creep along in tiny steps:
with the 3D DDA method by Amanatides and Woo it jumps from cell wall to cell wall and checks every cell exactly once.
inShadow(p, l, grid) → true / false
Strg+Enterführt ausruns
Dein Code läuft nur in deinem Browser (Web Worker, Zeitlimit 6 s). console.log(…)
schreibt unter den Editor – Vorsicht, project wird tausendfach aufgerufen.Your code runs
only in your browser (web worker, 6 s time limit). console.log(…) prints below the editor – careful,
project is called thousands of times.
Tests
Dein BildYour imageReferenzReference
Tipp 1: Was heißt „Anteil in Richtung right“?Hint 1: what does "the part along right" mean?
Ist right ein Einheitsvektor, dann sagt dot(d, cam.right), wie weit d
nach rechts zeigt – in Noppen. Mal cam.scale sind es Pixel.
If right is a unit vector, dot(d, cam.right) tells you how far d points
to the right – in studs. Times cam.scale it is pixels.
Tipp 2: Warum minus bei y?Hint 2: why minus for y?
Auf dem Canvas liegt (0, 0) oben links und y wächst nach unten. Was in der Welt höher ist, muss also eine
kleinere Bild-y-Koordinate bekommen.
On the canvas (0, 0) is the top left corner and y grows downwards. So something higher up in the world needs a
smaller image y.
Tipp 3: SortierenHint 3: sorting
faces.sort((a, b) => b.depth - a.depth) sortiert absteigend nach Tiefe. Danach
return faces; nicht vergessen.
faces.sort((a, b) => b.depth - a.depth) sorts by depth, largest first. Don't forget
return faces; afterwards.
Tipp 1: StrahlensatzHint 1: intercept theorem
Schau dir die Skizze „Perspektive“ unten an: Bildabstand ÷ focal = x ÷ z.
Also Bildabstand = cam.focal * x / z.
Look at the "perspective" sketch below: image offset ÷ focal = x ÷ z.
So image offset = cam.focal * x / z.
Tipp 2: Und die Tiefe?Hint 2: and the depth?
Gib z als dritten Wert zurück. Damit sortiert das Programm die Flächen (Maler-Algorithmus).
Return z as the third value. The program uses it to sort the faces (painter's algorithm).
Tipp 1: Skalarprodukt = KosinusHint 1: dot product = cosine
Für Einheitsvektoren ist dot(n, l) genau cos θ: 1 bei Licht von vorn, 0 bei streifendem Licht,
negativ, wenn das Licht von hinten kommt.
For unit vectors dot(n, l) is exactly cos θ: 1 for light straight on, 0 for grazing light,
negative when the light comes from behind.
Tipp 2: Keine negative HelligkeitHint 2: no negative brightness
Math.max(0, …) schneidet negative Werte ab: Eine Fläche im eigenen Schatten wird nicht „schwärzer als schwarz“.
Math.max(0, …) cuts off negative values: a face in its own shadow does not get "blacker than black".
Tipp 1: Erst einfach – Ray MarchingHint 1: simple first – ray marching
Laufe in kleinen Schritten (z. B. 0,05) von p in Richtung l. An jeder Stelle:
Zelle = Math.floor(x), Math.floor(y), Math.floor(z / grid.h). Belegt → true.
Außerhalb des Gitters → false. Das ist langsam und kann dünne Ecken verpassen, besteht aber fast alle Tests.
Walk in small steps (e.g. 0.05) from p in direction l. At every position:
cell = Math.floor(x), Math.floor(y), Math.floor(z / grid.h). Filled → true.
Outside the grid → false. It is slow and may miss thin corners, but passes almost all tests.
Tipp 2: DDA – von Wand zu WandHint 2: DDA – from wall to wall
Rechne in Gitterkoordinaten (z durch grid.h teilen, auch bei l). Der Strahl ist
o + t · d. Für jede Achse merkst du dir tMax (bei welchem t kommt die nächste Wand) und
tDelta (wie viel t eine ganze Zelle kostet). In jedem Schritt gehst du über die Wand mit dem kleinsten
tMax, änderst die Zelle um ±1 und erhöhst dieses tMax um tDelta.
Work in grid coordinates (divide z by grid.h, for l too). The ray is
o + t · d. For every axis keep tMax (at which t the next wall comes) and tDelta
(how much t one whole cell costs). In each step cross the wall with the smallest tMax, move the cell by ±1
and increase that tMax by tDelta.
Tipp 3: Division durch nullHint 3: division by zero
Ist eine Komponente von l genau 0, erreicht der Strahl auf dieser Achse nie eine Wand:
tMax = tDelta = Infinity. JavaScript rechnet damit richtig weiter.
If a component of l is exactly 0, the ray never reaches a wall on that axis:
tMax = tDelta = Infinity. JavaScript handles that correctly.
Die Mathematik dahinterThe maths behind it
Alles, was ein Renderer braucht, steckt in Vektoren und einem einzigen Rechenzeichen: dem Skalarprodukt.
Everything a renderer needs is made of vectors and a single operation: the dot product.
Vektoren und Skalarprodukta · b = aₓbₓ + a_yb_y + a_zb_z Ist b ein Einheitsvektor (Länge 1), dann ist
a · b die Länge des „Schattens“ von a auf b (dick markiert): Es misst, wie weit a in Richtung b zeigt.Vectors and the dot producta · b = aₓbₓ + a_yb_y + a_zb_z If b is a unit vector (length 1), a · b is the
length of the "shadow" of a on b (thick line): it measures how far a points in the direction of b.Parallelprojektiond = p − target bildX = W/2 + s·(d·right)bildY = H/2 − s·(d·up)
Alle Sehstrahlen sind parallel. Man zerlegt d in die Richtungen „rechts“ und „oben“ der Kamera – entfernte Steine
werden nicht kleiner, wie in einem technischen Plan.Orthographic projectiond = p − target imageX = W/2 + s·(d·right)imageY = H/2 − s·(d·up)
All rays of sight are parallel. You split d into the camera's "right" and "up" directions – distant bricks do not get
smaller, as in a technical drawing.Perspektive (Strahlensatz)y_Bild / f = y / z → y_Bild = f · y / z Der Sehstrahl vom Auge zum Punkt schneidet die Bildebene
im Abstand f. Doppelt so weit weg heißt halb so groß. Die Tiefe z ist der Anteil von p − eye in Blickrichtung.Perspective (intercept theorem)y_img / f = y / z → y_img = f · y / z The ray from the eye to the point crosses the image plane at
distance f. Twice as far away means half as big. The depth z is the part of p − eye along the viewing direction.Maler-Algorithmus
Fläche 1 liegt am weitesten hinten und wird zuerst gemalt, Fläche 3 zuletzt. Was davor liegt, übermalt einfach das Dahinter.
Darum sortiert man die Flächen absteigend nach ihrer Tiefe.Painter's algorithm
Face 1 is farthest back and is painted first, face 3 last. Whatever is in front simply paints over what is behind. That is
why the faces are sorted by depth, largest first.Licht nach LambertHelligkeit = max(0, n · l) = max(0, cos θ) Schräges Licht verteilt sich auf eine größere Fläche
(gestrichelte Strahlen), darum wird die Fläche dunkler. n ist die Normale, l zeigt zur Lichtquelle.Lambert lightingbrightness = max(0, n · l) = max(0, cos θ) Slanted light spreads over a larger area (dashed rays), so
the face gets darker. n is the normal, l points to the light source.3D-DDA (Amanatides & Woo)
Der Strahl geht immer über die nächstgelegene Zellwand – die Achse mit dem kleinsten tMax – und prüft jede Zelle, durch
die er läuft (hell). Trifft er einen Stein (dunkel), liegt p im Schatten.3D DDA (Amanatides & Woo)
The ray always crosses the nearest cell wall – the axis with the smallest tMax – and checks every cell it passes (light).
If it hits a brick (dark), p lies in shadow.