How to set the background color of a Viewport in 2D?

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

Just trying to do that, but I find no method for this in the documentation :s

General clear color (like the one on project settings) or you want to have many viewports with different clear color at the same time?

eons | 2016-10-19 22:47

I want to set the clear color of only this specific viewport (and different colors to others if needed)

Zylann | 2016-10-19 23:00

Clear color is something general, I don’t think you will be able to change it per viewport (but is possible to set transparent background on subviewport).

VisualServer has the setter for clear color, for individual viewports (if many) you will have to create a background image (frame/polygon/sprite).

eons | 2016-10-19 23:34

What is the point of being able to set a transparent background if setting a clear color doesn’t works? And why can you set a background color with the Environment if it doesn’t works anyways? I think there is something wrong here.

Zylann | 2016-10-20 18:38

:bust_in_silhouette: Reply From: Tybobobo
  1. Select “Scene” from the top left menu.
  2. Select “Project Settings
  3. Make sure the “General” tab is selected, and select the “Render” section.
  4. Check “default_clear_color” and select a background color.

I am using Godot Engine v2.2.alpha, so if this is not possible for you, it means you either have to wait for a newer release or get a more recent build.

This sets the global default clear color. I want to set the clear color of a specific viewport instead of the main one.

Zylann | 2016-10-20 18:40

:bust_in_silhouette: Reply From: mokalux

hi zylann,

I was looking at one of the answer you gave here : Is there method to use canvas drawing on top of the buffer of the previous? - Archive - Godot Forum

I messed around with your code and maybe found an answer to your current question.

Probable solution:

the node:


the code:

var vpgrtt = viewport.get_render_target_texture()
board = get_node("Control/Sprite")
board.set_pos(viewport.get_rect().size / 2.0)

(refer to your answer in the linked thread for more details about the code)

since you added a sprite to the control node, you can play around with the modulate option!
Note that the sprite can be empty (it is in my case) and serves only as reference and modulation.

It may be a solution, tell me what you think.

Thank you.


It’s another way of drawing a full-viewport rectangle using a texture, I was looking for something that uses an equivalent of glClear, because I doubt the background of Godot games is filled with a texture, right? So since the main window is a viewport, why can’t we set the background color of a Viewport node created manually, especially when there seem to be options to do so (but don’t work)?
Thanks for the answer anyways.

Zylann | 2016-12-03 15:02

:bust_in_silhouette: Reply From: Michael Marid

Use ColorRect node

That’s another workaround, but not what I am searching for.

Zylann | 2018-03-29 18:35

And actually that doesn’t seem to work if one wants a transparent background (because the clear already has non-zero alpha, a zero alpha ColorRect has no effect).

bluenote | 2019-12-16 10:26

:bust_in_silhouette: Reply From: Spartan322

To set the record for future questions on the subject, since at least Godot 2.1 you may use VisualServer.set_default_clear_color(Color) (Doc) to set the active viewport’s clear color.

Slightly related note: ProjectSettings (Doc) recommend you use the Viewport’s world.enviroment.background_mode/color variable for viewports however world and environment is a 3D viewport system, and the latter is assigned null in 2D environments and will not be used by the 2D engine, it appears individual viewport clear colors is a relatively noticeable oversight in Godot and it has to be hacked around codewise to properly apply it.