canvas.glsl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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 = true
  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. #ifdef USE_WORLD_VERTEX_COORDS
  158. vertex = (model_matrix * vec4(vertex, 0.0, 1.0)).xy;
  159. #endif
  160. {
  161. #CODE : VERTEX
  162. }
  163. #ifdef USE_NINEPATCH
  164. pixel_size_interp = abs(read_draw_data_dst_rect.zw) * vertex_base;
  165. #endif
  166. #if !defined(SKIP_TRANSFORM_USED) && !defined(USE_WORLD_VERTEX_COORDS)
  167. vertex = (model_matrix * vec4(vertex, 0.0, 1.0)).xy;
  168. #endif
  169. color_interp = color;
  170. if (use_pixel_snap) {
  171. vertex = floor(vertex + 0.5);
  172. // precision issue on some hardware creates artifacts within texture
  173. // offset uv by a small amount to avoid
  174. uv += 1e-5;
  175. }
  176. vertex = (canvas_transform * vec4(vertex, 0.0, 1.0)).xy;
  177. vertex_interp = vertex;
  178. uv_interp = uv;
  179. gl_Position = screen_transform * vec4(vertex, 0.0, 1.0);
  180. #ifdef USE_POINT_SIZE
  181. gl_PointSize = point_size;
  182. #endif
  183. }
  184. #[fragment]
  185. #include "canvas_uniforms_inc.glsl"
  186. #include "stdlib_inc.glsl"
  187. in vec2 uv_interp;
  188. in vec2 vertex_interp;
  189. in vec4 color_interp;
  190. #ifdef USE_NINEPATCH
  191. in vec2 pixel_size_interp;
  192. #endif
  193. // Can all be flat as they are the same for the whole batched instance
  194. flat in vec4 varying_A;
  195. flat in vec2 varying_B;
  196. #define read_draw_data_world_x varying_A.xy
  197. #define read_draw_data_world_y varying_A.zw
  198. #define read_draw_data_color_texture_pixel_size varying_B
  199. #ifndef USE_PRIMITIVE
  200. flat in vec4 varying_C;
  201. #define read_draw_data_ninepatch_margins varying_C
  202. #ifndef USE_ATTRIBUTES
  203. #ifdef USE_NINEPATCH
  204. flat in vec2 varying_D;
  205. #define read_draw_data_dst_rect_z varying_D.x
  206. #define read_draw_data_dst_rect_w varying_D.y
  207. #endif
  208. flat in vec4 varying_E;
  209. #define read_draw_data_src_rect varying_E
  210. #endif // USE_ATTRIBUTES
  211. #endif // USE_PRIMITIVE
  212. flat in uvec2 varying_F;
  213. flat in uvec4 varying_G;
  214. #define read_draw_data_flags varying_F.x
  215. #define read_draw_data_specular_shininess varying_F.y
  216. #define read_draw_data_lights varying_G
  217. #ifndef DISABLE_LIGHTING
  218. uniform sampler2D atlas_texture; //texunit:-2
  219. uniform sampler2D shadow_atlas_texture; //texunit:-3
  220. #endif // DISABLE_LIGHTING
  221. uniform sampler2D color_buffer; //texunit:-4
  222. uniform sampler2D sdf_texture; //texunit:-5
  223. uniform sampler2D normal_texture; //texunit:-6
  224. uniform sampler2D specular_texture; //texunit:-7
  225. uniform sampler2D color_texture; //texunit:0
  226. layout(location = 0) out vec4 frag_color;
  227. #ifdef MATERIAL_UNIFORMS_USED
  228. layout(std140) uniform MaterialUniforms{
  229. //ubo:4
  230. #MATERIAL_UNIFORMS
  231. };
  232. #endif
  233. #GLOBALS
  234. float vec4_to_float(vec4 p_vec) {
  235. 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;
  236. }
  237. vec2 screen_uv_to_sdf(vec2 p_uv) {
  238. return screen_to_sdf * p_uv;
  239. }
  240. float texture_sdf(vec2 p_sdf) {
  241. vec2 uv = p_sdf * sdf_to_tex.xy + sdf_to_tex.zw;
  242. float d = vec4_to_float(texture(sdf_texture, uv));
  243. d *= SDF_MAX_LENGTH;
  244. return d * tex_to_sdf;
  245. }
  246. vec2 texture_sdf_normal(vec2 p_sdf) {
  247. vec2 uv = p_sdf * sdf_to_tex.xy + sdf_to_tex.zw;
  248. const float EPSILON = 0.001;
  249. return normalize(vec2(
  250. vec4_to_float(texture(sdf_texture, uv + vec2(EPSILON, 0.0))) - vec4_to_float(texture(sdf_texture, uv - vec2(EPSILON, 0.0))),
  251. vec4_to_float(texture(sdf_texture, uv + vec2(0.0, EPSILON))) - vec4_to_float(texture(sdf_texture, uv - vec2(0.0, EPSILON)))));
  252. }
  253. vec2 sdf_to_screen_uv(vec2 p_sdf) {
  254. return p_sdf * sdf_to_screen;
  255. }
  256. #ifndef DISABLE_LIGHTING
  257. #ifdef LIGHT_CODE_USED
  258. vec4 light_compute(
  259. vec3 light_vertex,
  260. vec3 light_position,
  261. vec3 normal,
  262. vec4 light_color,
  263. float light_energy,
  264. vec4 specular_shininess,
  265. inout vec4 shadow_modulate,
  266. vec2 screen_uv,
  267. vec2 uv,
  268. vec4 color, bool is_directional) {
  269. vec4 light = vec4(0.0);
  270. vec3 light_direction = vec3(0.0);
  271. if (is_directional) {
  272. light_direction = normalize(mix(vec3(light_position.xy, 0.0), vec3(0, 0, 1), light_position.z));
  273. light_position = vec3(0.0);
  274. } else {
  275. light_direction = normalize(light_position - light_vertex);
  276. }
  277. #CODE : LIGHT
  278. return light;
  279. }
  280. #endif
  281. vec3 light_normal_compute(vec3 light_vec, vec3 normal, vec3 base_color, vec3 light_color, vec4 specular_shininess, bool specular_shininess_used) {
  282. float cNdotL = max(0.0, dot(normal, light_vec));
  283. if (specular_shininess_used) {
  284. //blinn
  285. vec3 view = vec3(0.0, 0.0, 1.0); // not great but good enough
  286. vec3 half_vec = normalize(view + light_vec);
  287. float cNdotV = max(dot(normal, view), 0.0);
  288. float cNdotH = max(dot(normal, half_vec), 0.0);
  289. float cVdotH = max(dot(view, half_vec), 0.0);
  290. float cLdotH = max(dot(light_vec, half_vec), 0.0);
  291. float shininess = exp2(15.0 * specular_shininess.a + 1.0) * 0.25;
  292. float blinn = pow(cNdotH, shininess);
  293. blinn *= (shininess + 8.0) * (1.0 / (8.0 * M_PI));
  294. float s = (blinn) / max(4.0 * cNdotV * cNdotL, 0.75);
  295. return specular_shininess.rgb * light_color * s + light_color * base_color * cNdotL;
  296. } else {
  297. return light_color * base_color * cNdotL;
  298. }
  299. }
  300. #ifdef USE_RGBA_SHADOWS
  301. #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)
  302. #else
  303. #define SHADOW_DEPTH(m_uv) (textureLod(shadow_atlas_texture, (m_uv), 0.0).r)
  304. #endif
  305. /* clang-format off */
  306. #define SHADOW_TEST(m_uv) { highp float sd = SHADOW_DEPTH(m_uv); shadow += step(sd, shadow_uv.z / shadow_uv.w); }
  307. /* clang-format on */
  308. //float distance = length(shadow_pos);
  309. vec4 light_shadow_compute(uint light_base, vec4 light_color, vec4 shadow_uv
  310. #ifdef LIGHT_CODE_USED
  311. ,
  312. vec3 shadow_modulate
  313. #endif
  314. ) {
  315. float shadow = 0.0;
  316. uint shadow_mode = light_array[light_base].flags & LIGHT_FLAGS_FILTER_MASK;
  317. if (shadow_mode == LIGHT_FLAGS_SHADOW_NEAREST) {
  318. SHADOW_TEST(shadow_uv.xy);
  319. } else if (shadow_mode == LIGHT_FLAGS_SHADOW_PCF5) {
  320. vec2 shadow_pixel_size = vec2(light_array[light_base].shadow_pixel_size, 0.0);
  321. SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 2.0);
  322. SHADOW_TEST(shadow_uv.xy - shadow_pixel_size);
  323. SHADOW_TEST(shadow_uv.xy);
  324. SHADOW_TEST(shadow_uv.xy + shadow_pixel_size);
  325. SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 2.0);
  326. shadow /= 5.0;
  327. } else { //PCF13
  328. vec2 shadow_pixel_size = vec2(light_array[light_base].shadow_pixel_size, 0.0);
  329. SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 6.0);
  330. SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 5.0);
  331. SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 4.0);
  332. SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 3.0);
  333. SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 2.0);
  334. SHADOW_TEST(shadow_uv.xy - shadow_pixel_size);
  335. SHADOW_TEST(shadow_uv.xy);
  336. SHADOW_TEST(shadow_uv.xy + shadow_pixel_size);
  337. SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 2.0);
  338. SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 3.0);
  339. SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 4.0);
  340. SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 5.0);
  341. SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 6.0);
  342. shadow /= 13.0;
  343. }
  344. vec4 shadow_color = godot_unpackUnorm4x8(light_array[light_base].shadow_color);
  345. #ifdef LIGHT_CODE_USED
  346. shadow_color.rgb *= shadow_modulate;
  347. #endif
  348. shadow_color.a *= light_color.a; //respect light alpha
  349. return mix(light_color, shadow_color, shadow);
  350. }
  351. void light_blend_compute(uint light_base, vec4 light_color, inout vec3 color) {
  352. uint blend_mode = light_array[light_base].flags & LIGHT_FLAGS_BLEND_MASK;
  353. switch (blend_mode) {
  354. case LIGHT_FLAGS_BLEND_MODE_ADD: {
  355. color.rgb += light_color.rgb * light_color.a;
  356. } break;
  357. case LIGHT_FLAGS_BLEND_MODE_SUB: {
  358. color.rgb -= light_color.rgb * light_color.a;
  359. } break;
  360. case LIGHT_FLAGS_BLEND_MODE_MIX: {
  361. color.rgb = mix(color.rgb, light_color.rgb, light_color.a);
  362. } break;
  363. }
  364. }
  365. #endif
  366. #ifdef USE_NINEPATCH
  367. 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) {
  368. float tex_size = 1.0 / tex_pixel_size;
  369. if (pixel < margin_begin) {
  370. return pixel * tex_pixel_size;
  371. } else if (pixel >= draw_size - margin_end) {
  372. return (tex_size - (draw_size - pixel)) * tex_pixel_size;
  373. } else {
  374. if (!bool(read_draw_data_flags & FLAGS_NINEPACH_DRAW_CENTER)) {
  375. draw_center--;
  376. }
  377. // np_repeat is passed as uniform using NinePatchRect::AxisStretchMode enum.
  378. if (np_repeat == 0) { // Stretch.
  379. // Convert to ratio.
  380. float ratio = (pixel - margin_begin) / (draw_size - margin_begin - margin_end);
  381. // Scale to source texture.
  382. return (margin_begin + ratio * (tex_size - margin_begin - margin_end)) * tex_pixel_size;
  383. } else if (np_repeat == 1) { // Tile.
  384. // Convert to offset.
  385. float ofs = mod((pixel - margin_begin), tex_size - margin_begin - margin_end);
  386. // Scale to source texture.
  387. return (margin_begin + ofs) * tex_pixel_size;
  388. } else if (np_repeat == 2) { // Tile Fit.
  389. // Calculate scale.
  390. float src_area = draw_size - margin_begin - margin_end;
  391. float dst_area = tex_size - margin_begin - margin_end;
  392. float scale = max(1.0, floor(src_area / max(dst_area, 0.0000001) + 0.5));
  393. // Convert to ratio.
  394. float ratio = (pixel - margin_begin) / src_area;
  395. ratio = mod(ratio * scale, 1.0);
  396. // Scale to source texture.
  397. return (margin_begin + ratio * dst_area) * tex_pixel_size;
  398. } else { // Shouldn't happen, but silences compiler warning.
  399. return 0.0;
  400. }
  401. }
  402. }
  403. #endif
  404. float msdf_median(float r, float g, float b, float a) {
  405. return min(max(min(r, g), min(max(r, g), b)), a);
  406. }
  407. void main() {
  408. vec4 color = color_interp;
  409. vec2 uv = uv_interp;
  410. vec2 vertex = vertex_interp;
  411. #if !defined(USE_ATTRIBUTES) && !defined(USE_PRIMITIVE)
  412. #ifdef USE_NINEPATCH
  413. int draw_center = 2;
  414. uv = vec2(
  415. 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),
  416. 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));
  417. if (draw_center == 0) {
  418. color.a = 0.0;
  419. }
  420. uv = uv * read_draw_data_src_rect.zw + read_draw_data_src_rect.xy; //apply region if needed
  421. #endif
  422. if (bool(read_draw_data_flags & FLAGS_CLIP_RECT_UV)) {
  423. uv = clamp(uv, read_draw_data_src_rect.xy, read_draw_data_src_rect.xy + abs(read_draw_data_src_rect.zw));
  424. }
  425. #endif
  426. #ifndef USE_PRIMITIVE
  427. if (bool(read_draw_data_flags & FLAGS_USE_MSDF)) {
  428. float px_range = read_draw_data_ninepatch_margins.x;
  429. float outline_thickness = read_draw_data_ninepatch_margins.y;
  430. vec4 msdf_sample = texture(color_texture, uv);
  431. vec2 msdf_size = vec2(textureSize(color_texture, 0));
  432. vec2 dest_size = vec2(1.0) / fwidth(uv);
  433. float px_size = max(0.5 * dot((vec2(px_range) / msdf_size), dest_size), 1.0);
  434. float d = msdf_median(msdf_sample.r, msdf_sample.g, msdf_sample.b, msdf_sample.a) - 0.5;
  435. if (outline_thickness > 0.0) {
  436. float cr = clamp(outline_thickness, 0.0, px_range / 2.0) / px_range;
  437. float a = clamp((d + cr) * px_size, 0.0, 1.0);
  438. color.a = a * color.a;
  439. } else {
  440. float a = clamp(d * px_size + 0.5, 0.0, 1.0);
  441. color.a = a * color.a;
  442. }
  443. } else if (bool(read_draw_data_flags & FLAGS_USE_LCD)) {
  444. vec4 lcd_sample = texture(color_texture, uv);
  445. if (lcd_sample.a == 1.0) {
  446. color.rgb = lcd_sample.rgb * color.a;
  447. } else {
  448. color = vec4(0.0, 0.0, 0.0, 0.0);
  449. }
  450. } else {
  451. #else
  452. {
  453. #endif
  454. color *= texture(color_texture, uv);
  455. }
  456. uint light_count = (read_draw_data_flags >> uint(FLAGS_LIGHT_COUNT_SHIFT)) & uint(0xF); //max 16 lights
  457. bool using_light = light_count > 0u || directional_light_count > 0u;
  458. vec3 normal;
  459. #if defined(NORMAL_USED)
  460. bool normal_used = true;
  461. #else
  462. bool normal_used = false;
  463. #endif
  464. if (normal_used || (using_light && bool(read_draw_data_flags & FLAGS_DEFAULT_NORMAL_MAP_USED))) {
  465. normal.xy = texture(normal_texture, uv).xy * vec2(2.0, -2.0) - vec2(1.0, -1.0);
  466. if (bool(read_draw_data_flags & FLAGS_TRANSPOSE_RECT)) {
  467. normal.xy = normal.yx;
  468. }
  469. if (bool(read_draw_data_flags & FLAGS_FLIP_H)) {
  470. normal.x = -normal.x;
  471. }
  472. if (bool(read_draw_data_flags & FLAGS_FLIP_V)) {
  473. normal.y = -normal.y;
  474. }
  475. normal.z = sqrt(max(0.0, 1.0 - dot(normal.xy, normal.xy)));
  476. normal_used = true;
  477. } else {
  478. normal = vec3(0.0, 0.0, 1.0);
  479. }
  480. vec4 specular_shininess;
  481. #if defined(SPECULAR_SHININESS_USED)
  482. bool specular_shininess_used = true;
  483. #else
  484. bool specular_shininess_used = false;
  485. #endif
  486. if (specular_shininess_used || (using_light && normal_used && bool(read_draw_data_flags & FLAGS_DEFAULT_SPECULAR_MAP_USED))) {
  487. specular_shininess = texture(specular_texture, uv);
  488. specular_shininess *= godot_unpackUnorm4x8(read_draw_data_specular_shininess);
  489. specular_shininess_used = true;
  490. } else {
  491. specular_shininess = vec4(1.0);
  492. }
  493. #if defined(SCREEN_UV_USED)
  494. vec2 screen_uv = gl_FragCoord.xy * screen_pixel_size;
  495. #else
  496. vec2 screen_uv = vec2(0.0);
  497. #endif
  498. vec2 color_texture_pixel_size = read_draw_data_color_texture_pixel_size.xy;
  499. vec3 light_vertex = vec3(vertex, 0.0);
  500. vec2 shadow_vertex = vertex;
  501. {
  502. float normal_map_depth = 1.0;
  503. #if defined(NORMAL_MAP_USED)
  504. vec3 normal_map = vec3(0.0, 0.0, 1.0);
  505. normal_used = true;
  506. #endif
  507. #CODE : FRAGMENT
  508. #if defined(NORMAL_MAP_USED)
  509. 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);
  510. #endif
  511. }
  512. if (normal_used) {
  513. //convert by item transform
  514. normal.xy = mat2(normalize(read_draw_data_world_x), normalize(read_draw_data_world_y)) * normal.xy;
  515. //convert by canvas transform
  516. normal = normalize((canvas_normal_transform * vec4(normal, 0.0)).xyz);
  517. }
  518. vec4 base_color = color;
  519. #ifdef MODE_LIGHT_ONLY
  520. float light_only_alpha = 0.0;
  521. #elif !defined(MODE_UNSHADED)
  522. color *= canvas_modulation;
  523. #endif
  524. #if !defined(DISABLE_LIGHTING) && !defined(MODE_UNSHADED)
  525. // Directional Lights
  526. for (uint i = 0u; i < directional_light_count; i++) {
  527. uint light_base = i;
  528. vec2 direction = light_array[light_base].position;
  529. vec4 light_color = light_array[light_base].color;
  530. #ifdef LIGHT_CODE_USED
  531. vec4 shadow_modulate = vec4(1.0);
  532. 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);
  533. #else
  534. if (normal_used) {
  535. vec3 light_vec = normalize(mix(vec3(direction, 0.0), vec3(0, 0, 1), light_array[light_base].height));
  536. light_color.rgb = light_normal_compute(light_vec, normal, base_color.rgb, light_color.rgb, specular_shininess, specular_shininess_used);
  537. } else {
  538. light_color.rgb *= base_color.rgb;
  539. }
  540. #endif
  541. if (bool(light_array[light_base].flags & LIGHT_FLAGS_HAS_SHADOW)) {
  542. 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.
  543. 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);
  544. light_color = light_shadow_compute(light_base, light_color, shadow_uv
  545. #ifdef LIGHT_CODE_USED
  546. ,
  547. shadow_modulate.rgb
  548. #endif
  549. );
  550. }
  551. light_blend_compute(light_base, light_color, color.rgb);
  552. #ifdef MODE_LIGHT_ONLY
  553. light_only_alpha += light_color.a;
  554. #endif
  555. }
  556. // Positional Lights
  557. for (uint i = 0u; i < MAX_LIGHTS_PER_ITEM; i++) {
  558. if (i >= light_count) {
  559. break;
  560. }
  561. uint light_base;
  562. if (i < 8u) {
  563. if (i < 4u) {
  564. light_base = read_draw_data_lights[0];
  565. } else {
  566. light_base = read_draw_data_lights[1];
  567. }
  568. } else {
  569. if (i < 12u) {
  570. light_base = read_draw_data_lights[2];
  571. } else {
  572. light_base = read_draw_data_lights[3];
  573. }
  574. }
  575. light_base >>= (i & 3u) * 8u;
  576. light_base &= uint(0xFF);
  577. 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.
  578. vec2 tex_uv_atlas = tex_uv * light_array[light_base].atlas_rect.zw + light_array[light_base].atlas_rect.xy;
  579. vec4 light_color = textureLod(atlas_texture, tex_uv_atlas, 0.0);
  580. vec4 light_base_color = light_array[light_base].color;
  581. #ifdef LIGHT_CODE_USED
  582. vec4 shadow_modulate = vec4(1.0);
  583. vec3 light_position = vec3(light_array[light_base].position, light_array[light_base].height);
  584. light_color.rgb *= light_base_color.rgb;
  585. 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);
  586. #else
  587. light_color.rgb *= light_base_color.rgb * light_base_color.a;
  588. if (normal_used) {
  589. vec3 light_pos = vec3(light_array[light_base].position, light_array[light_base].height);
  590. vec3 pos = light_vertex;
  591. vec3 light_vec = normalize(light_pos - pos);
  592. light_color.rgb = light_normal_compute(light_vec, normal, base_color.rgb, light_color.rgb, specular_shininess, specular_shininess_used);
  593. } else {
  594. light_color.rgb *= base_color.rgb;
  595. }
  596. #endif
  597. if (any(lessThan(tex_uv, vec2(0.0, 0.0))) || any(greaterThanEqual(tex_uv, vec2(1.0, 1.0)))) {
  598. //if outside the light texture, light color is zero
  599. light_color.a = 0.0;
  600. }
  601. if (bool(light_array[light_base].flags & LIGHT_FLAGS_HAS_SHADOW)) {
  602. 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.
  603. vec2 pos_norm = normalize(shadow_pos);
  604. vec2 pos_abs = abs(pos_norm);
  605. vec2 pos_box = pos_norm / max(pos_abs.x, pos_abs.y);
  606. 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?
  607. float tex_ofs;
  608. float dist;
  609. if (pos_rot.y > 0.0) {
  610. if (pos_rot.x > 0.0) {
  611. tex_ofs = pos_box.y * 0.125 + 0.125;
  612. dist = shadow_pos.x;
  613. } else {
  614. tex_ofs = pos_box.x * -0.125 + (0.25 + 0.125);
  615. dist = shadow_pos.y;
  616. }
  617. } else {
  618. if (pos_rot.x < 0.0) {
  619. tex_ofs = pos_box.y * -0.125 + (0.5 + 0.125);
  620. dist = -shadow_pos.x;
  621. } else {
  622. tex_ofs = pos_box.x * 0.125 + (0.75 + 0.125);
  623. dist = -shadow_pos.y;
  624. }
  625. }
  626. dist *= light_array[light_base].shadow_zfar_inv;
  627. //float distance = length(shadow_pos);
  628. vec4 shadow_uv = vec4(tex_ofs, light_array[light_base].shadow_y_ofs, dist, 1.0);
  629. light_color = light_shadow_compute(light_base, light_color, shadow_uv
  630. #ifdef LIGHT_CODE_USED
  631. ,
  632. shadow_modulate.rgb
  633. #endif
  634. );
  635. }
  636. light_blend_compute(light_base, light_color, color.rgb);
  637. #ifdef MODE_LIGHT_ONLY
  638. light_only_alpha += light_color.a;
  639. #endif
  640. }
  641. #endif
  642. #ifdef MODE_LIGHT_ONLY
  643. color.a *= light_only_alpha;
  644. #endif
  645. frag_color = color;
  646. }