UE4

[UE4] FMath의 선형보간법 종류와 차이점

Honey Badger 2022. 10. 22. 23:17

개요

  게임 개발을 하다보면 자연스러운 동작을 구현하기 위해 선형보간법을 사용한다. 언리얼에서 자주 사용하는 선형보간법에는 크게 3가지가 있다. 

 

 

1. FMath::Lerp

https://docs.unrealengine.com/4.26/en-US/API/Runtime/Core/Math/FMath/Lerp/1/

 

FMath::Lerp

Performs a linear interpolation between two values, Alpha ranges from 0-1

docs.unrealengine.com

두 값 사이의 선형 보간 수행( Alpha 범위 0~1 ), Alpha값을 이미 정해놓은 경우에 사용한다. FInterpTo보다 구현이 간단한 편이다. 

template<class T, class U>
static T Lerp
(
    const T & A,
    const T & B,
    const U & Alpha
)

A에서 B까지 알파 비율로 이동한다 가정했을 때 Lerp의 반환값은 "A + (B-A)*알파"이다.

예를 들어 5에서 10까지 0.5알파값으로 보간을 수행했을 때의 그래프는 다음과 같다. 목표값에 거의 다 수렴했을 때에도 계속 수행되므로 일정값 이상 근접하면 끊어주는 것이 성능에 더 좋을 것 같다. 

 

 

 

 

 

 

 

2. FMath::FInterpTo

 

float 값을 Current에서 Target으로 보간한다. Target까지의 거리에 따라 크기가 조정되므로 시작 속도가 강하고 쉽게 빠져나간다. 

https://docs.unrealengine.com/4.27/en-US/API/Runtime/Core/Math/FMath/FInterpTo/

 

FMath::FInterpTo

Interpolate float from Current to Target.

docs.unrealengine.com

static float FInterpTo
(
    float Current,
    float Target,
    float DeltaTime,
    float InterpSpeed
)

예를 들어 0.5의 속도로 10초만에 5에서 10으로 값을 증가시킨다고 가정하면 그래프는 다음과 같다.

 

 

 

 

 

 

 

3. FMath::FInterpConstantTo

  https://docs.unrealengine.com/4.27/en-US/API/Runtime/Core/Math/FMath/FInterpConstantTo/

 

FMath::FInterpConstantTo

Interpolate float from Current to Target with constant step

docs.unrealengine.com

static float FInterpConstantTo
(
    float Current,
    float Target,
    float DeltaTime,
    float InterpSpeed
)

값을 목표값으로 "일정한 속도"로 증가(감소)시킨다. 예를 들어 0.5의 속도로 10초만에 5에서 10으로 값을 증가시킨다고 가정하면 그래프는 다음과 같다.