Interpolating between normal angles

Hi All!

I have this object that is getting it’s orientation from the normals below it and I was wondering if there is a way to smooth the interpolation out?
I know I can apply smoothing on top of that rotation but I was wondering if anyone had tried interpolating between the neighbouring faces?

Thanks,
Pete

Is the mesh imported or created procedurally?

If it’s imported you can set normals on the import settings to be ‘Calculated’ and set the ‘Smoothing Angle’ to an angle you want.

1 Like

Yes, it’s really as simple as currentNormal = Vector3.Lerp(currentNormal, newNormal, whatever) or use SmoothDamp etc.

You can also try importing a smoothed mesh - the normals will at least be averaged between each…

And finally you can try a spherecastall, and average the results?

The normal that’s returned by a raycast will always be a “faceted” normal as it’s the actual surface facing. However it’s pretty easy to sample the angle of the underlying mesh’s vertices to get a smoother angle, assuming your mesh is smoothed (which it appears to be). No need to use SphereCastAll or multiple ray casts, though that’s a common way to do it too to handle moving between meshes.

Unity’s documentation even has an example implementation of exactly what you’re looking for:

3 Likes

Hey thanks guys! Sorry, now looking at the gif again the geometry is a little misleading, it is smooth, it just has different coloured faces.
What Bgolus mentioned was exactly what I was looking for though! Thanks mate!

Lookin’ Cool!
P.

1 Like

Very nice, didn’t know about that!

Hi, the result looks good but I don’t really understand how barycentricCoordinate really works.

Does it only work with a mesh collider because I’m trying to replicate the same smoothness but on a terrain surface. I use Quaternion.Lerp but it’s really not as smooth as what I’m seeing on petey’s gif.

I tried the example that was given by Bgolus with a simple sphere and I get this error when I rollover the sphere with the mouse…

IndexOutOfRangeException: Index was outside the bounds of the array.

All I did was copy the code from the unity doc on a camera and put a mesh collider on the sphere.

That sounds like you’re using the script on a mesh that doesn’t have any normals.

1 Like

Ok, turns out that the mesh was selected as convex and was not read/write enabled.

The link you provided works for me now, thanks.

Learned something new of what could be a new approach.