SKShader创建视差背景 [英] SKShader to create parallax background

查看:113
本文介绍了SKShader创建视差背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有固定相机的视差背景很容易做到,但是由于我正在制作自上而下的2D空间探索游戏,所以我发现只有一个SKSpriteNode可以填充屏幕,并且可以作为我的SKCameraNode的子代并使用SKShader绘制视差星空会更容易.

A parallax background with a fixed camera is easy to do, but since i'm making a topdown view 2D space exploration game, I figured that having a single SKSpriteNode filling the screen and being a child of my SKCameraNode and using a SKShader to draw a parallax starfield would be easier.

我继续使用shadertoy,发现了这个外观简单的着色器.我成功地在shadertoy上对其进行了调整,以接受vec2()作为我想作为SKAttribute传递的运动速度,以便它可以跟随我船的运动.

I went on shadertoy and found this simple looking shader. I adapted it successfully on shadertoy to accept a vec2() for the velocity of the movement that I want to pass as an SKAttribute so it can follow the movement of my ship.

以下是原始来源: https://www.shadertoy.com/view/XtjSDh

我设法进行了原始代码的转换,因此它可以编译而没有任何错误,但是屏幕上没有任何显示.我尝试了各个功能,它们确实可以生成固定图像.

I managed to make the conversion of the original code so it compiles without any error, but nothing shows up on the screen. I tried the individual functions and they do work to generate a fixed image.

是否有任何使其起作用的指针?

Any pointers to make it work?

谢谢!

推荐答案

代码很干净,现在可以正常工作.我为此设置了一个GitHub存储库.

我想我没有解释我想要的东西.我需要一个跟随摄像机的星空背景,就像在Subspace(过去的日子)中可以找到的一样

I guess I didnt explain what I wanted properly. I needed a starfield background that follows the camera like you could find in Subspace (back in the days)

结果非常酷,令人信服!当节点数量成为瓶颈时,我可能稍后再讲.我仍然坚信正确的方法是使用着色器!

The result is pretty cool and convincing! I'll probably come back to this later when the node quantity becomes a bottleneck. I'm still convinced that the proper way to do that is with shaders!

这是我在GitHub上的代码的链接.我希望它对某人有用.这项工作仍在进行中,但效果很好.回购中包括来自 SKTUtils (Ray Wenderlich的库,该库已在github上免费提供)的源以及我对Ray的工具的扩展,我称之为 nuts-n-bolts .这些只是每个人都应该发现有用的常见类型的扩展.您当然拥有 StarfieldNode InteractiveCameraNode 的来源以及一个小型演示项目.

Here is a link to my code on GitHub. I hope it can be useful to someone. It's still a work in progress but it works well. Included in the repo is the source from SKTUtils (a library by Ray Wenderlich that is already freely available on github) and from my own extension to Ray's tools that I called nuts-n-bolts. these are just extensions for common types that everyone should find useful. You, of course, have the source for the StarfieldNode and the InteractiveCameraNode along with a small demo project.

这篇关于SKShader创建视差背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