canvas.glsl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /* clang-format off */
  2. [vertex]
  3. layout(location = 0) in highp vec2 vertex;
  4. #ifdef USE_ATTRIB_LIGHT_ANGLE
  5. layout(location = 2) in highp float light_angle;
  6. #endif
  7. /* clang-format on */
  8. layout(location = 3) in vec4 color_attrib;
  9. #ifdef USE_ATTRIB_MODULATE
  10. layout(location = 5) in vec4 modulate_attrib; // attrib:5
  11. #endif
  12. // Usually, final_modulate is passed as a uniform. However during batching
  13. // If larger fvfs are used, final_modulate is passed as an attribute.
  14. // we need to read from the attribute in custom vertex shader
  15. // rather than the uniform. We do this by specifying final_modulate_alias
  16. // in shaders rather than final_modulate directly.
  17. #ifdef USE_ATTRIB_MODULATE
  18. #define final_modulate_alias modulate_attrib
  19. #else
  20. #define final_modulate_alias final_modulate
  21. #endif
  22. #ifdef USE_ATTRIB_LARGE_VERTEX
  23. // shared with skeleton attributes, not used in batched shader
  24. layout(location = 6) in vec2 translate_attrib; // attrib:6
  25. layout(location = 7) in vec4 basis_attrib; // attrib:7
  26. #endif
  27. #ifdef USE_SKELETON
  28. layout(location = 6) in uvec4 bone_indices; // attrib:6
  29. layout(location = 7) in vec4 bone_weights; // attrib:7
  30. #endif
  31. #ifdef USE_TEXTURE_RECT
  32. uniform vec4 dst_rect;
  33. uniform vec4 src_rect;
  34. #else
  35. #ifdef USE_INSTANCING
  36. layout(location = 8) in highp vec4 instance_xform0;
  37. layout(location = 9) in highp vec4 instance_xform1;
  38. layout(location = 10) in highp vec4 instance_xform2;
  39. layout(location = 11) in lowp vec4 instance_color;
  40. #ifdef USE_INSTANCE_CUSTOM
  41. layout(location = 12) in highp vec4 instance_custom_data;
  42. #endif
  43. #endif
  44. layout(location = 4) in highp vec2 uv_attrib;
  45. // skeleton
  46. #endif
  47. uniform highp vec2 color_texpixel_size;
  48. layout(std140) uniform CanvasItemData { //ubo:0
  49. highp mat4 projection_matrix;
  50. highp float time;
  51. };
  52. uniform highp mat4 modelview_matrix;
  53. uniform highp mat4 extra_matrix;
  54. out highp vec2 uv_interp;
  55. out mediump vec4 color_interp;
  56. #ifdef USE_ATTRIB_MODULATE
  57. // modulate doesn't need interpolating but we need to send it to the fragment shader
  58. flat out mediump vec4 modulate_interp;
  59. #endif
  60. #ifdef MODULATE_USED
  61. uniform mediump vec4 final_modulate;
  62. #endif
  63. #ifdef USE_NINEPATCH
  64. out highp vec2 pixel_size_interp;
  65. #endif
  66. #ifdef USE_SKELETON
  67. uniform mediump sampler2D skeleton_texture; // texunit:-4
  68. uniform highp mat4 skeleton_transform;
  69. uniform highp mat4 skeleton_transform_inverse;
  70. #endif
  71. #ifdef USE_LIGHTING
  72. layout(std140) uniform LightData { //ubo:1
  73. // light matrices
  74. highp mat4 light_matrix;
  75. highp mat4 light_local_matrix;
  76. highp mat4 shadow_matrix;
  77. highp vec4 light_color;
  78. highp vec4 light_shadow_color;
  79. highp vec2 light_pos;
  80. highp float shadowpixel_size;
  81. highp float shadow_gradient;
  82. highp float light_height;
  83. highp float light_outside_alpha;
  84. highp float shadow_distance_mult;
  85. };
  86. out vec4 light_uv_interp;
  87. out vec2 transformed_light_uv;
  88. out vec4 local_rot;
  89. #ifdef USE_SHADOWS
  90. out highp vec2 pos;
  91. #endif
  92. const bool at_light_pass = true;
  93. #else
  94. const bool at_light_pass = false;
  95. #endif
  96. #if defined(USE_MATERIAL)
  97. /* clang-format off */
  98. layout(std140) uniform UniformData { //ubo:2
  99. MATERIAL_UNIFORMS
  100. };
  101. /* clang-format on */
  102. #endif
  103. /* clang-format off */
  104. VERTEX_SHADER_GLOBALS
  105. /* clang-format on */
  106. void main() {
  107. vec4 color = color_attrib;
  108. #ifdef USE_INSTANCING
  109. mat4 extra_matrix_instance = extra_matrix * transpose(mat4(instance_xform0, instance_xform1, instance_xform2, vec4(0.0, 0.0, 0.0, 1.0)));
  110. color *= instance_color;
  111. #ifdef USE_INSTANCE_CUSTOM
  112. vec4 instance_custom = instance_custom_data;
  113. #else
  114. vec4 instance_custom = vec4(0.0);
  115. #endif
  116. #else
  117. mat4 extra_matrix_instance = extra_matrix;
  118. vec4 instance_custom = vec4(0.0);
  119. #endif
  120. #ifdef USE_TEXTURE_RECT
  121. if (dst_rect.z < 0.0) { // Transpose is encoded as negative dst_rect.z
  122. uv_interp = src_rect.xy + abs(src_rect.zw) * vertex.yx;
  123. } else {
  124. uv_interp = src_rect.xy + abs(src_rect.zw) * vertex;
  125. }
  126. highp vec4 outvec = vec4(dst_rect.xy + abs(dst_rect.zw) * mix(vertex, vec2(1.0, 1.0) - vertex, lessThan(src_rect.zw, vec2(0.0, 0.0))), 0.0, 1.0);
  127. #else
  128. uv_interp = uv_attrib;
  129. highp vec4 outvec = vec4(vertex, 0.0, 1.0);
  130. #endif
  131. #ifdef USE_PARTICLES
  132. //scale by texture size
  133. outvec.xy /= color_texpixel_size;
  134. #endif
  135. #define extra_matrix extra_matrix_instance
  136. float point_size = 1.0;
  137. //for compatibility with the fragment shader we need to use uv here
  138. vec2 uv = uv_interp;
  139. {
  140. /* clang-format off */
  141. VERTEX_SHADER_CODE
  142. /* clang-format on */
  143. }
  144. gl_PointSize = point_size;
  145. uv_interp = uv;
  146. #ifdef USE_NINEPATCH
  147. pixel_size_interp = abs(dst_rect.zw) * vertex;
  148. #endif
  149. #ifdef USE_ATTRIB_MODULATE
  150. // modulate doesn't need interpolating but we need to send it to the fragment shader
  151. modulate_interp = modulate_attrib;
  152. #endif
  153. #ifdef USE_ATTRIB_LARGE_VERTEX
  154. // transform is in attributes
  155. vec2 temp;
  156. temp = outvec.xy;
  157. temp.x = (outvec.x * basis_attrib.x) + (outvec.y * basis_attrib.z);
  158. temp.y = (outvec.x * basis_attrib.y) + (outvec.y * basis_attrib.w);
  159. temp += translate_attrib;
  160. outvec.xy = temp;
  161. #else
  162. // transform is in uniforms
  163. #if !defined(SKIP_TRANSFORM_USED)
  164. outvec = extra_matrix * outvec;
  165. outvec = modelview_matrix * outvec;
  166. #endif
  167. #endif // not large integer
  168. #undef extra_matrix
  169. color_interp = color;
  170. #ifdef USE_PIXEL_SNAP
  171. outvec.xy = floor(outvec + 0.5).xy;
  172. // precision issue on some hardware creates artifacts within texture
  173. // offset uv by a small amount to avoid
  174. uv_interp += 1e-5;
  175. #endif
  176. #ifdef USE_SKELETON
  177. if (bone_weights != vec4(0.0)) { //must be a valid bone
  178. //skeleton transform
  179. ivec4 bone_indicesi = ivec4(bone_indices);
  180. ivec2 tex_ofs = ivec2(bone_indicesi.x % 256, (bone_indicesi.x / 256) * 2);
  181. highp mat2x4 m;
  182. m = mat2x4(
  183. texelFetch(skeleton_texture, tex_ofs, 0),
  184. texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0)) *
  185. bone_weights.x;
  186. tex_ofs = ivec2(bone_indicesi.y % 256, (bone_indicesi.y / 256) * 2);
  187. m += mat2x4(
  188. texelFetch(skeleton_texture, tex_ofs, 0),
  189. texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0)) *
  190. bone_weights.y;
  191. tex_ofs = ivec2(bone_indicesi.z % 256, (bone_indicesi.z / 256) * 2);
  192. m += mat2x4(
  193. texelFetch(skeleton_texture, tex_ofs, 0),
  194. texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0)) *
  195. bone_weights.z;
  196. tex_ofs = ivec2(bone_indicesi.w % 256, (bone_indicesi.w / 256) * 2);
  197. m += mat2x4(
  198. texelFetch(skeleton_texture, tex_ofs, 0),
  199. texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0)) *
  200. bone_weights.w;
  201. mat4 bone_matrix = 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_transform_inverse;
  202. outvec = bone_matrix * outvec;
  203. }
  204. #endif
  205. gl_Position = projection_matrix * outvec;
  206. #ifdef USE_LIGHTING
  207. light_uv_interp.xy = (light_matrix * outvec).xy;
  208. light_uv_interp.zw = (light_local_matrix * outvec).xy;
  209. mat3 inverse_light_matrix = mat3(inverse(light_matrix));
  210. inverse_light_matrix[0] = normalize(inverse_light_matrix[0]);
  211. inverse_light_matrix[1] = normalize(inverse_light_matrix[1]);
  212. inverse_light_matrix[2] = normalize(inverse_light_matrix[2]);
  213. transformed_light_uv = (inverse_light_matrix * vec3(light_uv_interp.zw, 0.0)).xy; //for normal mapping
  214. #ifdef USE_SHADOWS
  215. pos = outvec.xy;
  216. #endif
  217. #ifdef USE_ATTRIB_LIGHT_ANGLE
  218. // we add a fixed offset because we are using the sign later,
  219. // and don't want floating point error around 0.0
  220. float la = abs(light_angle) - 1.0;
  221. // vector light angle
  222. vec4 vla;
  223. vla.xy = vec2(cos(la), sin(la));
  224. vla.zw = vec2(-vla.y, vla.x);
  225. vla.zw *= sign(light_angle);
  226. // apply the transform matrix.
  227. // The rotate will be encoded in the transform matrix for single rects,
  228. // and just the flips in the light angle.
  229. // For batching we will encode the rotation and the flips
  230. // in the light angle, and can use the same shader.
  231. local_rot.xy = normalize((modelview_matrix * (extra_matrix_instance * vec4(vla.xy, 0.0, 0.0))).xy);
  232. local_rot.zw = normalize((modelview_matrix * (extra_matrix_instance * vec4(vla.zw, 0.0, 0.0))).xy);
  233. #else
  234. local_rot.xy = normalize((modelview_matrix * (extra_matrix_instance * vec4(1.0, 0.0, 0.0, 0.0))).xy);
  235. local_rot.zw = normalize((modelview_matrix * (extra_matrix_instance * vec4(0.0, 1.0, 0.0, 0.0))).xy);
  236. #ifdef USE_TEXTURE_RECT
  237. local_rot.xy *= sign(src_rect.z);
  238. local_rot.zw *= sign(src_rect.w);
  239. #endif
  240. #endif // not using light angle
  241. #endif
  242. }
  243. /* clang-format off */
  244. [fragment]
  245. uniform mediump sampler2D color_texture; // texunit:0
  246. /* clang-format on */
  247. uniform highp vec2 color_texpixel_size;
  248. uniform mediump sampler2D normal_texture; // texunit:1
  249. in highp vec2 uv_interp;
  250. in mediump vec4 color_interp;
  251. #ifdef USE_ATTRIB_MODULATE
  252. flat in mediump vec4 modulate_interp;
  253. #endif
  254. #if defined(SCREEN_TEXTURE_USED)
  255. uniform sampler2D screen_texture; // texunit:-3
  256. #endif
  257. #if defined(SCREEN_UV_USED)
  258. uniform vec2 screen_pixel_size;
  259. #endif
  260. layout(std140) uniform CanvasItemData {
  261. highp mat4 projection_matrix;
  262. highp float time;
  263. };
  264. #ifdef USE_LIGHTING
  265. layout(std140) uniform LightData {
  266. highp mat4 light_matrix;
  267. highp mat4 light_local_matrix;
  268. highp mat4 shadow_matrix;
  269. highp vec4 light_color;
  270. highp vec4 light_shadow_color;
  271. highp vec2 light_pos;
  272. highp float shadowpixel_size;
  273. highp float shadow_gradient;
  274. highp float light_height;
  275. highp float light_outside_alpha;
  276. highp float shadow_distance_mult;
  277. };
  278. uniform lowp sampler2D light_texture; // texunit:-1
  279. in vec4 light_uv_interp;
  280. in vec2 transformed_light_uv;
  281. in vec4 local_rot;
  282. #ifdef USE_SHADOWS
  283. uniform highp sampler2D shadow_texture; // texunit:-2
  284. in highp vec2 pos;
  285. #endif
  286. const bool at_light_pass = true;
  287. #else
  288. const bool at_light_pass = false;
  289. #endif
  290. uniform mediump vec4 final_modulate;
  291. layout(location = 0) out mediump vec4 frag_color;
  292. #if defined(USE_MATERIAL)
  293. /* clang-format off */
  294. layout(std140) uniform UniformData {
  295. MATERIAL_UNIFORMS
  296. };
  297. /* clang-format on */
  298. #endif
  299. /* clang-format off */
  300. FRAGMENT_SHADER_GLOBALS
  301. /* clang-format on */
  302. void light_compute(
  303. inout vec4 light,
  304. inout vec2 light_vec,
  305. inout float light_height,
  306. inout vec4 light_color,
  307. vec2 light_uv,
  308. inout vec4 shadow_color,
  309. inout vec2 shadow_vec,
  310. vec3 normal,
  311. vec2 uv,
  312. #if defined(SCREEN_UV_USED)
  313. vec2 screen_uv,
  314. #endif
  315. vec4 color) {
  316. #if defined(USE_LIGHT_SHADER_CODE)
  317. /* clang-format off */
  318. LIGHT_SHADER_CODE
  319. /* clang-format on */
  320. #endif
  321. }
  322. #ifdef USE_TEXTURE_RECT
  323. uniform vec4 dst_rect;
  324. uniform vec4 src_rect;
  325. uniform bool clip_rect_uv;
  326. #ifdef USE_NINEPATCH
  327. in highp vec2 pixel_size_interp;
  328. uniform int np_repeat_v;
  329. uniform int np_repeat_h;
  330. uniform bool np_draw_center;
  331. // left top right bottom in pixel coordinates
  332. uniform vec4 np_margins;
  333. // there are two ninepatch modes, and we don't want to waste a conditional
  334. #if defined USE_NINEPATCH_SCALING
  335. float map_ninepatch_axis(float pixel, float draw_size, float tex_pixel_size, float margin_begin, float margin_end, float s_ratio, int np_repeat, inout int draw_center) {
  336. float tex_size = 1.0 / tex_pixel_size;
  337. float screen_margin_begin = margin_begin / s_ratio;
  338. float screen_margin_end = margin_end / s_ratio;
  339. if (pixel < screen_margin_begin) {
  340. return pixel * s_ratio * tex_pixel_size;
  341. } else if (pixel >= draw_size - screen_margin_end) {
  342. return (tex_size - (draw_size - pixel) * s_ratio) * tex_pixel_size;
  343. } else {
  344. if (!np_draw_center) {
  345. draw_center--;
  346. }
  347. if (np_repeat == 0) { //stretch
  348. //convert to ratio
  349. float ratio = (pixel - screen_margin_begin) / (draw_size - screen_margin_begin - screen_margin_end);
  350. //scale to source texture
  351. return (margin_begin + ratio * (tex_size - margin_begin - margin_end)) * tex_pixel_size;
  352. } else if (np_repeat == 1) { //tile
  353. //convert to ratio
  354. float ofs = mod((pixel - screen_margin_begin), tex_size - margin_begin - margin_end);
  355. //scale to source texture
  356. return (margin_begin + ofs) * tex_pixel_size;
  357. } else if (np_repeat == 2) { //tile fit
  358. //convert to ratio
  359. float src_area = draw_size - screen_margin_begin - screen_margin_end;
  360. float dst_area = tex_size - margin_begin - margin_end;
  361. float scale = max(1.0, floor(src_area / max(dst_area, 0.0000001) + 0.5));
  362. //convert to ratio
  363. float ratio = (pixel - screen_margin_begin) / src_area;
  364. ratio = mod(ratio * scale, 1.0);
  365. return (margin_begin + ratio * dst_area) * tex_pixel_size;
  366. }
  367. }
  368. }
  369. #else
  370. 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) {
  371. float tex_size = 1.0 / tex_pixel_size;
  372. if (pixel < margin_begin) {
  373. return pixel * tex_pixel_size;
  374. } else if (pixel >= draw_size - margin_end) {
  375. return (tex_size - (draw_size - pixel)) * tex_pixel_size;
  376. } else {
  377. if (!np_draw_center) {
  378. draw_center--;
  379. }
  380. // np_repeat is passed as uniform using NinePatchRect::AxisStretchMode enum.
  381. if (np_repeat == 0) { // Stretch.
  382. // Convert to ratio.
  383. float ratio = (pixel - margin_begin) / (draw_size - margin_begin - margin_end);
  384. // Scale to source texture.
  385. return (margin_begin + ratio * (tex_size - margin_begin - margin_end)) * tex_pixel_size;
  386. } else if (np_repeat == 1) { // Tile.
  387. // Convert to offset.
  388. float ofs = mod((pixel - margin_begin), tex_size - margin_begin - margin_end);
  389. // Scale to source texture.
  390. return (margin_begin + ofs) * tex_pixel_size;
  391. } else if (np_repeat == 2) { // Tile Fit.
  392. // Calculate scale.
  393. float src_area = draw_size - margin_begin - margin_end;
  394. float dst_area = tex_size - margin_begin - margin_end;
  395. float scale = max(1.0, floor(src_area / max(dst_area, 0.0000001) + 0.5));
  396. // Convert to ratio.
  397. float ratio = (pixel - margin_begin) / src_area;
  398. ratio = mod(ratio * scale, 1.0);
  399. // Scale to source texture.
  400. return (margin_begin + ratio * dst_area) * tex_pixel_size;
  401. } else { // Shouldn't happen, but silences compiler warning.
  402. return 0.0;
  403. }
  404. }
  405. }
  406. #endif
  407. #endif
  408. #endif
  409. uniform bool use_default_normal;
  410. void main() {
  411. vec4 color = color_interp;
  412. vec2 uv = uv_interp;
  413. #ifdef USE_TEXTURE_RECT
  414. #ifdef USE_NINEPATCH
  415. int draw_center = 2;
  416. #if defined USE_NINEPATCH_SCALING
  417. float s_ratio = max((1.0 / color_texpixel_size.x) / abs(dst_rect.z), (1.0 / color_texpixel_size.y) / abs(dst_rect.w));
  418. s_ratio = max(1.0, s_ratio);
  419. uv = vec2(
  420. map_ninepatch_axis(pixel_size_interp.x, abs(dst_rect.z), color_texpixel_size.x, np_margins.x, np_margins.z, s_ratio, np_repeat_h, draw_center),
  421. map_ninepatch_axis(pixel_size_interp.y, abs(dst_rect.w), color_texpixel_size.y, np_margins.y, np_margins.w, s_ratio, np_repeat_v, draw_center));
  422. if (draw_center == 0) {
  423. color.a = 0.0;
  424. }
  425. #else
  426. uv = vec2(
  427. map_ninepatch_axis(pixel_size_interp.x, abs(dst_rect.z), color_texpixel_size.x, np_margins.x, np_margins.z, np_repeat_h, draw_center),
  428. map_ninepatch_axis(pixel_size_interp.y, abs(dst_rect.w), color_texpixel_size.y, np_margins.y, np_margins.w, np_repeat_v, draw_center));
  429. if (draw_center == 0) {
  430. color.a = 0.0;
  431. }
  432. #endif
  433. uv = uv * src_rect.zw + src_rect.xy; //apply region if needed
  434. #endif
  435. if (clip_rect_uv) {
  436. uv = clamp(uv, src_rect.xy, src_rect.xy + abs(src_rect.zw));
  437. }
  438. #endif
  439. #if !defined(COLOR_USED)
  440. //default behavior, texture by color
  441. #ifdef USE_DISTANCE_FIELD
  442. const float smoothing = 1.0 / 32.0;
  443. float distance = textureLod(color_texture, uv, 0.0).a;
  444. color.a = smoothstep(0.5 - smoothing, 0.5 + smoothing, distance) * color.a;
  445. #else
  446. color *= texture(color_texture, uv);
  447. #endif
  448. #endif
  449. vec3 normal;
  450. #if defined(NORMAL_USED)
  451. bool normal_used = true;
  452. #else
  453. bool normal_used = false;
  454. #endif
  455. if (use_default_normal) {
  456. normal.xy = textureLod(normal_texture, uv, 0.0).xy * 2.0 - 1.0;
  457. normal.z = sqrt(max(0.0, 1.0 - dot(normal.xy, normal.xy)));
  458. normal_used = true;
  459. } else {
  460. normal = vec3(0.0, 0.0, 1.0);
  461. }
  462. #if defined(SCREEN_UV_USED)
  463. vec2 screen_uv = gl_FragCoord.xy * screen_pixel_size;
  464. #endif
  465. {
  466. float normal_depth = 1.0;
  467. #if defined(NORMALMAP_USED)
  468. vec3 normal_map = vec3(0.0, 0.0, 1.0);
  469. normal_used = true;
  470. #endif
  471. // If larger fvfs are used, final_modulate is passed as an attribute.
  472. // we need to read from this in custom fragment shaders or applying in the post step,
  473. // rather than using final_modulate directly.
  474. #if defined(final_modulate_alias)
  475. #undef final_modulate_alias
  476. #endif
  477. #ifdef USE_ATTRIB_MODULATE
  478. #define final_modulate_alias modulate_interp
  479. #else
  480. #define final_modulate_alias final_modulate
  481. #endif
  482. /* clang-format off */
  483. FRAGMENT_SHADER_CODE
  484. /* clang-format on */
  485. #if defined(NORMALMAP_USED)
  486. 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_depth);
  487. #endif
  488. }
  489. #ifdef DEBUG_ENCODED_32
  490. highp float enc32 = dot(color, highp vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0));
  491. color = vec4(vec3(enc32), 1.0);
  492. #endif
  493. #if !defined(MODULATE_USED)
  494. color *= final_modulate_alias;
  495. #endif
  496. #ifdef USE_LIGHTING
  497. vec2 light_vec = transformed_light_uv;
  498. vec2 shadow_vec = transformed_light_uv;
  499. if (normal_used) {
  500. normal.xy = mat2(local_rot.xy, local_rot.zw) * normal.xy;
  501. }
  502. float att = 1.0;
  503. vec2 light_uv = light_uv_interp.xy;
  504. vec4 light = texture(light_texture, light_uv);
  505. if (any(lessThan(light_uv_interp.xy, vec2(0.0, 0.0))) || any(greaterThanEqual(light_uv_interp.xy, vec2(1.0, 1.0)))) {
  506. color.a *= light_outside_alpha; //invisible
  507. } else {
  508. float real_light_height = light_height;
  509. vec4 real_light_color = light_color;
  510. vec4 real_light_shadow_color = light_shadow_color;
  511. #if defined(USE_LIGHT_SHADER_CODE)
  512. //light is written by the light shader
  513. light_compute(
  514. light,
  515. light_vec,
  516. real_light_height,
  517. real_light_color,
  518. light_uv,
  519. real_light_shadow_color,
  520. shadow_vec,
  521. normal,
  522. uv,
  523. #if defined(SCREEN_UV_USED)
  524. screen_uv,
  525. #endif
  526. color);
  527. #endif
  528. light *= real_light_color;
  529. if (normal_used) {
  530. vec3 light_normal = normalize(vec3(light_vec, -real_light_height));
  531. light *= max(dot(-light_normal, normal), 0.0);
  532. }
  533. color *= light;
  534. #ifdef USE_SHADOWS
  535. #ifdef SHADOW_VEC_USED
  536. mat3 inverse_light_matrix = mat3(light_matrix);
  537. inverse_light_matrix[0] = normalize(inverse_light_matrix[0]);
  538. inverse_light_matrix[1] = normalize(inverse_light_matrix[1]);
  539. inverse_light_matrix[2] = normalize(inverse_light_matrix[2]);
  540. shadow_vec = (mat3(inverse_light_matrix) * vec3(shadow_vec, 0.0)).xy;
  541. #else
  542. shadow_vec = light_uv_interp.zw;
  543. #endif
  544. float angle_to_light = -atan(shadow_vec.x, shadow_vec.y);
  545. float PI = 3.14159265358979323846264;
  546. /*int i = int(mod(floor((angle_to_light+7.0*PI/6.0)/(4.0*PI/6.0))+1.0, 3.0)); // +1 pq os indices estao em ordem 2,0,1 nos arrays
  547. float ang*/
  548. float su, sz;
  549. float abs_angle = abs(angle_to_light);
  550. vec2 point;
  551. float sh;
  552. if (abs_angle < 45.0 * PI / 180.0) {
  553. point = shadow_vec;
  554. sh = 0.0 + (1.0 / 8.0);
  555. } else if (abs_angle > 135.0 * PI / 180.0) {
  556. point = -shadow_vec;
  557. sh = 0.5 + (1.0 / 8.0);
  558. } else if (angle_to_light > 0.0) {
  559. point = vec2(shadow_vec.y, -shadow_vec.x);
  560. sh = 0.25 + (1.0 / 8.0);
  561. } else {
  562. point = vec2(-shadow_vec.y, shadow_vec.x);
  563. sh = 0.75 + (1.0 / 8.0);
  564. }
  565. highp vec4 s = shadow_matrix * vec4(point, 0.0, 1.0);
  566. s.xyz /= s.w;
  567. su = s.x * 0.5 + 0.5;
  568. sz = s.z * 0.5 + 0.5;
  569. //sz=lightlength(light_vec);
  570. highp float shadow_attenuation = 0.0;
  571. #ifdef USE_RGBA_SHADOWS
  572. #define SHADOW_DEPTH(m_tex, m_uv) dot(texture((m_tex), (m_uv)), vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0))
  573. #else
  574. #define SHADOW_DEPTH(m_tex, m_uv) (texture((m_tex), (m_uv)).r)
  575. #endif
  576. #ifdef SHADOW_USE_GRADIENT
  577. #define SHADOW_TEST(m_ofs) \
  578. { \
  579. highp float sd = SHADOW_DEPTH(shadow_texture, vec2(m_ofs, sh)); \
  580. shadow_attenuation += 1.0 - smoothstep(sd, sd + shadow_gradient, sz); \
  581. }
  582. #else
  583. #define SHADOW_TEST(m_ofs) \
  584. { \
  585. highp float sd = SHADOW_DEPTH(shadow_texture, vec2(m_ofs, sh)); \
  586. shadow_attenuation += step(sz, sd); \
  587. }
  588. #endif
  589. #ifdef SHADOW_FILTER_NEAREST
  590. SHADOW_TEST(su);
  591. #endif
  592. #ifdef SHADOW_FILTER_PCF3
  593. SHADOW_TEST(su + shadowpixel_size);
  594. SHADOW_TEST(su);
  595. SHADOW_TEST(su - shadowpixel_size);
  596. shadow_attenuation /= 3.0;
  597. #endif
  598. #ifdef SHADOW_FILTER_PCF5
  599. SHADOW_TEST(su + shadowpixel_size * 2.0);
  600. SHADOW_TEST(su + shadowpixel_size);
  601. SHADOW_TEST(su);
  602. SHADOW_TEST(su - shadowpixel_size);
  603. SHADOW_TEST(su - shadowpixel_size * 2.0);
  604. shadow_attenuation /= 5.0;
  605. #endif
  606. #ifdef SHADOW_FILTER_PCF7
  607. SHADOW_TEST(su + shadowpixel_size * 3.0);
  608. SHADOW_TEST(su + shadowpixel_size * 2.0);
  609. SHADOW_TEST(su + shadowpixel_size);
  610. SHADOW_TEST(su);
  611. SHADOW_TEST(su - shadowpixel_size);
  612. SHADOW_TEST(su - shadowpixel_size * 2.0);
  613. SHADOW_TEST(su - shadowpixel_size * 3.0);
  614. shadow_attenuation /= 7.0;
  615. #endif
  616. #ifdef SHADOW_FILTER_PCF9
  617. SHADOW_TEST(su + shadowpixel_size * 4.0);
  618. SHADOW_TEST(su + shadowpixel_size * 3.0);
  619. SHADOW_TEST(su + shadowpixel_size * 2.0);
  620. SHADOW_TEST(su + shadowpixel_size);
  621. SHADOW_TEST(su);
  622. SHADOW_TEST(su - shadowpixel_size);
  623. SHADOW_TEST(su - shadowpixel_size * 2.0);
  624. SHADOW_TEST(su - shadowpixel_size * 3.0);
  625. SHADOW_TEST(su - shadowpixel_size * 4.0);
  626. shadow_attenuation /= 9.0;
  627. #endif
  628. #ifdef SHADOW_FILTER_PCF13
  629. SHADOW_TEST(su + shadowpixel_size * 6.0);
  630. SHADOW_TEST(su + shadowpixel_size * 5.0);
  631. SHADOW_TEST(su + shadowpixel_size * 4.0);
  632. SHADOW_TEST(su + shadowpixel_size * 3.0);
  633. SHADOW_TEST(su + shadowpixel_size * 2.0);
  634. SHADOW_TEST(su + shadowpixel_size);
  635. SHADOW_TEST(su);
  636. SHADOW_TEST(su - shadowpixel_size);
  637. SHADOW_TEST(su - shadowpixel_size * 2.0);
  638. SHADOW_TEST(su - shadowpixel_size * 3.0);
  639. SHADOW_TEST(su - shadowpixel_size * 4.0);
  640. SHADOW_TEST(su - shadowpixel_size * 5.0);
  641. SHADOW_TEST(su - shadowpixel_size * 6.0);
  642. shadow_attenuation /= 13.0;
  643. #endif
  644. //color *= shadow_attenuation;
  645. color = mix(real_light_shadow_color, color, shadow_attenuation);
  646. //use shadows
  647. #endif
  648. }
  649. //use lighting
  650. #endif
  651. #ifdef LINEAR_TO_SRGB
  652. // regular Linear -> SRGB conversion
  653. vec3 a = vec3(0.055);
  654. color.rgb = mix((vec3(1.0) + a) * pow(color.rgb, vec3(1.0 / 2.4)) - a, 12.92 * color.rgb, lessThan(color.rgb, vec3(0.0031308)));
  655. #endif
  656. //color.rgb *= color.a;
  657. frag_color = color;
  658. }