From ffe0b869f5c55b1b74d5c9b65a39d0dc81ba3d4e Mon Sep 17 00:00:00 2001 From: Rob Blanckaert Date: Wed, 24 Apr 2024 21:41:18 -0700 Subject: [PATCH] Add LIGHT_VERTEX to fragment shader Adds a new variable to the fragment shader to specify the vertex position used when calculating lighting. --- drivers/gles3/shaders/scene.glsl | 14 ++++++++++++++ drivers/gles3/storage/material_storage.cpp | 2 ++ .../scene_shader_forward_clustered.cpp | 2 ++ .../forward_mobile/scene_shader_forward_mobile.cpp | 2 ++ .../forward_clustered/scene_forward_clustered.glsl | 14 ++++++++++++++ .../forward_mobile/scene_forward_mobile.glsl | 13 +++++++++++++ servers/rendering/shader_types.cpp | 1 + 7 files changed, 48 insertions(+) diff --git a/drivers/gles3/shaders/scene.glsl b/drivers/gles3/shaders/scene.glsl index 36bbca87283..1973eb56c2c 100644 --- a/drivers/gles3/shaders/scene.glsl +++ b/drivers/gles3/shaders/scene.glsl @@ -1511,10 +1511,24 @@ void main() { float alpha_antialiasing_edge = 0.0; vec2 alpha_texture_coordinate = vec2(0.0, 0.0); #endif // ALPHA_ANTIALIASING_EDGE_USED + +#ifdef LIGHT_VERTEX_USED + vec3 light_vertex = vertex; +#endif //LIGHT_VERTEX_USED + { #CODE : FRAGMENT } +#ifdef LIGHT_VERTEX_USED + vertex = light_vertex; +#ifdef USE_MULTIVIEW + view = -normalize(vertex - eye_offset); +#else + view = -normalize(vertex); +#endif //USE_MULTIVIEW +#endif //LIGHT_VERTEX_USED + #ifndef USE_SHADOW_TO_OPACITY #if defined(ALPHA_SCISSOR_USED) diff --git a/drivers/gles3/storage/material_storage.cpp b/drivers/gles3/storage/material_storage.cpp index 23376b4381a..62d22dac4d9 100644 --- a/drivers/gles3/storage/material_storage.cpp +++ b/drivers/gles3/storage/material_storage.cpp @@ -1274,6 +1274,7 @@ MaterialStorage::MaterialStorage() { actions.renames["CUSTOM2"] = "custom2_attrib"; actions.renames["CUSTOM3"] = "custom3_attrib"; actions.renames["OUTPUT_IS_SRGB"] = "SHADER_IS_SRGB"; + actions.renames["LIGHT_VERTEX"] = "light_vertex"; actions.renames["NODE_POSITION_WORLD"] = "model_matrix[3].xyz"; actions.renames["CAMERA_POSITION_WORLD"] = "scene_data.inv_view_matrix[3].xyz"; @@ -1320,6 +1321,7 @@ MaterialStorage::MaterialStorage() { actions.usage_defines["COLOR"] = "#define COLOR_USED\n"; actions.usage_defines["INSTANCE_CUSTOM"] = "#define ENABLE_INSTANCE_CUSTOM\n"; actions.usage_defines["POSITION"] = "#define OVERRIDE_POSITION\n"; + actions.usage_defines["LIGHT_VERTEX"] = "#define LIGHT_VERTEX_USED\n"; actions.usage_defines["ALPHA_SCISSOR_THRESHOLD"] = "#define ALPHA_SCISSOR_USED\n"; actions.usage_defines["ALPHA_HASH_SCALE"] = "#define ALPHA_HASH_USED\n"; diff --git a/servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.cpp b/servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.cpp index 209fabeddfa..8d865ba4405 100644 --- a/servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.cpp +++ b/servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.cpp @@ -624,6 +624,7 @@ void SceneShaderForwardClustered::init(const String p_defines) { actions.renames["CUSTOM2"] = "custom2_attrib"; actions.renames["CUSTOM3"] = "custom3_attrib"; actions.renames["OUTPUT_IS_SRGB"] = "SHADER_IS_SRGB"; + actions.renames["LIGHT_VERTEX"] = "light_vertex"; actions.renames["NODE_POSITION_WORLD"] = "read_model_matrix[3].xyz"; actions.renames["CAMERA_POSITION_WORLD"] = "scene_data.inv_view_matrix[3].xyz"; @@ -670,6 +671,7 @@ void SceneShaderForwardClustered::init(const String p_defines) { actions.usage_defines["COLOR"] = "#define COLOR_USED\n"; actions.usage_defines["INSTANCE_CUSTOM"] = "#define ENABLE_INSTANCE_CUSTOM\n"; actions.usage_defines["POSITION"] = "#define OVERRIDE_POSITION\n"; + actions.usage_defines["LIGHT_VERTEX"] = "#define LIGHT_VERTEX_USED\n"; actions.usage_defines["ALPHA_SCISSOR_THRESHOLD"] = "#define ALPHA_SCISSOR_USED\n"; actions.usage_defines["ALPHA_HASH_SCALE"] = "#define ALPHA_HASH_USED\n"; diff --git a/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp b/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp index a2f112669c0..0810f567cbd 100644 --- a/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp +++ b/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp @@ -528,6 +528,7 @@ void SceneShaderForwardMobile::init(const String p_defines) { actions.renames["CUSTOM2"] = "custom2_attrib"; actions.renames["CUSTOM3"] = "custom3_attrib"; actions.renames["OUTPUT_IS_SRGB"] = "SHADER_IS_SRGB"; + actions.renames["LIGHT_VERTEX"] = "light_vertex"; actions.renames["NODE_POSITION_WORLD"] = "read_model_matrix[3].xyz"; actions.renames["CAMERA_POSITION_WORLD"] = "scene_data.inv_view_matrix[3].xyz"; @@ -574,6 +575,7 @@ void SceneShaderForwardMobile::init(const String p_defines) { actions.usage_defines["COLOR"] = "#define COLOR_USED\n"; actions.usage_defines["INSTANCE_CUSTOM"] = "#define ENABLE_INSTANCE_CUSTOM\n"; actions.usage_defines["POSITION"] = "#define OVERRIDE_POSITION\n"; + actions.usage_defines["LIGHT_VERTEX"] = "#define LIGHT_VERTEX\n"; actions.usage_defines["ALPHA_SCISSOR_THRESHOLD"] = "#define ALPHA_SCISSOR_USED\n"; actions.usage_defines["ALPHA_HASH_SCALE"] = "#define ALPHA_HASH_USED\n"; diff --git a/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl b/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl index 359d7799e55..59f1b1fd94c 100644 --- a/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl +++ b/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl @@ -1022,6 +1022,11 @@ void fragment_shader(in SceneData scene_data) { inv_view_matrix[1][3] = 0.0; inv_view_matrix[2][3] = 0.0; #endif + +#ifdef LIGHT_VERTEX_USED + vec3 light_vertex = vertex; +#endif //LIGHT_VERTEX_USED + mat4 read_view_matrix = scene_data.view_matrix; vec2 read_viewport_size = scene_data.viewport_size; { @@ -1032,6 +1037,15 @@ void fragment_shader(in SceneData scene_data) { transmittance_color.a *= sss_strength; #endif +#ifdef LIGHT_VERTEX_USED + vertex = light_vertex; +#ifdef USE_MULTIVIEW + view = -normalize(vertex - eye_offset); +#else + view = -normalize(vertex); +#endif //USE_MULTIVIEW +#endif //LIGHT_VERTEX_USED + #ifndef USE_SHADOW_TO_OPACITY #ifdef ALPHA_SCISSOR_USED diff --git a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl index c26313092bb..f0a51418568 100644 --- a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl +++ b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl @@ -830,6 +830,10 @@ void main() { inv_view_matrix[2][3] = 0.0; #endif +#ifdef LIGHT_VERTEX_USED + vec3 light_vertex = vertex; +#endif //LIGHT_VERTEX_USED + mat4 read_view_matrix = scene_data.view_matrix; vec2 read_viewport_size = scene_data.viewport_size; @@ -837,6 +841,15 @@ void main() { #CODE : FRAGMENT } +#ifdef LIGHT_VERTEX_USED + vertex = light_vertex; +#ifdef USE_MULTIVIEW + view = -normalize(vertex - eye_offset); +#else + view = -normalize(vertex); +#endif //USE_MULTIVIEW +#endif //LIGHT_VERTEX_USED + #ifdef LIGHT_TRANSMITTANCE_USED #ifdef SSS_MODE_SKIN transmittance_color.a = sss_strength; diff --git a/servers/rendering/shader_types.cpp b/servers/rendering/shader_types.cpp index aff874f452a..af51083dc33 100644 --- a/servers/rendering/shader_types.cpp +++ b/servers/rendering/shader_types.cpp @@ -111,6 +111,7 @@ ShaderTypes::ShaderTypes() { shader_modes[RS::SHADER_SPATIAL].functions["vertex"].built_ins["EYE_OFFSET"] = constt(ShaderLanguage::TYPE_VEC3); shader_modes[RS::SHADER_SPATIAL].functions["fragment"].built_ins["VERTEX"] = constt(ShaderLanguage::TYPE_VEC3); + shader_modes[RS::SHADER_SPATIAL].functions["fragment"].built_ins["LIGHT_VERTEX"] = ShaderLanguage::TYPE_VEC3; shader_modes[RS::SHADER_SPATIAL].functions["fragment"].built_ins["FRAGCOORD"] = constt(ShaderLanguage::TYPE_VEC4); shader_modes[RS::SHADER_SPATIAL].functions["fragment"].built_ins["FRONT_FACING"] = constt(ShaderLanguage::TYPE_BOOL); shader_modes[RS::SHADER_SPATIAL].functions["fragment"].built_ins["NORMAL"] = ShaderLanguage::TYPE_VEC3;