在Shader编程中,插值算法是实现平滑过渡的关键技术。

线性插值

线性插值 Lerp

Lerp 是最基本的插值,适用于颜色、位置、UV等标量或向量。


球形线性插值 Slerp

Slerp 常用于在单位向量或四元数之间进行插值,特别是在动画和相机旋转中,它能够保持恒定的角速度,从而实现更自然的过渡效果。

GLSL
vec3 slerp(vec3 v0, vec3 v1, float t) {
	float theta = acos(dot(v0, v1));
	return (sin((1.0 - t) * theta) * v0 + sin(t * theta) * v1) / sin(theta);
}
点击展开查看更多

非线性插值

Smoothstep

Smoothstep 将值映射到范围[ edge0 , edge1 ],但以一种更平滑的方式,使得结果在接近边界时的变化更加缓慢,而在中间部分变化较快

Smootherstep

Smootherstep 则是对 Smoothstep 的改进,提供了更加平滑的过渡效果。

GLSL
float Smootherstep(float edge0, float edge1, float t)
{
	float t = saturate((x - edge0) / (edge1 - edge0));
	return t * t * t * (t * (6.0f * t - 15.0f) + 10.0f);
}
点击展开查看更多

正弦插值 Sine Interpolation

正弦插值通常指的是利用正弦函数的特性进行平滑过渡的一种插值技术。实际上并没有一个标准的“正弦插值”公式,通常是开发者自定义的。不常用

GLSL
float f(float t) {
    return sin((PI / 2.0f) * t);
}
点击展开查看更多

其中PI可能需要手动定义

版权声明

作者: Chaim

链接: https://chaim.eu.org/posts/%E6%8F%92%E5%80%BC%E7%AE%97%E6%B3%95/

许可证: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Please attribute the source, use non-commercially, and maintain the same license.

开始搜索

输入关键词搜索文章内容

↑↓
ESC
⌘K 快捷键