SOLVED Custom Shadow Color?

Slothagami

Member
I have been battling with the shadows in my games for quite a while now and can't seem to do this myself,

I want to have shadows for each object, in the shape of their object, and using a different color based on the color behind the shadow (to fit with the color palette) I am very new to shaders and don't know how I might do this, or if I even need a shader for this. I want to avoid just having a surface with the shadow's making the pixels x% darker, this game is more art focused so I want the shadow colors to have the hue shifting from my color pallet so that it matches the other sprites I've drawn.

This is the desired result:
Mockup-export.pngMockup-export.png
The sprite's shadow is over two colors and I have a custom shadow color for each I would like to use.

If possible I would also like it to obey the depth like in the second picture (not as important).

Thanks in advance!

The Final result in game:
1594549631937.png

GML:
// The shader
// the vertex shader has not been changed

varying vec2 v_vTexcoord;
varying vec4 v_vColour;

uniform sampler2D alpha_mask;

void main()
{
    vec4 color = texture2D( gm_BaseTexture, v_vTexcoord );
    vec4 mask = texture2D( alpha_mask, v_vTexcoord );
    
    if(color == vec4(0.6, 0.756862745, 0.505882353, mask.a)) // green
        color = vec4(0.2578125, 0.6, 0.576470588, mask.a);
        
    if(color == vec4(vec3(0.388235294), mask.a)) // grey
        color = vec4(vec2(0.207843137), 0.231372549, mask.a);
        
    // add other colors here as needed
    // color is vec4(red / 255, green / 255, blue / 255, 1)
    
    gl_FragColor = color;
}
GML:
// Draw GUI Begin Event
surface_set_target(shadow_surf);
    draw_clear_alpha(c_black, 0);
    shader_set(shd_white);
        with all {
            if sprite_exists(sprite_index) and visible {
                var 
                cx = camera_get_view_x(view_camera[0]),
                cy = camera_get_view_y(view_camera[0]),
                cw = camera_get_view_width(view_camera[0]),
                ch = camera_get_view_height(view_camera[0]),
                scale = GUI_W / cw;
                
                if x <= cx + cw and x + sprite_width >= cx {// if x on screen
                    if y <= cy + ch and y + sprite_width >= cy {//if y on screen
                        draw_sprite_ext(sprite_index, image_index, (x - cx) * scale, (y - cy) * scale, scale, -scale, 0, c_purple, 0.2);
                    }
                }
            }
        }
    shader_reset();
surface_reset_target();

var 
texture = surface_get_texture(shadow_surf),
sampler = shader_get_sampler_index(shd_shadowcolors, "alpha_mask");

shader_set(shd_shadowcolors);
    // send the shadow_surf to the shader as the alpha mask
    texture_set_stage(sampler, texture);
    
    draw_surface(application_surface, 0, 0);
shader_reset();
 
Last edited:

curato

Member
you could always drop a black version of the character sprite with some alpha so the color it is being dropped on shows through. You could even change its color based on what the player is standing on, but to dynamically change the color per pixel like you are probably thinking is a shader.
 

Slothagami

Member
you could always drop a black version of the character sprite with some alpha so the color it is being dropped on shows through. You could even change its color based on what the player is standing on, but to dynamically change the color per pixel like you are probably thinking is a shader.
Yeah I realized that this is pretty similar to a color swap shader, I don't even know how to start making one of those though.
 

rIKmAN

Member
Yeah I realized that this is pretty similar to a color swap shader, I don't even know how to start making one of those though.
If you don't mind spending a few bucks the Retro Palette Swapper asset by Pixelated Pope will save you way more than $4 worth of your time and can be found on the Marketplace or on Itch.io.

If you don't wanna spend $4 and/or would rather learn to do it yourself then some time spent Googling will help you out.
From a quick Google I found this, this and this (which is made in 1.4 but should be the same for GMS2) and I'm sure there is more info / tutorials if you spend more time than I did searching Google / the forum.
 
I mean, have you tried just a black (or better yet, a very deep dark purple colour, seems to fit shadows better than pure black) semi-transparent shadow without dealing with any shader stuff? It'll darken the colours underneath in a roughly similar way to what you are describing.
 

Slothagami

Member
I mean, have you tried just a black (or better yet, a very deep dark purple colour, seems to fit shadows better than pure black) semi-transparent shadow without dealing with any shader stuff? It'll darken the colours underneath in a roughly similar way to what you are describing.
Thanks for the help! I tried it, but I'm not quite getting the result I'm after. I did some research and I think a color lookup table is what I need.
 

vdweller

Member
Here is what I did for I dream of you and ice cream:

You don't need shaders. Create a "shadowed" version of the floor (as in, a separate image) and using surface masks display that shadowed region under your character(s). Works great!
 

Slothagami

Member
Here is what I did for I dream of you and ice cream:

You don't need shaders. Create a "shadowed" version of the floor (as in, a separate image) and using surface masks display that shadowed region under your character(s). Works great!
Thanks! That's really helpful! It's too late now though, I've already finished my color swap shader! (the color swap part at least) But I'll have to remember that for next time!
 
Top