Error with Else If from Shadertoy

#1

Hi :slight_smile:
I wanted to import this shaders from shadertoy but have an error at (as often) :

“else if (PASSINDEX == 1) {”
with return : ERROR: 0:235: ‘else’ : syntax error: syntax error

I search for this error and found it can come for ==1
but nothing seems to work
Who can help me ?

https://www.shadertoy.com/view/lly3DG

Thank you :slight_smile:

0 Likes

#2

Give this a try…

/*
{
  "IMPORTED" : [

  ],
  "CATEGORIES" : [
    "Automatically Converted",
    "Shadertoy"
  ],
  "DESCRIPTION" : "Automatically converted from https:\/\/www.shadertoy.com\/view\/lly3DG by Flyguy.   Drawing shapes on an oscilloscope using stereo sound as X\/Y inputs. Similar to the works done by Jerobeam Fenderson: \nhttps:\/\/www.youtube.com\/user\/jerobeamfenderson1",
  "PASSES" : [
    {
      "TARGET" : "BufferA",
      "PERSISTENT" : true,
      "FLOAT" : true
    },
    {

    }
  ],
  "INPUTS" : [

  ]
}
*/


/*
Drawing on an oscilloscope in XY mode using stereo sound.
Screenshot of the sound on a basic XY scope simulator: http://i.imgur.com/SxskO1E.png

Music using the same technique by Jerobeam Fenderson: https://www.youtube.com/user/jerobeamfenderson1

Some XY oscilloscope demos:
Youscope - https://youtu.be/s1eNjUgaB-g
Oscillofun - https://youtu.be/o4YyI6_y6kw
Beams of Light - https://youtu.be/lVdWxKZVYC0
*/

//Timing constants (seconds)
#define T_FRAME 0.0167 //Frame duration (0.0167s = ~60fps) 
#define T_LINE  0.0020 //Duration of a 1-unit long line

float tau = atan(1.0)*8.0;

float gShapeTime = 0.0;
mat4 gModel = mat4(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);

vec2 cossin(float x)
{
	return vec2(cos(x), sin(x));   
}

//Single point projection
vec2 Project(vec3 p0)
{
    vec3 vanish = vec3(0.0,-2.0,0.0);
    
    p0 -= vanish;
    
	return length(vanish) * p0.xz / p0.y;
}

//Transformation functions
mat4 Translate(vec3 v)
{
    return mat4(vec4(1,0,0,v.x), vec4(0,1,0,v.y), vec4(0,0,1,v.z), vec4(0,0,0,1));
}

//Angle-axis rotation
mat4 Rotate(vec3 u,float a)
{
    float c = cos(a);
    float s = sin(a);
    u = normalize(u);
    
    vec4 c0 = vec4(c + (u.x*u.x) * (1.0-c), (u.y*u.x) * (1.0-c) + (u.z*s), (u.z*u.x) * (1.0-c) - (u.y*s),0.0);    
    vec4 c1 = vec4((u.x*u.y) * (1.0-c) - (u.z*s), c + (u.y*u.y) * (1.0-c), (u.z*u.y) * (1.0-c) + (u.x*s),0.0); 
    vec4 c2 = vec4((u.x*u.z) * (1.0-c) + (u.y*s), (u.y*u.z) * (1.0-c) - (u.x*s), c + (u.z*u.z) * (1.0-c),0.0);
    
    //return mat4(mat3(c0,c1,c2));
    return mat4(c0, c1, c2, vec4(0.0));
}

mat4 Scale(vec3 v)
{
    return mat4(vec4(v.x,0,0,0), vec4(0,v.y,0,0), vec4(0,0,v.z,0), vec4(0,0,0,1));
}

//2D line waveform
vec2 Line(vec2 p0, vec2 p1, float td, float t)
{
    float tl = td * distance(p0, p1);
    
    t -= gShapeTime;
    gShapeTime += tl;
    
    if(t >= 0.0 && t < tl)
    {
        return mix(p0, p1, t/tl);
    }
    
    return vec2(0);
}

//3D Projected line
vec2 Line3d(vec3 p0, vec3 p1, float td, float t)
{
        p0 = (vec4(p0,1.0) * gModel).xyz;
        p1 = (vec4(p1,1.0) * gModel).xyz;

        p0.xy = Project(p0);
        p1.xy = Project(p1);

        return Line(p0.xy, p1.xy, td, t);
}

vec2 mainSound( float time )
{
    float ft = mod(time, T_FRAME);
    
    gModel *= Scale(vec3(0.25));
    gModel *= Rotate(vec3(0, 0, 1), time);
    gModel *= Rotate(vec3(0, 1, 0), time);
    gModel *= Rotate(vec3(1, 0, 0), time);
    gModel *= Translate(vec3(0.4 * cossin(1.0 * time), 0.4 * sin(0.6 * time)));
    
    vec3 cube[8];
    cube[0] = vec3(-1,-1,-1);
    cube[1] = vec3( 1,-1,-1);
    cube[2] = vec3(-1, 1,-1);
    cube[3] = vec3( 1, 1,-1);
    cube[4] = vec3(-1,-1, 1);
    cube[5] = vec3( 1,-1, 1);
    cube[6] = vec3(-1, 1, 1);
    cube[7] = vec3( 1, 1, 1);
    
    vec2 sout = vec2(0);
    
	sout += Line3d(cube[0],cube[1], T_LINE, ft);
    sout += Line3d(cube[1],cube[3], T_LINE, ft);
    sout += Line3d(cube[3],cube[2], T_LINE, ft);
    sout += Line3d(cube[2],cube[0], T_LINE, ft);
    
    sout += Line3d(cube[4],cube[5], T_LINE, ft);
    sout += Line3d(cube[5],cube[7], T_LINE, ft);
    sout += Line3d(cube[7],cube[6], T_LINE, ft);
    sout += Line3d(cube[6],cube[4], T_LINE, ft);
    
    sout += Line3d(cube[0],cube[4], T_LINE, ft);
    sout += Line3d(cube[5],cube[1], T_LINE, ft);
    sout += Line3d(cube[2],cube[6], T_LINE, ft);
    sout += Line3d(cube[7],cube[3], T_LINE, ft);
    
    return vec2( sout );
}
/*
Drawing on an oscilloscope in XY mode using stereo sound.
Screenshot of the sound on a basic XY scope simulator: http://i.imgur.com/SxskO1E.png

Music using the same technique by Jerobeam Fenderson: https://www.youtube.com/user/jerobeamfenderson1

Some XY oscilloscope demos:
Youscope - https://youtu.be/s1eNjUgaB-g
Oscillofun - https://youtu.be/o4YyI6_y6kw
Beams of Light - https://youtu.be/lVdWxKZVYC0
*/


//Projected line
float Line3d(vec3 p0,vec3 p1,vec2 uv)
{
    p0 = (vec4(p0,1.0) * gModel).xyz;
    p1 = (vec4(p1,1.0) * gModel).xyz;
    
	p0.xy = Project(p0);
	p1.xy = Project(p1);
    
	vec2 dir = normalize(p1.xy - p0.xy);	
	uv = (uv - p0.xy) * mat2(dir.x, dir.y, -dir.y, dir.x);	
    
    float d = distance(uv, clamp(uv, vec2(0.0), vec2(distance(p0.xy, p1.xy), 0.0)));
    
	return smoothstep(4.0/RENDERSIZE.y, 0.0, d);
}

void main() {
	if (PASSINDEX == 0)	{
	
	}
	else if (PASSINDEX == 1)	{


	    vec2 res = RENDERSIZE.xy / RENDERSIZE.y;
		vec2 uv = (gl_FragCoord.xy / RENDERSIZE.y) - (res / 2.0);
	    uv *= 2.0;
	    
	    float time = TIME;
	    
	    gModel *= Scale(vec3(0.25));
	    gModel *= Rotate(vec3(0, 0, 1), time);
	    gModel *= Rotate(vec3(0, 1, 0), time);
	    gModel *= Rotate(vec3(1, 0, 0), time);
	    gModel *= Translate(vec3(0.4 * cossin(1.0 * time), 0.4 * sin(0.6 * time)));
	    
	    vec3 cube[8];
	    cube[0] = vec3(-1,-1,-1);
	    cube[1] = vec3( 1,-1,-1);
	    cube[2] = vec3(-1, 1,-1);
	    cube[3] = vec3( 1, 1,-1);
	    cube[4] = vec3(-1,-1, 1);
	    cube[5] = vec3( 1,-1, 1);
	    cube[6] = vec3(-1, 1, 1);
	    cube[7] = vec3( 1, 1, 1);
	    
	    vec3 cout = vec3(0);
	    
		cout += Line3d(cube[0],cube[1], uv);
	    cout += Line3d(cube[1],cube[3], uv);
	    cout += Line3d(cube[3],cube[2], uv);
	    cout += Line3d(cube[2],cube[0], uv);
	    
	    cout += Line3d(cube[4],cube[5], uv);
	    cout += Line3d(cube[5],cube[7], uv);
	    cout += Line3d(cube[7],cube[6], uv);
	    cout += Line3d(cube[6],cube[4], uv);
	    
	    cout += Line3d(cube[0],cube[4], uv);
	    cout += Line3d(cube[5],cube[1], uv);
	    cout += Line3d(cube[2],cube[6], uv);
	    cout += Line3d(cube[7],cube[3], uv);
	    
	    cout *= vec3(0.1,0.8,0.1);
	    
	    gl_FragColor = vec4(cout, 1.0);
	}
}
``
0 Likes

#3

:thinking:
It works fine but I can’t find what you changed ?

But thought it was audio réactif but it’s not ^^

0 Likes

#4

A few tips and notes!

  1. If you use a text tool like BBEdit / TextMate, you can copy / paste my edited version into a text file and do a “difference” against the original that’ll show you all of the things that I changed.

  2. It’s an audio generator, and it looks like the ISF Editor interpreted that as a multi-pass shader and got a little confused when it was putting together the { … } for the first pass.

Original looked like

    	if (PASSINDEX == 0)	{
    	
    	else if (PASSINDEX == 1)	{
    	...
    	}

My lazy fix…

    	if (PASSINDEX == 0)	{
    	
    	}
    	else if (PASSINDEX == 1)	{
    	...
    	}

But actually since this doesn’t need multiple render passes, you could get rid of that if/else entirely and just keep the part that is in the ‘else’

  1. The other changes I had to make were a little more subtle but not particularly major: for some reason all of the custom functions in the original shader were listed twice, so I had to delete the duplicates. Second, one of the functions used a trick for making a mat4 out of a mat3 (small difference in open gl versions); this was a pretty minor change in the format that I figured out from a stackoverflow post when googling for the error message.
0 Likes

#5

a bit tricky
at this day I wouldn’t find by my own

If you need help in sound design - mixing - or mastering tell me
would be a pleasure to help you for once :slightly_smiling_face:

1 Like