Skip to content

Commit

Permalink
Support for Mobile Foward and enum rename
Browse files Browse the repository at this point in the history
  • Loading branch information
guerro323 committed Dec 13, 2023
1 parent fa18762 commit 2a7a454
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 35 deletions.
6 changes: 3 additions & 3 deletions doc/classes/BaseMaterial3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -731,11 +731,11 @@
<constant name="SPECULAR_DISABLED" value="2" enum="SpecularMode">
No specular blob. This is slightly faster to render than other specular modes.
</constant>
<constant name="SPECULAR_OCCLUSION" value="0" enum="SpecularOcclusionMode">
<constant name="SPECULAR_OCCLUSION_DEFAULT" value="0" enum="SpecularOcclusionMode">
Default specular occlusion, reduce the specular intensity by current [LightmapGI] pixel brightness.
</constant>
<constant name="SPECULAR_OCCLUSION_CORRECT" value="1" enum="SpecularOcclusionMode">
Specular occlusion that gives a more realistic appearence by keeping more energy from mixing metallic and roughness values.
<constant name="SPECULAR_OCCLUSION_CONSERVATIVE" value="1" enum="SpecularOcclusionMode">
Specular occlusion that conserve most of its energy from mixing metallic and roughness values, resulting in more realistic surfaces.
</constant>
<constant name="SPECULAR_OCCLUSION_DISABLED" value="2" enum="SpecularOcclusionMode">
No specular occlusion.
Expand Down
12 changes: 6 additions & 6 deletions scene/resources/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,11 +754,11 @@ void BaseMaterial3D::_update_shader() {
}

switch (specular_occlusion_mode) {
case SPECULAR_OCCLUSION:
case SPECULAR_OCCLUSION_DEFAULT:
code += ",specular_occlusion";
break;
case SPECULAR_OCCLUSION_CORRECT:
code += ",specular_occlusion_correct";
case SPECULAR_OCCLUSION_CONSERVATIVE:
code += ",specular_occlusion_conservative";
break;
case SPECULAR_OCCLUSION_DISABLED:
break;
Expand Down Expand Up @@ -2770,7 +2770,7 @@ void BaseMaterial3D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::INT, "shading_mode", PROPERTY_HINT_ENUM, "Unshaded,Per-Pixel,Per-Vertex"), "set_shading_mode", "get_shading_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "diffuse_mode", PROPERTY_HINT_ENUM, "Burley,Lambert,Lambert Wrap,Toon"), "set_diffuse_mode", "get_diffuse_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "specular_mode", PROPERTY_HINT_ENUM, "SchlickGGX,Toon,Disabled"), "set_specular_mode", "get_specular_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "specular_occlusion_mode", PROPERTY_HINT_ENUM, "Default,Correct,Disabled"), "set_specular_occlusion_mode", "get_specular_occlusion_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "specular_occlusion_mode", PROPERTY_HINT_ENUM, "Default,Conservative,Disabled"), "set_specular_occlusion_mode", "get_specular_occlusion_mode");
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "disable_ambient_light"), "set_flag", "get_flag", FLAG_DISABLE_AMBIENT_LIGHT);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "disable_fog"), "set_flag", "get_flag", FLAG_DISABLE_FOG);

