在GMS2中使用Surfaces实现屏幕撕裂波纹效果

作者:nikles
翻译:highway★
原文地址:在GMS2中使用Surfaces实现屏幕撕裂 / 波纹效果

初衷

在玩过Environmental Station Alpha之后,我也想实现Hempuli(上句那个游戏的开发者)在他的游戏做出的效果。我不知道该如何实现,所有我不得不从头开始,思考不同的方法。

我对shader(译注:着色器,如果你是初学者,还是敬而远之,对美术基础和数学还有编程的要求挺高的)一窍不通,所以只能用surface了。我写了一些代码,然后立刻撞墙……我有点儿懵逼,就去yoyogames的论坛上求助,看了其他人的评论之后,我终于想出一个相当不错的解决办法。

实际代码

在你的游戏控制object(起名随意,比如oGame、oCont之类的)的create事件中,写下面的这些东西

代码:

// 名字缩写,方便后面用,要不然代码太长,看着费劲
dw = display_get_width()
dh = display_get_height()

tearings_surface = surface_create(dw, dh) // 我们要把这个绘制到surface
tearings_y = 0
band_num = 16 // 屏幕上要出现多少个横条
band_height = dh / band_num
tearings_x_offset = 32 //你要怎样水平移动横条
tearing_speed = 4 // 修改这里可以加速/减速

下面这些东西写在刚才那个object的post-draw事件中。

代码:

// 如果surface不存在便创建它
if !surface_exists(tearings_surface)
tearings_surface = surface_create(display_get_width(), display_get_height())

// 给surface设置目标
surface_set_target(tearings_surface)
draw_clear_alpha(c_black, 0)

// 我们将部分应用surface绘制在撕裂surface上
for (var current_band = 0; current_band < band_num * 2; current_band++)
{
  draw_surface_part(application_surface, 0, band_height * current_band - tearings_y, dw, band_height, sin( (degtorad(360) / band_num ) * current_band) * tearings_x_offset , band_height * current_band - tearings_y)
}

// 重置目标surface
surface_reset_target()

// 实际绘制surface
draw_surface_stretched(tearings_surface, -tearings_x_offset, 0, dw + tearings_x_offset * 2, dh)

// 移动撕裂波纹效果
tearings_y = (tearings_y + tearing_speed) % (band_height * band_num)

搞定.

我的做法就是这样~我还会再弄一个垂直撕裂的类似版本出来(水下关卡?没准儿~)希望这篇小文章对你有帮助。

关于surface的使用要注意:如果你不用的时候,请记住一定要释放surface,否则会引起内存泄漏越来越卡或者可能崩溃。

2021-04-28 15:14
Comments
Write a Comment