유니티(Unity) | Random Point Inside Sphere
Programming/Unity
2017. 10. 8. 13:57
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public Vector3 RandomPointInsideSphere(float radius) { float u = Random.Range(0f, 1f); float v = Random.Range(0f, 1f); float theta = 2f * Mathf.PI * u; float phi = Mathf.Acos(2f * v - 1f); float sqrt = Mathf.Sqrt(Random.Range(0f, 1f)); radius *= sqrt; Vector3 vec; vec.x = radius * Mathf.Sin(phi) * Mathf.Cos(theta); vec.y = radius * Mathf.Sin(phi) * Mathf.Sin(theta); vec.z = radius * Mathf.Cos(phi); return vec; } | cs |