canvas.glsl 26 KB

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