canvas.glsl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /* clang-format off */
  2. #[modes]
  3. mode_quad =
  4. mode_ninepatch = #define USE_NINEPATCH
  5. mode_primitive = #define USE_PRIMITIVE
  6. mode_attributes = #define USE_ATTRIBUTES
  7. mode_instanced = #define USE_ATTRIBUTES \n#define USE_INSTANCING
  8. #[specializations]
  9. DISABLE_LIGHTING = false
  10. USE_RGBA_SHADOWS = false
  11. SINGLE_INSTANCE = false
  12. #[vertex]
  13. #ifdef USE_ATTRIBUTES
  14. layout(location = 0) in vec2 vertex_attrib;
  15. layout(location = 3) in vec4 color_attrib;
  16. layout(location = 4) in vec2 uv_attrib;
  17. #ifdef USE_INSTANCING
  18. layout(location = 1) in highp vec4 instance_xform0;
  19. layout(location = 2) in highp vec4 instance_xform1;
  20. layout(location = 5) in highp uvec4 instance_color_custom_data; // Color packed into xy, custom_data packed into zw for compatibility with 3D
  21. #endif // USE_INSTANCING
  22. #endif // USE_ATTRIBUTES
  23. #include "stdlib_inc.glsl"
  24. layout(location = 6) in highp vec4 attrib_A;
  25. layout(location = 7) in highp vec4 attrib_B;
  26. layout(location = 8) in highp vec4 attrib_C;
  27. layout(location = 9) in highp vec4 attrib_D;
  28. layout(location = 10) in highp vec4 attrib_E;
  29. #ifdef USE_PRIMITIVE
  30. layout(location = 11) in highp uvec4 attrib_F;
  31. #else
  32. layout(location = 11) in highp vec4 attrib_F;
  33. #endif
  34. layout(location = 12) in highp uvec4 attrib_G;
  35. layout(location = 13) in highp uvec4 attrib_H;
  36. #define read_draw_data_world_x attrib_A.xy
  37. #define read_draw_data_world_y attrib_A.zw
  38. #define read_draw_data_world_ofs attrib_B.xy
  39. #define read_draw_data_color_texture_pixel_size attrib_B.zw
  40. #ifdef USE_PRIMITIVE
  41. #define read_draw_data_point_a attrib_C.xy
  42. #define read_draw_data_point_b attrib_C.zw
  43. #define read_draw_data_point_c attrib_D.xy
  44. #define read_draw_data_uv_a attrib_D.zw
  45. #define read_draw_data_uv_b attrib_E.xy
  46. #define read_draw_data_uv_c attrib_E.zw
  47. #define read_draw_data_color_a_rg attrib_F.x
  48. #define read_draw_data_color_a_ba attrib_F.y
  49. #define read_draw_data_color_b_rg attrib_F.z
  50. #define read_draw_data_color_b_ba attrib_F.w
  51. #define read_draw_data_color_c_rg attrib_G.x
  52. #define read_draw_data_color_c_ba attrib_G.y
  53. #else
  54. #define read_draw_data_modulation attrib_C
  55. #define read_draw_data_ninepatch_margins attrib_D
  56. #define read_draw_data_dst_rect attrib_E
  57. #define read_draw_data_src_rect attrib_F
  58. #endif
  59. #define read_draw_data_flags attrib_G.z
  60. #define read_draw_data_specular_shininess attrib_G.w
  61. #define read_draw_data_lights attrib_H
  62. // Varyings so the per-instance info can be used in the fragment shader
  63. flat out vec4 varying_A;
  64. flat out vec2 varying_B;
  65. #ifndef USE_PRIMITIVE
  66. flat out vec4 varying_C;
  67. #ifndef USE_ATTRIBUTES
  68. #ifdef USE_NINEPATCH
  69. flat out vec2 varying_D;
  70. #endif
  71. flat out vec4 varying_E;
  72. #endif
  73. #endif
  74. flat out uvec2 varying_F;
  75. flat out uvec4 varying_G;
  76. // This needs to be outside clang-format so the ubo comment is in the right place
  77. #ifdef MATERIAL_UNIFORMS_USED
  78. layout(std140) uniform MaterialUniforms{ //ubo:4
  79. #MATERIAL_UNIFORMS
  80. };
  81. #endif
  82. /* clang-format on */
  83. #include "canvas_uniforms_inc.glsl"
  84. out vec2 uv_interp;
  85. out vec4 color_interp;
  86. out vec2 vertex_interp;
  87. #ifdef USE_NINEPATCH
  88. out vec2 pixel_size_interp;
  89. #endif
  90. #GLOBALS
  91. void main() {
  92. varying_A = vec4(read_draw_data_world_x, read_draw_data_world_y);
  93. varying_B = read_draw_data_color_texture_pixel_size;
  94. #ifndef USE_PRIMITIVE
  95. varying_C = read_draw_data_ninepatch_margins;
  96. #ifndef USE_ATTRIBUTES
  97. #ifdef USE_NINEPATCH
  98. varying_D = vec2(read_draw_data_dst_rect.z, read_draw_data_dst_rect.w);
  99. #endif // USE_NINEPATCH
  100. varying_E = read_draw_data_src_rect;
  101. #endif // !USE_ATTRIBUTES
  102. #endif // USE_PRIMITIVE
  103. varying_F = uvec2(read_draw_data_flags, read_draw_data_specular_shininess);
  104. varying_G = read_draw_data_lights;
  105. vec4 instance_custom = vec4(0.0);
  106. #ifdef USE_PRIMITIVE
  107. vec2 vertex;
  108. vec2 uv;
  109. vec4 color;
  110. if (gl_VertexID % 3 == 0) {
  111. vertex = read_draw_data_point_a;
  112. uv = read_draw_data_uv_a;
  113. color = vec4(unpackHalf2x16(read_draw_data_color_a_rg), unpackHalf2x16(read_draw_data_color_a_ba));
  114. } else if (gl_VertexID % 3 == 1) {
  115. vertex = read_draw_data_point_b;
  116. uv = read_draw_data_uv_b;
  117. color = vec4(unpackHalf2x16(read_draw_data_color_b_rg), unpackHalf2x16(read_draw_data_color_b_ba));
  118. } else {
  119. vertex = read_draw_data_point_c;
  120. uv = read_draw_data_uv_c;
  121. color = vec4(unpackHalf2x16(read_draw_data_color_c_rg), unpackHalf2x16(read_draw_data_color_c_ba));
  122. }
  123. #elif defined(USE_ATTRIBUTES)
  124. vec2 vertex = vertex_attrib;
  125. vec4 color = color_attrib * read_draw_data_modulation;
  126. vec2 uv = uv_attrib;
  127. #ifdef USE_INSTANCING
  128. if (bool(read_draw_data_flags & FLAGS_INSTANCING_HAS_COLORS)) {
  129. vec4 instance_color = vec4(unpackHalf2x16(instance_color_custom_data.x), unpackHalf2x16(instance_color_custom_data.y));
  130. color *= instance_color;
  131. }
  132. if (bool(read_draw_data_flags & FLAGS_INSTANCING_HAS_CUSTOM_DATA)) {
  133. instance_custom = vec4(unpackHalf2x16(instance_color_custom_data.z), unpackHalf2x16(instance_color_custom_data.w));
  134. }
  135. #endif
  136. #else
  137. 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));
  138. vec2 vertex_base = vertex_base_arr[gl_VertexID % 6];
  139. vec2 uv = read_draw_data_src_rect.xy + abs(read_draw_data_src_rect.zw) * ((read_draw_data_flags & FLAGS_TRANSPOSE_RECT) != uint(0) ? vertex_base.yx : vertex_base.xy);
  140. vec4 color = read_draw_data_modulation;
  141. vec2 vertex = read_draw_data_dst_rect.xy + abs(read_draw_data_dst_rect.zw) * mix(vertex_base, vec2(1.0, 1.0) - vertex_base, lessThan(read_draw_data_src_rect.zw, vec2(0.0, 0.0)));
  142. #endif
  143. mat4 model_matrix = mat4(vec4(read_draw_data_world_x, 0.0, 0.0), vec4(read_draw_data_world_y, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(read_draw_data_world_ofs, 0.0, 1.0));
  144. #ifdef USE_INSTANCING
  145. model_matrix = model_matrix * transpose(mat4(instance_xform0, instance_xform1, vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0)));
  146. #endif // USE_INSTANCING
  147. #if !defined(USE_ATTRIBUTES) && !defined(USE_PRIMITIVE)
  148. if (bool(read_draw_data_flags & FLAGS_USING_PARTICLES)) {
  149. //scale by texture size
  150. vertex /= read_draw_data_color_texture_pixel_size;
  151. }
  152. #endif
  153. vec2 color_texture_pixel_size = read_draw_data_color_texture_pixel_size;
  154. #ifdef USE_POINT_SIZE
  155. float point_size = 1.0;
  156. #endif
  157. {
  158. #CODE : VERTEX
  159. }
  160. #ifdef USE_NINEPATCH
  161. pixel_size_interp = abs(read_draw_data_dst_rect.zw) * vertex_base;
  162. #endif
  163. #if !defined(SKIP_TRANSFORM_USED)
  164. vertex = (model_matrix * vec4(vertex, 0.0, 1.0)).xy;
  165. #endif
  166. color_interp = color;
  167. if (use_pixel_snap) {
  168. vertex = floor(vertex + 0.5);
  169. // precision issue on some hardware creates artifacts within texture
  170. // offset uv by a small amount to avoid
  171. uv += 1e-5;
  172. }
  173. vertex = (canvas_transform * vec4(vertex, 0.0, 1.0)).xy;
  174. vertex_interp = vertex;
  175. uv_interp = uv;
  176. gl_Position = screen_transform * vec4(vertex, 0.0, 1.0);
  177. #ifdef USE_POINT_SIZE
  178. gl_PointSize = point_size;
  179. #endif
  180. }
  181. #[fragment]
  182. #include "canvas_uniforms_inc.glsl"
  183. #include "stdlib_inc.glsl"
  184. in vec2 uv_interp;
  185. in vec2 vertex_interp;
  186. in vec4 color_interp;
  187. #ifdef USE_NINEPATCH
  188. in vec2 pixel_size_interp;
  189. #endif
  190. // Can all be flat as they are the same for the whole batched instance
  191. flat in vec4 varying_A;
  192. flat in vec2 varying_B;
  193. #define read_draw_data_world_x varying_A.xy
  194. #define read_draw_data_world_y varying_A.zw
  195. #define read_draw_data_color_texture_pixel_size varying_B
  196. #ifndef USE_PRIMITIVE
  197. flat in vec4 varying_C;
  198. #define read_draw_data_ninepatch_margins varying_C
  199. #ifndef USE_ATTRIBUTES
  200. #ifdef USE_NINEPATCH
  201. flat in vec2 varying_D;
  202. #define read_draw_data_dst_rect_z varying_D.x
  203. #define read_draw_data_dst_rect_w varying_D.y
  204. #endif
  205. flat in vec4 varying_E;
  206. #define read_draw_data_src_rect varying_E
  207. #endif // USE_ATTRIBUTES
  208. #endif // USE_PRIMITIVE
  209. flat in uvec2 varying_F;
  210. flat in uvec4 varying_G;
  211. #define read_draw_data_flags varying_F.x
  212. #define read_draw_data_specular_shininess varying_F.y
  213. #define read_draw_data_lights varying_G
  214. #ifndef DISABLE_LIGHTING
  215. uniform sampler2D atlas_texture; //texunit:-2
  216. uniform sampler2D shadow_atlas_texture; //texunit:-3
  217. #endif // DISABLE_LIGHTING
  218. uniform sampler2D color_buffer; //texunit:-4
  219. uniform sampler2D sdf_texture; //texunit:-5
  220. uniform sampler2D normal_texture; //texunit:-6
  221. uniform sampler2D specular_texture; //texunit:-7
  222. uniform sampler2D color_texture; //texunit:0
  223. layout(location = 0) out vec4 frag_color;
  224. #ifdef MATERIAL_UNIFORMS_USED
  225. layout(std140) uniform MaterialUniforms{
  226. //ubo:4
  227. #MATERIAL_UNIFORMS
  228. };
  229. #endif
  230. #GLOBALS
  231. float vec4_to_float(vec4 p_vec) {
  232. 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;
  233. }
  234. vec2 screen_uv_to_sdf(vec2 p_uv) {
  235. return screen_to_sdf * p_uv;
  236. }
  237. float texture_sdf(vec2 p_sdf) {
  238. vec2 uv = p_sdf * sdf_to_tex.xy + sdf_to_tex.zw;
  239. float d = vec4_to_float(texture(sdf_texture, uv));
  240. d *= SDF_MAX_LENGTH;
  241. return d * tex_to_sdf;
  242. }
  243. vec2 texture_sdf_normal(vec2 p_sdf) {
  244. vec2 uv = p_sdf * sdf_to_tex.xy + sdf_to_tex.zw;
  245. const float EPSILON = 0.001;
  246. return normalize(vec2(
  247. vec4_to_float(texture(sdf_texture, uv + vec2(EPSILON, 0.0))) - vec4_to_float(texture(sdf_texture, uv - vec2(EPSILON, 0.0))),
  248. vec4_to_float(texture(sdf_texture, uv + vec2(0.0, EPSILON))) - vec4_to_float(texture(sdf_texture, uv - vec2(0.0, EPSILON)))));
  249. }
  250. vec2 sdf_to_screen_uv(vec2 p_sdf) {
  251. return p_sdf * sdf_to_screen;
  252. }
  253. #ifndef DISABLE_LIGHTING
  254. #ifdef LIGHT_CODE_USED
  255. vec4 light_compute(
  256. vec3 light_vertex,
  257. vec3 light_position,
  258. vec3 normal,
  259. vec4 light_color,
  260. float light_energy,
  261. vec4 specular_shininess,
  262. inout vec4 shadow_modulate,
  263. vec2 screen_uv,
  264. vec2 uv,
  265. vec4 color, bool is_directional) {
  266. vec4 light = vec4(0.0);
  267. vec3 light_direction = vec3(0.0);
  268. if (is_directional) {
  269. light_direction = normalize(mix(vec3(light_position.xy, 0.0), vec3(0, 0, 1), light_position.z));
  270. light_position = vec3(0.0);
  271. } else {
  272. light_direction = normalize(light_position - light_vertex);
  273. }
  274. #CODE : LIGHT
  275. return light;
  276. }
  277. #endif
  278. vec3 light_normal_compute(vec3 light_vec, vec3 normal, vec3 base_color, vec3 light_color, vec4 specular_shininess, bool specular_shininess_used) {
  279. float cNdotL = max(0.0, dot(normal, light_vec));
  280. if (specular_shininess_used) {
  281. //blinn
  282. vec3 view = vec3(0.0, 0.0, 1.0); // not great but good enough
  283. vec3 half_vec = normalize(view + light_vec);
  284. float cNdotV = max(dot(normal, view), 0.0);
  285. float cNdotH = max(dot(normal, half_vec), 0.0);
  286. float cVdotH = max(dot(view, half_vec), 0.0);
  287. float cLdotH = max(dot(light_vec, half_vec), 0.0);
  288. float shininess = exp2(15.0 * specular_shininess.a + 1.0) * 0.25;
  289. float blinn = pow(cNdotH, shininess);
  290. blinn *= (shininess + 8.0) * (1.0 / (8.0 * M_PI));
  291. float s = (blinn) / max(4.0 * cNdotV * cNdotL, 0.75);
  292. return specular_shininess.rgb * light_color * s + light_color * base_color * cNdotL;
  293. } else {
  294. return light_color * base_color * cNdotL;
  295. }
  296. }
  297. #ifdef USE_RGBA_SHADOWS
  298. #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)
  299. #else
  300. #define SHADOW_DEPTH(m_uv) (textureLod(shadow_atlas_texture, (m_uv), 0.0).r)
  301. #endif
  302. /* clang-format off */
  303. #define SHADOW_TEST(m_uv) { highp float sd = SHADOW_DEPTH(m_uv); shadow += step(sd, shadow_uv.z / shadow_uv.w); }
  304. /* clang-format on */
  305. //float distance = length(shadow_pos);
  306. vec4 light_shadow_compute(uint light_base, vec4 light_color, vec4 shadow_uv
  307. #ifdef LIGHT_CODE_USED
  308. ,
  309. vec3 shadow_modulate
  310. #endif
  311. ) {
  312. float shadow = 0.0;
  313. uint shadow_mode = light_array[light_base].flags & LIGHT_FLAGS_FILTER_MASK;
  314. if (shadow_mode == LIGHT_FLAGS_SHADOW_NEAREST) {
  315. SHADOW_TEST(shadow_uv.xy);
  316. } else if (shadow_mode == LIGHT_FLAGS_SHADOW_PCF5) {
  317. vec2 shadow_pixel_size = vec2(light_array[light_base].shadow_pixel_size, 0.0);
  318. SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 2.0);
  319. SHADOW_TEST(shadow_uv.xy - shadow_pixel_size);
  320. SHADOW_TEST(shadow_uv.xy);
  321. SHADOW_TEST(shadow_uv.xy + shadow_pixel_size);
  322. SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 2.0);
  323. shadow /= 5.0;
  324. } else { //PCF13
  325. vec2 shadow_pixel_size = vec2(light_array[light_base].shadow_pixel_size, 0.0);
  326. SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 6.0);
  327. SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 5.0);
  328. SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 4.0);
  329. SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 3.0);
  330. SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 2.0);
  331. SHADOW_TEST(shadow_uv.xy - shadow_pixel_size);
  332. SHADOW_TEST(shadow_uv.xy);
  333. SHADOW_TEST(shadow_uv.xy + shadow_pixel_size);
  334. SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 2.0);
  335. SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 3.0);
  336. SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 4.0);
  337. SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 5.0);
  338. SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 6.0);
  339. shadow /= 13.0;
  340. }
  341. vec4 shadow_color = godot_unpackUnorm4x8(light_array[light_base].shadow_color);
  342. #ifdef LIGHT_CODE_USED
  343. shadow_color.rgb *= shadow_modulate;
  344. #endif
  345. shadow_color.a *= light_color.a; //respect light alpha
  346. return mix(light_color, shadow_color, shadow);
  347. }
  348. void light_blend_compute(uint light_base, vec4 light_color, inout vec3 color) {
  349. uint blend_mode = light_array[light_base].flags & LIGHT_FLAGS_BLEND_MASK;
  350. switch (blend_mode) {
  351. case LIGHT_FLAGS_BLEND_MODE_ADD: {
  352. color.rgb += light_color.rgb * light_color.a;
  353. } break;
  354. case LIGHT_FLAGS_BLEND_MODE_SUB: {
  355. color.rgb -= light_color.rgb * light_color.a;
  356. } break;
  357. case LIGHT_FLAGS_BLEND_MODE_MIX: {
  358. color.rgb = mix(color.rgb, light_color.rgb, light_color.a);
  359. } break;
  360. }
  361. }
  362. #endif
  363. #ifdef USE_NINEPATCH
  364. 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) {
  365. float tex_size = 1.0 / tex_pixel_size;
  366. if (pixel < margin_begin) {
  367. return pixel * tex_pixel_size;
  368. } else if (pixel >= draw_size - margin_end) {
  369. return (tex_size - (draw_size - pixel)) * tex_pixel_size;
  370. } else {
  371. if (!bool(read_draw_data_flags & FLAGS_NINEPACH_DRAW_CENTER)) {
  372. draw_center--;
  373. }
  374. // np_repeat is passed as uniform using NinePatchRect::AxisStretchMode enum.
  375. if (np_repeat == 0) { // Stretch.
  376. // Convert to ratio.
  377. float ratio = (pixel - margin_begin) / (draw_size - margin_begin - margin_end);
  378. // Scale to source texture.
  379. return (margin_begin + ratio * (tex_size - margin_begin - margin_end)) * tex_pixel_size;
  380. } else if (np_repeat == 1) { // Tile.
  381. // Convert to offset.
  382. float ofs = mod((pixel - margin_begin), tex_size - margin_begin - margin_end);
  383. // Scale to source texture.
  384. return (margin_begin + ofs) * tex_pixel_size;
  385. } else if (np_repeat == 2) { // Tile Fit.
  386. // Calculate scale.
  387. float src_area = draw_size - margin_begin - margin_end;
  388. float dst_area = tex_size - margin_begin - margin_end;
  389. float scale = max(1.0, floor(src_area / max(dst_area, 0.0000001) + 0.5));
  390. // Convert to ratio.
  391. float ratio = (pixel - margin_begin) / src_area;
  392. ratio = mod(ratio * scale, 1.0);
  393. // Scale to source texture.
  394. return (margin_begin + ratio * dst_area) * tex_pixel_size;
  395. } else { // Shouldn't happen, but silences compiler warning.
  396. return 0.0;
  397. }
  398. }
  399. }
  400. #endif
  401. float msdf_median(float r, float g, float b, float a) {
  402. return min(max(min(r, g), min(max(r, g), b)), a);
  403. }
  404. void main() {
  405. vec4 color = color_interp;
  406. vec2 uv = uv_interp;
  407. vec2 vertex = vertex_interp;
  408. #if !defined(USE_ATTRIBUTES) && !defined(USE_PRIMITIVE)
  409. #ifdef USE_NINEPATCH
  410. int draw_center = 2;
  411. uv = vec2(
  412. map_ninepatch_axis(pixel_size_interp.x, abs(read_draw_data_dst_rect_z), read_draw_data_color_texture_pixel_size.x, read_draw_data_ninepatch_margins.x, read_draw_data_ninepatch_margins.z, int(read_draw_data_flags >> FLAGS_NINEPATCH_H_MODE_SHIFT) & 0x3, draw_center),
  413. map_ninepatch_axis(pixel_size_interp.y, abs(read_draw_data_dst_rect_w), read_draw_data_color_texture_pixel_size.y, read_draw_data_ninepatch_margins.y, read_draw_data_ninepatch_margins.w, int(read_draw_data_flags >> FLAGS_NINEPATCH_V_MODE_SHIFT) & 0x3, draw_center));
  414. if (draw_center == 0) {
  415. color.a = 0.0;
  416. }
  417. uv = uv * read_draw_data_src_rect.zw + read_draw_data_src_rect.xy; //apply region if needed
  418. #endif
  419. if (bool(read_draw_data_flags & FLAGS_CLIP_RECT_UV)) {
  420. uv = clamp(uv, read_draw_data_src_rect.xy, read_draw_data_src_rect.xy + abs(read_draw_data_src_rect.zw));
  421. }
  422. #endif
  423. #ifndef USE_PRIMITIVE
  424. if (bool(read_draw_data_flags & FLAGS_USE_MSDF)) {
  425. float px_range = read_draw_data_ninepatch_margins.x;
  426. float outline_thickness = read_draw_data_ninepatch_margins.y;
  427. vec4 msdf_sample = texture(color_texture, uv);
  428. vec2 msdf_size = vec2(textureSize(color_texture, 0));
  429. vec2 dest_size = vec2(1.0) / fwidth(uv);
  430. float px_size = max(0.5 * dot((vec2(px_range) / msdf_size), dest_size), 1.0);
  431. float d = msdf_median(msdf_sample.r, msdf_sample.g, msdf_sample.b, msdf_sample.a) - 0.5;
  432. if (outline_thickness > 0.0) {
  433. float cr = clamp(outline_thickness, 0.0, px_range / 2.0) / px_range;
  434. float a = clamp((d + cr) * px_size, 0.0, 1.0);
  435. color.a = a * color.a;
  436. } else {
  437. float a = clamp(d * px_size + 0.5, 0.0, 1.0);
  438. color.a = a * color.a;
  439. }
  440. } else if (bool(read_draw_data_flags & FLAGS_USE_LCD)) {
  441. vec4 lcd_sample = texture(color_texture, uv);
  442. if (lcd_sample.a == 1.0) {
  443. color.rgb = lcd_sample.rgb * color.a;
  444. } else {
  445. color = vec4(0.0, 0.0, 0.0, 0.0);
  446. }
  447. } else {
  448. #else
  449. {
  450. #endif
  451. color *= texture(color_texture, uv);
  452. }
  453. uint light_count = (read_draw_data_flags >> uint(FLAGS_LIGHT_COUNT_SHIFT)) & uint(0xF); //max 16 lights
  454. bool using_light = light_count > 0u || directional_light_count > 0u;
  455. vec3 normal;
  456. #if defined(NORMAL_USED)
  457. bool normal_used = true;
  458. #else
  459. bool normal_used = false;
  460. #endif
  461. if (normal_used || (using_light && bool(read_draw_data_flags & FLAGS_DEFAULT_NORMAL_MAP_USED))) {
  462. normal.xy = texture(normal_texture, uv).xy * vec2(2.0, -2.0) - vec2(1.0, -1.0);
  463. if (bool(read_draw_data_flags & FLAGS_FLIP_H)) {
  464. normal.x = -normal.x;
  465. }
  466. if (bool(read_draw_data_flags & FLAGS_FLIP_V)) {
  467. normal.y = -normal.y;
  468. }
  469. normal.z = sqrt(max(0.0, 1.0 - dot(normal.xy, normal.xy)));
  470. normal_used = true;
  471. } else {
  472. normal = vec3(0.0, 0.0, 1.0);
  473. }
  474. vec4 specular_shininess;
  475. #if defined(SPECULAR_SHININESS_USED)
  476. bool specular_shininess_used = true;
  477. #else
  478. bool specular_shininess_used = false;
  479. #endif
  480. if (specular_shininess_used || (using_light && normal_used && bool(read_draw_data_flags & FLAGS_DEFAULT_SPECULAR_MAP_USED))) {
  481. specular_shininess = texture(specular_texture, uv);
  482. specular_shininess *= godot_unpackUnorm4x8(read_draw_data_specular_shininess);
  483. specular_shininess_used = true;
  484. } else {
  485. specular_shininess = vec4(1.0);
  486. }
  487. #if defined(SCREEN_UV_USED)
  488. vec2 screen_uv = gl_FragCoord.xy * screen_pixel_size;
  489. #else
  490. vec2 screen_uv = vec2(0.0);
  491. #endif
  492. vec2 color_texture_pixel_size = read_draw_data_color_texture_pixel_size.xy;
  493. vec3 light_vertex = vec3(vertex, 0.0);
  494. vec2 shadow_vertex = vertex;
  495. {
  496. float normal_map_depth = 1.0;
  497. #if defined(NORMAL_MAP_USED)
  498. vec3 normal_map = vec3(0.0, 0.0, 1.0);
  499. normal_used = true;
  500. #endif
  501. #CODE : FRAGMENT
  502. #if defined(NORMAL_MAP_USED)
  503. normal = mix(vec3(0.0, 0.0, 1.0), normal_map * vec3(2.0, -2.0, 1.0) - vec3(1.0, -1.0, 0.0), normal_map_depth);
  504. #endif
  505. }
  506. if (normal_used) {
  507. //convert by item transform
  508. normal.xy = mat2(normalize(read_draw_data_world_x), normalize(read_draw_data_world_y)) * normal.xy;
  509. //convert by canvas transform
  510. normal = normalize((canvas_normal_transform * vec4(normal, 0.0)).xyz);
  511. }
  512. vec4 base_color = color;
  513. #ifdef MODE_LIGHT_ONLY
  514. color = vec4(0.0);
  515. #elif !defined(MODE_UNSHADED)
  516. color *= canvas_modulation;
  517. #endif
  518. #if !defined(DISABLE_LIGHTING) && !defined(MODE_UNSHADED)
  519. // Directional Lights
  520. for (uint i = 0u; i < directional_light_count; i++) {
  521. uint light_base = i;
  522. vec2 direction = light_array[light_base].position;
  523. vec4 light_color = light_array[light_base].color;
  524. #ifdef LIGHT_CODE_USED
  525. vec4 shadow_modulate = vec4(1.0);
  526. 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);
  527. #else
  528. if (normal_used) {
  529. vec3 light_vec = normalize(mix(vec3(direction, 0.0), vec3(0, 0, 1), light_array[light_base].height));
  530. light_color.rgb = light_normal_compute(light_vec, normal, base_color.rgb, light_color.rgb, specular_shininess, specular_shininess_used);
  531. } else {
  532. light_color.rgb *= base_color.rgb;
  533. }
  534. #endif
  535. if (bool(light_array[light_base].flags & LIGHT_FLAGS_HAS_SHADOW)) {
  536. 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.
  537. 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);
  538. light_color = light_shadow_compute(light_base, light_color, shadow_uv
  539. #ifdef LIGHT_CODE_USED
  540. ,
  541. shadow_modulate.rgb
  542. #endif
  543. );
  544. }
  545. light_blend_compute(light_base, light_color, color.rgb);
  546. }
  547. // Positional Lights
  548. for (uint i = 0u; i < MAX_LIGHTS_PER_ITEM; i++) {
  549. if (i >= light_count) {
  550. break;
  551. }
  552. uint light_base;
  553. if (i < 8u) {
  554. if (i < 4u) {
  555. light_base = read_draw_data_lights[0];
  556. } else {
  557. light_base = read_draw_data_lights[1];
  558. }
  559. } else {
  560. if (i < 12u) {
  561. light_base = read_draw_data_lights[2];
  562. } else {
  563. light_base = read_draw_data_lights[3];
  564. }
  565. }
  566. light_base >>= (i & 3u) * 8u;
  567. light_base &= uint(0xFF);
  568. 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.
  569. vec2 tex_uv_atlas = tex_uv * light_array[light_base].atlas_rect.zw + light_array[light_base].atlas_rect.xy;
  570. vec4 light_color = textureLod(atlas_texture, tex_uv_atlas, 0.0);
  571. vec4 light_base_color = light_array[light_base].color;
  572. #ifdef LIGHT_CODE_USED
  573. vec4 shadow_modulate = vec4(1.0);
  574. vec3 light_position = vec3(light_array[light_base].position, light_array[light_base].height);
  575. light_color.rgb *= light_base_color.rgb;
  576. 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);
  577. #else
  578. light_color.rgb *= light_base_color.rgb * light_base_color.a;
  579. if (normal_used) {
  580. vec3 light_pos = vec3(light_array[light_base].position, light_array[light_base].height);
  581. vec3 pos = light_vertex;
  582. vec3 light_vec = normalize(light_pos - pos);
  583. light_color.rgb = light_normal_compute(light_vec, normal, base_color.rgb, light_color.rgb, specular_shininess, specular_shininess_used);
  584. } else {
  585. light_color.rgb *= base_color.rgb;
  586. }
  587. #endif
  588. if (any(lessThan(tex_uv, vec2(0.0, 0.0))) || any(greaterThanEqual(tex_uv, vec2(1.0, 1.0)))) {
  589. //if outside the light texture, light color is zero
  590. light_color.a = 0.0;
  591. }
  592. if (bool(light_array[light_base].flags & LIGHT_FLAGS_HAS_SHADOW)) {
  593. 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.
  594. vec2 pos_norm = normalize(shadow_pos);
  595. vec2 pos_abs = abs(pos_norm);
  596. vec2 pos_box = pos_norm / max(pos_abs.x, pos_abs.y);
  597. 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?
  598. float tex_ofs;
  599. float dist;
  600. if (pos_rot.y > 0.0) {
  601. if (pos_rot.x > 0.0) {
  602. tex_ofs = pos_box.y * 0.125 + 0.125;
  603. dist = shadow_pos.x;
  604. } else {
  605. tex_ofs = pos_box.x * -0.125 + (0.25 + 0.125);
  606. dist = shadow_pos.y;
  607. }
  608. } else {
  609. if (pos_rot.x < 0.0) {
  610. tex_ofs = pos_box.y * -0.125 + (0.5 + 0.125);
  611. dist = -shadow_pos.x;
  612. } else {
  613. tex_ofs = pos_box.x * 0.125 + (0.75 + 0.125);
  614. dist = -shadow_pos.y;
  615. }
  616. }
  617. dist *= light_array[light_base].shadow_zfar_inv;
  618. //float distance = length(shadow_pos);
  619. vec4 shadow_uv = vec4(tex_ofs, light_array[light_base].shadow_y_ofs, dist, 1.0);
  620. light_color = light_shadow_compute(light_base, light_color, shadow_uv
  621. #ifdef LIGHT_CODE_USED
  622. ,
  623. shadow_modulate.rgb
  624. #endif
  625. );
  626. }
  627. light_blend_compute(light_base, light_color, color.rgb);
  628. }
  629. #endif
  630. frag_color = color;
  631. }