膨胀:取一个像素周围的点,取最亮的点为当前的点颜色,为膨胀效果
腐蚀:取一个像素周围的点,取最暗的点为当前的点颜色,为腐蚀效果
膨胀Fragment Shader
varying vec2 M_coord;
varying vec3 M_normal;
varying vec3 M_WordPos;
uniform sampler2D U_MainTexture;
uniform sampler2D U_SubTexture;
void main()
{
vec4 maxValue=vec4(0.0);
int coreSize=3;
int halfCoreSize=coreSize/2;
float texelOffset=1/200.0;
for(int y=0;y<coreSize;y++)
{
for(int x=0;x<coreSize;x++)
{
vec4 color=texture2D(U_MainTexture,
M_coord+vec2((-halfCoreSize+x)*texelOffset,
(-halfCoreSize+y)*texelOffset));
maxValue=max(maxValue,color);
}
}
gl_FragColor=maxValue;
}
腐蚀Fragment Shader
varying vec2 M_coord;
varying vec3 M_normal;
varying vec3 M_WordPos;
uniform sampler2D U_MainTexture;
uniform sampler2D U_SubTexture;
void main()
{
vec4 minValue=vec4(1.0);
int coreSize=3;
int halfCoreSize=coreSize/2;
float texelOffset=1/100.0;
for(int y=0;y<coreSize;y++)
{
for(int x=0;x<coreSize;x++)
{
vec4 color=texture2D(U_MainTexture,
M_coord+vec2((-halfCoreSize+x)*texelOffset,
(-halfCoreSize+y)*texelOffset));
minValue=min(minValue,color);
}
}
gl_FragColor=minValue;
}
转载于:https://blog.51cto.com/douzhq/1931056