【问题标题】:Opengl Shader : Interpolating between two texturesOpengl Shader:在两个纹理之间进行插值
【发布时间】:2019-11-01 21:58:49
【问题描述】:

我正在尝试用 c++ 和 opengl 构建一个随机地形生成器,但我在纹理方面遇到了一些问题。 我有两种质地:草本和岩石:

如您所见,山顶上有岩石,下面有药草。我通过顶点的高度在这两个纹理之间进行插值。我的高度值在 -1 和 1 之间。所以在我的片段着色器中我这样做:

float height = clamp(vs_position.y/20.f, -1f, 1f );

height = exp(height) / exp(1.f);


vec3 final_texture = mix(
vec3(texture(texture0, vs_texcoord)), 
vec3(texture(texture1, vs_texcoord)), 
vec3(height));

第一行并不重要,在我将顶点数据发送到我的着色器之前,我将高度乘以 20 以获得更明显的山丘。

Thirst line : 我将两种纹理按高度混合。

第二行:为了获得“更强”的插值,我使用 softmax 函数使高度值接近 0 或 1。老实说,它并没有做太多,也许我没有正确使用这个函数。

我想要的是做一个更强的softmax,并且能够控制插值的水平。所以我的高度值应该更接近 0 或 1。因为我的插值现在看起来不太好。 因此,例如,如果我的身高是 0.7,则应将其转换为 0.95。并且 f(0.3) = 0.05,f(0.5) = 0.5。类似的东西。 你知道这方面的技巧吗?

编辑: 我做的 !

很简单,我只是重复了几次softmax函数:

float softmax(float value, float max , int iteration, float offset)
{
    value += offset;

    for(int i=0; i<iteration; i++)
    {
        value = exp(value);
        max = exp(max);
    }

    value = (value/max);
    value = clamp(value, 0.f, 1.f);

    return value;
}

我使用偏移量来选择插值开始的高度

【问题讨论】:

  • 高度是在 [-20.0, 20.0] 范围内还是在 [0.0, 20.0] 范围内?
  • 它是 [-20.0, 20.0]

标签: opengl glsl shader fragment-shader


【解决方案1】:

我建议使用smoothstep 将高度从其范围(例如[-20.0, 20.0])“平滑”映射到范围[0.0, 1.0] 以通过mix 进行插值。例如:
smoothstep 执行 Hermite interpolation

vec3 color0 = texture(texture0, vs_texcoord).rgb;
vec3 color1 = texture(texture1, vs_texcoord).rgb;

float a = smoothstep(-20.0, 20.0, height);
vec3 final_texture = mix(color0, color1, a);

注意,甚至可以将插值限制在一定范围内。下面使用来自texture0 的颜色,如果height &lt; -1.0texture1 如果height &gt; -1.0。在两者之间,它在纹理之间平滑地插值:

float a = smoothstep(-1.0, 1.0, height);
vec3 final_texture = mix(color0, color1, a);

【讨论】:

    猜你喜欢
    • 2014-08-12
    • 2013-05-07
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    相关资源
    最近更新 更多