can somebody please help me figure out why this shader doesn’t work?
I think there is some coordinate problems
#define PI2 6.28318530717959
#define PNUM 20
vec2 filterUV1(vec2 uv, std::Texture2d iChannel1)
{
// iq’s improved texture filtering (Shader - Shadertoy BETA)
vec2 x = uv * (iChannel1.size).xy;
vec2 p = floor(x);
vec2 f = fract(x);
f = ff(3.0-2.0*f);
return (p+f)/(iChannel1.size).xy;
}
vec4 getPixel(int x, int y, std::Texture2d iChannel0)
{
return iChannel0.sample(vec2(float(x)+.5,float(y)+.5) / (iChannel0.size).xy);
}
bool isPixel(int x, int y, vec2 fragCoord, std::Texture2d iChannel0)
{
vec2 iResolution = std::getRenderTargetSize();
fragCoord = fragment(floor(std::getRenderTargetSize() * std::getVertexTexCoord()));
vec2 c=fragCoord/iResolution.xy * (iChannel0.size).xy;
return ( int(c.x)==x && int(c.y)==y );
}
vec2 readPos(int i, std::Texture2d iChannel0)
{
return getPixel(i,0,iChannel0).xy;
}
bool writePos(vec2 pos, int i, vec2 fragCoord, std::Texture2d iChannel0, inout vec4 fragColor)
{
fragCoord = fragment(floor(std::getRenderTargetSize() * std::getVertexTexCoord()));
if (isPixel(i,0,fragCoord, iChannel0))
{
fragColor.xy=pos; return true;
}
return false;
}
vec4 getRand(vec2 pos, std::Texture2d iChannel1)
{
return iChannel1.sample(filterUV1(pos/vec2(400,300), iChannel1));
}
float dotDist(vec2 pos,vec2 fragCoord)
{
fragCoord = fragment(floor(std::getRenderTargetSize() * std::getVertexTexCoord()));
return length(pos-fragCoord);
}
// iq: Inigo Quilez :: computer graphics, mathematics, shaders, fractals, demoscene and more
float lineDist(vec2 a,vec2 b,vec2 p)
{
vec2 pa = p - a, ba = b - a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h );
}
vec4 drawDot(vec2 pos,float r, vec2 fragCoord)
{
fragCoord = fragment(floor(std::getRenderTargetSize() * std::getVertexTexCoord()));
return vec4(clamp(r-length(pos-fragCoord),0.,1.)/r*3.);
}
#define N(x) (x.yx*vec2(1,-1))
// gives a parametric position on a pentagram with radius 1 within t=0…5
// (maybe there’s more elegant ways to do this…)
vec2 pentaPos(float t)
{
float w=sqrt((5.+sqrt(5.)).5);
float s=sqrt(1.-ww*.25);
float ang=-floor(t)PI22./5.;
vec2 x=vec2(cos(ang),sin(ang));
return -N(x)s+xw*(fract(t)-.5);
}
void mainImage( in float iFrame, in vec2 iMouse, in std::Texture2d iChannel0, in std::Texture2d iChannel1, out vec4 fragColor )
{
vec2 iResolution = std::getRenderTargetSize();
vec2 fragCoord = fragment(floor(std::getRenderTargetSize() * std::getVertexTexCoord()));
float iTime = std::getTime();
float time=float(iFrame)*1./60.;
vec2 uv=fragCoord/iResolution.xy;
float v=0.;
for(int i=0;i<50;i++)
v += iChannel0.sample(getRand(vec2(i,0), iChannel1).xy).x/50.;
fragColor = iChannel0.sample(uv);
int pnum = int(min(iResolution.y/50.0,float(PNUM-1)));
bool write=false;
for(int i=0;i<PNUM;i++)
{
bool isMouse = (i==pnum);
// breaking here if i>pnum didnt work in windows (failed to unloll loop)
if(i<=pnum) {
vec2 pos;
pos=readPos(i, iChannel0);
vec2 oldpos=pos;
float ang = (getRand(pos, iChannel1)+getRand(pos+vec2(1,3)*time, iChannel1)).x*PI2;
pos+=vec2(.7,0)
+vec2(4,5)*vec2(cos(15.*time+float(i)),
.5*sin(15.*time+float(i)+.5)+
.5*sin(21.*time+float(i)+.5))*getRand(pos, iChannel1).x;
//+vec2(.2,2)*vec2(cos(ang),sin(ang));
//vec4 c = drawDot(mod(pos,iResolution.xy),2.5,fragCoord);
if(isMouse)
{
pos=iMouse.xy;
if(iMouse.xy==vec2(0) && mod(iTime+5.,37.7)>18.)
{
pos=pentaPos(iTime*.5)*.45*iResolution.y+iResolution.xy*.5;
pos+=(getRand(pos*.6+iTime*vec2(.1,1.), iChannel1).xy-.5)*7./500.*iResolution.y;
}
if(length(oldpos-pos)>40.) oldpos=pos;
}
vec2 mpos=mod(pos,iResolution.xy);
//float dd = dotDist(mpos,fragCoord);
float dd = lineDist(mpos,oldpos-(pos-mpos),fragCoord);
vec4 c = vec4(clamp((isMouse?5.:3.)-dd,0.,1.9),0,max(0.,1.-dd/40.),0);
if(mpos==oldpos-(pos-mpos)) c=vec4(0.); // ignore 0-length segments
if(getRand(pos*.3+time, iChannel1).z>.8 && !isMouse)
pos+=vec2(10,0);
else
fragColor = max(fragColor,c);
if(writePos(pos, i, fragCoord, iChannel0, fragColor)) write=true;
}
}
if(!write)
{
fragColor.z=max(-1.,fragColor.z-.002);
fragColor.x=max(0.,fragColor.x-.003);
}
if(iTime<0.)
{
fragColor=vec4(0,0,.6,0);
for(int i=0;i<PNUM;i++)
{
if(i<=pnum-int((iTime-5.0)*0.125*float(PNUM)))
{
vec4 rnd = iChannel1.sample(vec2(float(i)+.5,.5) / (iChannel1.size).xy);
writePos(vec2(0.+rnd.x*80.,iResolution.y/float(pnum)*float(i+1)), i, fragCoord, iChannel0, fragColor);
}
}
}
}