我的目标是这种效果:(仅水平轮廓线):
我确实找到了这个例子 http://www.gamedev.net/topic/529926-terrain-contour-lines-using-pixel-shader/#entry4431505,但是它创建了水平and垂直轮廓线。我不太明白如何调用fwidth()
正在生成线条。
uniform float gsize;//size of the grid
uniform float gwidth;//grid lines'width in pixels
varying vec3 P;
void main()
{
vec3 f = abs(fract (P * gsize)-0.5);
vec3 df = fwidth(P * gsize);
float mi=max(0.0,gwidth-1.0), ma=max(1.0,gwidth);//should be uniforms
vec3 g=clamp((f-df*mi)/(df*(ma-mi)),max(0.0,1.0-gwidth),1.0);//max(0.0,1.0-gwidth) should also be sent as uniform
float c = g.x * g.y * g.z;
gl_FragColor = vec4(c, c, c, 1.0);
gl_FragColor = gl_FragColor * gl_Color;
}
我如何将该示例修改为仅水平线?
有更好的解决方案吗?
Update:
下面修改后的着色器的解决方案。使用浮动仅使用y
value.
void main() {
float f = fract (_pos.y * 15.0);
float df = fwidth(_pos.y * 15.0);
float g = smoothstep(df * 1.0, df * 2.0, f);
float c = g;
gl_FragColor = vec4(c, c, c, 1.0);
}