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

作者:highway★
2018-04-06
15 15 2

前言

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

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

原作者为 nikles,本文由 highway★负责翻译。

实际代码

在你的游戏控制 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 // 修改这里可以加速/减速

I place the following code inside a draw_post event of my controller。下面这些东西写在刚才那个 object 的 draw_post 事件中。

代码(已修正):

// 如果 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,否则会引起内存泄漏越来越卡或者可能崩溃。




本文为用户投稿,不代表 indienova 观点。

近期点赞的会员

 分享这篇文章

您可能还会对这些文章感兴趣

参与此文章的讨论

  1. 吴会赛 2018-07-31

    嗯,666

  2. 魔女La 2019-04-21

    赞了!求更多开发技巧教程。

您需要登录或者注册后才能发表评论

登录/注册