canvas.glsl 25 KB

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