diff options
Diffstat (limited to 'drivers/gles3/shaders/canvas.glsl')
-rw-r--r-- | drivers/gles3/shaders/canvas.glsl | 360 |
1 files changed, 174 insertions, 186 deletions
diff --git a/drivers/gles3/shaders/canvas.glsl b/drivers/gles3/shaders/canvas.glsl index 9c426dd3ef..c1c26ed963 100644 --- a/drivers/gles3/shaders/canvas.glsl +++ b/drivers/gles3/shaders/canvas.glsl @@ -10,6 +10,7 @@ mode_instanced = #define USE_ATTRIBUTES \n#define USE_INSTANCING #[specializations] DISABLE_LIGHTING = false +USE_RGBA_SHADOWS = false #[vertex] @@ -18,15 +19,11 @@ layout(location = 0) in vec2 vertex_attrib; layout(location = 3) in vec4 color_attrib; layout(location = 4) in vec2 uv_attrib; -layout(location = 10) in uvec4 bone_attrib; -layout(location = 11) in vec4 weight_attrib; - #ifdef USE_INSTANCING -layout(location = 5) in highp vec4 instance_xform0; -layout(location = 6) in highp vec4 instance_xform1; -layout(location = 7) in lowp vec4 instance_color; -layout(location = 8) in highp vec4 instance_custom_data; +layout(location = 1) in highp vec4 instance_xform0; +layout(location = 2) in highp vec4 instance_xform1; +layout(location = 5) in highp uvec4 instance_color_custom_data; // Color packed into xy, custom_data packed into zw for compatibility with 3D #endif @@ -44,8 +41,6 @@ layout(std140) uniform MaterialUniforms{ //ubo:4 #include "canvas_uniforms_inc.glsl" #include "stdlib_inc.glsl" -uniform sampler2D transforms_texture; //texunit:-1 - out vec2 uv_interp; out vec4 color_interp; out vec2 vertex_interp; @@ -61,20 +56,18 @@ out vec2 pixel_size_interp; void main() { vec4 instance_custom = vec4(0.0); - draw_data_instance = gl_InstanceID; -#ifdef USE_PRIMITIVE - //weird bug, - //this works +#ifdef USE_PRIMITIVE + draw_data_instance = gl_InstanceID; vec2 vertex; vec2 uv; vec4 color; - if (gl_VertexID == 0) { + if (gl_VertexID % 3 == 0) { vertex = draw_data[draw_data_instance].point_a; uv = draw_data[draw_data_instance].uv_a; color = vec4(unpackHalf2x16(draw_data[draw_data_instance].color_a_rg), unpackHalf2x16(draw_data[draw_data_instance].color_a_ba)); - } else if (gl_VertexID == 1) { + } else if (gl_VertexID % 3 == 1) { vertex = draw_data[draw_data_instance].point_b; uv = draw_data[draw_data_instance].uv_b; color = vec4(unpackHalf2x16(draw_data[draw_data_instance].color_b_rg), unpackHalf2x16(draw_data[draw_data_instance].color_b_ba)); @@ -83,10 +76,9 @@ void main() { uv = draw_data[draw_data_instance].uv_c; color = vec4(unpackHalf2x16(draw_data[draw_data_instance].color_c_rg), unpackHalf2x16(draw_data[draw_data_instance].color_c_ba)); } - uvec4 bones = uvec4(0, 0, 0, 0); - vec4 bone_weights = vec4(0.0); #elif defined(USE_ATTRIBUTES) + draw_data_instance = gl_InstanceID; #ifdef USE_INSTANCING draw_data_instance = 0; #endif @@ -94,23 +86,20 @@ void main() { vec4 color = color_attrib * draw_data[draw_data_instance].modulation; vec2 uv = uv_attrib; - uvec4 bones = bone_attrib; - vec4 bone_weights = weight_attrib; - #ifdef USE_INSTANCING + vec4 instance_color = vec4(unpackHalf2x16(instance_color_custom_data.x), unpackHalf2x16(instance_color_custom_data.y)); color *= instance_color; - instance_custom = instance_custom_data; + instance_custom = vec4(unpackHalf2x16(instance_color_custom_data.z), unpackHalf2x16(instance_color_custom_data.w)); #endif #else - - vec2 vertex_base_arr[4] = vec2[](vec2(0.0, 0.0), vec2(0.0, 1.0), vec2(1.0, 1.0), vec2(1.0, 0.0)); - vec2 vertex_base = vertex_base_arr[gl_VertexID]; + draw_data_instance = gl_VertexID / 6; + vec2 vertex_base_arr[6] = vec2[](vec2(0.0, 0.0), vec2(0.0, 1.0), vec2(1.0, 1.0), vec2(1.0, 0.0), vec2(0.0, 0.0), vec2(1.0, 1.0)); + vec2 vertex_base = vertex_base_arr[gl_VertexID % 6]; vec2 uv = draw_data[draw_data_instance].src_rect.xy + abs(draw_data[draw_data_instance].src_rect.zw) * ((draw_data[draw_data_instance].flags & FLAGS_TRANSPOSE_RECT) != uint(0) ? vertex_base.yx : vertex_base.xy); vec4 color = draw_data[draw_data_instance].modulation; vec2 vertex = draw_data[draw_data_instance].dst_rect.xy + abs(draw_data[draw_data_instance].dst_rect.zw) * mix(vertex_base, vec2(1.0, 1.0) - vertex_base, lessThan(draw_data[draw_data_instance].src_rect.zw, vec2(0.0, 0.0))); - uvec4 bones = uvec4(0, 0, 0, 0); #endif @@ -127,6 +116,8 @@ void main() { } #endif + vec2 color_texture_pixel_size = draw_data[draw_data_instance].color_texture_pixel_size.xy; + #ifdef USE_POINT_SIZE float point_size = 1.0; #endif @@ -151,48 +142,6 @@ void main() { uv += 1e-5; } -#ifdef USE_ATTRIBUTES -#if 0 - if (bool(draw_data[draw_data_instance].flags & FLAGS_USE_SKELETON) && bone_weights != vec4(0.0)) { //must be a valid bone - //skeleton transform - ivec4 bone_indicesi = ivec4(bone_indices); - - uvec2 tex_ofs = bone_indicesi.x * 2; - - mat2x4 m; - m = mat2x4( - texelFetch(skeleton_buffer, tex_ofs + 0), - texelFetch(skeleton_buffer, tex_ofs + 1)) * - bone_weights.x; - - tex_ofs = bone_indicesi.y * 2; - - m += mat2x4( - texelFetch(skeleton_buffer, tex_ofs + 0), - texelFetch(skeleton_buffer, tex_ofs + 1)) * - bone_weights.y; - - tex_ofs = bone_indicesi.z * 2; - - m += mat2x4( - texelFetch(skeleton_buffer, tex_ofs + 0), - texelFetch(skeleton_buffer, tex_ofs + 1)) * - bone_weights.z; - - tex_ofs = bone_indicesi.w * 2; - - m += mat2x4( - texelFetch(skeleton_buffer, tex_ofs + 0), - texelFetch(skeleton_buffer, tex_ofs + 1)) * - bone_weights.w; - - mat4 bone_matrix = skeleton_data.skeleton_transform * transpose(mat4(m[0], m[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))) * skeleton_data.skeleton_transform_inverse; - - //outvec = bone_matrix * outvec; - } -#endif -#endif - vertex = (canvas_transform * vec4(vertex, 0.0, 1.0)).xy; vertex_interp = vertex; @@ -210,8 +159,10 @@ void main() { #include "canvas_uniforms_inc.glsl" #include "stdlib_inc.glsl" +#ifndef DISABLE_LIGHTING uniform sampler2D atlas_texture; //texunit:-2 uniform sampler2D shadow_atlas_texture; //texunit:-3 +#endif // DISABLE_LIGHTING uniform sampler2D screen_texture; //texunit:-4 uniform sampler2D sdf_texture; //texunit:-5 uniform sampler2D normal_texture; //texunit:-6 @@ -241,13 +192,19 @@ layout(std140) uniform MaterialUniforms{ }; #endif +#GLOBALS + +float vec4_to_float(vec4 p_vec) { + return dot(p_vec, vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0)) * 2.0 - 1.0; +} + vec2 screen_uv_to_sdf(vec2 p_uv) { return screen_to_sdf * p_uv; } float texture_sdf(vec2 p_sdf) { vec2 uv = p_sdf * sdf_to_tex.xy + sdf_to_tex.zw; - float d = texture(sdf_texture, uv).r; + float d = vec4_to_float(texture(sdf_texture, uv)); d *= SDF_MAX_LENGTH; return d * tex_to_sdf; } @@ -257,16 +214,15 @@ vec2 texture_sdf_normal(vec2 p_sdf) { const float EPSILON = 0.001; return normalize(vec2( - texture(sdf_texture, uv + vec2(EPSILON, 0.0)).r - texture(sdf_texture, uv - vec2(EPSILON, 0.0)).r, - texture(sdf_texture, uv + vec2(0.0, EPSILON)).r - texture(sdf_texture, uv - vec2(0.0, EPSILON)).r)); + vec4_to_float(texture(sdf_texture, uv + vec2(EPSILON, 0.0))) - vec4_to_float(texture(sdf_texture, uv - vec2(EPSILON, 0.0))), + vec4_to_float(texture(sdf_texture, uv + vec2(0.0, EPSILON))) - vec4_to_float(texture(sdf_texture, uv - vec2(0.0, EPSILON))))); } vec2 sdf_to_screen_uv(vec2 p_sdf) { return p_sdf * sdf_to_screen; } -#GLOBALS - +#ifndef DISABLE_LIGHTING #ifdef LIGHT_CODE_USED vec4 light_compute( @@ -281,6 +237,14 @@ vec4 light_compute( vec2 uv, vec4 color, bool is_directional) { vec4 light = vec4(0.0); + vec3 light_direction = vec3(0.0); + + if (is_directional) { + light_direction = normalize(mix(vec3(light_position.xy, 0.0), vec3(0, 0, 1), light_position.z)); + light_position = vec3(0.0); + } else { + light_direction = normalize(light_position - light_vertex); + } #CODE : LIGHT @@ -289,49 +253,6 @@ vec4 light_compute( #endif -#ifdef USE_NINEPATCH - -float map_ninepatch_axis(float pixel, float draw_size, float tex_pixel_size, float margin_begin, float margin_end, int np_repeat, inout int draw_center) { - float tex_size = 1.0 / tex_pixel_size; - - if (pixel < margin_begin) { - return pixel * tex_pixel_size; - } else if (pixel >= draw_size - margin_end) { - return (tex_size - (draw_size - pixel)) * tex_pixel_size; - } else { - if (!bool(draw_data[draw_data_instance].flags & FLAGS_NINEPACH_DRAW_CENTER)) { - draw_center--; - } - - // np_repeat is passed as uniform using NinePatchRect::AxisStretchMode enum. - if (np_repeat == 0) { // Stretch. - // Convert to ratio. - float ratio = (pixel - margin_begin) / (draw_size - margin_begin - margin_end); - // Scale to source texture. - return (margin_begin + ratio * (tex_size - margin_begin - margin_end)) * tex_pixel_size; - } else if (np_repeat == 1) { // Tile. - // Convert to offset. - float ofs = mod((pixel - margin_begin), tex_size - margin_begin - margin_end); - // Scale to source texture. - return (margin_begin + ofs) * tex_pixel_size; - } else if (np_repeat == 2) { // Tile Fit. - // Calculate scale. - float src_area = draw_size - margin_begin - margin_end; - float dst_area = tex_size - margin_begin - margin_end; - float scale = max(1.0, floor(src_area / max(dst_area, 0.0000001) + 0.5)); - // Convert to ratio. - float ratio = (pixel - margin_begin) / src_area; - ratio = mod(ratio * scale, 1.0); - // Scale to source texture. - return (margin_begin + ratio * dst_area) * tex_pixel_size; - } else { // Shouldn't happen, but silences compiler warning. - return 0.0; - } - } -} - -#endif - vec3 light_normal_compute(vec3 light_vec, vec3 normal, vec3 base_color, vec3 light_color, vec4 specular_shininess, bool specular_shininess_used) { float cNdotL = max(0.0, dot(normal, light_vec)); @@ -355,6 +276,20 @@ vec3 light_normal_compute(vec3 light_vec, vec3 normal, vec3 base_color, vec3 lig } } +#ifdef USE_RGBA_SHADOWS + +#define SHADOW_DEPTH(m_uv) (dot(textureLod(shadow_atlas_texture, (m_uv), 0.0), vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0)) * 2.0 - 1.0) + +#else + +#define SHADOW_DEPTH(m_uv) (textureLod(shadow_atlas_texture, (m_uv), 0.0).r) + +#endif + +/* clang-format off */ +#define SHADOW_TEST(m_uv) { highp float sd = SHADOW_DEPTH(m_uv); shadow += step(sd, shadow_uv.z / shadow_uv.w); } +/* clang-format on */ + //float distance = length(shadow_pos); vec4 light_shadow_compute(uint light_base, vec4 light_color, vec4 shadow_uv #ifdef LIGHT_CODE_USED @@ -362,40 +297,38 @@ vec4 light_shadow_compute(uint light_base, vec4 light_color, vec4 shadow_uv vec3 shadow_modulate #endif ) { - float shadow; - uint shadow_mode = light_data[light_base].flags & LIGHT_FLAGS_FILTER_MASK; + float shadow = 0.0; + uint shadow_mode = light_array[light_base].flags & LIGHT_FLAGS_FILTER_MASK; if (shadow_mode == LIGHT_FLAGS_SHADOW_NEAREST) { - shadow = textureProjLod(shadow_atlas_texture, shadow_uv, 0.0).x; + SHADOW_TEST(shadow_uv.xy); } else if (shadow_mode == LIGHT_FLAGS_SHADOW_PCF5) { - vec4 shadow_pixel_size = vec4(light_data[light_base].shadow_pixel_size, 0.0, 0.0, 0.0); - shadow = 0.0; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv - shadow_pixel_size * 2.0, 0.0).x; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv - shadow_pixel_size, 0.0).x; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv, 0.0).x; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv + shadow_pixel_size, 0.0).x; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv + shadow_pixel_size * 2.0, 0.0).x; + vec2 shadow_pixel_size = vec2(light_array[light_base].shadow_pixel_size, 0.0); + SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 2.0); + SHADOW_TEST(shadow_uv.xy - shadow_pixel_size); + SHADOW_TEST(shadow_uv.xy); + SHADOW_TEST(shadow_uv.xy + shadow_pixel_size); + SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 2.0); shadow /= 5.0; } else { //PCF13 - vec4 shadow_pixel_size = vec4(light_data[light_base].shadow_pixel_size, 0.0, 0.0, 0.0); - shadow = 0.0; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv - shadow_pixel_size * 6.0, 0.0).x; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv - shadow_pixel_size * 5.0, 0.0).x; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv - shadow_pixel_size * 4.0, 0.0).x; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv - shadow_pixel_size * 3.0, 0.0).x; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv - shadow_pixel_size * 2.0, 0.0).x; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv - shadow_pixel_size, 0.0).x; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv, 0.0).x; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv + shadow_pixel_size, 0.0).x; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv + shadow_pixel_size * 2.0, 0.0).x; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv + shadow_pixel_size * 3.0, 0.0).x; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv + shadow_pixel_size * 4.0, 0.0).x; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv + shadow_pixel_size * 5.0, 0.0).x; - shadow += textureProjLod(shadow_atlas_texture, shadow_uv + shadow_pixel_size * 6.0, 0.0).x; + vec2 shadow_pixel_size = vec2(light_array[light_base].shadow_pixel_size, 0.0); + SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 6.0); + SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 5.0); + SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 4.0); + SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 3.0); + SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 2.0); + SHADOW_TEST(shadow_uv.xy - shadow_pixel_size); + SHADOW_TEST(shadow_uv.xy); + SHADOW_TEST(shadow_uv.xy + shadow_pixel_size); + SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 2.0); + SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 3.0); + SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 4.0); + SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 5.0); + SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 6.0); shadow /= 13.0; } - vec4 shadow_color = unpackUnorm4x8(light_data[light_base].shadow_color); + vec4 shadow_color = godot_unpackUnorm4x8(light_array[light_base].shadow_color); #ifdef LIGHT_CODE_USED shadow_color.rgb *= shadow_modulate; #endif @@ -406,7 +339,7 @@ vec4 light_shadow_compute(uint light_base, vec4 light_color, vec4 shadow_uv } void light_blend_compute(uint light_base, vec4 light_color, inout vec3 color) { - uint blend_mode = light_data[light_base].flags & LIGHT_FLAGS_BLEND_MASK; + uint blend_mode = light_array[light_base].flags & LIGHT_FLAGS_BLEND_MASK; switch (blend_mode) { case LIGHT_FLAGS_BLEND_MODE_ADD: { @@ -421,6 +354,51 @@ void light_blend_compute(uint light_base, vec4 light_color, inout vec3 color) { } } +#endif + +#ifdef USE_NINEPATCH + +float map_ninepatch_axis(float pixel, float draw_size, float tex_pixel_size, float margin_begin, float margin_end, int np_repeat, inout int draw_center) { + float tex_size = 1.0 / tex_pixel_size; + + if (pixel < margin_begin) { + return pixel * tex_pixel_size; + } else if (pixel >= draw_size - margin_end) { + return (tex_size - (draw_size - pixel)) * tex_pixel_size; + } else { + if (!bool(draw_data[draw_data_instance].flags & FLAGS_NINEPACH_DRAW_CENTER)) { + draw_center--; + } + + // np_repeat is passed as uniform using NinePatchRect::AxisStretchMode enum. + if (np_repeat == 0) { // Stretch. + // Convert to ratio. + float ratio = (pixel - margin_begin) / (draw_size - margin_begin - margin_end); + // Scale to source texture. + return (margin_begin + ratio * (tex_size - margin_begin - margin_end)) * tex_pixel_size; + } else if (np_repeat == 1) { // Tile. + // Convert to offset. + float ofs = mod((pixel - margin_begin), tex_size - margin_begin - margin_end); + // Scale to source texture. + return (margin_begin + ofs) * tex_pixel_size; + } else if (np_repeat == 2) { // Tile Fit. + // Calculate scale. + float src_area = draw_size - margin_begin - margin_end; + float dst_area = tex_size - margin_begin - margin_end; + float scale = max(1.0, floor(src_area / max(dst_area, 0.0000001) + 0.5)); + // Convert to ratio. + float ratio = (pixel - margin_begin) / src_area; + ratio = mod(ratio * scale, 1.0); + // Scale to source texture. + return (margin_begin + ratio * dst_area) * tex_pixel_size; + } else { // Shouldn't happen, but silences compiler warning. + return 0.0; + } + } +} + +#endif + float msdf_median(float r, float g, float b, float a) { return min(max(min(r, g), min(max(r, g), b)), a); } @@ -473,7 +451,13 @@ void main() { float a = clamp(d * px_size + 0.5, 0.0, 1.0); color.a = a * color.a; } - + } else if (bool(draw_data[draw_data_instance].flags & FLAGS_USE_LCD)) { + vec4 lcd_sample = texture(color_texture, uv); + if (lcd_sample.a == 1.0) { + color.rgb = lcd_sample.rgb * color.a; + } else { + color = vec4(0.0, 0.0, 0.0, 0.0); + } } else { #else { @@ -481,8 +465,8 @@ void main() { color *= texture(color_texture, uv); } - uint light_count = (draw_data[draw_data_instance].flags >> FLAGS_LIGHT_COUNT_SHIFT) & uint(0xF); //max 16 lights - bool using_light = light_count > uint(0) || directional_light_count > uint(0); + uint light_count = (draw_data[draw_data_instance].flags >> uint(FLAGS_LIGHT_COUNT_SHIFT)) & uint(0xF); //max 16 lights + bool using_light = light_count > 0u || directional_light_count > 0u; vec3 normal; @@ -511,7 +495,7 @@ void main() { if (specular_shininess_used || (using_light && normal_used && bool(draw_data[draw_data_instance].flags & FLAGS_DEFAULT_SPECULAR_MAP_USED))) { specular_shininess = texture(specular_texture, uv); - specular_shininess *= unpackUnorm4x8(draw_data[draw_data_instance].specular_shininess); + specular_shininess *= godot_unpackUnorm4x8(draw_data[draw_data_instance].specular_shininess); specular_shininess_used = true; } else { specular_shininess = vec4(1.0); @@ -523,6 +507,8 @@ void main() { vec2 screen_uv = vec2(0.0); #endif + vec2 color_texture_pixel_size = draw_data[draw_data_instance].color_texture_pixel_size.xy; + vec3 light_vertex = vec3(vertex, 0.0); vec2 shadow_vertex = vertex; @@ -548,10 +534,7 @@ void main() { normal = normalize((canvas_normal_transform * vec4(normal, 0.0)).xyz); } - vec3 base_color = color.rgb; - if (bool(draw_data[draw_data_instance].flags & FLAGS_USING_LIGHT_MASK)) { - color = vec4(0.0); //invisible by default due to using light mask - } + vec4 base_color = color; #ifdef MODE_LIGHT_ONLY color = vec4(0.0); @@ -561,28 +544,32 @@ void main() { #if !defined(DISABLE_LIGHTING) && !defined(MODE_UNSHADED) - for (uint i = uint(0); i < directional_light_count; i++) { + // Directional Lights + + for (uint i = 0u; i < directional_light_count; i++) { uint light_base = i; - vec2 direction = light_data[light_base].position; - vec4 light_color = light_data[light_base].color; + vec2 direction = light_array[light_base].position; + vec4 light_color = light_array[light_base].color; #ifdef LIGHT_CODE_USED vec4 shadow_modulate = vec4(1.0); - light_color = light_compute(light_vertex, vec3(direction, light_data[light_base].height), normal, light_color, light_color.a, specular_shininess, shadow_modulate, screen_uv, uv, color, true); + light_color = light_compute(light_vertex, vec3(direction, light_array[light_base].height), normal, light_color, light_color.a, specular_shininess, shadow_modulate, screen_uv, uv, base_color, true); #else if (normal_used) { - vec3 light_vec = normalize(mix(vec3(direction, 0.0), vec3(0, 0, 1), light_data[light_base].height)); - light_color.rgb = light_normal_compute(light_vec, normal, base_color, light_color.rgb, specular_shininess, specular_shininess_used); + vec3 light_vec = normalize(mix(vec3(direction, 0.0), vec3(0, 0, 1), light_array[light_base].height)); + light_color.rgb = light_normal_compute(light_vec, normal, base_color.rgb, light_color.rgb, specular_shininess, specular_shininess_used); + } else { + light_color.rgb *= base_color.rgb; } #endif - if (bool(light_data[light_base].flags & LIGHT_FLAGS_HAS_SHADOW)) { - vec2 shadow_pos = (vec4(shadow_vertex, 0.0, 1.0) * mat4(light_data[light_base].shadow_matrix[0], light_data[light_base].shadow_matrix[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy; //multiply inverse given its transposed. Optimizer removes useless operations. + if (bool(light_array[light_base].flags & LIGHT_FLAGS_HAS_SHADOW)) { + vec2 shadow_pos = (vec4(shadow_vertex, 0.0, 1.0) * mat4(light_array[light_base].shadow_matrix[0], light_array[light_base].shadow_matrix[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy; //multiply inverse given its transposed. Optimizer removes useless operations. - vec4 shadow_uv = vec4(shadow_pos.x, light_data[light_base].shadow_y_ofs, shadow_pos.y * light_data[light_base].shadow_zfar_inv, 1.0); + vec4 shadow_uv = vec4(shadow_pos.x, light_array[light_base].shadow_y_ofs, shadow_pos.y * light_array[light_base].shadow_zfar_inv, 1.0); light_color = light_shadow_compute(light_base, light_color, shadow_uv #ifdef LIGHT_CODE_USED @@ -597,50 +584,51 @@ void main() { // Positional Lights - for (uint i = uint(0); i < MAX_LIGHTS_PER_ITEM; i++) { + for (uint i = 0u; i < MAX_LIGHTS_PER_ITEM; i++) { if (i >= light_count) { break; } uint light_base; - if (i < uint(8)) { - if (i < uint(4)) { - light_base = draw_data[draw_data_instance].lights.x; + if (i < 8u) { + if (i < 4u) { + light_base = draw_data[draw_data_instance].lights[0]; } else { - light_base = draw_data[draw_data_instance].lights.y; + light_base = draw_data[draw_data_instance].lights[1]; } } else { - if (i < uint(12)) { - light_base = draw_data[draw_data_instance].lights.z; + if (i < 12u) { + light_base = draw_data[draw_data_instance].lights[2]; } else { - light_base = draw_data[draw_data_instance].lights.w; + light_base = draw_data[draw_data_instance].lights[3]; } } - light_base >>= (i & uint(3)) * uint(8); + light_base >>= (i & 3u) * 8u; light_base &= uint(0xFF); - vec2 tex_uv = (vec4(vertex, 0.0, 1.0) * mat4(light_data[light_base].texture_matrix[0], light_data[light_base].texture_matrix[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy; //multiply inverse given its transposed. Optimizer removes useless operations. - vec2 tex_uv_atlas = tex_uv * light_data[light_base].atlas_rect.zw + light_data[light_base].atlas_rect.xy; + vec2 tex_uv = (vec4(vertex, 0.0, 1.0) * mat4(light_array[light_base].texture_matrix[0], light_array[light_base].texture_matrix[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy; //multiply inverse given its transposed. Optimizer removes useless operations. + vec2 tex_uv_atlas = tex_uv * light_array[light_base].atlas_rect.zw + light_array[light_base].atlas_rect.xy; vec4 light_color = textureLod(atlas_texture, tex_uv_atlas, 0.0); - vec4 light_base_color = light_data[light_base].color; + vec4 light_base_color = light_array[light_base].color; #ifdef LIGHT_CODE_USED vec4 shadow_modulate = vec4(1.0); - vec3 light_position = vec3(light_data[light_base].position, light_data[light_base].height); + vec3 light_position = vec3(light_array[light_base].position, light_array[light_base].height); light_color.rgb *= light_base_color.rgb; - light_color = light_compute(light_vertex, light_position, normal, light_color, light_base_color.a, specular_shininess, shadow_modulate, screen_uv, uv, color, false); + light_color = light_compute(light_vertex, light_position, normal, light_color, light_base_color.a, specular_shininess, shadow_modulate, screen_uv, uv, base_color, false); #else light_color.rgb *= light_base_color.rgb * light_base_color.a; if (normal_used) { - vec3 light_pos = vec3(light_data[light_base].position, light_data[light_base].height); + vec3 light_pos = vec3(light_array[light_base].position, light_array[light_base].height); vec3 pos = light_vertex; vec3 light_vec = normalize(light_pos - pos); - float cNdotL = max(0.0, dot(normal, light_vec)); - light_color.rgb = light_normal_compute(light_vec, normal, base_color, light_color.rgb, specular_shininess, specular_shininess_used); + light_color.rgb = light_normal_compute(light_vec, normal, base_color.rgb, light_color.rgb, specular_shininess, specular_shininess_used); + } else { + light_color.rgb *= base_color.rgb; } #endif if (any(lessThan(tex_uv, vec2(0.0, 0.0))) || any(greaterThanEqual(tex_uv, vec2(1.0, 1.0)))) { @@ -648,37 +636,37 @@ void main() { light_color.a = 0.0; } - if (bool(light_data[light_base].flags & LIGHT_FLAGS_HAS_SHADOW)) { - vec2 shadow_pos = (vec4(shadow_vertex, 0.0, 1.0) * mat4(light_data[light_base].shadow_matrix[0], light_data[light_base].shadow_matrix[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy; //multiply inverse given its transposed. Optimizer removes useless operations. + if (bool(light_array[light_base].flags & LIGHT_FLAGS_HAS_SHADOW)) { + vec2 shadow_pos = (vec4(shadow_vertex, 0.0, 1.0) * mat4(light_array[light_base].shadow_matrix[0], light_array[light_base].shadow_matrix[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy; //multiply inverse given its transposed. Optimizer removes useless operations. vec2 pos_norm = normalize(shadow_pos); vec2 pos_abs = abs(pos_norm); vec2 pos_box = pos_norm / max(pos_abs.x, pos_abs.y); vec2 pos_rot = pos_norm * mat2(vec2(0.7071067811865476, -0.7071067811865476), vec2(0.7071067811865476, 0.7071067811865476)); //is there a faster way to 45 degrees rot? float tex_ofs; - float distance; + float dist; if (pos_rot.y > 0.0) { if (pos_rot.x > 0.0) { tex_ofs = pos_box.y * 0.125 + 0.125; - distance = shadow_pos.x; + dist = shadow_pos.x; } else { tex_ofs = pos_box.x * -0.125 + (0.25 + 0.125); - distance = shadow_pos.y; + dist = shadow_pos.y; } } else { if (pos_rot.x < 0.0) { tex_ofs = pos_box.y * -0.125 + (0.5 + 0.125); - distance = -shadow_pos.x; + dist = -shadow_pos.x; } else { tex_ofs = pos_box.x * 0.125 + (0.75 + 0.125); - distance = -shadow_pos.y; + dist = -shadow_pos.y; } } - distance *= light_data[light_base].shadow_zfar_inv; + dist *= light_array[light_base].shadow_zfar_inv; //float distance = length(shadow_pos); - vec4 shadow_uv = vec4(tex_ofs, light_data[light_base].shadow_y_ofs, distance, 1.0); + vec4 shadow_uv = vec4(tex_ofs, light_array[light_base].shadow_y_ofs, dist, 1.0); light_color = light_shadow_compute(light_base, light_color, shadow_uv #ifdef LIGHT_CODE_USED @@ -690,7 +678,7 @@ void main() { light_blend_compute(light_base, light_color, color.rgb); } -#endif // UNSHADED +#endif frag_color = color; } |