• 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 What is wrong with this shader?

trippity

Member
I'm trying to implement this 'simple' shader outlined here.
The default 'pass through' shader GMS2 generates works fine without issues, but when I follow the steps for this one I get the following:
GLSL:
GML:
############################################################################################
FATAL ERROR in Vertex Shader compilation
ShaderName: sh_lighting

Invalid shader
at gml_Object_octrl_Draw_64 (line 19) - shader_set(sh_lighting);
############################################################################################
gml_Object_octrl_Draw_64 (line 19)
or GLSL ES:
Compile failure:
Vertex Shader: sh_lighting at line 8 : 'gm_BaseTexture'
Vertex Shader: sh_lighting at line 8 : 'texture2D'
Vertex Shader: sh_lighting at line 8 : '='
Vertex Shader: sh_lighting at line 27 : 'gl_FragColor'
Vertex Shader: sh_lighting at line 27 : 'assign'

Shader Code:
Code:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

uniform sampler2D lighting;
void main()
{
    vec4 mult = texture2D(lighting, v_vTexcoord);
    vec4 main = texture2D(gm_BaseTexture, v_vTexcoord);
    vec4 finalcol;
    vec4 lightcol;

    lightcol.r = mult.r * 5.;
    lightcol.b = mult.b * 5.;
    lightcol.g = mult.g * 5.;


    finalcol.r = mix(main.r,main.r*lightcol.r,mult.r);
    finalcol.g = mix(main.g,main.g*lightcol.g,mult.g);
    finalcol.b = mix(main.b,main.b*lightcol.b,mult.b);
    finalcol.a = 1.;

    float darkness = .4;

    finalcol.r = mix(finalcol.r,finalcol.r*darkness,1.-mult.r);
    finalcol.g = mix(finalcol.g,finalcol.g*darkness,1.-mult.g);
    finalcol.b = mix(finalcol.b,finalcol.b*darkness,1.-mult.b);
           
    gl_FragColor = finalcol;
}

I'm not sure if this is due to shader type & syntaxing or missing DLL's as I have read elsewhere..but considering the simple passthrough shader works I think I'm ok regarding that ..
 

FoxyOfJungle

Kazan Games
You've pasted the code into Vertex Shader. You should put it on Fragment/Pixel.

Note the errors (comes from Vertex Shader):
Compile failure:
Vertex Shader: sh_lighting at line 8 : 'gm_BaseTexture'
Vertex Shader: sh_lighting at line 8 : 'texture2D'
Vertex Shader: sh_lighting at line 8 : '='
Vertex Shader: sh_lighting at line 27 : 'gl_FragColor'
Vertex Shader: sh_lighting at line 27 : 'assign'
Not found gl_FragColor and texture2D() for example, which only exists in fragment.

And that language is GLSL ES, so put that.
 

trippity

Member
You've pasted the code into Vertex Shader. You should put it on Fragment/Pixel.

Note the errors (comes from Vertex Shader):


Not found gl_FragColor and texture2D() for example, which only exists in fragment.

And that language is GLSL ES, so put that.
Hi FoxyofJungle,

Thank you! As a disclaimer this is my first foray into shaders so I do apologise for the obvious errors/oversights.

With respect to " You've pasted the code into Vertex Shader. You should put it on Fragment/Pixel. "

How do I do that exactly?

I set the shader like so:
GML:
shader_set(sh_lighting);
var tex = surface_get_texture(lighting);
var handle = shader_get_sampler_index(sh_lighting,"lighting");
texture_set_stage(handle,tex);
draw_surface(application_surface,0,0);
shader_reset();
 

FoxyOfJungle

Kazan Games
When opening the shader file:

What has .vsh is the Vertex Shader;
What has .fsh is the Fragment Shader.



So, you must put all the code in the place indicated above.
I suggest reading: Shaders.

shader_set(sh_lighting); var tex = surface_get_texture(lighting); var handle = shader_get_sampler_index(sh_lighting,"lighting"); texture_set_stage(handle,tex); draw_surface(application_surface,0,0); shader_reset();
This part is outside of Shader, it's just GML language to define this shader in something.
 

trippity

Member
Thank you kindly for that! :)

While it 'compiles' I don't believe my setup is working. For the vertex component I left as is with the pass through code and to troubleshoot I added the below line to change the screen colour. I am able to change colour but only get a black screen with the stock shader code above..

EDIT: Managed to get it somewhat working. Issue was with how the surfaces were created.

Color test:
Code:
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
Pass through:
GML:
attribute vec3 in_Position;                  // (x,y,z)
//attribute vec3 in_Normal;                  // (x,y,z)     unused in this shader.
attribute vec4 in_Colour;                    // (r,g,b,a)
attribute vec2 in_TextureCoord;              // (u,v)

varying vec2 v_vTexcoord;
varying vec4 v_vColour;

void main()
{
    vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);
    gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
  
    v_vColour = in_Colour;
    v_vTexcoord = in_TextureCoord;
}
 
Last edited:
Top