1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

Shaders Draw above a shader.

Discussion in 'Programming' started by Niels, Feb 14, 2020.

  1. Niels

    Niels Member

    Joined:
    Jun 22, 2016
    Posts:
    831
    Hi everyone,

    I have a question about shaders and layers.

    For my top-down game I want to assign a water distortion shader to a certain instance layer.

    The idea is this:
    1.instances layer (above water, shouldnt't be affected by shader)

    2. instances_below (this layer should be distorted by the watershader)
    3. background (just a sea texture)

    The shader I build is based upon the follow tutorial by Gaming reverends:


    tbh I have no clue on how to set the shader to a certain depth/layer.
     
  2. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,043
    You should be able to use layer_script_begin() / layer_script_end().


    The "Extended Example" in the manual actually shows your use case of setting a shader to affect the given layer using these functions too, so you should be able to use that as a template for setting your own shader to the layer you want to be affected by it.

    Manual page: https://docs2.yoyogames.com/source/...eference/rooms/layers/layer_script_begin.html


    edit:
    There is a bug logged on Mantis regarding using surfaces with the layer script functions, Fritz seems to have posted a workaround which might be worth looking at if you have any problems and are using surfaces yourself:
    https://bugs.yoyogames.com/view.php?id=31461
     
    Last edited: Feb 14, 2020
  3. Niels

    Niels Member

    Joined:
    Jun 22, 2016
    Posts:
    831
    Thx for the reply.
    But both layer scripts as well as layer_shader functions are made to be called once (at the creation of the layer), but my shader has a time variable that scrolls the texture distortion. I'm not sure how I can have a object that holds the shader AND use the scripts or layer_shader function.
     
  4. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    2,102
    Niels likes this.
  5. Niels

    Niels Member

    Joined:
    Jun 22, 2016
    Posts:
    831
    OK that would explain a lot! Except why you would call the scripts in the room create event (like recommended in the manual), the room create event is only called once right?
     
  6. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    2,102
    Not sure what you're referencing. Maybe you're looking at them telling you how to set up layer script? That of course needs to be done only once, and then it gets called every step.
     
    Niels likes this.
  7. Niels

    Niels Member

    Joined:
    Jun 22, 2016
    Posts:
    831
    I was aiming at this codeblock which says the script is called at the room create event
     
  8. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,043
    You can pass in your time variable each frame using uniforms, if you look at the example code they do it with colour_to_find and colour_to_set and pass in static values, but you could set your time uinform this way by passing in the variable that counts your time to the shader instead (if I've understood you correctly).

    As NightFrost pointed out the scripts need to be setup once in a Create Event which then tells GMS2:

    1) The script set in layer_script_begin() should run before this layer is rendered
    2) The script set in layer_script_end() should run after this layer is rendered

    It doesn't just run once for that frame when you call those functions, you are basically assigning the scripts to be run before/after that layer is rendered for the rest of the program execution.
     
    Niels likes this.
  9. Niels

    Niels Member

    Joined:
    Jun 22, 2016
    Posts:
    831
    Thanks for the replies, it's a bit more clear how these things work now!
    Will try to make it work tonight
     
    rIKmAN likes this.

Share This Page