1.两个向量之间插值
float3 slerpBetweenVector(float3 from, float3 to, float delta){
quatf model_start = quatf::fromDirectedRotation(normalize(from), normalize(from));
quatf model_end = quatf::fromDirectedRotation(normalize(from), normalize(to));
quatf result = slerp(model_start, model_end, delta);
float len_from = length(from);
float len_to = length(to);
return normalize(result * from) * slerpBetweenValue(len_from,len_to,delta);
}
quatf::fromDirectedRotation为求两向量间的旋转的四元数,见Filament源码
2.两个点之间插值
float slerpBetweenValue(float from, float to, float delta){
return from+(to-from)*delta;
}
float3 slerpBetweenPoint(float3 point_from, float3 point_to, float delta){
return float3(slerpBetweenValue(point_from.x,point_to.x,delta),
slerpBetweenValue(point_from.y,point_to.y,delta),
slerpBetweenValue(point_from.z,point_to.z,delta));
}
3.两个Transform之间插值
void slerpBetweenTransform(const std::shared_ptr<Transform> from,const std::shared_ptr<Transform> to, float delta,const std::shared_ptr<Transform>& result){
result->setPosition(slerpBetweenPoint(from->getPosition(),to->getPosition(),delta));
result->setRotation(slerp(from->getRotation(),to->getRotation(),delta));
result->setScale(slerpBetweenPoint(from->getScale(),to->getScale(),delta));
}