• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Shaders Random Error?

TomOmNom

Member
I don't deal with shaders much, so I used a converter to turn shadertoy code into something usable. Unfortunately, it's throwing a seemingly unexplainable error, and I'm clueless on how to fix it.
I've tried restarting the software but I cannot get it to work.

Lines 1-25:
GML:
uniform vec3 iResolution; 
varying vec2 fragCoord; 

float character(int n, vec2 p)
{
    p = floor(p*vec2(4.0, -4.0) + 2.5);
    if (clamp(p.x, 0.0, 4.0) == p.x)
    {
        if (clamp(p.y, 0.0, 4.0) == p.y)    
        {
                int a = int(floor(p.x) + 5.0 * floor(p.y));
            if (((n >> a) & 1) == 1) return 1.0;
        }    

    }
    return 0.0;
}

void main( void )
{
    vec2 pix = fragCoord.xy;
    vec3 col = texture2D(gm_BaseTexture, floor(pix/8.0)*8.0/iResolution.xy).rgb;    
    
    float gray = 0.3 * col.r + 0.59 * col.g + 0.11 * col.b;
    
    int n =  4096;                // .
    if (gray > 0.2) n = 65600;    // :
    if (gray > 0.3) n = 332772;   // *
    if (gray > 0.4) n = 15255086; // o 
    if (gray > 0.5) n = 23385164; // &
    if (gray > 0.6) n = 15252014; // 8
    if (gray > 0.7) n = 13199452; // @
    if (gray > 0.8) n = 11512810; // #
    
    vec2 p = mod(pix/4.0, 2.0) - vec2(1.0);
    
    if (iMouse.z > 0.5)    col = gray*vec3(character(n, p));
    else col = col*character(n, p);
    
    gl_FragColor = vec4(col, 1.0);
}
Any suggestions welcome.
 
Last edited:

rytan451

Member
The error is probably above this. GMS actually adds a lot of boilerplate code to the start of GLSL shaders, which makes the line numbers in the debug output incorrect.
 

TomOmNom

Member
I've edited the code in the original to include lines 1-25, comments removed. I can't spot any errors, but I know essentially nothing of GLSL, or even why most of this code is here.
If the error is above as you say, it should be in there.
 
Last edited:

Tyg

Member
that is not shader code...create a new shader and leave the varrying vecs
this code is necessary for the shader
you dont return like a function
the return is gl_FragColor and will be green if it is spelled correct
and you just have a function
there are lots of limitations to shaders it has its own functions and not GML functions
you need a void main()
only use the fsh fragment shader
the vsh is for vertex if your going to pass in vertexs from a buffer for 3D or primative drawing
that could not be the all the code from shadertoy you just have a function clipped
if you want to convert shadertoy shaders i could show you how
ill admit it was a hard concept for me to grasp at first on how everything boils down to one var gl_FragColor
if theres any interest i could post a thread on how to convert and implement a shadertoy shader 2D and 3D
yes 2D you can use it on a sprite doesnt have to be 3D and even video from an animated gif
This one has glowing lines floating across it..very cool
ss.jpg
 
Last edited:

TomOmNom

Member
Let me restate. That was only part of the shader. I've edited it again to include all of the shader. There is a void main, It's all in the fragment shader, I already had the vertex shader part done too, definitely a gl_FragColor in the second part of the shader as well. I only included the troublesome bit of code originally.

What if you remove that line? Maybe there's a non-printing character that's causing a problem.
Tried that a couple times as I suspected it might be the problem. Turns out it was not, unfortunately.

The problem is something with syntax, not the purpose of the code. I can troubleshoot that part on my own (hopefully lol).
 

Tyg

Member
ok...i see what is your fragCoord referencing?
there is a gl_FragCoord
you chopped out v_vTexcoord and v_vColor again...they reference the texture
v_vTexcoord and v_vColour will give you all the info you need about the texture
v_vTextcoord is the x,y of the point in the texture that its is at(varying)
gl_FragCoord follows along with it as well as v_vColour
fragCoord is referancing a varying vec but doesnt know what it is
better to use the built in gl stuff
and use uvs

****your fragCoord should actually be your v_vTexcoord, that is the variable that gamemaker tracks don't delete it
if you want something like a mask to follow you could do this and this is the only reason you would need a new varying vec to follow the v_v s if that makes sense to you

varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying vec2 v_vMaskcoord;

uniform sampler2D iChannel0;

