【发布时间】:2020-02-21 19:05:32
【问题描述】:
我似乎在 2d 输入上得到了方正的柏林噪声地形。
我一直在关注这个https://www.shadertoy.com/view/4tGSzW,它使用 WebGL 而不是 opengl。我已经用范围为 0.0 到 1.0 的浮点数组替换了从一些样本纹理中获取的渐变方法。
#version 330 core
out vec4 FragColor;
uniform float gradient[256];
float fade(float t)
{
return t*t*t*(t*(t*6.0-15.0)+10.0);
}
vec2 grad(vec2 p){
vec2 v = vec2(gradient[int(p.x)&255],gradient[int(p.y)&255]);
return normalize(v.xy*2.0 - vec2(1.0));
}
float noise(vec2 p){
vec2 p0 = floor(p);
vec2 p1 = p0 + vec2(1.0,0.0);
vec2 p2 = p0 + vec2(0.0,1.0);
vec2 p3 = p0 + vec2(1.0,1.0);
vec2 g0 = grad(p0);
vec2 g1 = grad(p1);
vec2 g2 = grad(p2);
vec2 g3 = grad(p3);
float t0 = p.x - p0.x;
float fade_t0 = fade(t0);
float t1 = p.y - p0.y;
float fade_t1 = fade(t1);
float p0p1 = (1.0-fade_t0)*dot(g0,(p-p0)) + fade_t0*dot(g1,(p-p1));
float p2p3 = (1.0-fade_t0)*dot(g2,(p-p2)) + fade_t0*dot(g3,(p-p3));
return ((1.0-fade_t1)*p0p1 + fade_t1*p2p3);
}
void main()
{
float n = noise(vec2(gl_FragCoord.x,gl_FragCoord.y)/64.0)*1.0 +
noise(vec2(gl_FragCoord.x,gl_FragCoord.y)/32.0) * 0.5 +
noise(vec2(gl_FragCoord.x,gl_FragCoord.y)/16.0) * 0.25 +
noise(vec2(gl_FragCoord.x,gl_FragCoord.y)/8.0) * 0.125;
FragColor = vec4(vec3(n*0.5+0.5),1.0);
}
【问题讨论】:
-
你能张贴一张你的噪音看起来如何的照片吗?
-
@Kalle Halvarsson 完成
标签: opengl glsl shader procedural-programming