Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The following shows how to create an alpha mask using layer scripts.
- In this solution the background will be preserved and anything drawn by masked areas will still show the background.
- Setup:
- 1. Leave the background alone.
- 2. Create layers with the following names and the order should look the same in the room editor:
- [ hidden_end ] - Any layer type, as this just execute the end script.
- ...optional layers that will also be alpha masked
- [ hidden_start ] - Asset or object layer. Things can be placed here and will be hidden if overlapping with alpha masked areas.
- [ alpha_mask ] - Asset layer where you place sprites to define the alpha masked area.
- [ Background ] - Default background layer.
- 3. Place the following code in an object that is created when the room begins, or simply place it an objects "room start" event.
- */
- // ************* Layer scripts for alpha masking **************** //
- layer_script_begin(layer_get_id("alpha_mask"),
- function()
- {
- if (event_type != ev_draw || event_number != 0) return;
- // Alpha test ensures the background isn't erased and we can use any shapes
- gpu_set_alphatestenable(true);
- gpu_set_alphatestref(200);
- // Mark areas with 0 alpha
- gpu_set_blendmode_ext_sepalpha(bm_zero, bm_one, bm_zero, bm_zero);
- });
- layer_script_begin(layer_get_id("hidden_start"),
- function()
- {
- if (event_type != ev_draw || event_number != 0) return;
- // Cannot draw on areas with 0 alpha
- gpu_set_blendmode_ext_sepalpha(bm_dest_alpha, bm_inv_dest_alpha, bm_dest_alpha, bm_one);
- });
- layer_script_end(layer_get_id("hidden_end"),
- function()
- {
- if (event_type != ev_draw || event_number != 0) return;
- gpu_set_alphatestenable(false);
- // Flip the alpha channel back to 1
- gpu_set_blendmode_ext_sepalpha(bm_dest_colour, bm_zero, bm_one, bm_zero);
- var _cam = camera_get_active();
- draw_rectangle_color(
- camera_get_view_x(_cam),
- camera_get_view_y(_cam),
- camera_get_view_width(_cam),
- camera_get_view_height(_cam),
- c_white, c_white, c_white, c_white, false
- );
- // Back to normal
- gpu_set_blendmode(bm_normal);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement