canvas.glsl 24 KB

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