If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

## Pixar in a Box

### Course: Pixar in a Box>Unit 15

Lesson 2: Mathematics of rendering

# 5. 3D ray tracing part 1

Now we are ready to ray trace in 3D. We'll look at the problem of ray triangle intersection.

## Want to join the conversation?

• Shouldn't there be a " +d " in the last equation?
• From the author:Great catch thanks @phil! We will add a clarification immediately and update the video as soon as possible.
• How would you write the big equation in terms of t?
• So the equation is (using t in place of t*):
``a(1-t)Cx + atPx + b(1-t)Cy + btPy + c(1-t)Cz + ctPz + d = 0aCx - atCx + atPx + bCy - btCy + btPy + cCz - ctCz + ctPz + d = 0aCx + bCy + cCz + d + t(aPx + bPy + cPz - aCx - bCy - cCz) = 0t(aCx + bCy + cCz - aPx - bPy - cPz) = aCx + bCy + cCz + dt = (aCx + bCy + cCz + d) / (aCx + bCy + cCz - aPx - bPy - cPz)``

I think, though it's possible I made a mistake.
• How do you check if a ray actually intersect with plane? (Plane parallel with ray never have intersection)
• if a plane is infinite it is easy:
a ray can be described by a vector and a point. Take the vector and do a dot product with the normal of the plane. If the result is 0, the plane and the ray are parallel. If the result is negative, the ray points towards the plane. If it is positive, the ray points away from the plane.

If instead of an infinite plane you are looking for the intersection with a triangle, you need to do a few more tests but that's the idea.
• So if I were in, say, 7D, would the equation be:
`at + bu + cv + dw + fx + gy + hz + j = 0`?

(Note: I removed `e` and `i` to prevent confusion.)
• I think that would be the equation for a 6 dimensional object (manifold?) in 7 dimensions. If you wanted to define a plane in 7D, then I think you'd need 5 (different) equations of that form.
• what is the value of A,B,C and D?
(1 vote)
• In the beginning of the video, the subtitles say "retracing" instead of "ray-tracing". Is this intentional?
(1 vote)
• Oh, it eventually matches some points in linear algebra.
(1 vote)
• how do you ray trace a sphere or ellipsoid?
a sphere can be defined by xx + yy + zz = rr
so t should equal sqrt((sq(r)-sq(C.x)-sq(C.y)-sq(C.z)+sq(O.x)+sq(O.y)+sq(O.z))/(sq(P.x)+sq(P.y)+sq(P.z)-sq(C.x)-sq(C.y)-sq(C.z)))
and an ellipsoid can be described by
xx/aa + yy/bb + zz/cc = 1
so t should equal sqrt((w*w*h*h*d*d - C.x*C.x*h*h*d*d + O.x*O.x*h*h*d*d - C.y*C.y*w*w*d*d + O.y*O.y*w*w*d*d - C.z*C.z*w*w*h*h + O.z*O.z*w*w*h*h)/(C.x*C.x*h*h*d*d + P.x*P.x*h*h*d*d - C.y*C.y*w*w*d*d + P.y*P.y*w*w*d*d - C.z*C.z*w*w*h*h + P.z*P.z*w*w*h*h))
but the weight equation does not always work
(1 vote)
• In fact, where are the linear and plane equations coming from?
(1 vote)
• It is talking about tris, but in 3d programs, you should always have as few tris as possible, but have mainly quads, right?
(1 vote)

## Video transcript

- Now that we can retrace in 2D, we can finally go back to the problem we really wanna solve: retracing in 3D. In particular, we'll need to retrace flat planes, houses, and, ultimately, characters. A character like Carl is a complex shape but, as we discussed in the character modeling lesson, he can be broken down into lots of small, four-sided quadrilaterals, that is, four-sided polygons. And each quadrilateral can be converted into two triangles by adding an edge that connects diagonal points. That leads to the question: How do you intersect a ray with a triangle? It turns out, that that's one of the most fundamental calculations that a ray tracer performs. Here's a scene consisting of just one triangle. Our real scenes contain millions of triangles. But, once we know how to intersect a single triangle, our ray tracer just keeps doing that, over and over again. Now, I don't know about you, but I don't wanna do the same thing over and over again. So, it's a good thing we have computers to help us out and that they don't get tired. As in 2D, we start by setting up a coordinate system. But, this time, there are three directions: X, Y, and Z. As we explained earlier, we pick a camera position, call it C, and a viewing direction. And we construct an image plane perpendicular to the viewing direction. This is where our image will be formed. Let's pick a pixel, P, on the image plane and construct the parametric representation of the ray, CP, as R(t) = (1-t)C + tP. This is the same equation we saw in 2D, but now it represents three separate equations. One for X coordinates, one for Y coordinates, and one for Z coordinates. Remember that, in the previous video, we saw that, in two dimensions, every line can be written in implicit form as ax + by + c = 0. Very similar to this is the equation for a plane. And every triangle lies in a plane. The equation for a plane can be written in implicit form as ax + by + cz + d = 0. The intersection point, I, we're looking for, is in the plane of the triangle, meaning that aIx + bIy + cIz + d = 0, where Ix, Iy, and Iz are the coordinates of I. I is also on the ray, meaning that there's a value of t, again, let's call it t*, such that I = R(t*) which equals (1-t*)c + t*P which is really the three equations shown here. One for X, one for Y, and one for Z. Now we have four equations and four unknowns. To solve this system of equations, we can follow the recipe from 2D and substitute the last three equations into the first one. This gives us one equation with only one unknown, t*. But, it turns out, when you put all these substitutions in, it looks pretty scary. But, remember, it's not that bad. We're just plugging one value from one equation into another. Solve this for t*, then substitute back into the ray equations to get Ix, Iy, and Iz. Now, I know we've gone kind of fast, but the next exercise will let you practice computing intersection points for yourself.