我试图实现这个公式,以产生凹凸,但我面临一些问题。结果看上去不一样--更黑了。

这是我的结果(没有相同的参数),但它要暗得多,我不明白为什么。

这是相关的代码。
// randx, randy and frequencies are array with some random values for each sin wave.
for (int x = 0; x < _width; ++x)
{
    for (int y = 0; y < _height; ++y)
    {
        float color = 0.0f;
        for (int i = 0; i < _iterations; ++i)
        {
            val += Mathf.Sin(Mathf.Sqrt(Mathf.Pow(x - randx[i], 2.0f) + Mathf.Pow(y - randy[i], 2.0f)) * 1.0f / (2.08f + 5.0f * frequencies[i]));
        }
        color /= (float)_iterations;
    }
}知道我为什么会得到这个结果吗?
非常感谢!
编辑:多亏了@trichoplax,它才能做到这一点。
float tmp = Mathf.Sin(Mathf.Sqrt(Mathf.Pow(x - randx[i], 2.0f) + Mathf.Pow(y - randy[i], 2.0f)) * 1.0f / (2.08f + 5.0f * frequencies[i]));
tmp = tmp * 0.5f + 0.5f;
val += tmp;发布于 2016-04-07 23:10:28
当你取一些正弦波的平均值时,你的颜色值将从-1到1不等。从你的例子图像来看,这个数值范围的上半部分(从0到1)会导致颜色,其他地方都是黑色的。
如果用于显示结果的任何内容只能处理正值,则需要将结果转换为正确的值范围。例如,要将range -1比1转换为range 0至1,您可以添加1并除以2。
在示例代码的上下文中,这可以通过以下代码行来实现
color /= (float)_iterations;使用
color += 1;
color /= 2;https://computergraphics.stackexchange.com/questions/2283
复制相似问题