Looking for help passing individual tile layers to a shader!

Resolved

Hey all,

I currently have a decent "wave" shader that will produce an effect similar to this (not my work, but it's the same gist).

However, I've encountered a problem. When I apply the shader to my tile layers, they are passing in the UVs of my tileset texture page, so I will occasionally have pixels taken from adjacent (and differently colored) tiles on the tileset.

So I'm wondering, how do I "reference" the layer itself as it's own texture page/UVs? I think I may have to draw the layer to a surface, and use the surface as a texture page, but how would I do that using layer code?

Thank you!

EDIT: The "wave effect" I am referring to is the slight left/right movement to simulate heat rising over the entire screen.

EDIT 2: And, I should mention that I want to only apply the "wave" to specific layers (namely, the tile layers, and not the player/enemy layers). I think this rules out the application surface (which is the default surface tile layers are drawn to)?

Posted Aug 20, 2020, 8:54 PM UTC · edited

39
Elvenshae•

Since I don't know much about shaders, have you considered putting each tileset into its own texture group? Then they'd be isolated on each texture page.

There's also a setting you can change in your tileset menu where you can increase the output border (x and y), which will repeat the outside row of pixel on your tiles so that they have additional real estate to play with. Might prevent your shader from being able to grab outside of the tile's desired UV.

2
AgentLymOP•

Hey, thank you for your reply!

Unfortunately, your first suggestion won't work for me because I've got, for example, grass and dirt (bright green next to dark brown) right next to each other in the tileset sprite itself.

You're second suggestion, however, sounds like it could definitely help mitigate that "tile leakage" that I described! I'll check into that for sure! Optimally, I'd still like to grab the pixels from the next tile over in the tile map built in the room editor.

2
colorsbot•

I've detected the names of 2 colors in your comment. Please allow me to provide visual representations. Bright green (#66ff00) Dark brown (#654321)


^(Learn more about me | Don't want me replying on your color word comments again? Respond to this comment with: "colorsbot opt out words")

3
oldmankcrtfm•

I don't understand why you would draw the tile layer to a surface, then use that as a texture page?

The shader happens on the GPU, basically as it's being drawn to the screen.

Tile layer -> surface -> shader -> screen. Any offsets would apply to the surface, so i imagine this would not run into the problems you'd get from adjacent tiles on the texture page.

1
AgentLymOP•

That's what I thought, but setting the shader using layer_shader() to a tile layer seems to use UVs from the tileset sprite texture page, not the tilemap as it looks in the room editor window. So I thought that drawing the tile layers to a separate surface (and using that surface as a texture using surface_get_texture() ) would solve that problem. But, I am not sure how to draw tile layers to a custom surface.

1
oldmankcrtfm•

Pretty sure there's a specific draw tilemap function, right?

Yep, looks like there is: https://docs2.yoyogames.com/source/_build/3_scripting/4_gml_reference/drawing/sprites_and_tiles/draw_tilemap.html

1
AgentLymOP•

Wow, that definitely helped get me going in the right direction. I also found this forum thread which (after a lot of messing around with my current drawing set-up) is working like a charm, and shouldn't be too difficult to fit into my current project.

For those looking for a straight answer, this is basically what I did for all my non-tile layers (which will be handled using the draw_tilemap() function u/oldmankc pointed out):

/// IN THE LAYER SCRIPT BEGIN FUNCTION
if(event_type == ev_draw)
{    
    if(event_number == 0)
    {
        surface_set_target(surface_id);
        draw_clear_alpha(c_white, 0);
        
        camera_apply(view_camera[view_current]);
    } 
}

/// IN THE LAYER SCRIPT END FUNCTION
if(event_type == ev_draw)
{    
    if(event_number == 0)
    {
        surface_reset_target();
    } 
}

Then, in a separate screen-drawing object, I turn off automatic drawing of the application surface, and draw all the layer-surfaces in the proper order onto the "GUI Layer" in the Draw GUI Begin event (making sure to align the surfaces to the proper GUI position/size), and apply the shaders directly to each surface as I desire. I haven't done all the work yet, but this should be the solution to my problem. Thanks to everyone who helped!

PS: If your individual game objects are drawing on their own surfaces for whatever reason (a few of mine were), you will probably have to make some adjustments, such as writing camera_apply[...] again after finishing drawing on their custom surfaces. It was a little tricky, but I got mine working (for now).

1
oldmankcrtfm•

For the surface, I imagine you break up the surface into a number of rows and offset the U position of each pixel in the row by a sin curve or something? Probably keep the surface a few pixels larger than the screen to account for a "bleed" area for padding?

1