Task 3 of 5
Here is the whole idea of path tracing. Fire a ray from the eye through a pixel. Where it lands, pick a random new direction and carry on. Multiply the surviving light by each surface's albedo as you go, and when the path finally escapes into the sky, that sky's brightness — scaled by everything it passed through — is your estimate of what the pixel should be.
One estimate. It is unbiased: average enough of them and you get the exact answer, no approximation anywhere. It is also, on its own, nearly worthless — one path is a single sample of an integral over every direction light could possibly have arrived from, and it is wrong by tens of percent.
The bounce itself is one line of geometry. Take a uniformly random point on the unit sphere, add it to the surface normal, and normalize: that gives you a cosine-weighted direction on the hemisphere — more likely to leave straight up than sideways, which is exactly how a matte surface actually scatters. Because the sampling density already matches the physics, no correction factor is needed; the throughput just picks up the albedo and moves on.
albedo, then send the ray off along normal + random unit vector,
normalized.through by albedo, once per bouncenx + sx, ny + sy, nz + szsphereT and planeT both assume a unit direction(sx, sy, sz) is a random point on the whole sphere, so half of
those directions point into the surface. Adding the unit normal shifts the
sphere so it sits tangent to the surface: every direction then points outward, and the
density of directions comes out proportional to the cosine of the angle from the
normal — which is Lambert's law, for free.
through = through * albedo;
dx = nx + sx;
dy = ny + sy;
dz = nz + sz;
len = Math.sqrt(dx * dx + dy * dy + dz * dz);
dx = dx / len;
dy = dy / len;
dz = dz / len;Grainy. Every pixel should be visibly wrong by itself, with soft shadows and the contact between the spheres and the floor only hinted at through the noise. If your image comes out smooth, the bounce is not random and the next task has nothing to average.
This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.