opengl shader 绘制心形

来源:互联网 发布:创建sql数据库视图 编辑:程序博客网 时间:2024/06/07 09:58

顶点着色器

uniform mat4 uMVPMatrix; //总变换矩阵

attribute vec3 aPosition;  //顶点位置
attribute vec2 aTexCoor;    //顶点纹理坐标
varying vec2 vTextureCoord;  //用于传递给片元着色器的变量
varying vec2 vPosition;


void main()     
{    
vPosition = aPosition.xy;                      
   gl_Position = uMVPMatrix * vec4(aPosition,1); //根据总变换矩阵计算此次绘制此顶点位置
   vTextureCoord = aTexCoor;//将接收的纹理坐标传递给片元着色器

}   

片段着色器


precision mediump float;
varying vec2 vTextureCoord; //接收从顶点着色器过来的参数
varying vec2 vPosition;
uniform sampler2D sTexture1;//纹理内容数据
uniform float sGlobalTime;


float smoothstepmy( float x, float y, float a )
{
if( a < x )
return 0.0;
else if( a > y )
return 1.0;
else
{
float m = a - x;
float n = y - x;
return m/n;
}
}




void main()                         
{           
vec4 layer1 = texture2D(sTexture1, vTextureCoord);
//float d = length( vPosition) - 0.2;
//float t = smoothstepmy( 0.1, 0.11, d );
//vec4 layer2 = vec4( 1.0, 1.0, 0.5, 1.0 - t );
//gl_FragColor = mix( layer1, layer2, layer2.a );


   //给此片元从纹理中采样出颜色值            
   //gl_FragColor = texture2D(sTexture1, vTextureCoord);
   
    vec2 p = vPosition;
p.y -= 0.25;


    // background color
    //vec3 bcol = vec3(1.0,0.8,0.7-0.07*p.y)*(1.0-0.25*length(p));
    vec3 bcol = layer1.xyz;
    // animate
    float tt = mod(sGlobalTime, 1.5)/1.5;
    float ss = pow(tt, 0.2)*0.5 + 0.5;
    ss = 1.0 + ss*0.5*sin(tt*6.2831*3.0 + p.y*0.5)*exp(-tt*4.0);
    p *= vec2(0.5,1.5) + ss*vec2(0.5,-0.5);
   
    // shape
    float a = atan(p.x,p.y)/3.141593;
    float r = length(p);
    float h = abs(a);
    float d = (13.0*h - 22.0*h*h + 10.0*h*h*h)/(6.0-5.0*h);


// color
float s = 1.0-0.5*clamp(r/d,0.0,1.0);
s = 0.75 + 0.75*p.x;
s *= 1.0-0.25*r;
s = 0.5 + 0.6*s;
s *= 0.5+0.5*pow( 1.0-clamp(r/d, 0.0, 1.0 ), 0.1 );
vec3 hcol = vec3(1.0,0.5*r,0.3)*s;

    vec3 col = mix( bcol, hcol, smoothstepmy( -0.01, 0.01, d-r) );


    gl_FragColor = vec4(col,1.0);
    
}              


0 0
原创粉丝点击