1.shadertoy 代码
//用面积算,因为绘制得依赖uv坐标,所以必须把屏幕考虑进去
float DistLine(vec3 ballPos,vec3 screen,vec3 camPos)
{
return length(cross(camPos - ballPos,screen)) / length(screen);
}
//用距离算
float CamDist(vec3 ballPos,vec3 camPos)
{
return length(camPos - ballPos);
}
float DrawPoint(vec3 ballPos,vec3 screen,vec3 camPos)
{
//近大远小
float d = DistLine(ballPos,screen,camPos);
d = smoothstep(0.10,0.09,d);
return d;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord.xy / iResolution.xy;
uv -= 0.5;
//将uv的x坐标进行放大
uv.x *= iResolution.x / iResolution.y;
float t = iTime;
vec3 ballPos = vec3(3.0 * sin(t),2.0,3.0 * cos(t));
vec3 lookat = vec3(0.5);
float zoom = 1.0;
vec3 f = normalize(lookat - ballPos);
vec3 r = cross(vec3(0.0,1.0,0.0),f);
vec3 u = cross(f,r);
vec3 c = ballPos + f * zoom;
vec3 i = c + uv.x * r + uv.y * u;
//要么屏幕拉近,要么摄像机拉近
vec3 screen = i - ballPos;
float d = 0.0;
d += DrawPoint(ballPos,screen,vec3(0.0,0.0,0.0));
d += DrawPoint(ballPos,screen,vec3(0.0,0.0,1.0));
d += DrawPoint(ballPos,screen,vec3(0.0,1.0,0.0));
d += DrawPoint(ballPos,screen,vec3(0.0,1.0,1.0));
d += DrawPoint(ballPos,screen,vec3(1.0,0.0,0.0));
d += DrawPoint(ballPos,screen,vec3(1.0,0.0,1.0));
d += DrawPoint(ballPos,screen,vec3(1.0,1.0,0.0));
d += DrawPoint(ballPos,screen,vec3(1.0,1.0,1.0));
fragColor = vec4(d);
}
2.效果: