Skip to content

Commit

Permalink
Don't add unnecessary uniforms in BaseMaterial3D generated shader code
Browse files Browse the repository at this point in the history
This avoids adding UV2, point size or AO-related uniforms when they aren't used
(e.g. because ORMMaterial3D doesn't read the AO texture).
  • Loading branch information
Calinou committed Dec 10, 2024
1 parent a372214 commit e5c5fee
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions scene/resources/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,9 @@ uniform ivec2 albedo_texture_size;
)";
}

code += "uniform float point_size : hint_range(0.1, 128.0, 0.1);\n";
if (flags[FLAG_USE_POINT_SIZE]) {
code += "uniform float point_size : hint_range(0.1, 128.0, 0.1);\n";
}

if (!orm) {
code += vformat(R"(
Expand Down Expand Up @@ -1040,12 +1042,19 @@ uniform sampler2D texture_flowmap : hint_anisotropy, %s;
texfilter_str);
}
if (features[FEATURE_AMBIENT_OCCLUSION]) {
code += vformat(R"(
if (!orm) {
code += vformat(R"(
uniform sampler2D texture_ambient_occlusion : hint_default_white, %s;
uniform vec4 ao_texture_channel;
uniform float ao_light_affect : hint_range(0.0, 1.0, 0.01);
)",
texfilter_str);
texfilter_str);
} else {
// `texture_ambient_occlusion` and `ao_texture_channel` are not used in ORM mode.
code += R"(
uniform float ao_light_affect : hint_range(0.0, 1.0, 0.01);
)";
}
}

if (features[FEATURE_DETAIL]) {
Expand Down Expand Up @@ -1115,10 +1124,13 @@ varying vec3 uv2_power_normal;
code += R"(
uniform vec3 uv1_scale;
uniform vec3 uv1_offset;
uniform vec3 uv2_scale;
uniform vec3 uv2_offset;
)";

if (flags[FLAG_UV2_USE_TRIPLANAR] || (features[FEATURE_DETAIL] && detail_uv == DETAIL_UV_2) || (features[FEATURE_AMBIENT_OCCLUSION] && flags[FLAG_AO_ON_UV2]) || (features[FEATURE_EMISSION] && flags[FLAG_EMISSION_ON_UV2])) {
code += "uniform vec3 uv2_scale;\n";
code += "uniform vec3 uv2_offset;\n";
}

// Generate vertex shader.
code += R"(
void vertex() {)";
Expand Down

0 comments on commit e5c5fee

Please sign in to comment.