膨胀:取一个像素周围的点,取最亮的点为当前的点颜色,为膨胀效果
腐蚀:取一个像素周围的点,取最暗的点为当前的点颜色,为腐蚀效果
膨胀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; }
本文出自 “不会飞的纸飞机” 博客,请务必保留此出处http://douzhq.blog.51cto.com/12552184/1931072
原文地址:http://douzhq.blog.51cto.com/12552184/1931072