canvas.glsl 26 KB

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