canvas.glsl 22 KB

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