# Compute normal from 3d shape equation [duplicate]

For my raytracing needs, I'd like to find a way to compute a normal from a given 3d equation. This equation can be either a parametric one or a cartesian one (preferably in canonical form).

From my researches on the internet, it seems to be doable by computing the gradient of the equation but I want to be sure about that.

I manually tried to test against the sphere cartesian equation and it worked but that's not enough to prove that that is the solution.

• This is correct, if by "equation" you mean the level surface of a function $F(x,y,z) = c$ that represents the surface. Then taking the gradient of $F$ evaluated at a point on that surface gives you the (outward) normal for the surface at that point. "Outward" here corresponds to the direction in which the surface "moves" when the constant $c$ increases. – hardmath Dec 19 '16 at 19:24
• Alright thanks for answering. I just wanted a confirmation to be sure it was right. – Telokis Dec 19 '16 at 19:27
• @hardmath What about parametric equations ? Does it work more or less the same way ? For instance, using the mobius strip's one found on Wikipedia – Telokis Dec 19 '16 at 21:36
• If you have a specific "parameterization" of a surface in mind, I would suggest posting a new Question with the details of that parametric representation, so that Readers can respond in detail. Note that if by "parametric" you mean $z = f(x,y)$, this corresponds to $F(x,y,z) = 0$ where $F(x,y,z) = z - f(x,y)$. Then the gradient approach applies accordingly. – hardmath Dec 20 '16 at 16:24
• One other caveat: It is possible that $F(x,y,z)$ turns out to have a degenerate gradient, i.e. that the gradient is a zero vector at one or more points on the surface. In that case one cannot "normalize" the zero vector into one of unit length. Additional information (perhaps from studying the gradient in a "neighborhood" of a point) is needed to determine a unit normal vector in such cases. – hardmath Dec 20 '16 at 16:33