最近我一直在尝试使用WebGL,偶然发现了一个很酷的小演示这里(源代码这里),我想稍微改动一下以得到一些很棒的效果。
我想要改变地形生成的方式。不是使用10个Simplex噪声层(位于simplex3d.shader中):
float h = 0.0;
for(int i=0; i<10; i++){
float factor = pow(2.0, float(i));
h += snoise(vec3(uv*factor, delta*float(i+1)))/(pow(factor, 0.88)*10.0);
}
我希望能够加载自定义的黑白高度图像到场景中,并从中生成地形。我对GLSL还比较陌生,在网上找到合适的资源并开始工作时遇到了麻烦。
非常感谢任何帮助!
编辑:
vertex:
attribute vec2 position;
void main(){
gl_Position = vec4(position, 0.0, 1.0);
}
fragment:
uniform vec2 viewport;
uniform sampler2D u_heightmap;
void main(){
float scale = 0.5;
float bias = 0.25;
vec2 texCoord;
// Get the height value and calculate the new texture coord.
float h = scale * texture2D(u_heightmap, texCoord).r - bias;
vec2 newTexCoord = h * viewport + texCoord;
vec4 texColor = texture2D(u_heightmap, newTexCoord);
gl_FragColor = texColor;
}
编辑2:
vertex:
attribute vec2 position;
void main(){
gl_Position = vec4(position, 0.0, 1.0);
}
fragment:
uniform sampler2D heightmap;
uniform vec2 viewport;
void main(){
float scale = 1.0;
float bias = 0.25;
vec2 uv = gl_FragCoord.xy/viewport;
float h = 0.0;
h = scale * ((texture2D(heightmap, uv).r) - bias);
clamp(h, 0.0, 1.0);
gl_FragColor = vec4(0.0, h, 0.0, 1.0);
}