+1 vote

Hi everybody, i'm actualy searching for a way to make the background of a sub viewport transparent, how can i make the black as alpha


thank you in advance for your answers

in Engine by (16 points)

1 Answer

+1 vote
Best answer

I'm not sure why you need a viewport, but you can probably attach a CanvasItemMaterial on the sprite, with a custom shader like this:

// This shader will make an item more transparent the darker it is.
// Adjust the ramp to choose how dark it must be to become transparent.
uniform float ramp = 1.0;

// Get texture pixel color
vec4 col = tex(TEXTURE, UV);

// Compute grey scale taking luminosity of colors into account
float gs = col.r*0.299 + col.g*0.587 + col.b*0.114;

// Compute modified transparency
float alpha = col.a * clamp(gs*ramp, 0.0, 1.0);

// Assign final color
COLOR.rgba = vec4(col.r, col.g, col.b, alpha);

You could probably make your thing transparent in the first place, but I don't know which are your requirements/constraints.

Edit: if your thing is actually transparent, you could simply try to enable transparent BG in the inspector when selecting your viewport.

by (27,902 points)
selected by

Thank ! It worked perfectly, but the problem was that, if the background was set invisible in the sub viewport, all dispeared completly on main scene so i added a test cube and all worked like in the built in example for 3D in 2D

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.