shadertoy-star

本文介绍了一种使用GLSL着色器语言实现星空背景及云层效果的技术方案。通过定义多个几何形状绘制函数,如星星、三角形和圆形,实现了动态星空及漂浮云朵的视觉效果。该方案利用了噪声函数、三角函数等数学工具来增强场景的真实感。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开链接


float rand(vec2 co){
    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

float dir(vec2 a, vec2 b, vec2 c)
{
	return (a.x - c.x) * (b.y - c.y) - (b.x - c.x) * (a.y - c.y);
}


bool insideTri(vec2 p, vec2 a, vec2 b, vec2 c)
{
	bool b1 = dir(p, a, b) < 0.0;
	bool b2 = dir(p, b, c) < 0.0;
	bool b3 = dir(p, c, a) < 0.0;
  	return ((b1 == b2) && (b2 == b3));
}

void applyColor(vec3 paint, inout vec3 col, vec2 p, vec2 a, vec2 b, vec2 c)
{
	if (insideTri(p, a, b, c)) col = mix(col, paint, 0.4);
}

void rotate(inout vec2 point, float r) {
    point = vec2(point.x*sin(r) +point.y*cos(r), point.x*cos(r)- point.y*sin(r));
}


float PI = 3.14159265358979323846264;

void drawTri(vec2 offset1, vec2 offset2, float size, float rotation, vec3 triColour, inout vec3 col, vec2 pos) {
    float oneThirdPhase = PI*2.0/3.0;
    float twoThirdPhase = PI*4.0/3.0;
    vec2 triTop = vec2(0, 1) * size + offset1;
    vec2 triBL = vec2(sin(oneThirdPhase),cos(oneThirdPhase)) * size + offset1;
    vec2 triBR = vec2(sin(twoThirdPhase),cos(twoThirdPhase)) * size + offset1;
    rotate(triTop, rotation);
    rotate(triBL, rotation);
    rotate(triBR, rotation);
    applyColor(triColour, col, pos, triTop + offset2, triBL+offset2, triBR+ offset2);
}


void drawStar(vec2 offset, float size, float rotation, vec3 triColour, inout vec3 col, vec2 pos) {
    vec2 topTriOff = offset;
    vec2 bottomTriOff = offset;
    float topTriRot = PI + rotation;
    float bottomTriRot = rotation;
    drawTri(vec2(0), topTriOff, size, topTriRot, triColour, col, pos);
    drawTri(vec2(0), bottomTriOff, size, bottomTriRot, triColour, col, pos);
}

vec3 hsv2rgb(vec3 c)
{
    vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}

void drawCircle(vec2 offset, float rad2, vec3 circColour, inout vec3 col, vec2 pos) {
    if (length(offset-pos)<rad2) {
        col = mix(col, circColour, 0.52);
    }
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
	vec2 uv = fragCoord.xy / iResolution.xy;
    fragColor.rgb = vec3(0.75,0.8,1.0);
    vec3 starColourHSV = vec3(0,1,1);
    float starheight = 0.0;
    for (int i=0; i<100; ++i) {
        starheight = rand(vec2(float(i),5.0));
        starColourHSV.x = 0.9-starheight;
	    drawStar(vec2(0.1+mod((float(i)+iGlobalTime)/12.0,0.8), 0.1+starheight/8.0+sin(uv.x*PI)/4.0), 0.03, iGlobalTime+rand(vec2(float(i),3)), hsv2rgb(starColourHSV),fragColor.rgb, fragCoord.xy/iResolution.x);
    }
    
    for (int i = 0; i < 10; ++i) {
        vec2 cloudOffset = vec2(0.1+rand(vec2(float(i),8))/12.0, 0.2+rand(vec2(float(i), 9))/12.0);
        drawCircle(cloudOffset,0.05, vec3(1.0), fragColor.rgb, fragCoord.xy/iResolution.x);
    }
    for (int i = 0; i < 10; ++i) {
        vec2 cloudOffset = vec2(0.85+rand(vec2(float(i),8))/12.0, 0.19+rand(vec2(float(i), 9))/12.0);
        drawCircle(cloudOffset,0.05, vec3(1.0), fragColor.rgb, fragCoord.xy/iResolution.x);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值