diff --git a/data/shaders/sp_grass.frag b/data/shaders/sp_grass.frag index 688767e06..207ecf5b1 100644 --- a/data/shaders/sp_grass.frag +++ b/data/shaders/sp_grass.frag @@ -1,19 +1,3 @@ -#ifdef Use_Bindless_Texture -flat in sampler2D tex_layer_0; -flat in sampler2D tex_layer_2; -#else -// spm layer 1 texture -uniform sampler2D tex_layer_0; -// gloss map -uniform sampler2D tex_layer_2; -#endif - -#ifdef Use_Array_Texture -uniform sampler2DArray tex_array; -flat in float array_0; -flat in float array_2; -#endif - flat in float hue_change; in vec3 normal; @@ -25,15 +9,11 @@ layout(location = 2) out vec2 o_gloss_map; #stk_include "utils/encode_normal.frag" #stk_include "utils/rgb_conversion.frag" +#stk_include "utils/sp_texture_sampling.frag" void main(void) { -#ifdef Use_Array_Texture - vec4 col = texture(tex_array, vec3(uv, array_0)); -#else - vec4 col = texture(tex_layer_0, uv); -#endif - + vec4 col = sampleTextureSlot0(uv); if (col.a < 0.5) { discard; @@ -57,16 +37,8 @@ void main(void) #endif o_diffuse_color = vec4(final_color, 1.0); -#if defined(Advanced_Lighting_Enabled) -#ifdef Use_Array_Texture - vec4 layer_2 = texture(tex_array, vec3(uv, array_2)); -#else - vec4 layer_2 = texture(tex_layer_2, uv); -#endif - + vec4 layer_2 = sampleTextureSlot2(uv); o_normal_depth.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; o_normal_depth.z = layer_2.x; o_gloss_map = 0.1 * layer_2.yz; - -#endif } diff --git a/data/shaders/sp_normal_map.frag b/data/shaders/sp_normal_map.frag index 9412f9a6d..c3bb12eef 100644 --- a/data/shaders/sp_normal_map.frag +++ b/data/shaders/sp_normal_map.frag @@ -1,24 +1,3 @@ -#ifdef Use_Bindless_Texture -flat in sampler2D tex_layer_0; -flat in sampler2D tex_layer_2; -flat in sampler2D tex_layer_3; -#else -// spm layer 1 texture -uniform sampler2D tex_layer_0; -// gloss map -uniform sampler2D tex_layer_2; -// normal map -uniform sampler2D tex_layer_3; -#endif - - -#ifdef Use_Array_Texture -uniform sampler2DArray tex_array; -flat in float array_0; -flat in float array_2; -flat in float array_3; -#endif - flat in float hue_change; in vec4 color; @@ -33,15 +12,11 @@ layout(location = 2) out vec2 o_gloss_map; #stk_include "utils/encode_normal.frag" #stk_include "utils/rgb_conversion.frag" +#stk_include "utils/sp_texture_sampling.frag" void main() { -#ifdef Use_Array_Texture - vec4 col = texture(tex_array, vec3(uv, array_0)); -#else - vec4 col = texture(tex_layer_0, uv); -#endif - + vec4 col = sampleTextureSlot0(uv); if (hue_change > 0.0) { float mask = col.a; @@ -68,13 +43,7 @@ void main() #endif o_diffuse_color = vec4(final_color, 1.0); -#if defined(Advanced_Lighting_Enabled) -#ifdef Use_Array_Texture - vec4 layer_3 = texture(tex_array, vec3(uv, array_3)); -#else - vec4 layer_3 = texture(tex_layer_3, uv); -#endif - + vec4 layer_3 = sampleTextureSlot3(uv); vec3 tangent_space_normal = 2.0 * layer_3.xyz - 1.0; vec3 frag_tangent = normalize(tangent); vec3 frag_bitangent = normalize(bitangent); @@ -82,15 +51,8 @@ void main() mat3 t_b_n = mat3(frag_tangent, frag_bitangent, frag_normal); vec3 world_normal = t_b_n * tangent_space_normal; -#ifdef Use_Array_Texture - vec4 layer_2 = texture(tex_array, vec3(uv, array_2)); -#else - vec4 layer_2 = texture(tex_layer_2, uv); -#endif - + vec4 layer_2 = sampleTextureSlot2(uv); o_normal_depth.xy = 0.5 * EncodeNormal(normalize(world_normal)) + 0.5; o_normal_depth.z = layer_2.x; o_gloss_map = layer_2.yz; - -#endif } diff --git a/data/shaders/sp_solid.frag b/data/shaders/sp_solid.frag index a828acd6e..53bac0125 100644 --- a/data/shaders/sp_solid.frag +++ b/data/shaders/sp_solid.frag @@ -1,19 +1,3 @@ -#ifdef Use_Bindless_Texture -flat in sampler2D tex_layer_0; -flat in sampler2D tex_layer_2; -#else -// spm layer 1 texture -uniform sampler2D tex_layer_0; -// gloss map -uniform sampler2D tex_layer_2; -#endif - -#ifdef Use_Array_Texture -uniform sampler2DArray tex_array; -flat in float array_0; -flat in float array_2; -#endif - flat in float hue_change; in vec4 color; @@ -26,15 +10,11 @@ layout(location = 2) out vec2 o_gloss_map; #stk_include "utils/encode_normal.frag" #stk_include "utils/rgb_conversion.frag" +#stk_include "utils/sp_texture_sampling.frag" void main(void) { -#ifdef Use_Array_Texture - vec4 col = texture(tex_array, vec3(uv, array_0)); -#else - vec4 col = texture(tex_layer_0, uv); -#endif - + vec4 col = sampleTextureSlot0(uv); if (hue_change > 0.0) { float mask = col.a; @@ -61,16 +41,8 @@ void main(void) #endif o_diffuse_color = vec4(final_color, 1.0); -#if defined(Advanced_Lighting_Enabled) -#ifdef Use_Array_Texture - vec4 layer_2 = texture(tex_array, vec3(uv, array_2)); -#else - vec4 layer_2 = texture(tex_layer_2, uv); -#endif - + vec4 layer_2 = sampleTextureSlot2(uv); o_normal_depth.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; o_normal_depth.z = layer_2.x; o_gloss_map = layer_2.yz; - -#endif } diff --git a/data/shaders/utils/sp_texture_sampling.frag b/data/shaders/utils/sp_texture_sampling.frag new file mode 100644 index 000000000..e85adeb7d --- /dev/null +++ b/data/shaders/utils/sp_texture_sampling.frag @@ -0,0 +1,78 @@ +// Wrapper to allow easy sampling for material texture slots +#if defined(Use_Array_Texture) +flat in float array_0; +flat in float array_1; +flat in float array_2; +flat in float array_3; +flat in float array_4; +flat in float array_5; +uniform sampler2DArray tex_array; +#elif defined(Use_Bindless_Texture) +flat in sampler2D tex_layer_0; +flat in sampler2D tex_layer_1; +flat in sampler2D tex_layer_2; +flat in sampler2D tex_layer_3; +flat in sampler2D tex_layer_4; +flat in sampler2D tex_layer_5; +#else +uniform sampler2D tex_layer_0; +uniform sampler2D tex_layer_1; +uniform sampler2D tex_layer_2; +uniform sampler2D tex_layer_3; +uniform sampler2D tex_layer_4; +uniform sampler2D tex_layer_5; +#endif + +vec4 sampleTextureSlot0(vec2 uv) +{ +#ifdef Use_Array_Texture + return texture(tex_array, vec3(uv, array_0)); +#else + return texture(tex_layer_0, uv); +#endif +} + +vec4 sampleTextureSlot1(vec2 uv) +{ +#ifdef Use_Array_Texture + return texture(tex_array, vec3(uv, array_1)); +#else + return texture(tex_layer_1, uv); +#endif +} + +vec4 sampleTextureSlot2(vec2 uv) +{ +#ifdef Use_Array_Texture + return texture(tex_array, vec3(uv, array_2)); +#else + return texture(tex_layer_2, uv); +#endif +} + +vec4 sampleTextureSlot3(vec2 uv) +{ +#ifdef Use_Array_Texture + return texture(tex_array, vec3(uv, array_3)); +#else + return texture(tex_layer_3, uv); +#endif +} + +vec4 sampleTextureSlot4(vec2 uv) +{ +#ifdef Use_Array_Texture + return texture(tex_array, vec3(uv, array_4)); +#else + return texture(tex_layer_4, uv); +#endif +} + +vec4 sampleTextureSlot5(vec2 uv) +{ +#ifdef Use_Array_Texture + return texture(tex_array, vec3(uv, array_5)); +#else + return texture(tex_layer_5, uv); +#endif +}