Masking 3D Objects with a Sprite
Asked Answered
R

1

0

Hi! I've got a fun effect I'm trying to achieve. I want to mask out a mesh so that it only renders what's underneath the shape of my sprite.

Basically, I've got a sphere with two materials: one black, one green (the green one having no depth test). I only want to render the green parts of this sphere, where my little lump of pixels covers it. (It's worth noting that my "sprite" isn't a Sprite3D - it's a quad mesh with a shader on it that takes a texture. Otherwise, the transparency around the sprite would not work with this effect.)

I followed this tutorial to get this far:

But I think I'm seeing the limitations in terms of what I want to use it for. I'm seeing now that the black sphere is simply covering the green, and lowering the alpha/not drawing the black sphere makes the green completely take over.

So what I'm looking for is a way to not draw any black pixels. I want to see the world behind it. Perhaps a shader that can check where the sprite is overlapping and only draw that. I'm not sure if my current method is close to this or needs to be scrapped entirely, but any advice is greatly appreciated. Thanks!

Runny answered 13/4, 2023 at 18:25 Comment(0)
R
0

Aha! I found a solution in this video:

The answer was to make a shader that read the SCREEN_TEXTURE and SCREEN_UV - that's exactly what I was looking for!

It took minimal fiddling to get it to work in Godot 3, but here's the entire shader:

shader_type spatial;
render_mode cull_back, unshaded;

void fragment() {
	ALBEDO = textureLod(SCREEN_TEXTURE, SCREEN_UV, 0.0).rgb;
}

Basically one line!

Runny answered 13/4, 2023 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.