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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

The least expensive way to get sin of two 3d vectors.

Discussion in 'Shaders' started by CauseMoss, Sep 17, 2019.

  1. CauseMoss

    CauseMoss

    Joined:
    Sep 16, 2017
    Posts:
    20
    Hello.
    What's the best, the least expensive, way to get the sine of two vectors?
    Sorry for such basic question.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,256
    I don't know if this is the absolute cheapest way, but it's how I would do it:
    Code (csharp):
    1. // the dot product of two normalized vectors is the cosine of the angle between
    2. float cosine = dot(normalize(vecA), normalize(vecB));
    3.  
    4. // sin²(x) + cos²(x) = 1.0, so sin(x) = √(1.0 - cos²(x))
    5. float sine = sqrt(1 - (cosine * cosine));
     
  3. CauseMoss

    CauseMoss

    Joined:
    Sep 16, 2017
    Posts:
    20
    Thank you.
    I thought it was the fastest way too.