从噪声纹理中导出不确定性值?

时间:2014-03-23 22:08:13

标签: opengl graphics glsl texture-mapping simplex-noise

我正在尝试实施Sketchy Drawings。我正处于过程的一部分,该过程要求使用噪声纹理来导出不确定性值,这将为边缘图提供偏移。

这是我的圆环边缘图的图片:

enter image description here

以下是我使用Perlin函数建议的噪声纹理:

enter image description here

我将它们分别保存为edgeTexturenoiseTexture中的纹理。

现在我停留在你必须通过噪声纹理导出的不确定性值来偏移边缘图的纹理坐标的部分。这张图片出自本书:

enter image description here

offs = turbulence(s, t); 
offt = turbulence(1 - s, 1 - t);

我暂时忽略了2x2矩阵。这是我当前的片段着色器尝试及其产生的结果:

#version 330

out vec4 vFragColor;

uniform sampler2D edgeTexture;
uniform sampler2D noiseTexture;

smooth in vec2 vTexCoords;

float turbulence(float s, float t) 
{
    float sum = 0;
    float scale = 1;
    float s1 = 1;
    vec2 coords = vec2(s,t);

    for (int i=0; i < 10; i++)
    {
        vec4 noise = texture(noiseTexture, 0.25 * s1 * coords);
        sum += scale * noise.x;
        scale = scale / 2;
        s1 = s1 * 2;
    }

    return sum;
}

void main( void )
{
    float off_s = turbulence(vTexCoords.s, vTexCoords.t);
    float off_t = turbulence(1 - vTexCoords.s, 1 - vTexCoords.t);

    vFragColor = texture(edgeTexture, vTexCoords + vec2(off_s, off_t));
}

enter image description here

显然,我对vTexCoords的补充很远,但我不明白为什么。我已经尝试了其他几个湍流函数定义,但没有一个接近所需的输出,所以我认为我的整体方法在某处有缺陷。非常感谢任何帮助,如果我不清楚,请发表评论。圆环的理想输出看起来就像我想象的粗略绘制的圆圈。

1 个答案:

答案 0 :(得分:1)

您的湍流函数将返回范围(0,1)中的值。首先,您需要更改此值以获取以0为中心的值。这应该在函数的循环内完成,否则您将得到一个奇怪的分布。首先,我认为你应该改变这条线:

vec4 noise = texture(noiseTexture, 0.25 * s1 * coords);

vec4 noise = texture(noiseTexture, 0.25 * s1 * coords) * 2.0 - 1.0;

然后,您需要缩放偏移量,以便您不会将边缘纹理采样距离正在绘制的片段太远。变化:

vFragColor = texture(edgeTexture, vTexCoords + vec2(off_s, off_t));

vFragColor = texture(edgeTexture, vTexCoords + vec2(off_s, off_t) * off_scale);

其中off_scale是通过实验选择的一些小值(可能约为0.05)。