Merge pull request #31202 from azagaya/light-data

Create shadow_vec for altering shadow computation
This commit is contained in:
Rémi Verschelde 2019-09-19 20:03:04 +02:00 committed by GitHub
commit 28265fb526
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 17 deletions

View file

@ -948,6 +948,7 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() {
actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT_UV"] = "light_uv"; actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT_UV"] = "light_uv";
actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT"] = "light"; actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT"] = "light";
actions[VS::SHADER_CANVAS_ITEM].renames["SHADOW_COLOR"] = "shadow_color"; actions[VS::SHADER_CANVAS_ITEM].renames["SHADOW_COLOR"] = "shadow_color";
actions[VS::SHADER_CANVAS_ITEM].renames["SHADOW_VEC"] = "shadow_vec";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["COLOR"] = "#define COLOR_USED\n"; actions[VS::SHADER_CANVAS_ITEM].usage_defines["COLOR"] = "#define COLOR_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n"; actions[VS::SHADER_CANVAS_ITEM].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n";
@ -957,6 +958,7 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() {
actions[VS::SHADER_CANVAS_ITEM].usage_defines["NORMALMAP"] = "#define NORMALMAP_USED\n"; actions[VS::SHADER_CANVAS_ITEM].usage_defines["NORMALMAP"] = "#define NORMALMAP_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n"; actions[VS::SHADER_CANVAS_ITEM].usage_defines["LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
actions[VS::SHADER_CANVAS_ITEM].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n"; actions[VS::SHADER_CANVAS_ITEM].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["SHADOW_VEC"] = "#define SHADOW_VEC_USED\n";
// Ported from GLES3 // Ported from GLES3

View file

@ -331,6 +331,7 @@ void light_compute(
inout vec4 light_color, inout vec4 light_color,
vec2 light_uv, vec2 light_uv,
inout vec4 shadow_color, inout vec4 shadow_color,
inout vec2 shadow_vec,
vec3 normal, vec3 normal,
vec2 uv, vec2 uv,
#if defined(SCREEN_UV_USED) #if defined(SCREEN_UV_USED)
@ -407,6 +408,7 @@ FRAGMENT_SHADER_CODE
#ifdef USE_LIGHTING #ifdef USE_LIGHTING
vec2 light_vec = transformed_light_uv; vec2 light_vec = transformed_light_uv;
vec2 shadow_vec = transformed_light_uv;
if (normal_used) { if (normal_used) {
normal.xy = mat2(local_rot.xy, local_rot.zw) * normal.xy; normal.xy = mat2(local_rot.xy, local_rot.zw) * normal.xy;
@ -434,6 +436,7 @@ FRAGMENT_SHADER_CODE
real_light_color, real_light_color,
light_uv, light_uv,
real_light_shadow_color, real_light_shadow_color,
shadow_vec,
normal, normal,
uv, uv,
#if defined(SCREEN_UV_USED) #if defined(SCREEN_UV_USED)
@ -452,11 +455,18 @@ FRAGMENT_SHADER_CODE
color *= light; color *= light;
#ifdef USE_SHADOWS #ifdef USE_SHADOWS
// Reset light_vec to compute shadows, the shadow map is created from the light origin, so it only
// makes sense to compute shadows from there.
light_vec = light_uv_interp.zw;
float angle_to_light = -atan(light_vec.x, light_vec.y); #ifdef SHADOW_VEC_USED
mat3 inverse_light_matrix = mat3(light_matrix);
inverse_light_matrix[0] = normalize(inverse_light_matrix[0]);
inverse_light_matrix[1] = normalize(inverse_light_matrix[1]);
inverse_light_matrix[2] = normalize(inverse_light_matrix[2]);
shadow_vec = (inverse_light_matrix * vec3(shadow_vec, 0.0)).xy;
#else
shadow_vec = light_uv_interp.zw;
#endif
float angle_to_light = -atan(shadow_vec.x, shadow_vec.y);
float PI = 3.14159265358979323846264; float PI = 3.14159265358979323846264;
/*int i = int(mod(floor((angle_to_light+7.0*PI/6.0)/(4.0*PI/6.0))+1.0, 3.0)); // +1 pq os indices estao em ordem 2,0,1 nos arrays /*int i = int(mod(floor((angle_to_light+7.0*PI/6.0)/(4.0*PI/6.0))+1.0, 3.0)); // +1 pq os indices estao em ordem 2,0,1 nos arrays
float ang*/ float ang*/
@ -467,18 +477,18 @@ FRAGMENT_SHADER_CODE
vec2 point; vec2 point;
float sh; float sh;
if (abs_angle < 45.0 * PI / 180.0) { if (abs_angle < 45.0 * PI / 180.0) {
point = light_vec; point = shadow_vec;
sh = 0.0 + (1.0 / 8.0); sh = 0.0 + (1.0 / 8.0);
} else if (abs_angle > 135.0 * PI / 180.0) { } else if (abs_angle > 135.0 * PI / 180.0) {
point = -light_vec; point = -shadow_vec;
sh = 0.5 + (1.0 / 8.0); sh = 0.5 + (1.0 / 8.0);
} else if (angle_to_light > 0.0) { } else if (angle_to_light > 0.0) {
point = vec2(light_vec.y, -light_vec.x); point = vec2(shadow_vec.y, -shadow_vec.x);
sh = 0.25 + (1.0 / 8.0); sh = 0.25 + (1.0 / 8.0);
} else { } else {
point = vec2(-light_vec.y, light_vec.x); point = vec2(-shadow_vec.y, shadow_vec.x);
sh = 0.75 + (1.0 / 8.0); sh = 0.75 + (1.0 / 8.0);
} }

View file

@ -941,6 +941,7 @@ ShaderCompilerGLES3::ShaderCompilerGLES3() {
actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT_UV"] = "light_uv"; actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT_UV"] = "light_uv";
actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT"] = "light"; actions[VS::SHADER_CANVAS_ITEM].renames["LIGHT"] = "light";
actions[VS::SHADER_CANVAS_ITEM].renames["SHADOW_COLOR"] = "shadow_color"; actions[VS::SHADER_CANVAS_ITEM].renames["SHADOW_COLOR"] = "shadow_color";
actions[VS::SHADER_CANVAS_ITEM].renames["SHADOW_VEC"] = "shadow_vec";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["COLOR"] = "#define COLOR_USED\n"; actions[VS::SHADER_CANVAS_ITEM].usage_defines["COLOR"] = "#define COLOR_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n"; actions[VS::SHADER_CANVAS_ITEM].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n";
@ -949,6 +950,7 @@ ShaderCompilerGLES3::ShaderCompilerGLES3() {
actions[VS::SHADER_CANVAS_ITEM].usage_defines["NORMAL"] = "#define NORMAL_USED\n"; actions[VS::SHADER_CANVAS_ITEM].usage_defines["NORMAL"] = "#define NORMAL_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["NORMALMAP"] = "#define NORMALMAP_USED\n"; actions[VS::SHADER_CANVAS_ITEM].usage_defines["NORMALMAP"] = "#define NORMALMAP_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n"; actions[VS::SHADER_CANVAS_ITEM].usage_defines["LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["SHADOW_VEC"] = "#define SHADOW_VEC_USED\n";
actions[VS::SHADER_CANVAS_ITEM].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n"; actions[VS::SHADER_CANVAS_ITEM].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
/** SPATIAL SHADER **/ /** SPATIAL SHADER **/

View file

@ -345,6 +345,7 @@ void light_compute(
inout vec4 light_color, inout vec4 light_color,
vec2 light_uv, vec2 light_uv,
inout vec4 shadow_color, inout vec4 shadow_color,
inout vec2 shadow_vec,
vec3 normal, vec3 normal,
vec2 uv, vec2 uv,
#if defined(SCREEN_UV_USED) #if defined(SCREEN_UV_USED)
@ -512,6 +513,7 @@ FRAGMENT_SHADER_CODE
#ifdef USE_LIGHTING #ifdef USE_LIGHTING
vec2 light_vec = transformed_light_uv; vec2 light_vec = transformed_light_uv;
vec2 shadow_vec = transformed_light_uv;
if (normal_used) { if (normal_used) {
normal.xy = mat2(local_rot.xy, local_rot.zw) * normal.xy; normal.xy = mat2(local_rot.xy, local_rot.zw) * normal.xy;
@ -539,6 +541,7 @@ FRAGMENT_SHADER_CODE
real_light_color, real_light_color,
light_uv, light_uv,
real_light_shadow_color, real_light_shadow_color,
shadow_vec,
normal, normal,
uv, uv,
#if defined(SCREEN_UV_USED) #if defined(SCREEN_UV_USED)
@ -557,11 +560,16 @@ FRAGMENT_SHADER_CODE
color *= light; color *= light;
#ifdef USE_SHADOWS #ifdef USE_SHADOWS
// Reset light_vec to compute shadows, the shadow map is created from the light origin, so it only #ifdef SHADOW_VEC_USED
// makes sense to compute shadows from there. mat3 inverse_light_matrix = mat3(light_matrix);
light_vec = light_uv_interp.zw; inverse_light_matrix[0] = normalize(inverse_light_matrix[0]);
inverse_light_matrix[1] = normalize(inverse_light_matrix[1]);
float angle_to_light = -atan(light_vec.x, light_vec.y); inverse_light_matrix[2] = normalize(inverse_light_matrix[2]);
shadow_vec = (mat3(inverse_light_matrix) * vec3(shadow_vec, 0.0)).xy;
#else
shadow_vec = light_uv_interp.zw;
#endif
float angle_to_light = -atan(shadow_vec.x, shadow_vec.y);
float PI = 3.14159265358979323846264; float PI = 3.14159265358979323846264;
/*int i = int(mod(floor((angle_to_light+7.0*PI/6.0)/(4.0*PI/6.0))+1.0, 3.0)); // +1 pq os indices estao em ordem 2,0,1 nos arrays /*int i = int(mod(floor((angle_to_light+7.0*PI/6.0)/(4.0*PI/6.0))+1.0, 3.0)); // +1 pq os indices estao em ordem 2,0,1 nos arrays
float ang*/ float ang*/
@ -572,18 +580,18 @@ FRAGMENT_SHADER_CODE
vec2 point; vec2 point;
float sh; float sh;
if (abs_angle < 45.0 * PI / 180.0) { if (abs_angle < 45.0 * PI / 180.0) {
point = light_vec; point = shadow_vec;
sh = 0.0 + (1.0 / 8.0); sh = 0.0 + (1.0 / 8.0);
} else if (abs_angle > 135.0 * PI / 180.0) { } else if (abs_angle > 135.0 * PI / 180.0) {
point = -light_vec; point = -shadow_vec;
sh = 0.5 + (1.0 / 8.0); sh = 0.5 + (1.0 / 8.0);
} else if (angle_to_light > 0.0) { } else if (angle_to_light > 0.0) {
point = vec2(light_vec.y, -light_vec.x); point = vec2(shadow_vec.y, -shadow_vec.x);
sh = 0.25 + (1.0 / 8.0); sh = 0.25 + (1.0 / 8.0);
} else { } else {
point = vec2(-light_vec.y, light_vec.x); point = vec2(-shadow_vec.y, shadow_vec.x);
sh = 0.75 + (1.0 / 8.0); sh = 0.75 + (1.0 / 8.0);
} }

View file

@ -235,6 +235,7 @@ ShaderTypes::ShaderTypes() {
shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"].built_ins["TEXTURE_PIXEL_SIZE"] = constt(ShaderLanguage::TYPE_VEC2); shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"].built_ins["TEXTURE_PIXEL_SIZE"] = constt(ShaderLanguage::TYPE_VEC2);
shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"].built_ins["SCREEN_UV"] = constt(ShaderLanguage::TYPE_VEC2); shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"].built_ins["SCREEN_UV"] = constt(ShaderLanguage::TYPE_VEC2);
shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"].built_ins["LIGHT_VEC"] = ShaderLanguage::TYPE_VEC2; shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"].built_ins["LIGHT_VEC"] = ShaderLanguage::TYPE_VEC2;
shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"].built_ins["SHADOW_VEC"] = ShaderLanguage::TYPE_VEC2;
shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"].built_ins["LIGHT_HEIGHT"] = ShaderLanguage::TYPE_FLOAT; shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"].built_ins["LIGHT_HEIGHT"] = ShaderLanguage::TYPE_FLOAT;
shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"].built_ins["LIGHT_COLOR"] = ShaderLanguage::TYPE_VEC4; shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"].built_ins["LIGHT_COLOR"] = ShaderLanguage::TYPE_VEC4;
shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"].built_ins["LIGHT_UV"] = constt(ShaderLanguage::TYPE_VEC2); shader_modes[VS::SHADER_CANVAS_ITEM].functions["light"].built_ins["LIGHT_UV"] = constt(ShaderLanguage::TYPE_VEC2);