Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Interpolating between normal angles

Discussion in 'Scripting' started by petey, Feb 10, 2017.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    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
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    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.
     
    Skuxlife likes this.
  3. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    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?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    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:
    https://docs.unity3d.com/ScriptReference/RaycastHit-barycentricCoordinate.html
     
    Steven-1, hippocoder and Xarbrough like this.
  5. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    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.
     
    jo82 likes this.
  6. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Very nice, didn't know about that!
     
  7. jo82

    jo82

    Joined:
    Dec 3, 2015
    Posts:
    43
    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.
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    That sounds like you're using the script on a mesh that doesn't have any normals.
     
    jo82 likes this.
  9. jo82

    jo82

    Joined:
    Dec 3, 2015
    Posts:
    43
    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.