How to mask a color in a viewport Sprite

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By xvegatble
:warning: Old Version Published before Godot 3 was released.

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

Imgur

thank you in advance for your answers

:bust_in_silhouette: Reply From: Zylann

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.

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

xvegatble | 2017-01-30 01:09