How can I pass and get UV from a function?
Asked Answered
G

1

0

Ive gota fragment shader.
Ideally Id like to have a function like this:

void draw_normal_pixel() {  
	vec2 base_uv = UV;
	vec4 albedo_tex = texture(texture_albedo,base_uv);
	albedo_tex *= COLOR;
	ALBEDO = albedo.rgb * albedo_tex.rgb;
	SPECULAR = specular;
	ALPHA = albedo.a * albedo_tex.a;
	ALPHA_SCISSOR_THRESHOLD = .3;
}

because I have a ton of if else statements and they all call the above code.
Without the function I have a lot of code duplication.

But outside of the fragment function UV doesnt mean anything. So if I write that function I get
"unknown identifier in expression: "UV"

Im also worried that the function may add extra overhead and slow the shader down.
What is the most optimal way to eliminate the code duplication?

Godiva answered 26/1, 2023 at 18:56 Comment(0)
L
0

Godot 3 or godot 4? It's always good to include this info, so if nothing else people can link to the correct documentation.

It's been a while since I've used a custom function in a shader but I think you should be able to have a function return value in there something along the lines of:

vec4 myColorFunc(vec3 inputOfVec3Type, int inputOfIntType) {
    vec4 vectorSampleValue;
    vectorSampleValue = some logic here
    return vectorSampleValue
}

void fragment() {
    ALBEDO = myColorFunc.rgb; // ALBEDO is a vec3
}
Lacasse answered 27/1, 2023 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.