godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.07k stars 69 forks source link

Add the `range_lerp()` function to the shading language #5112

Open fire-forge opened 1 year ago

fire-forge commented 1 year ago

Describe the project you are working on

A wallpaper generator app that uses shaders to create wallpaper images.

Describe the problem or limitation you are having in your project

range_lerp() is a handy function that I use frequently in GDScript, but it is absent from the shading language.

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

Add the range_lerp() function from GDScript to the shading language.

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

This is the range_lerp() function adapted from Godot's source code into the format used for shaders:

float range_lerp(float value, float istart, float istop, float ostart, float ostop) { 
         return ostart + (ostop - ostart) * ((value - istart) / (istop - istart)); 
 }

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

In 3.x, it can be worked around by copy-pasting the function above into shaders as needed, which is what I have been doing.

In 4.0, a shader includes can be used to share this function between multiple shader files.

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

It is a simple function that would be convenient to have built-in.

Calinou commented 1 year ago

On paper, this sounds good to implement. My only concern is that this function is not part of standard GLSL, unlike all the other built-in shader functions currently supported (to my knowledge).

fire-forge commented 1 year ago

@Calinou I didn't know all of the shader functions are included in GLSL. The question now would be if we should allow adding functions to Godot's shading language that are not included in GLSL.

clayjohn commented 1 year ago

@Calinou I didn't know all of the shader functions are included in GLSL. The question now would be if we should allow adding functions to Godot's shading language that are not included in GLSL.

In Godot 4.0 we added the ability to expose custom functions to the Godot Shading Language. So far we only using it for emit_subparticle() in Particles shaders, but we should definitely start adding some helpful higher-level shader functions.

In my opinion, we should create a glsl.inc file called something like shader_language_includes.glsl.inc that is included in all shader files that exposes all of our custom functions like this

On the subject of range_lerp(), it sounds like a good addition to me. It is used extensively in shader programming (although I know it is remap())

JoNax97 commented 1 year ago

I agree with clayjohn that remap is a preferable name. In fact, when starting with GDScript I looked for that functionality and couldn't find it. There's no way in hell I could have found it by the name of range_lerp :/

fire-forge commented 1 year ago

I agree with clayjohn that remap is a preferable name. In fact, when starting with GDScript I looked for that functionality and couldn't find it. There's no way in hell I could have found it by the name of range_lerp :/

I had the same problem when I first started using Godot too. I assumed there wasn't a remap function until I stumbled across range_lerp when looking around in the documentation.

If we do add the range_lerp function to shaders as remap, we should rename the GDScript/C#/C++ function too.