continue
So I'm looking to create an effect of having a bubble around my player which, when he enters a hidden area (hidden by tilemaps) the bubble activates and it essentially has an xray effect. So I can see the background, the ground and all the items inside the area I just can't see the blocks themselves.
So pretty much going from this
And as I go further in the more gets revealed
I have no idea what to even begin searching for this. So any direction would be greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我想弄清楚一些东西:在玩家附近的事物很容易,您使用灯和着色器时,让事物出现。 中不可能通过该方法在播放器附近时消失(3D具有
flags_use_shadow_to_opacity
)。在2D 为了显示什么和不显示什么。然后,我们将使用着色器使用该纹理掩码来制作一种选择性消失的材料。要创建该纹理,我们将使用
viewport
,因此我们可以从中获得viewPortTexture
。fiewport
设置是这样的:设置
viewport
带有以下属性:为
Sprite
您想要灰度纹理,也许具有透明度。这将是您想在玩家周围揭示的形状。对于
colorrect
,您想将背景颜色设置为黑色或白色。sprite
上颜色的相反。接下来,您将将脚本附加到
fiewport
。它必须处理两个问题:移动
Sprite
以匹配玩家的位置。看起来这样:您将设置
target_path
以引用玩家avatar。在此代码中
target.get_viewport()。get_canvas_transform()。Origin
将使我们在屏幕上为我们提供目标节点(player avatar)的位置。我们将sprite
匹配。处理窗口大小。看起来这样:
在此代码中,我们连接到
“ size_changed” root
devieport (与窗口关联的一个),然后更改此fiewport的大小
匹配。接下来是着色器。转到您的
tilemap
或您想消失的任何东西并添加着色器材料。这是它的代码:如您所见,第一行将设置红色,绿色和蓝色通道以匹配节点已经具有的纹理。但是,Alpha通道将设置为掩模纹理的一个通道(在这种情况下为红色)。
注意:上面的代码将使黑色部分中的任何内容完全不可见,而白色部分中的任何内容都完全可见。如果要倒置,请更改
color.a = texture(bask,screen_uv).r;
tocolor.a = 1.0-纹理(mask,screen_uv).r; 。
当然,我们需要设置掩盖纹理。设置该代码后,在着色器材料“蒙版”的着色器材料下应该有一个着色器参数,将其设置为新的
viewPortTexture
,然后将fiewport
设置为我们设置的前。我们完成了。
我从
plus plus plus plus plus 一些瓷砖来自。当然,它们都是公共领域。
这就是:
尝试不同的纹理以获得不同的结果。另外,您可以在
sprite
中添加一个着色器,以获得额外的效果。例如,通过像这样的代码给sprite
给予着色器材料来添加一些涟漪:因此,您可以得到此结果:
何时有一个瞬间上述动画斯托克。那是因为我没有完美地切开循环。游戏中不是问题。此外,动画的帧每秒比游戏要少得多。
Addendum 我想添加的几件事:
BLIT_RECT_RECT_RECT
。您可能还对blit_rect_mask
。感兴趣
Backbuffercopy
。丢弃
fragments。First of all, I want to get something out of the way: Making things appear when they are nearby the player is easy, you use a light and a shader. Making things disappear when they are nearby the player by that approach is impossible in 2D (3D has
flags_use_shadow_to_opacity
).This is the plan: We are going to create a texture that will work as mask for what to show and what not to show. Then we will use that texture mask with a shader to make a material that selectively disappears. To create that texture, we are going to use a
Viewport
, so we can get aViewportTexture
from it.The
Viewport
setup is like this:Set the
Viewport
with the following properties:For the
Sprite
you want a grayscale texture, perhaps with transparency. It will be the shape you want to reveal around the player.And for the
ColorRect
you want to set the background color as either black or white. Whatever is the opposite of the color on theSprite
.Next, you are going to attach a script to the
Viewport
. It has to deal with two concerns:Move the
Sprite
to match the position of the player. That looks like this:And you are going to set the
target_path
to reference the player avatar.In this code
target.get_viewport().get_canvas_transform().origin
will give us the position of the target node (the player avatar) on the screen. And we are placing theSprite
to match.Handle window resizes. That looks like this:
In this code we connect to the
"size_changed"
of the rootViewport
(the one associated with the Window), and change the size of thisViewport
to match.The next thing is the shader. Go to your
TileMap
or whatever you want to make disappear and add a shader material. This is the code for it:As you can see, the first line will be setting the red, green, and blue channels to match the texture the node already has. But the alpha channel will be set to one of the channels (the red one in this case) of the mask texture.
Note: The above code will make whatever is in the black parts fully invisible, and whatever is in the white parts fully visible. If you want to invert that, change
COLOR.a = texture(mask, SCREEN_UV).r;
toCOLOR.a = 1.0 - texture(mask, SCREEN_UV).r;
.We, of course, need to set that mask texture. After you set that code, there should be a shader param under the shader material called "Mask", set it to a new
ViewportTexture
and set theViewport
to the one we set before.And we are done.
I tested this with this texture from publicdomainvectors.org:
Plus some tiles from Kenney. They are all, of course, under public domain.
This is how it looks like:
Experiment with different textures for different results. Also, you can add a shader to the
Sprite
for extra effect. For example add some ripples, by giving a shader material to theSprite
with code like this one:So you get this result:
There is an instant when the above animation stutters. That is because I didn't cut the loop perfectly. Not an issue in game. Also the animation has much less frames per second than the game would.
Addendum A couple things I want to add:
blit_rect
. You might also be interested inblit_rect_mask
.BackBufferCopy
.discard
fragments.