うにてぃブログ

主にUnityとC#に関する記事を書いていきます

【Unity】昔 Vector2.Lerp や Vector3.Lerp が重いと言われていた気がするので調べてみた

https://github.com/Unity-Technologies/UnityCsReference
から 2017.1 の処理を見たのですが、特に重そうな処理をしておらず

Mathf.Lerp を要素分計算しているだけだった

もしかしたら Unity4系 だったら処理が違ったのかもしれないが、古いので調査は断念・・・

f:id:hacchi_man:20200719233701p:plain:w500

    // Mathf.Lerp
    public static float Lerp(float a, float b, float t)
    {
      return a + (b - a) * Mathf.Clamp01(t);
    }


    // Vector3.Lerp
    public static Vector3 Lerp(Vector3 a, Vector3 b, float t)
    {
      t = Mathf.Clamp01(t);
      return new Vector3(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t);
    }
 
    // Color.Lerp
    public static Color Lerp(Color a, Color b, float t)
    {
      t = Mathf.Clamp01(t);
      return new Color(a.r + (b.r - a.r) * t, a.g + (b.g - a.g) * t, a.b + (b.b - a.b) * t, a.a + (b.a - a.a) * t);
    }