← Pixel Dreamer Studio

Make WorldEnvironment color grading apply to a 2D game

Environment adjustments grade the 3D picture. Set Background > Mode to Canvas and the 2D canvas goes through them as well.

In the Environment, set Background > Mode to Canvas (env.background_mode = Environment.BG_CANVAS). In our Godot 4.7 test with a LUT that swaps red and blue, a red 3D quad came out blue, but a red 2D ColorRect stayed red at (204, 51, 25), on the main canvas and in a CanvasLayer. With Background Mode Canvas the same ColorRect came out blue, (15, 45, 209). CanvasLayers above layer 0 stay ungraded unless you raise Max Layer, which keeps a HUD's colors exact.

The code

For when you want a strength slider, exact colors, or to grade layers you choose: add_grade() adds a CanvasLayer with a full-screen ColorRect that looks every pixel below it up in your LUT, set to let clicks through. Save it as lut_2d.gd, then for example: const Lut2D := preload("res://lut_2d.gd") and Lut2D.add_grade(self, preload("res://my_lut.png"), 100, 0.8), where the PNG is a LUT imported as a Texture3D. HUD and menus on layers above 100 keep their own colors.

extends RefCounted
## LUT color grading for 2D games and 2D layers, with a strength setting.
## A full-screen ColorRect reads the screen and looks every pixel up in a 3D LUT.
## The built-in way is the Environment's Background Mode set to Canvas; this one also grades CanvasLayers you pick.

const SHADER := """shader_type canvas_item;
uniform sampler2D screen_tex : hint_screen_texture, filter_linear;
uniform sampler3D lut : filter_linear, repeat_disable;
uniform float amount : hint_range(0.0, 1.0) = 1.0;

void fragment() {
	vec3 c = clamp(texture(screen_tex, SCREEN_UV).rgb, 0.0, 1.0);
	float n = float(textureSize(lut, 0).x);
	vec3 graded = texture(lut, c * (n - 1.0) / n + 0.5 / n).rgb;  // sample texel centers
	COLOR = vec4(mix(c, graded, amount), 1.0);
}
"""


## Adds a CanvasLayer at layer with a full-screen grade. Everything drawn below that layer is graded.
## Put HUD and menus on a higher layer to keep their colors exact.
static func add_grade(parent: Node, lut: Texture3D, layer := 100, amount := 1.0) -> CanvasLayer:
	var cl := CanvasLayer.new()
	cl.layer = layer
	var rect := ColorRect.new()
	rect.set_anchors_preset(Control.PRESET_FULL_RECT)
	rect.mouse_filter = Control.MOUSE_FILTER_IGNORE  # a full-screen ColorRect would block clicks
	var sh := Shader.new()
	sh.code = SHADER
	var mat := ShaderMaterial.new()
	mat.shader = sh
	mat.set_shader_parameter("lut", lut)
	mat.set_shader_parameter("amount", amount)
	rect.material = mat
	cl.add_child(rect)
	parent.add_child(cl)
	return cl

What we measured

A 16 x 16 x 16 LUT that swaps red and blue, set as the Environment's color correction with Adjustments on. We read the color at the middle of a ColorRect or quad colored (204, 51, 25). Two runs gave the same numbers.

Why it happens

Adjustments are applied to the 3D render, and 2D is drawn on top of it afterwards, so it misses them. Background Mode Canvas makes the canvas layers up to Max Layer the background of the 3D render, so they go through the Environment like a 3D scene does. Like the 3D quad, they came out a little off the plain swap. The shader in add_grade() reads the finished screen instead and gave the exact swapped values. We tested with the Compatibility renderer only.

What to change

Ready-made grades: 24 Game Genre LUTs has 24 color grades for horror, cyberpunk, cozy, fantasy, wasteland and retro games. Its Godot 4 version comes as 24 WorldEnvironment looks with each LUT already imported as a Texture3D, plus a script that copies just the grade, glow, fog and tonemap onto your own WorldEnvironment.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: 24 Game Genre LUTs has 24 color grades as .cube files and strips, plus 24 Godot 4 WorldEnvironment looks with each LUT already imported as a Texture3D.

More how-tos: Shaders, materials and lights