쫑가 과정

두 각(Quaternion) 사이를 부드럽게 이어준다 / Quaternion.Slerp 본문

Unity/이론

두 각(Quaternion) 사이를 부드럽게 이어준다 / Quaternion.Slerp

쫑가 2022. 1. 14. 19:59

Quaternion.Slerp


https://docs.unity3d.com/ScriptReference/Quaternion.Slerp.html

 

Unity - Scripting API: Quaternion.Slerp

Use this to create a rotation which smoothly interpolates between the first quaternion a to the second quaternion b, based on the value of the parameter t. If the value of the parameter is close to 0, the output will be close to a, if it is close to 1, the

docs.unity3d.com

 

public static Quaternion Slerp(Quaternion a, Quaternion b, float t);

a = 시작 값(value)으로, t = 0일 때 반환.

b= 종료 값(value)으로, t = 1일 때 반환.

t = 보간 비율 (a와 b 값 사이를 채우는 비율) @ [0,1] 범위 고정.

 

비율 t 에 의해 쿼터니언 a와 b 사이를 구형으로 보간(채운다)한다. 

쿼터니언 a와 b 사이에 구형(완만한 곡선)으로 보간 되는 쿼터니언으로 반환.

(t가 0.5라면 a와 b 사이의 중앙 쿼터니언을 반환한다)

 

첫 번째 쿼터니언 a에서 두 번째 쿼터니언 b 사이를 부드럽게 보관하는 회전을 생성.

매개변수의 값이 0에 가까우면 출력이 a에 가깝고 1에 가까우면 b에 가깝습니다.

Lerp는 선형, Slerp는 구형

 

예) b 값을 따라 부드럽게 회전하는 a

void Update()
{
	transform.rotation = Quaternion.Slerp(from.transform.rotation, to.transform.rotation, 0.01f);
}

from은 캡슐 오브젝트 - 초기 지정 rotation은 (0, 90, 90)

to는 사각 오브젝트 - 초기 지정 rotation은 (0, 0, 0)

from의 회전이 to의 회전을 0.01f 씩 보간하며 따라간다.

void Update()
{
	transform.rotation = Quaternion.Slerp(from.transform.rotation, to.transform.rotation, timeCount);
	timeCount = timeCount + Time.deltaTime;);
}

빠릿해졌다.

예 2) 마우스 방향으로 부드럽게 회전하기.

void Update()
{
	Vector3 mousePosition = Input.mousePosition;
	Vector3 playerPosition = transform.position;
	mousePosition.z = playerPosition.y + playerCamera.transform.position.y;
	Vector3 target = playerCamera.ScreenToWorldPoint(mousePosition);
	Vector3 dir = target - playerPosition;
	Quaternion rot = Quaternion.LookRotation(dir);
	transform.rotation = Quaternion.Slerp(transform.rotation, rot, 0.2f);
}

부드러움이 잘 안보이는 게 아쉽지만 실제론 부드럽다.


@ Quaternion.Lerp 

차이점은 Lerp는 선형 보간이라 직선으로 표현된다. 더 단순.

https://docs.unity3d.com/ScriptReference/Quaternion.Lerp.html

 

Unity - Scripting API: Quaternion.Lerp

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

https://tiborstanko.sk/lerp-vs-slerp.html

 

Lerp vs Slerp · Tibor Stanko

Here’s a demonstration of the difference between two common unit vector interpolation schemes. The first option is to use linear interpolation in the ambient Euclidean space, followed by a normalization step. Simple and fast, but the angular velocity is

tiborstanko.sk

 

Comments