直线到曲线

本文关键字:曲线 | 更新日期: 2023-09-27 17:54:17

我正在尝试想出一种技术来生成一个陨石坑的高度图。我现在正试图保持小而简单,并有一个简单的算法,其中陨石坑的中心被标记,并在其半径内,它周围的像素根据距离中心的远近而着色。

这种方法的问题是它产生V形的陨石坑,我需要更多的"U"形结果。我不熟悉正弦波的使用,我得到的感觉插值不会很快工作给定多少像素可以在流星半径内。

有什么建议吗?

直线到曲线

我假设你有一个纹理或其他图片格式,可以根据宽度和高度(x,y)进行索引,并且陨石坑也在纹理内的x,y中给出,这将产生这样的算法。

Texture2D texture = ...;
Vector2 craterPosition = new Vector2(50,50);
double maxDistance = Math.Sqrt(Math.Pow(texture.Width,2) + Math.Pow(texture.Height,2));
for(int x = 0; x < texture.Width; x++)
{
    for(int y = 0; y < texture.Height; y++)
    {
        double distance = Math.Sqrt(Math.Pow(x - craterPosition.x, 2) + Math.Pow(y - craterPosition.y, 2));
        //the lower distance is, the more intense the impact of the crater should be
        //I don't know your color scheme, but let's assume that R=0.0f is the lowest point and R = 1.0f is the highest
        distance /= maxDistance;
        double height = (Math.Cos(distance * Math.Pi + Math.Pi) + 1.0) / 2.0;
        texture[x,y] = new Color((float)height,0,0,1);        
    }
}

这里最重要的一行可能是

double height = (Math.Cos(distance * Math.Pi + Math.Pi) + 1.0) / 2.0;

看一下余弦曲线http://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Cos.svg/500px-Cos.svg.png它有一个很好的光滑的"陨石坑"从Pi到TwoPi。由于我们的距离变量从0到1缩放,我们使用距离*Pi + Pi。但它会给我们一个从-1到1的结果。因此,我们在最终结果上加1,然后除以2,得到0到1之间高度的平滑结果。