void main()
{
vec4 O_Color = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord ); // The original sprite texture
vec4 M_Color = v_vColour * texture2D( iChannel0, v_vMaskcoord ); // The imported texture...it still needs something to follow and it is using v_vColour here
// Mcolor follows v_vColour but uses the iChannels texture
gl_FragColor = vec4(M_Color.rgb, O_Color.a); // the imported texture with the originals alpha mask
}


The reason you have to keep the template vars is that is the minum that gamemaker needs and uses
add to it..but dont delete it

**** also its void main() ..you have void main(void) :)
 
Last edited:

TomOmNom

Member
First line of void main. I don't know how the shader works, so I wouldn't know if that's the problem. But also, the error is above that section in the code, so it's not that line that's causing the problem.
 

Tyg

Member
Ill plug in your code and see
For reference the error is 2 lines above the error line number that is given
im working through it...but there are obvious errors
like a variable it is red because it is a constant used internally for alpha rgba...use some other variable name like ab and it should go white
the same with p

shaders in general don't like ints i rarely use them except for booleans...it is choking all over the place with them
i changed them to floats and managed to get it to compile
where are you getting this iMouse.z from?
iResolution should also be a vec2
Looks like some kind of grayscale shader
Ill post what i have so far, but it doesnt seem to do anything
whats it supposed to do?

also in this line if (iMouse.z > 0.5) col = gray*vec3(character(n, p));
not sure what mouse.z is but gray which is an int is multiplied by a vec3 that calls a function character that returns a float, crazy stuff...lol

This will compile
------------------------------------------
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

uniform vec2 iRez;
uniform vec2 iMouse;

float character(float n, vec2 pa)
{
pa = floor(pa*vec2(4.0, -4.0) + 2.5);
if (clamp(pa.x, 0.0, 4.0) == pa.x)
{
if (clamp(pa.y, 0.0, 4.0) == pa.y)
{
n = 1.;
float ab = float(floor(pa.x) + 5.0 * floor(pa.y));
if (n > ab)
return 1.0;
}
}
return 0.0;
}

void main( )
{
vec2 pix = v_vTexcoord;
vec3 col = texture2D(gm_BaseTexture, floor(pix/8.0)*8.0/iRez.xy).rgb;

float gray = 0.3 * col.r + 0.59 * col.g + 0.11 * col.b;

float n = 4096.; // .
if (gray > 0.2) n = 65600.; // :
if (gray > 0.3) n = 332772.; // *
if (gray > 0.4) n = 15255086.; // o
if (gray > 0.5) n = 23385164.; // &
if (gray > 0.6) n = 15252014.; // 8
if (gray > 0.7) n = 13199452.; // @
if (gray > 0.8) n = 11512810.; // #

vec2 pa = mod(pix/4.0, 2.0) - vec2(1.0);

if (iMouse.x > 0.5) col = gray*vec3(character(n, pa));
else col = col*character(n, pa);

gl_FragColor = vec4(col, 1.0);
}

-----------------------------
if you let me know what this is supposed to do i can help you with a proper shader...im trying but its a mess :)
if you think its just going to output a character, wrong...there is no text type in shader, you would have to use the fonts bitmap texture and subdivide it or use glyphs
Did you dig this out of someones recycle bin...it was put there for a reason..lol
 
Last edited:

Tyg

Member
Ok i found it, ill see if i can get it to work
ok theres quite a few errors, gamemaker doesnt know the function round on line 16
i wouldnt recommend using that converter
"round" is available in Cg but not in GLSL
Ok thats not even the big problem
shaders have no text type and this shader assumes you can just pick chars out of the air
Gamemaker stores it chars in a texture page , you cant even access the obsolete DEFAULT_CHARSET, not yelling thats how its spelled
So i made a font like the default, managed to get the texture page into the shader
But its not nice, you can't do a texelFetch like some of the shaders imply anymore
so ill have to try and breakup the texture page in chars, but hopefully gamemaker hasnt encoded them (sigh)
stored as a png file...great png decoding,
think it will be easier to just make a bitmap texture...boy this sure opened pandoras box....lol
Its no problem, i was wanting to do a bitmap font shader anyways
 
Last edited:
Shouldn't this line:
GML:
int a = int(floor(p.x) + 5.0 * floor(p.y));
be THIS?:
GML:
int a = int(floor(p.x) + 5 * int(floor(p.y)));
Nevermind, I saw the parenthesis at the wrong place!
 
Top