• 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!

Legacy GM Shader_prim, models, and a bunch of Q's

M

Misty

Guest
In the Gm help file they have a shader_prim. Can't find it anywhere, and I am just looking for a basic shader that does the same thing as d3d_model_vertex_normal_texture_color. Your basic shader that doesn't do anything, just draws a primitive with fog.

The other thing is, when I use vertex_buffers it is actually slower than d3d_models unless I send them to the shader, and the shader is only very slightly faster than d3d_models, so I may just be switching back to d3d_models. I froze the vertex buffer and its still slower than d3d_models.
 
Hey, in case you don't get a more expert reply, you can try to use this thing that I've cobbled together in an inexpert manner. I may have made a mistake while editing it to post here, so read through it and see if anything looks badly wrong. You'll need to pass in the sun position and color, ambient color, fog color, and fog distances as uniforms because as far as I know there's no way to get them out of the gamemaker constants (dispite what the manual says.) If I'm wrong about that, I'd love to to know about it. Note: I'm not totally sure if I'm rotating the normal vector correctly. It appears to work in my game, but you should double check that.
Code:
//VERTEX
attribute vec3 in_Position;
attribute vec4 in_Colour;
attribute vec2 in_TextureCoord;
attribute vec3 in_Normal;

uniform vec3 sun_pos;
uniform vec3 sun_color;
uniform vec3 ambient_color;
uniform vec2 fog_dist;
uniform vec3 fog_color;

varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying float fog_ammount;

void main()
{  
  gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * vec4( in_Position, 1.0);

  //rotate normal vector by world matrix.  (double check that this produces correct results)
  vec4 normal_Transformed = gm_Matrices[MATRIX_WORLD] * vec4(in_Normal,0.0);

  //my sun_pos is negated, I guess it depends on whether your sun_pos defines the vector to the sun or the vector the sun rays are pointing
  //sun_pos and in_Normal are assumed to be already normalized
  vec3 sun_light = sun_color * clamp(dot(-sun_pos,normal_Transformed.xyz),0.0,1.0); 

  v_vColour = in_Colour * vec4(sun_light + ambient_color,1.0);

  //might be better to use length(gl_Position.xyz) instead of just gl_Position.z
  //fog_dist.x = fog near distance, fog_dist.y = 1/(fog far dist - far near dist)
  fog_ammount = clamp((gl_Position.z - fog_dist.x) * fog_dist.y,0.0,1.0); 
}

//FRAGMENT
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying float fog_ammount;

void main()
{
  vec4 tex_colour = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord);
  gl_FragColor = vec4(mix(tex_colour.xyz,fog_color,fog_ammount),tex_colour.a);
}
 
Last edited:
M

Misty

Guest
Thanks for the code, and I've tried various things, but I cannot get the shader to compiile. I wish there was a GM library of shaders that we could just use that had the basic d3d_shader with normals and fog.
 
Out of curiosity, what do you need the shader for? If it does nothing beyond what you described so far, it is just reinventing the shader that game maker uses by default.

The shader I posted contained a few errors, which I think I have corrected. I'm still not 10% sure about transforming the normal vector. It appears correct based on the small test I did. You could always look up how to transform it correctly. My monitor is on the fritz or I would do it myself.

The, hopefully, corrected shader code:
Code:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying float fog_ammount;

void main()
{ 
  gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * vec4( in_Position, 1.0);
  
  //might be better to use length(gl_Position.xyz) instead of just gl_Position.z
  //fog_dist.x = fog near distance, fog_dist.y = 1/(fog far dist - far near dist)
  fog_ammount = clamp((gl_Position.z - fog_dist.x) * fog_dist.y,0.0,1.0);
  
  //rotate normal vector by world matrix.  (double check that this produces correct results)
  vec4 normal_Transformed = normalize(gm_Matrices[MATRIX_WORLD] * vec4(in_Normal,0.0));
  
  //my sun_pos is negated, I guess it depends on whether your sun_pos defines the vector to the sun or the vector the sun rays are pointing
  //sun_pos and in_Normal are assumed to be already normalized
  vec3 sun_light = sun_color * clamp(dot(-sun_pos,normal_Transformed.xyz),0.0,1.0);
  
  v_vColour = in_Colour * vec4(sun_light + ambient_color,1.0);
  
  v_vTexcoord = in_TextureCoord;
}
//FRAGMENT
uniform vec3 fog_color;

varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying float fog_ammount;

void main()
{
  vec4 tex_colour = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord);
  gl_FragColor = vec4(mix(tex_colour.xyz,fog_color,fog_ammount),tex_colour.a);
}
[/spoilr]
 
Last edited:
Top