Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide shader functions for converting between srgb and linear #9150

Open
Cammymoop opened this issue Feb 21, 2024 · 2 comments
Open

Provide shader functions for converting between srgb and linear #9150

Cammymoop opened this issue Feb 21, 2024 · 2 comments

Comments

@Cammymoop
Copy link

Describe the project you are working on

Shaders, especially particle and other effects

Describe the problem or limitation you are having in your project

Some effects can benefit from being done in either linear or srgb space. In addition, not all cases of needing a linear or srgb color result/input are handled by the source_color. Mainly color constants or literal color vectors.

The srgb <-> linear conversion functions used by godot's shaders could easily be copied and/or imported into shaders, but it's very hard for a user to find this code or formula.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Provide the existing srgb_to_linear and linear_to_srgb functions as built-in functions to godot shaders.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

vec3 linear_to_srgb(vec3 color) {
	// If going to srgb, clamp from 0 to 1.
	color = clamp(color, vec3(0.0), vec3(1.0));
	const vec3 a = vec3(0.055f);
	return mix(
		(vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a,
		12.92f * color.rgb,
		lessThan(color.rgb, vec3(0.0031308f))
	);
}

vec3 srgb_to_linear(vec3 color) {
	return mix(
		pow((color.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)),
		color.rgb * (1.0 / 12.92),
		lessThan(color.rgb, vec3(0.04045))
	);
}

If this enhancement will not be used often, can it be worked around with a few lines of script?

If you know where to look you can find these, but it's a pretty basic feature that shouldn't be this hard to find.

Is there a reason why this should be core and not an add-on in the asset library?

It's already in the core shaders, just needs to be provided to user shaders.

@Calinou
Copy link
Member

Calinou commented Feb 22, 2024

Adding built-in functions to the shader language that have no GLSL counterpart is a difficult proposition. See godotengine/godot#77239 (comment) which was rejected in the past.

@Cammymoop
Copy link
Author

Adding built-in functions to the shader language that have no GLSL counterpart is a difficult proposition. See godotengine/godot#77239 (comment) which was rejected in the past.

I don't disagree with the points made in 77239 but I'm not sure if there's an easy alternative solution like there was for that. Perhaps adding the code as an example into the documentation could help? Doesn't sound like a good approach going forward either, and I'm not sure how someone is supposed to be able to find out how to do this otherwise. (Speaking from experience it's very hard to figure out exactly what the math is for the conversion, even after looking through search results)

The fact that godot will automatically do this for your color uniforms but doesn't let you do it for consts (no hints) makes this feel like a missing feature.

Oh, looks like this also isn't the first time adding extra functions anyway godotengine/godot#43886
documented here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants