当前位置:网站首页>Shadertoy realizes simple compass

Shadertoy realizes simple compass

2022-06-22 02:01:00 The end of the wind

For the first time contact glsl, Wrote a simple version of the rotating compass demo, On the first code :

​
/**
*@param uv      uv
*@param col     color
*@param probe_l  Probe length 
*@param probe_w  Probe width 
*@param center  center
*/
vec4 campass(vec2 uv,vec4 col,float probe_l,float probe_w,vec2 center){
    float half_l=probe_l/float(2);
    float half_w=probe_w/float(2);
    float si=sin(iTime*.7);
    float cs=cos(iTime*.7);
    //  Transformation matrix 
    mat2 trans=mat2(cs,si,-si,cs);
    //  probe 4 The vertices 
    vec2 dot1=vec2(center.x-half_l,0.);
    vec2 dot2=vec2(0,center.y+half_w);
    vec2 dot3=vec2(center.x+half_l,0.);
    vec2 dot4=vec2(0.,center.y-half_w);
    
    //  Yes 4 Vertex matrix transformation 
    dot1=dot1*trans;
    dot2=dot2*trans;
    dot3=dot3*trans;
    dot4=dot4*trans;
    
    //  Vector cross product ,uv The vector corresponding to each vertex line , Cross multiply the sides of a polygon , Cross product result  z  Positive value \ Same negative , To judge  uv  The coordinates are in the corresponding polygon 
    vec3 inner12=cross(vec3(uv-dot1,.0),vec3(dot2-dot1,0.));
    vec3 inner23=cross(vec3(uv-dot2,0.),vec3(dot3-dot2,0.));
    vec3 inner34=cross(vec3(uv-dot3,0.),vec3(dot4-dot3,0.));
    vec3 inner41=cross(vec3(uv-dot4,0.),vec3(dot1-dot4,0.));
    vec3 inner24=cross(vec3(uv-dot4,0.),vec3(dot4-dot2,0.));
    
    // Judge the two ends of the probe , Fill in different colors 
    if((inner12.z>0.&&inner24.z>0.&&inner41.z>0.)||((inner12.z<0.&&inner24.z<0.&&inner41.z<0.))){
        return col;
    }
    
    if((inner23.z>0.&&inner34.z>0.&&inner24.z<0.)||((inner23.z<0.&&inner34.z<0.&&inner24.z>0.))){
        vec4 newcol=col*2.;
        return newcol;
    }
    
    //  draw a circle 
    float panel_r=length(uv-center);
    if(panel_r<half_l+.001&&panel_r>half_l-.001){
        return vec4(vec3(0.),1.);
    }
    return vec4(vec3(1.),1.);
}

void mainImage(out vec4 fragColor,in vec2 fragCoord)
{
    // uv Range (-0.5~0.5)
    vec2 uv=fragCoord/iResolution.xy-.5;
    uv.y=uv.y*(iResolution.y/iResolution.x);
    float cs=cos(iTime*.047),si=sin(iTime*.79);
    
    vec4 col=vec4(cs,si,.0,.5);
    
    col=campass(uv,col,.4,.05,vec2(.0,.0));
    fragColor=col;
}

​

There seems to be nothing to say , Look directly at the renderings :

design sketch

 

原网站

版权声明
本文为[The end of the wind]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220120197299.html