vec4 drawTriangle(in vec2 p, in vec2 A, in vec2 B, in vec2 C)
{
vec2 AB = B-A;
vec2 BC = C-B;
vec2 CA = A-C;
vec2 AP = p - A;
vec2 BP = p - B;
vec2 CP = p - C;
vec3 cPAB = cross(vec3(AP,.0),vec3(AB,.0));
vec3 cCAB = cross(vec3(-CA,.0),vec3(AB,.0));
bool bPA = dot(cPAB, cCAB) >= .0;
vec3 cPAC = cross(vec3(CP,.0), vec3(CA,.0));
vec3 cBAC = cross(vec3(-BC,.0), vec3(CA,.0));
bool bPC = dot(cPAC, cBAC) >= .0;
vec3 cPBC = cross(vec3(BP,.0), vec3(BC,.0));
vec3 cABC = cross(vec3(-AB,.0), vec3(BC,.0));
bool bPB = dot(cPBC, cABC) >= .0;
if (bPC && bPB && bPA)
{
return vec4(0.9, 0.1, 0.1, 1.0);
}
else
{
return vec4(0.0, 0.0, 0.0, 0.0);
}
}
vec4 drawCircle(in vec2 p, in vec2 o, in float radius)
{
float f = smoothstep(radius - 0.003, radius + 0.003, length(p-o));
return vec4(0.4,0.2,0.6,1.0-f);
}
vec4 drawRect(in vec2 p, in vec2 coord, in vec2 size)
{
vec2 v = p - coord;
if(0.5*size.x > abs(v.x) && 0.5*size.y > abs(v.y))
{
return vec4(0.1,0.8,0.1,1.0);
}
return vec4(0.0,0.0,0.0,0.0);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
//vec2 uv = fragCoord.xy / iResolution.xy;
vec2 p = (fragCoord.xy - 0.5*iResolution.xy)/iResolution.yy;
vec4 layer0 = vec4(1.0, 0.8, 0.7, 1.0);
vec4 layer1 = drawRect(p, vec2(.1,.1), vec2(0.4,0.4));
vec4 col = mix(layer0, layer1, layer1.a);
vec4 layer2 = drawCircle(p, vec2(-0.1,-0.1),0.3);
col = mix(col, layer2, layer2.a);
vec4 layer3 = drawTriangle(p, vec2(-0.5,-0.4), vec2(-0.3,0.0), vec2(-0.1, -0.4));
col = mix(col, layer3, layer3.a);
fragColor = col;
}
上ShaderToy看别人的炫酷Shader,自己也想动手写来玩玩,学习一下glsl,今天就先写个画基础图形的Shader,在这里做一下记录。