canvas.glsl 26 KB

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