+1 vote

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

asked Oct 19, 2016 in Engine by Zylann (27,004 points)

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

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

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).

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.

4 Answers

–5 votes
  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 "defaultclearcolor" 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.

answered Oct 20, 2016 by Tybobobo (455 points)

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

0 votes

hi zylann,

I was looking at one of the answer you gave here : https://godotengine.org/qa/9084/there-method-use-canvas-drawing-top-the-buffer-the-previous

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.


answered Dec 3, 2016 by mokalux (169 points)

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.

0 votes

Use ColorRect node

answered Mar 29, 2018 by Michael Marid (24 points)

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

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).

0 votes

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.

answered Apr 2, 2019 by Spartan322 (14 points)
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.