游戏开发论坛

 找回密码
 立即注册
搜索
查看: 4844|回复: 0

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

[复制链接]

8722

主题

8783

帖子

1万

积分

版主

Rank: 7Rank: 7Rank: 7

积分
11952
发表于 2018-4-9 11:41:38 | 显示全部楼层 |阅读模式
原作者为 nikles,本文由 highway★负责翻译。

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

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

8752-1520406790.gif

实际代码

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

代码:

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

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

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

代码(已修正):

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

  4. // 给 surface 设置目标
  5. surface_set_target(tearings_surface)

  6. draw_clear_alpha(c_black, 0)

  7. // 我们将部分应用 surface 绘制在撕裂 surface 上
  8. for (var current_band = 0; current_band < band_num * 2; current_band++)
  9. {
  10.         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)
  11. }

  12. // 始终重置目标 surface
  13. surface_reset_target()
  14. // 绘制实际 surface
  15. draw_surface_stretched(tearings_surface, -tearings_x_offset, 0, dw + tearings_x_offset * 2, dh)
  16. // 移动撕裂
  17. tearings_y = (tearings_y + tearing_speed) % (band_height * band_num)
复制代码

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

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

via:indienova


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2024-4-25 20:01

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表