canvas.glsl 23 KB

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