我试图制作一个降雪动画,为什么我的动画在循环外声明y
时不起作用?
vec3 circle(in vec2 r, in vec2 center, in float radius)
{
return vec3(smoothstep(1.0, 0.0, smoothstep(0.0, 0.01, length(r-center)-radius)));
}
void snow(inout vec3 ret, in vec2 r, in float time)
{
vec3 membership;
membership = vec3(0.0);
// Why this declaration of "y" DOESNT work?
//float y;
float x;
for(float j=0.0; j < 1.0; j+=0.9)
{
for(float i=0.0; i < 1.0; i+=0.3)
{
// But this one DOES ?
float y;
y = iGlobalTime * 0.3;
x = -0.98 + i*0.3;
membership+= circle(r, vec2(x, 0.5 - mod(y, 1.0)), 0.001);
ret = mix(ret, vec3(0.9, 0.9, 1.0), membership);
}
}
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 r = (fragCoord.xy - 0.5 * iResolution.xy) / iResolution.y ;
vec3 ret = vec3(0.0, 1.0 - (r.y+1.4)*0.5, 1.0 - (r.y+0.9)*0.5);
snow(ret, r, iGlobalTime);
fragColor = vec4(ret.x, ret.y, ret.z, 1.);
}