Expand Down Expand Up @@ -3037,8 +3037,8 @@ void BaseMaterial3D::_bind_methods() {
BIND_ENUM_CONSTANT(SPECULAR_TOON);
BIND_ENUM_CONSTANT(SPECULAR_DISABLED);

BIND_ENUM_CONSTANT(SPECULAR_OCCLUSION);
BIND_ENUM_CONSTANT(SPECULAR_OCCLUSION_CORRECT);
BIND_ENUM_CONSTANT(SPECULAR_OCCLUSION_DEFAULT);
BIND_ENUM_CONSTANT(SPECULAR_OCCLUSION_CONSERVATIVE);
BIND_ENUM_CONSTANT(SPECULAR_OCCLUSION_DISABLED);

BIND_ENUM_CONSTANT(BILLBOARD_DISABLED);
Expand Down
6 changes: 3 additions & 3 deletions scene/resources/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ class BaseMaterial3D : public Material {
};

enum SpecularOcclusionMode {
SPECULAR_OCCLUSION,
SPECULAR_OCCLUSION_CORRECT,
SPECULAR_OCCLUSION_DEFAULT,
SPECULAR_OCCLUSION_CONSERVATIVE,
SPECULAR_OCCLUSION_DISABLED,
SPECULAR_OCCLUSION_MAX
};
Expand Down Expand Up @@ -545,7 +545,7 @@ class BaseMaterial3D : public Material {
CullMode cull_mode = CULL_BACK;
bool flags[FLAG_MAX] = {};
SpecularMode specular_mode = SPECULAR_SCHLICK_GGX;
SpecularOcclusionMode specular_occlusion_mode = SPECULAR_OCCLUSION;
SpecularOcclusionMode specular_occlusion_mode = SPECULAR_OCCLUSION_DEFAULT;
DiffuseMode diffuse_mode = DIFFUSE_BURLEY;
BillboardMode billboard_mode;
EmissionOperator emission_op = EMISSION_OP_ADD;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ void SceneShaderForwardClustered::init(const String p_defines) {
actions.render_mode_defines["unshaded"] = "#define MODE_UNSHADED\n";
actions.render_mode_defines["debug_shadow_splits"] = "#define DEBUG_DRAW_PSSM_SPLITS\n";
actions.render_mode_defines["fog_disabled"] = "#define FOG_DISABLED\n";
actions.render_mode_defines["specular_occlusion_correct"] = "#define SPECULAR_OCCLUSION_CORRECT\n";
actions.render_mode_defines["specular_occlusion_conservative"] = "#define SPECULAR_OCCLUSION_CONSERVATIVE\n";
actions.render_mode_defines["specular_occlusion"] = "#define SPECULAR_OCCLUSION\n";

actions.base_texture_binding_index = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,8 @@ void SceneShaderForwardMobile::init(const String p_defines) {
actions.render_mode_defines["unshaded"] = "#define MODE_UNSHADED\n";
actions.render_mode_defines["debug_shadow_splits"] = "#define DEBUG_DRAW_PSSM_SPLITS\n";
actions.render_mode_defines["fog_disabled"] = "#define FOG_DISABLED\n";
actions.render_mode_defines["specular_occlusion_conservative"] = "#define SPECULAR_OCCLUSION_CONSERVATIVE\n";
actions.render_mode_defines["specular_occlusion"] = "#define SPECULAR_OCCLUSION\n";

actions.base_texture_binding_index = 1;
actions.texture_layout_set = RenderForwardMobile::MATERIAL_UNIFORM_SET;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1359,30 +1359,30 @@ void fragment_shader(in SceneData scene_data) {
const float c5 = 0.247708;

vec3 c = (c1 * lightmap_captures.data[index].sh[8].rgb * (wnormal.x * wnormal.x - wnormal.y * wnormal.y) +
c3 * lightmap_captures.data[index].sh[6].rgb * wnormal.z * wnormal.z +
c4 * lightmap_captures.data[index].sh[0].rgb -
c5 * lightmap_captures.data[index].sh[6].rgb +
2.0 * c1 * lightmap_captures.data[index].sh[4].rgb * wnormal.x * wnormal.y +
2.0 * c1 * lightmap_captures.data[index].sh[7].rgb * wnormal.x * wnormal.z +
2.0 * c1 * lightmap_captures.data[index].sh[5].rgb * wnormal.y * wnormal.z +
2.0 * c2 * lightmap_captures.data[index].sh[3].rgb * wnormal.x +
2.0 * c2 * lightmap_captures.data[index].sh[1].rgb * wnormal.y +
2.0 * c2 * lightmap_captures.data[index].sh[2].rgb * wnormal.z) *
c3 * lightmap_captures.data[index].sh[6].rgb * wnormal.z * wnormal.z +
c4 * lightmap_captures.data[index].sh[0].rgb -
c5 * lightmap_captures.data[index].sh[6].rgb +
2.0 * c1 * lightmap_captures.data[index].sh[4].rgb * wnormal.x * wnormal.y +
2.0 * c1 * lightmap_captures.data[index].sh[7].rgb * wnormal.x * wnormal.z +
2.0 * c1 * lightmap_captures.data[index].sh[5].rgb * wnormal.y * wnormal.z +
2.0 * c2 * lightmap_captures.data[index].sh[3].rgb * wnormal.x +
2.0 * c2 * lightmap_captures.data[index].sh[1].rgb * wnormal.y +
2.0 * c2 * lightmap_captures.data[index].sh[2].rgb * wnormal.z) *
scene_data.emissive_exposure_normalization;

#if defined(SPECULAR_OCCLUSION) || defined(SPECULAR_OCCLUSION_CORRECT)
#if defined(SPECULAR_OCCLUSION) || defined(SPECULAR_OCCLUSION_CONSERVATIVE)
float specular_occlusion = (c.r * 0.3 + c.g * 0.59 + c.b * 0.11) * 2.0; // brightness from lightmap color
specular_occlusion = min(1.0, specular_occlusion * scene_data.emissive_exposure_normalization);

#if defined(SPECULAR_OCCLUSION_CORRECT)
#if defined(SPECULAR_OCCLUSION_CONSERVATIVE)
float reflective_f = (1.0 - roughness) * metallic;
// 10.0 is a number picked by hand, it gives the intended effect in most scenarios
// (low enough for occlusion, high enough for reaction to lights and shadows)
specular_occlusion = max(min(reflective_f * specular_occlusion * 10.0, 1.0), specular_occlusion);
#endif // defined(SPECULAR_OCCLUSION_CORRECT)
specular_occlusion = max(min(reflective_f * specular_occlusion * 10.0, 1.0), specular_occlusion);
#endif // defined(SPECULAR_OCCLUSION_CONSERVATIVE)

specular_light *= specular_occlusion;
#endif // defined(SPECULAR_OCCLUSION) || defined(SPECULAR_OCCLUSION_CORRECT)
#endif // defined(SPECULAR_OCCLUSION) || defined(SPECULAR_OCCLUSION_CONSERVATIVE)
ambient_light += c;

} else if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) { // has actual lightmap
Expand Down Expand Up @@ -1415,24 +1415,24 @@ void fragment_shader(in SceneData scene_data) {
}

} else {
#if defined(SPECULAR_OCCLUSION) || defined(SPECULAR_OCCLUSION_CORRECT)
#if defined(SPECULAR_OCCLUSION) || defined(SPECULAR_OCCLUSION_CONSERVATIVE)
vec3 c = textureLod(sampler2DArray(lightmap_textures[ofs], DEFAULT_SAMPLER_LINEAR_CLAMP), uvw, 0.0).rgb * lightmaps.data[ofs].exposure_normalization;

float specular_occlusion = (c.r * 0.3 + c.g * 0.59 + c.b * 0.11) * 2.0; // brightness from lightmap color
specular_occlusion = min(1.0, specular_occlusion * lightmaps.data[ofs].exposure_normalization);

#if defined(SPECULAR_OCCLUSION_CORRECT)
#if defined(SPECULAR_OCCLUSION_CONSERVATIVE)
float reflective_f = (1.0 - roughness) * metallic;
// 10.0 is a number picked by hand, it gives the intended effect in most scenarios
// (low enough for occlusion, high enough for reaction to lights and shadows)
specular_occlusion = max(min(reflective_f * specular_occlusion * 10.0, 1.0), specular_occlusion);
#endif // defined(SPECULAR_OCCLUSION_CORRECT)
specular_occlusion = max(min(reflective_f * specular_occlusion * 10.0, 1.0), specular_occlusion);
#endif // defined(SPECULAR_OCCLUSION_CONSERVATIVE)

specular_light *= specular_occlusion;
ambient_light += c;
#else // defined(SPECULAR_OCCLUSION) || defined(SPECULAR_OCCLUSION_CORRECT)
#else // defined(SPECULAR_OCCLUSION) || defined(SPECULAR_OCCLUSION_CONSERVATIVE)
ambient_light += textureLod(sampler2DArray(lightmap_textures[ofs], DEFAULT_SAMPLER_LINEAR_CLAMP), uvw, 0.0).rgb * lightmaps.data[ofs].exposure_normalization;
#endif // defined(SPECULAR_OCCLUSION) || defined(SPECULAR_OCCLUSION_CORRECT)
#endif // defined(SPECULAR_OCCLUSION) || defined(SPECULAR_OCCLUSION_CONSERVATIVE)
}
}
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ void main() {
const float c3 = 0.743125;
const float c4 = 0.886227;
const float c5 = 0.247708;
ambient_light += (c1 * lightmap_captures.data[index].sh[8].rgb * (wnormal.x * wnormal.x - wnormal.y * wnormal.y) +
vec3 c = (c1 * lightmap_captures.data[index].sh[8].rgb * (wnormal.x * wnormal.x - wnormal.y * wnormal.y) +
c3 * lightmap_captures.data[index].sh[6].rgb * wnormal.z * wnormal.z +
c4 * lightmap_captures.data[index].sh[0].rgb -
c5 * lightmap_captures.data[index].sh[6].rgb +
Expand All @@ -1150,6 +1150,21 @@ void main() {
2.0 * c2 * lightmap_captures.data[index].sh[2].rgb * wnormal.z) *
scene_data.emissive_exposure_normalization;

#if defined(SPECULAR_OCCLUSION) || defined(SPECULAR_OCCLUSION_CONSERVATIVE)
float specular_occlusion = (c.r * 0.3 + c.g * 0.59 + c.b * 0.11) * 2.0; // brightness from lightmap color
specular_occlusion = min(1.0, specular_occlusion * scene_data.emissive_exposure_normalization);

#if defined(SPECULAR_OCCLUSION_CONSERVATIVE)
float reflective_f = (1.0 - roughness) * metallic;
// 10.0 is a number picked by hand, it gives the intended effect in most scenarios
// (low enough for occlusion, high enough for reaction to lights and shadows)
specular_occlusion = max(min(reflective_f * specular_occlusion * 10.0, 1.0), specular_occlusion);
#endif // defined(SPECULAR_OCCLUSION_CONSERVATIVE)

specular_light *= specular_occlusion;
#endif // defined(SPECULAR_OCCLUSION) || defined(SPECULAR_OCCLUSION_CONSERVATIVE)
ambient_light += c;

} else if (bool(instances.data[draw_call.instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) { // has actual lightmap
bool uses_sh = bool(instances.data[draw_call.instance_index].flags & INSTANCE_FLAGS_USE_SH_LIGHTMAP);
uint ofs = instances.data[draw_call.instance_index].gi_offset & 0xFFFF;
Expand Down Expand Up @@ -1180,7 +1195,24 @@ void main() {
}

} else {
#if defined(SPECULAR_OCCLUSION) || defined(SPECULAR_OCCLUSION_CONSERVATIVE)
vec3 c = textureLod(sampler2DArray(lightmap_textures[ofs], DEFAULT_SAMPLER_LINEAR_CLAMP), uvw, 0.0).rgb * lightmaps.data[ofs].exposure_normalization;

float specular_occlusion = (c.r * 0.3 + c.g * 0.59 + c.b * 0.11) * 2.0; // brightness from lightmap color
specular_occlusion = min(1.0, specular_occlusion * lightmaps.data[ofs].exposure_normalization);

#if defined(SPECULAR_OCCLUSION_CONSERVATIVE)
float reflective_f = (1.0 - roughness) * metallic;
// 10.0 is a number picked by hand, it gives the intended effect in most scenarios
// (low enough for occlusion, high enough for reaction to lights and shadows)
specular_occlusion = max(min(reflective_f * specular_occlusion * 10.0, 1.0), specular_occlusion);
#endif // defined(SPECULAR_OCCLUSION_CONSERVATIVE)

specular_light *= specular_occlusion;
ambient_light += c;
#else // defined(SPECULAR_OCCLUSION) || defined(SPECULAR_OCCLUSION_CONSERVATIVE)
ambient_light += textureLod(sampler2DArray(lightmap_textures[ofs], DEFAULT_SAMPLER_LINEAR_CLAMP), uvw, 0.0).rgb * lightmaps.data[ofs].exposure_normalization;
#endif // defined(SPECULAR_OCCLUSION) || defined(SPECULAR_OCCLUSION_CONSERVATIVE)
}
}

Expand Down
2 changes: 1 addition & 1 deletion servers/rendering/shader_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ ShaderTypes::ShaderTypes() {
shader_modes[RS::SHADER_SPATIAL].modes.push_back({ PNAME("debug_shadow_splits") });
shader_modes[RS::SHADER_SPATIAL].modes.push_back({ PNAME("fog_disabled") });
shader_modes[RS::SHADER_SPATIAL].modes.push_back({ PNAME("specular_occlusion") });
shader_modes[RS::SHADER_SPATIAL].modes.push_back({ PNAME("specular_occlusion_correct") });
shader_modes[RS::SHADER_SPATIAL].modes.push_back({ PNAME("specular_occlusion_conservative") });
}

/************ CANVAS ITEM **************************/
Expand Down

0 comments on commit 2a7a454

Please sign in to comment.