简体   繁体   English

如何优化这个smoothstep函数? 有什么替代方法吗?

[英]How to optimize this smoothstep function ? Is there any alternative?

In one of my projects, I use the following smoothstep() function :在我的一个项目中,我使用了以下smoothstep()函数:

float smoothstep(float a, float b, float m, int n)
{
    for(int i = 0 ; i < n ; i++)
    {
        m = m * m * (3 - 2 * m);
    }
    return a + (b - a) * m;
}

It works great, however, it has two disadvantages :它工作得很好,但是,它有两个缺点:

  • It's slow (especially for big values of n )它很慢(特别是对于n大值)
  • It doesn't work for non integer values (eg : n = 1.5 )它不适用于非整数值(例如: n = 1.5

Is there an alternative (excluding precalculating points and then interpolating) providing better performance (and same behavior), or another function giving a great approximation ?是否有替代方案(不包括预先计算的点,然后进行插值)提供更好的性能(和相同的行为),或者另一个提供很好近似的函数?

You should be able to precompute the "m" term, since it doesn't rely on a or b, and assuming you're doing this over an entire interpolation, this should speed up your code significantly.您应该能够预先计算“m”项,因为它不依赖于 a 或 b,并且假设您在整个插值中执行此操作,这应该会显着加快您的代码速度。

Alternatively, you could use the built in MathHelper.Smoothstep method, which provides a cubic interpolation, rather than the linear interpolation you get out of your version.或者,您可以使用内置的 MathHelper.Smoothstep 方法,该方法提供三次插值,而不是您从版本中获得的线性插值。 There are also other, more advanced, interpolators in that class, also.该类中还有其他更高级的内插器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM