KITASENJU DESIGN BLOG

memo, html, javascript, unity

vector3の高速化

www.slideshare.net

public static float Normalize(ref Vector3 v) { 
    // Magnitude関数のインライン化 
    float mag = (float)System.Math.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z); 
    v.x /= mag; 
    v.y /= mag; 
    v.z /= mag; 
    // 正規化と同時にスカラー値を返して同じ処理を繰り返さない 
    return mag; 
} // 正規化したベクトルを返す、インスタンスの値渡しは行わない 


public static float Normalize(ref Vector3 v, out Vector3 vv) { 
    float mag = (float)System.Math.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z); 
    vv.x = v.x / mag; 
    vv.y = v.y / mag; 
    vv.z = v.z / mag; 
    return mag; 
} 


// 弾丸用のY座標省略 
public static float NormalizeXZ(ref Vector3 v) { 
    float mag = (float)System.Math.Sqrt(v.x * v.x + v.z * v.z); v.x /= mag; v.z /= mag; v.y = 0f; return mag; 
}
"FOOTER"