*/}}

la_tns_shaders.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /*
  2. * LaGUI: A graphical application framework.
  3. * Copyright (C) 2022-2023 Wu Yiming
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "la_5.h"
  19. extern "C" const char* TNS_SHADER_COLOR_COMMON=R"(
  20. #define M_PI 3.1415926535897932384626433832795
  21. float rad(float d){ return d/180.0*M_PI; }
  22. float deg(float r){ return r*180.0/M_PI; }
  23. float cbrt( float x ){
  24. return sign(x)*pow(abs(x),1.0f/3.0f);
  25. }
  26. float srgb_transfer_function(float a){
  27. return .0031308f >= a ? 12.92f * a : 1.055f * pow(a, .4166666666666667f) - .055f;
  28. }
  29. float srgb_transfer_function_inv(float a){
  30. return .04045f < a ? pow((a + .055f) / 1.055f, 2.4f) : a / 12.92f;
  31. }
  32. vec3 to_log_srgb(vec3 color){
  33. return vec3(srgb_transfer_function(color.r),srgb_transfer_function(color.g),srgb_transfer_function(color.b));
  34. }
  35. vec3 to_linear_srgb(vec3 color){
  36. return vec3(srgb_transfer_function_inv(color.r),srgb_transfer_function_inv(color.g),srgb_transfer_function_inv(color.b));
  37. }
  38. vec3 to_linear_clay(vec3 color){
  39. return vec3(pow(color.r,2.19921875),pow(color.g,2.19921875),pow(color.b,2.19921875));
  40. }
  41. vec3 to_log_clay(vec3 color){
  42. return vec3(pow(color.r,1.0/2.19921875),pow(color.g,1.0/2.19921875),pow(color.b,1.0/2.19921875));
  43. }
  44. vec3 linear_srgb_to_oklab(vec3 c){
  45. float l = 0.4122214708f * c.r + 0.5363325363f * c.g + 0.0514459929f * c.b;
  46. float m = 0.2119034982f * c.r + 0.6806995451f * c.g + 0.1073969566f * c.b;
  47. float s = 0.0883024619f * c.r + 0.2817188376f * c.g + 0.6299787005f * c.b;
  48. float l_ = cbrt(l);
  49. float m_ = cbrt(m);
  50. float s_ = cbrt(s);
  51. return vec3(
  52. 0.2104542553f * l_ + 0.7936177850f * m_ - 0.0040720468f * s_,
  53. 1.9779984951f * l_ - 2.4285922050f * m_ + 0.4505937099f * s_,
  54. 0.0259040371f * l_ + 0.7827717662f * m_ - 0.8086757660f * s_
  55. );
  56. }
  57. vec3 oklab_to_linear_srgb(vec3 c){
  58. float l_ = c.x + 0.3963377774f * c.y + 0.2158037573f * c.z;
  59. float m_ = c.x - 0.1055613458f * c.y - 0.0638541728f * c.z;
  60. float s_ = c.x - 0.0894841775f * c.y - 1.2914855480f * c.z;
  61. float l = l_ * l_ * l_;
  62. float m = m_ * m_ * m_;
  63. float s = s_ * s_ * s_;
  64. return vec3(
  65. +4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s,
  66. -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s,
  67. -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s
  68. );
  69. }
  70. vec3 oklab_to_xyz(vec3 c){
  71. float l_ = c.x + 0.3963377774f * c.y + 0.2158037573f * c.z;
  72. float m_ = c.x - 0.1055613458f * c.y - 0.0638541728f * c.z;
  73. float s_ = c.x - 0.0894841775f * c.y - 1.2914855480f * c.z;
  74. float l = l_ * l_ * l_;
  75. float m = m_ * m_ * m_;
  76. float s = s_ * s_ * s_;
  77. mat3 mat=inverse(mat3(vec3(+0.8189330101,+0.0329845436,+0.0482003018),
  78. vec3(+0.3618667424,+0.9293118715,+0.2643662691),
  79. vec3(-0.1288597137,+0.0361456387,+0.6338517070)));
  80. return mat*vec3(l,m,s);
  81. }
  82. float compute_max_saturation(float a, float b){ float k0, k1, k2, k3, k4, wl, wm, ws;
  83. if (-1.88170328f * a - 0.80936493f * b > 1.f){ k0 = +1.19086277f; k1 = +1.76576728f; k2 = +0.59662641f; k3 = +0.75515197f; k4 = +0.56771245f;
  84. wl = +4.0767416621f; wm = -3.3077115913f; ws = +0.2309699292f;
  85. }
  86. else if (1.81444104f * a - 1.19445276f * b > 1.f){ k0 = +0.73956515f; k1 = -0.45954404f; k2 = +0.08285427f; k3 = +0.12541070f; k4 = +0.14503204f;
  87. wl = -1.2684380046f; wm = +2.6097574011f; ws = -0.3413193965f;
  88. }
  89. else{ k0 = +1.35733652f; k1 = -0.00915799f; k2 = -1.15130210f; k3 = -0.50559606f; k4 = +0.00692167f;
  90. wl = -0.0041960863f; wm = -0.7034186147f; ws = +1.7076147010f;
  91. } float S = k0 + k1 * a + k2 * b + k3 * a * a + k4 * a * b;
  92. float k_l = +0.3963377774f * a + 0.2158037573f * b;
  93. float k_m = -0.1055613458f * a - 0.0638541728f * b;
  94. float k_s = -0.0894841775f * a - 1.2914855480f * b;{
  95. float l_ = 1.f + S * k_l;
  96. float m_ = 1.f + S * k_m;
  97. float s_ = 1.f + S * k_s;
  98. float l = l_ * l_ * l_;
  99. float m = m_ * m_ * m_;
  100. float s = s_ * s_ * s_;
  101. float l_dS = 3.f * k_l * l_ * l_;
  102. float m_dS = 3.f * k_m * m_ * m_;
  103. float s_dS = 3.f * k_s * s_ * s_;
  104. float l_dS2 = 6.f * k_l * k_l * l_;
  105. float m_dS2 = 6.f * k_m * k_m * m_;
  106. float s_dS2 = 6.f * k_s * k_s * s_;
  107. float f = wl * l + wm * m + ws * s;
  108. float f1 = wl * l_dS + wm * m_dS + ws * s_dS;
  109. float f2 = wl * l_dS2 + wm * m_dS2 + ws * s_dS2;
  110. S = S - f * f1 / (f1 * f1 - 0.5f * f * f2);
  111. }
  112. return S;
  113. }
  114. vec2 find_cusp(float a, float b){ float S_cusp = compute_max_saturation(a, b); vec3 rgb_at_max = oklab_to_linear_srgb(vec3( 1, S_cusp * a, S_cusp * b ));
  115. float L_cusp = cbrt(1.f / max(max(rgb_at_max.r, rgb_at_max.g), rgb_at_max.b));
  116. float C_cusp = L_cusp * S_cusp;
  117. return vec2( L_cusp , C_cusp );
  118. }
  119. float find_gamut_intersection(float a, float b, float L1, float C1, float L0, vec2 cusp){ float t;
  120. if (((L1 - L0) * cusp.y - (cusp.x - L0) * C1) <= 0.f){
  121. t = cusp.y * L0 / (C1 * cusp.x + cusp.y * (L0 - L1));
  122. }
  123. else{ t = cusp.y * (L0 - 1.f) / (C1 * (cusp.x - 1.f) + cusp.y * (L0 - L1)); {
  124. float dL = L1 - L0;
  125. float dC = C1;
  126. float k_l = +0.3963377774f * a + 0.2158037573f * b;
  127. float k_m = -0.1055613458f * a - 0.0638541728f * b;
  128. float k_s = -0.0894841775f * a - 1.2914855480f * b;
  129. float l_dt = dL + dC * k_l;
  130. float m_dt = dL + dC * k_m;
  131. float s_dt = dL + dC * k_s; {
  132. float L = L0 * (1.f - t) + t * L1;
  133. float C = t * C1;
  134. float l_ = L + C * k_l;
  135. float m_ = L + C * k_m;
  136. float s_ = L + C * k_s;
  137. float l = l_ * l_ * l_;
  138. float m = m_ * m_ * m_;
  139. float s = s_ * s_ * s_;
  140. float ldt = 3.f * l_dt * l_ * l_;
  141. float mdt = 3.f * m_dt * m_ * m_;
  142. float sdt = 3.f * s_dt * s_ * s_;
  143. float ldt2 = 6.f * l_dt * l_dt * l_;
  144. float mdt2 = 6.f * m_dt * m_dt * m_;
  145. float sdt2 = 6.f * s_dt * s_dt * s_;
  146. float r = 4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s - 1.f;
  147. float r1 = 4.0767416621f * ldt - 3.3077115913f * mdt + 0.2309699292f * sdt;
  148. float r2 = 4.0767416621f * ldt2 - 3.3077115913f * mdt2 + 0.2309699292f * sdt2;
  149. float u_r = r1 / (r1 * r1 - 0.5f * r * r2);
  150. float t_r = -r * u_r;
  151. float g = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s - 1.f;
  152. float g1 = -1.2684380046f * ldt + 2.6097574011f * mdt - 0.3413193965f * sdt;
  153. float g2 = -1.2684380046f * ldt2 + 2.6097574011f * mdt2 - 0.3413193965f * sdt2;
  154. float u_g = g1 / (g1 * g1 - 0.5f * g * g2);
  155. float t_g = -g * u_g;
  156. float b = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s - 1.f;
  157. float b1 = -0.0041960863f * ldt - 0.7034186147f * mdt + 1.7076147010f * sdt;
  158. float b2 = -0.0041960863f * ldt2 - 0.7034186147f * mdt2 + 1.7076147010f * sdt2;
  159. float u_b = b1 / (b1 * b1 - 0.5f * b * b2);
  160. float t_b = -b * u_b;
  161. t_r = u_r >= 0.f ? t_r : 10000.f;
  162. t_g = u_g >= 0.f ? t_g : 10000.f;
  163. t_b = u_b >= 0.f ? t_b : 10000.f;
  164. t += min(t_r, min(t_g, t_b));
  165. }
  166. }
  167. }
  168. return t;
  169. }
  170. float find_gamut_intersection(float a, float b, float L1, float C1, float L0){ vec2 cusp = find_cusp(a, b);
  171. return find_gamut_intersection(a, b, L1, C1, L0, cusp);
  172. }
  173. vec3 gamut_clip_preserve_chroma(vec3 rgb){
  174. if (rgb.r < 1.f && rgb.g < 1.f && rgb.b < 1.f && rgb.r > 0.f && rgb.g > 0.f && rgb.b > 0.f)
  175. return rgb;
  176. vec3 lab = linear_srgb_to_oklab(rgb);
  177. float L = lab.x;
  178. float eps = 0.00001f;
  179. float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
  180. float a_ = lab.y / C;
  181. float b_ = lab.z / C;
  182. float L0 = clamp(L, 0.f, 1.f);
  183. float t = find_gamut_intersection(a_, b_, L, C, L0);
  184. float L_clipped = L0 * (1.f - t) + t * L;
  185. float C_clipped = t * C;
  186. return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
  187. }
  188. vec3 gamut_clip_project_to_0_5(vec3 rgb){
  189. if (rgb.r < 1.f && rgb.g < 1.f && rgb.b < 1.f && rgb.r > 0.f && rgb.g > 0.f && rgb.b > 0.f)
  190. return rgb;
  191. vec3 lab = linear_srgb_to_oklab(rgb);
  192. float L = lab.x;
  193. float eps = 0.00001f;
  194. float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
  195. float a_ = lab.y / C;
  196. float b_ = lab.z / C;
  197. float L0 = 0.5;
  198. float t = find_gamut_intersection(a_, b_, L, C, L0);
  199. float L_clipped = L0 * (1.f - t) + t * L;
  200. float C_clipped = t * C;
  201. return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
  202. }
  203. vec3 gamut_clip_project_to_L_cusp(vec3 rgb){
  204. if (rgb.r < 1.f && rgb.g < 1.f && rgb.b < 1.f && rgb.r > 0.f && rgb.g > 0.f && rgb.b > 0.f)
  205. return rgb;
  206. vec3 lab = linear_srgb_to_oklab(rgb);
  207. float L = lab.x;
  208. float eps = 0.00001f;
  209. float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
  210. float a_ = lab.y / C;
  211. float b_ = lab.z / C; vec2 cusp = find_cusp(a_, b_);
  212. float L0 = cusp.x;
  213. float t = find_gamut_intersection(a_, b_, L, C, L0);
  214. float L_clipped = L0 * (1.f - t) + t * L;
  215. float C_clipped = t * C;
  216. return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
  217. }
  218. vec3 gamut_clip_adaptive_L0_0_5(vec3 rgb, float alpha){
  219. if (rgb.r < 1.f && rgb.g < 1.f && rgb.b < 1.f && rgb.r > 0.f && rgb.g > 0.f && rgb.b > 0.f)
  220. return rgb;
  221. vec3 lab = linear_srgb_to_oklab(rgb);
  222. float L = lab.x;
  223. float eps = 0.00001f;
  224. float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
  225. float a_ = lab.y / C;
  226. float b_ = lab.z / C;
  227. float Ld = L - 0.5f;
  228. float e1 = 0.5f + abs(Ld) + alpha * C;
  229. float L0 = 0.5f * (1.f + sign(Ld) * (e1 - sqrt(e1 * e1 - 2.f * abs(Ld))));
  230. float t = find_gamut_intersection(a_, b_, L, C, L0);
  231. float L_clipped = L0 * (1.f - t) + t * L;
  232. float C_clipped = t * C;
  233. return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
  234. }
  235. vec3 gamut_clip_adaptive_L0_L_cusp(vec3 rgb, float alpha){
  236. if (rgb.r < 1.f && rgb.g < 1.f && rgb.b < 1.f && rgb.r > 0.f && rgb.g > 0.f && rgb.b > 0.f)
  237. return rgb;
  238. vec3 lab = linear_srgb_to_oklab(rgb);
  239. float L = lab.x;
  240. float eps = 0.00001f;
  241. float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
  242. float a_ = lab.y / C;
  243. float b_ = lab.z / C; vec2 cusp = find_cusp(a_, b_);
  244. float Ld = L - cusp.x;
  245. float k = 2.f * (Ld > 0.f ? 1.f - cusp.x : cusp.x);
  246. float e1 = 0.5f * k + abs(Ld) + alpha * C / k;
  247. float L0 = cusp.x + 0.5f * (sign(Ld) * (e1 - sqrt(e1 * e1 - 2.f * k * abs(Ld))));
  248. float t = find_gamut_intersection(a_, b_, L, C, L0);
  249. float L_clipped = L0 * (1.f - t) + t * L;
  250. float C_clipped = t * C;
  251. return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
  252. }
  253. float toe(float x){
  254. float k_1 = 0.206f;
  255. float k_2 = 0.03f;
  256. float k_3 = (1.f + k_1) / (1.f + k_2);
  257. return 0.5f * (k_3 * x - k_1 + sqrt((k_3 * x - k_1) * (k_3 * x - k_1) + 4.f * k_2 * k_3 * x));
  258. }
  259. float toe_inv(float x){
  260. float k_1 = 0.206f;
  261. float k_2 = 0.03f;
  262. float k_3 = (1.f + k_1) / (1.f + k_2);
  263. return (x * x + k_1 * x) / (k_3 * (x + k_2));
  264. }
  265. vec2 to_ST(vec2 cusp){
  266. float L = cusp.x;
  267. float C = cusp.y;
  268. return vec2( C / L, C / (1.f - L) );
  269. }
  270. vec2 get_ST_mid(float a_, float b_){
  271. float S = 0.11516993f + 1.f / (
  272. +7.44778970f + 4.15901240f * b_
  273. + a_ * (-2.19557347f + 1.75198401f * b_
  274. + a_ * (-2.13704948f - 10.02301043f * b_
  275. + a_ * (-4.24894561f + 5.38770819f * b_ + 4.69891013f * a_
  276. )))
  277. );
  278. float T = 0.11239642f + 1.f / (
  279. +1.61320320f - 0.68124379f * b_
  280. + a_ * (+0.40370612f + 0.90148123f * b_
  281. + a_ * (-0.27087943f + 0.61223990f * b_
  282. + a_ * (+0.00299215f - 0.45399568f * b_ - 0.14661872f * a_
  283. )))
  284. );
  285. return vec2( S, T );
  286. }
  287. vec3 get_Cs(float L, float a_, float b_){
  288. vec2 cusp = find_cusp(a_, b_);
  289. float C_max = find_gamut_intersection(a_, b_, L, 1.f, L, cusp);
  290. vec2 ST_max = to_ST(cusp); float k = C_max / min((L * ST_max.x), (1.f - L) * ST_max.y);
  291. float C_mid;{
  292. vec2 ST_mid = get_ST_mid(a_, b_); float C_a = L * ST_mid.x;
  293. float C_b = (1.f - L) * ST_mid.y;
  294. C_mid = 0.9f * k * sqrt(sqrt(1.f / (1.f / (C_a * C_a * C_a * C_a) + 1.f / (C_b * C_b * C_b * C_b))));
  295. }
  296. float C_0;{ float C_a = L * 0.4f;
  297. float C_b = (1.f - L) * 0.8f; C_0 = sqrt(1.f / (1.f / (C_a * C_a) + 1.f / (C_b * C_b)));
  298. }
  299. return vec3( C_0, C_mid, C_max );
  300. }
  301. vec3 okhsl_to_srgb(vec3 hsl){
  302. float h = hsl.x;
  303. float s = hsl.y;
  304. float l = hsl.z;
  305. if (l == 1.0f){
  306. return vec3( 1.f, 1.f, 1.f );
  307. }
  308. else if (l == 0.f){
  309. return vec3( 0.f, 0.f, 0.f );
  310. }
  311. float a_ = cos(2.f * M_PI * h);
  312. float b_ = sin(2.f * M_PI * h);
  313. float L = toe_inv(l);
  314. vec3 cs = get_Cs(L, a_, b_);
  315. float C_0 = cs.x;
  316. float C_mid = cs.y;
  317. float C_max = cs.z;
  318. float mid = 0.8f;
  319. float mid_inv = 1.25f;
  320. float C, t, k_0, k_1, k_2;
  321. if (s < mid){
  322. t = mid_inv * s;
  323. k_1 = mid * C_0;
  324. k_2 = (1.f - k_1 / C_mid);
  325. C = t * k_1 / (1.f - k_2 * t);
  326. }
  327. else{
  328. t = (s - mid)/ (1.f - mid);
  329. k_0 = C_mid;
  330. k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
  331. k_2 = (1.f - (k_1) / (C_max - C_mid));
  332. C = k_0 + t * k_1 / (1.f - k_2 * t);
  333. }
  334. vec3 rgb = oklab_to_linear_srgb(vec3( L, C * a_, C * b_ ));
  335. return vec3(
  336. srgb_transfer_function(rgb.r),
  337. srgb_transfer_function(rgb.g),
  338. srgb_transfer_function(rgb.b)
  339. );
  340. }
  341. vec3 okhsl_to_linear_srgb(vec3 hsl){
  342. float h = hsl.x;
  343. float s = hsl.y;
  344. float l = hsl.z;
  345. if (l == 1.0f){
  346. return vec3( 1.f, 1.f, 1.f );
  347. }
  348. else if (l == 0.f){
  349. return vec3( 0.f, 0.f, 0.f );
  350. }
  351. float a_ = cos(2.f * M_PI * h);
  352. float b_ = sin(2.f * M_PI * h);
  353. float L = toe_inv(l);
  354. vec3 cs = get_Cs(L, a_, b_);
  355. float C_0 = cs.x;
  356. float C_mid = cs.y;
  357. float C_max = cs.z;
  358. float mid = 0.8f;
  359. float mid_inv = 1.25f;
  360. float C, t, k_0, k_1, k_2;
  361. if (s < mid){
  362. t = mid_inv * s;
  363. k_1 = mid * C_0;
  364. k_2 = (1.f - k_1 / C_mid);
  365. C = t * k_1 / (1.f - k_2 * t);
  366. }
  367. else{
  368. t = (s - mid)/ (1.f - mid);
  369. k_0 = C_mid;
  370. k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
  371. k_2 = (1.f - (k_1) / (C_max - C_mid));
  372. C = k_0 + t * k_1 / (1.f - k_2 * t);
  373. }
  374. return oklab_to_linear_srgb(vec3( L, C * a_, C * b_ ));
  375. }
  376. vec3 okhsl_to_xyz(vec3 hsl){
  377. float h = hsl.x;
  378. float s = hsl.y;
  379. float l = hsl.z;
  380. if (l == 1.0f){
  381. return vec3( 1.f, 1.f, 1.f );
  382. }
  383. else if (l == 0.f){
  384. return vec3( 0.f, 0.f, 0.f );
  385. }
  386. float a_ = cos(2.f * M_PI * h);
  387. float b_ = sin(2.f * M_PI * h);
  388. float L = toe_inv(l);
  389. vec3 cs = get_Cs(L, a_, b_);
  390. float C_0 = cs.x;
  391. float C_mid = cs.y;
  392. float C_max = cs.z;
  393. float mid = 0.8f;
  394. float mid_inv = 1.25f;
  395. float C, t, k_0, k_1, k_2;
  396. if (s < mid){
  397. t = mid_inv * s;
  398. k_1 = mid * C_0;
  399. k_2 = (1.f - k_1 / C_mid);
  400. C = t * k_1 / (1.f - k_2 * t);
  401. }
  402. else{
  403. t = (s - mid)/ (1.f - mid);
  404. k_0 = C_mid;
  405. k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
  406. k_2 = (1.f - (k_1) / (C_max - C_mid));
  407. C = k_0 + t * k_1 / (1.f - k_2 * t);
  408. }
  409. return oklab_to_xyz(vec3( L, C * a_, C * b_ ));
  410. }
  411. vec3 srgb_to_okhsl(vec3 rgb){
  412. vec3 lab = linear_srgb_to_oklab(vec3(
  413. srgb_transfer_function_inv(rgb.r),
  414. srgb_transfer_function_inv(rgb.g),
  415. srgb_transfer_function_inv(rgb.b)
  416. ));
  417. float C = sqrt(lab.y * lab.y + lab.z * lab.z);
  418. float a_ = lab.y / C;
  419. float b_ = lab.z / C;
  420. float L = lab.x;
  421. float h = 0.5f + 0.5f * atan(-lab.z, -lab.y) / M_PI;
  422. vec3 cs = get_Cs(L, a_, b_);
  423. float C_0 = cs.x;
  424. float C_mid = cs.y;
  425. float C_max = cs.z;
  426. float mid = 0.8f;
  427. float mid_inv = 1.25f;
  428. float s;
  429. if (C < C_mid){
  430. float k_1 = mid * C_0;
  431. float k_2 = (1.f - k_1 / C_mid);
  432. float t = C / (k_1 + k_2 * C);
  433. s = t * mid;
  434. }
  435. else{
  436. float k_0 = C_mid;
  437. float k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
  438. float k_2 = (1.f - (k_1) / (C_max - C_mid));
  439. float t = (C - k_0) / (k_1 + k_2 * (C - k_0));
  440. s = mid + (1.f - mid) * t;
  441. }
  442. float l = toe(L);
  443. return vec3( h, s, l );
  444. }
  445. vec3 okhsv_to_srgb(vec3 hsv){
  446. float h = hsv.x;
  447. float s = hsv.y;
  448. float v = hsv.z;
  449. float a_ = cos(2.f * M_PI * h);
  450. float b_ = sin(2.f * M_PI * h);
  451. vec2 cusp = find_cusp(a_, b_);
  452. vec2 ST_max = to_ST(cusp);
  453. float S_max = ST_max.x;
  454. float T_max = ST_max.y;
  455. float S_0 = 0.5f;
  456. float k = 1.f- S_0 / S_max; float L_v = 1.f - s * S_0 / (S_0 + T_max - T_max * k * s);
  457. float C_v = s * T_max * S_0 / (S_0 + T_max - T_max * k * s);
  458. float L = v * L_v;
  459. float C = v * C_v; float L_vt = toe_inv(L_v);
  460. float C_vt = C_v * L_vt / L_v;
  461. float L_new = toe_inv(L);
  462. C = C * L_new / L;
  463. L = L_new;
  464. vec3 rgb_scale = oklab_to_linear_srgb(vec3( L_vt, a_ * C_vt, b_ * C_vt ));
  465. float scale_L = cbrt(1.f / max(max(rgb_scale.r, rgb_scale.g), max(rgb_scale.b, 0.f)));
  466. L = L * scale_L;
  467. C = C * scale_L;
  468. vec3 rgb = oklab_to_linear_srgb(vec3( L, C * a_, C * b_ ));
  469. return vec3(
  470. srgb_transfer_function(rgb.r),
  471. srgb_transfer_function(rgb.g),
  472. srgb_transfer_function(rgb.b)
  473. );
  474. })" R"(
  475. vec3 srgb_to_okhsv(vec3 rgb){
  476. vec3 lab = linear_srgb_to_oklab(vec3(
  477. srgb_transfer_function_inv(rgb.r),
  478. srgb_transfer_function_inv(rgb.g),
  479. srgb_transfer_function_inv(rgb.b)
  480. ));
  481. float C = sqrt(lab.y * lab.y + lab.z * lab.z);
  482. float a_ = lab.y / C;
  483. float b_ = lab.z / C;
  484. float L = lab.x;
  485. float h = 0.5f + 0.5f * atan(-lab.z, -lab.y) / M_PI;
  486. vec2 cusp = find_cusp(a_, b_);
  487. vec2 ST_max = to_ST(cusp);
  488. float S_max = ST_max.x;
  489. float T_max = ST_max.y;
  490. float S_0 = 0.5f;
  491. float k = 1.f - S_0 / S_max;
  492. float t = T_max / (C + L * T_max);
  493. float L_v = t * L;
  494. float C_v = t * C;
  495. float L_vt = toe_inv(L_v);
  496. float C_vt = C_v * L_vt / L_v; vec3 rgb_scale = oklab_to_linear_srgb(vec3( L_vt, a_ * C_vt, b_ * C_vt ));
  497. float scale_L = cbrt(1.f / max(max(rgb_scale.r, rgb_scale.g), max(rgb_scale.b, 0.f)));
  498. L = L / scale_L;
  499. C = C / scale_L;
  500. C = C * toe(L) / L;
  501. L = toe(L);
  502. float v = L / L_v;
  503. float s = (S_0 + T_max) * C_v / ((T_max * S_0) + T_max * k * C_v);
  504. return vec3 (h, s, v );
  505. }
  506. vec3 sRGB2XYZ(vec3 color){
  507. mat3 mat=mat3(vec3(0.4124564,0.3575761,0.1804375),
  508. vec3(0.2126729,0.7151522,0.0721750),
  509. vec3(0.0193339,0.1191920,0.9503041));
  510. return color*mat;
  511. }
  512. vec3 Clay2XYZ(vec3 color){
  513. mat3 mat=mat3(vec3(0.5767309,0.1855540,0.1881852),
  514. vec3(0.2973769,0.6273491,0.0752741),
  515. vec3(0.0270343,0.0706872,0.9911085));
  516. return color*mat;
  517. }
  518. vec3 D65P32XYZ(vec3 color){
  519. mat3 mat=mat3(vec3(0.4865709,0.2656677,0.1982173),
  520. vec3(0.2289746,0.6917385,0.0792869),
  521. vec3(0.0000000,0.0451134,1.0439444));
  522. return color*mat;
  523. }
  524. vec3 XYZ2sRGB(vec3 xyz){
  525. mat3 mat=mat3(vec3(3.2404542,-1.5371385,-0.4985314),
  526. vec3(-0.9692660,1.8760108,0.0415560),
  527. vec3(0.0556434,-0.2040259,1.0572252));
  528. return xyz*mat;
  529. }
  530. vec3 XYZ2Clay(vec3 xyz){
  531. mat3 mat=mat3(vec3(2.0413690,-0.5649464,-0.3446944),
  532. vec3(-0.9692660,1.8760108,0.0415560),
  533. vec3(0.0134474,-0.1183897,1.0154096));
  534. return xyz*mat;
  535. }
  536. vec3 XYZ2D65P3(vec3 xyz){
  537. mat3 mat=mat3(vec3(2.4934969,-0.9313836,-0.4027108),
  538. vec3(-0.8294890,1.7626641,0.0236247),
  539. vec3(0.0358458,-0.0761724,0.9568845));
  540. return xyz*mat;
  541. }
  542. vec3 YUV2sRGB(vec3 yuv){
  543. yuv[0]=1.1643*(yuv[0]-0.0625);
  544. yuv[1]-=0.5; yuv[2]-=0.5;
  545. vec3 rgb;
  546. rgb[0] = (yuv[0] + 1.793 * yuv[2]);
  547. rgb[1] = (yuv[0] - 0.213 * yuv[1] - 0.533 * yuv[2]);
  548. rgb[2] = (yuv[0] + 2.112 * yuv[1]);
  549. return rgb;
  550. }
  551. #define htsize HalftoneSize
  552. vec4 rgb2cmyk(vec3 rgb){
  553. vec4 cmyk; cmyk.w=1.-max(max(rgb.r,rgb.g),rgb.b);
  554. float k1=1.-cmyk.w;
  555. cmyk.r=(k1-rgb.r)/k1; cmyk.g=(k1-rgb.g)/k1; cmyk.b=(k1-rgb.b)/k1;
  556. return cmyk;
  557. }
  558. vec3 cmyk2rgb(vec4 cmyk){
  559. vec3 rgb; float k1=1.-cmyk.w;
  560. rgb.r=(1.-cmyk.r)*k1; rgb.g=(1.-cmyk.g)*k1; rgb.b=(1.-cmyk.b)*k1;
  561. return rgb;
  562. }
  563. float rand(vec2 co){
  564. return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453);
  565. }
  566. float HalftoneSingle(float a,float ps,float theta,float sm){
  567. float psize=ps; vec2 ctr=vec2(psize/2.,psize/2.); vec2 pt=vec2(psize,psize);
  568. vec2 xy=gl_FragCoord.xy; xy=vec2(sin(theta)*xy.x-cos(theta)*xy.y,cos(theta)*xy.x+sin(theta)*xy.y);
  569. xy.x=xy.x+rand(xy)/1.; xy.y=xy.y+rand(xy)/1.; ivec2 xyi=ivec2(int(xy.x/psize),int(xy.y/psize));
  570. vec2 xyf=mod(xy,pt);
  571. float px1=(sm==1.)?(3.0f/psize):(0.000001);
  572. float cmp=(pow(a,1.)*psize/2.*(1.414+px1)); float fac=distance(xyf,ctr)/cmp;
  573. return smoothstep(1.+px1,1.-px1,fac);
  574. }
  575. vec4 halftone(vec4 color){
  576. vec4 cmyk=rgb2cmyk(color.rgb); float a=color.a*(gl_FragCoord.x/400.0f);
  577. cmyk.r=HalftoneSingle(cmyk.r,htsize,rad(15.0),1.);
  578. cmyk.g=HalftoneSingle(cmyk.g,htsize,rad(75.0),1.);
  579. cmyk.b=HalftoneSingle(cmyk.b,htsize,rad(0.),1.);
  580. cmyk.a=HalftoneSingle(cmyk.a,htsize,rad(45.0),1.);
  581. color.rgb=cmyk2rgb(cmyk);
  582. //color.a=HalftoneSingle(a,htsize,rad(30),0);
  583. return color;
  584. }
  585. )";
  586. extern "C" const char* TNS_VERTEX_SIMPLE_MATCAP = R"(#version 310 es
  587. precision highp float;
  588. uniform mat4 mProjection;
  589. uniform mat4 mModel;
  590. uniform mat4 mView;
  591. in vec4 vVertex;
  592. in vec3 vNormal;
  593. smooth out vec3 fNormal;
  594. void main(){
  595. gl_Position = mProjection * mView * mModel * vVertex;
  596. vec3 N = ( mView * mModel * vec4(vNormal,0)).xyz;
  597. fNormal = normalize(N);
  598. })";
  599. extern "C" const char* TNS_FRAGMENT_SIMPLE_MATCAP = R"(#version 310 es
  600. precision highp float;
  601. smooth in vec3 fNormal;
  602. out vec4 outColor;
  603. float Interpolate(float between1,float between2,float value1,float value2,float key){
  604. float i = (key-between1)/(between2-between1);
  605. return value1*(1.-i)+value2*i;
  606. }
  607. void main(){
  608. float value = dot(vec3(0,0,1),fNormal);
  609. if(value<0.65) value=0.15;
  610. else if(value>=0.65 && value<0.85) value=Interpolate(0.65,0.85,0.15,0.75,value);
  611. else if(value>=0.85 && value<0.95) value=0.75;
  612. else if(value>=0.95) value=0.9;
  613. outColor = vec4(vec3(0.84, 0.41, 0.16)*value,1);
  614. })";
  615. extern "C" const char* TNS_VERTEX_GRID = R"(#version 310 es
  616. precision highp float;
  617. uniform mat4 mProjection;
  618. uniform mat4 mModel;
  619. uniform mat4 mView;
  620. in vec4 vVertex;
  621. in vec4 vColor;
  622. in vec2 vUV;
  623. out vec4 fColor;
  624. out vec2 uv;
  625. void main(){
  626. vec4 pos = mProjection * mView * mModel * vVertex;
  627. gl_Position = pos;
  628. fColor = vColor;
  629. uv = vUV;
  630. })";
  631. extern "C" const char* TNS_FRAGMENT_TRANSPARNT_GRID = R"(#version 310 es
  632. precision highp float;
  633. in vec4 fColor;
  634. in vec2 uv;
  635. out vec4 outColor;
  636. void main(){
  637. vec4 c = fColor;
  638. c.a = sin(uv.x)*sin(uv.y)>0.?c.a:0.;
  639. outColor = c;
  640. })";
  641. extern "C" const char* LA_IMM_VERTEX_SHADER = R"(#version 310 es
  642. precision highp float;
  643. uniform mat4 mProjection;
  644. uniform mat4 mModel;
  645. uniform mat4 mView;
  646. uniform int DoOffset;
  647. in vec4 vVertex;
  648. in vec4 vColor;
  649. in vec3 vNormal;
  650. in vec2 vUV;
  651. out vec4 fColor;
  652. out vec2 fUV;
  653. flat out vec3 fNormal;
  654. out vec3 fGPos;
  655. void main(){
  656. vec4 pos=mProjection * mView * mModel * vVertex;
  657. if(DoOffset!=0){ pos.xyw*=1.0+0.00005*float(DoOffset); }
  658. gl_Position = pos;
  659. fColor = vColor;
  660. fUV=vUV;
  661. fGPos=vec3((mModel * vVertex).xyz);
  662. fNormal= normalize((mModel * vec4(vNormal,0.)).xyz);
  663. })";
  664. extern "C" const char* LA_IMM_FRAGMENT_SHADER = R"(#version 310 es
  665. precision highp float;
  666. precision highp int;
  667. uniform highp sampler2D TexColor;
  668. uniform highp sampler2DMS TexColorMS;
  669. uniform highp usampler2D TexColorUI;
  670. uniform highp sampler3D TexLut;
  671. uniform int UseLut;
  672. uniform int TextureMode;
  673. uniform int ColorMode;
  674. uniform int MultiplyColor;
  675. uniform int SampleAmount;
  676. uniform int UseNormal;
  677. uniform float UseHalftone;
  678. uniform float HalftoneSize;
  679. uniform int InputColorSpace;
  680. uniform int OutputColorSpace;
  681. uniform int Composing;
  682. uniform float ComposingGamma;
  683. uniform float ComposingBlackpoint;
  684. uniform int ShowStripes;
  685. uniform float HCYGamma;
  686. uniform vec3 uObjectPos;
  687. uniform vec3 uViewPos;
  688. uniform vec3 uViewDir;
  689. in vec4 fColor;
  690. in vec2 fUV;
  691. flat in vec3 fNormal;
  692. in vec3 fGPos;
  693. layout(location = 0) out vec4 outColor;
  694. layout(location = 1) out vec3 outNormal;
  695. layout(location = 2) out vec3 outGPos;
  696. #with TNS_SHADER_COLOR_COMMON
  697. #with TNS_SHADER_LIBRARY
  698. #with TNS_GLES_UINT_TEXTURE
  699. vec4 ConvertColorSpace(vec4 _rgba){
  700. vec3 color=_rgba.rgb;
  701. if(InputColorSpace!=OutputColorSpace){
  702. if(ColorMode==0){
  703. if(InputColorSpace==0) color=to_linear_srgb(color);
  704. else if(InputColorSpace==1) color=to_linear_clay(color);
  705. else if(InputColorSpace==2) color=to_linear_srgb(color);
  706. }
  707. vec3 xyz; if(ColorMode==1){ color.y=pow(color.y,max(HCYGamma,1.)); color=okhsl_to_linear_srgb(color); }
  708. if(InputColorSpace==1){ xyz=Clay2XYZ(color); }
  709. else if(InputColorSpace==0){ xyz=sRGB2XYZ(color); }
  710. else if(InputColorSpace==2){ xyz=D65P32XYZ(color); }
  711. if(OutputColorSpace==0){ color=to_log_srgb(XYZ2sRGB(xyz)); /*_rgba.a=srgb_transfer_function(_rgba.a);*/ }
  712. else if(OutputColorSpace==1){ color=to_log_clay(XYZ2Clay(xyz)); /*_rgba.a=pow(_rgba.a,1.0/2.19921875);*/ }
  713. else if(OutputColorSpace==2){ color=to_log_srgb(XYZ2D65P3(xyz)); /*_rgba.a=pow(_rgba.a,1.0/2.19921875);*/ }
  714. }else{
  715. if(ColorMode==1){ color.y=pow(color.y,max(HCYGamma,1.)); color=okhsl_to_srgb(color); }
  716. else if(ColorMode==0){
  717. color = color;
  718. }else{
  719. if(OutputColorSpace==0){ color=to_log_srgb(color); /*_rgba.a=srgb_transfer_function(_rgba.a);*/ }
  720. else if(OutputColorSpace==1){ color=to_log_clay(color); /*_rgba.a=pow(_rgba.a,1.0/2.19921875);*/ }
  721. else if(OutputColorSpace==2){ color=to_log_srgb(color); /*_rgba.a=pow(_rgba.a,1.0/2.19921875);*/ }
  722. }
  723. }
  724. if(ShowStripes!=0){
  725. if(color.r>1.00001||color.g>1.00001||color.b>1.00001||color.r<0.||color.g<0.||color.b<0.){ color=mix(color,vec3(0.5,0.5,0.5),(sin((gl_FragCoord.x+gl_FragCoord.y)/2.)>0.)?1.:0.5); }
  726. }
  727. return vec4(color,_rgba.a);
  728. }
  729. #ifdef GLES_UINT_TEXTURE
  730. vec4 cunpack(uint d){
  731. return vec4(float(d&0xFFu)/255.,float((d>>8u)&0xFFu)/255.,float((d>>16u)&0xFFu)/255.,float((d>>24u)&0xFFu)/255.);
  732. }
  733. #endif
  734. void main(){
  735. vec4 color=vec4(1,0,1,1);
  736. if(TextureMode==0){ color = fColor; if(UseHalftone>0.) color.a=HalftoneSingle(color.a,htsize,rad(7.),0.); }
  737. else if(TextureMode==1){color = vec4(fColor.rgb,fColor.a*texture(TexColor,fUV.st).r);}
  738. else if(TextureMode==2){
  739. color=texture(TexColor,fUV.st);
  740. if(MultiplyColor!=0){color*=fColor;}
  741. }else if(TextureMode==3){
  742. color=vec4(0,0,0,0);
  743. ivec2 texSize = textureSize(TexColorMS);
  744. for(int i=0;i<SampleAmount;i++){ vec4 res=texelFetch(TexColorMS, ivec2(fUV * vec2(texSize)),i); if(res[0]>-1e19) color+=res; };
  745. color/=float(SampleAmount);
  746. if(MultiplyColor!=0){color*=fColor;}
  747. }else if(TextureMode==4){
  748. #ifdef GLES_UINT_TEXTURE
  749. color=cunpack(texture(TexColorUI,fUV.st).r);
  750. #else
  751. color=vec4(texture(TexColorUI,fUV.st))/vec4(65535.);
  752. #endif
  753. if(MultiplyColor!=0){color*=fColor;}
  754. }else if(TextureMode==101){ // YUYV
  755. ivec2 tsize = textureSize(TexColor,0);
  756. vec2 pixel = fUV.st * vec2(tsize);
  757. ivec2 tpixel = ivec2(pixel);
  758. vec4 yuyv = texture(TexColor,fUV.st); //texelFetch(TexColor,tpixel,0);
  759. vec3 yuv; yuv.yz=yuyv.yw;
  760. if(fract(pixel.x)<=1.){ yuv.x=yuyv.x; }else{ yuv.x=yuyv.z; }
  761. color=vec4(YUV2sRGB(yuv),1.0f);
  762. if(MultiplyColor!=0){color*=fColor;}
  763. }else if(TextureMode==102){ // UYVY
  764. ivec2 tsize = textureSize(TexColor,0);
  765. vec2 pixel = fUV.st * vec2(tsize);
  766. ivec2 tpixel = ivec2(pixel);
  767. vec4 uyvy = texture(TexColor,fUV.st); //texelFetch(TexColor,tpixel,0);
  768. vec3 yuv; yuv.yz=uyvy.xz;
  769. if(fract(pixel.x)<=1.){ yuv.x=uyvy.y; }else{ yuv.x=uyvy.w; }
  770. color=vec4(YUV2sRGB(yuv),1.0f);
  771. if(MultiplyColor!=0){color*=fColor;}
  772. }
  773. if(UseNormal!=0){
  774. color.a=HalftoneSingle(color.a,htsize,rad(7.),0.);
  775. if(color.a==0.) discard;
  776. float light_factor=dot(fNormal,vec3(0,0,1));
  777. vec3 vd=uViewDir;
  778. if(length(uViewDir)<0.5){ vd=fGPos-uViewPos; }
  779. float view=dot(fNormal,vd);
  780. float factor=abs(light_factor);
  781. if(light_factor*view>0.){ factor=0.; }
  782. color=vec4(color.rgb*mix(0.2,1.,factor),color.a);
  783. vec3 oNormal=fNormal; if(view<0.){ oNormal=-fNormal; }
  784. outNormal = oNormal;
  785. }
  786. color=ConvertColorSpace(color); color.a=clamp(color.a,0.,1.);
  787. if(Composing!=0){
  788. if(color.rgb!=vec3(0,0,0)){
  789. //vec3 cl=srgb_to_okhsl(color.rgb);
  790. //cl.z=cl.z/1.0f*(1.0f-ComposingBlackpoint)+ComposingBlackpoint;
  791. //cl.z=pow(cl.z,ComposingGamma);
  792. //color.rgb=okhsl_to_srgb(cl);
  793. color.rgb=pow(color.rgb,vec3(ComposingGamma,ComposingGamma,ComposingGamma));
  794. color.r=color.r/1.0f*(1.0f-ComposingBlackpoint)+ComposingBlackpoint;
  795. color.g=color.g/1.0f*(1.0f-ComposingBlackpoint)+ComposingBlackpoint;
  796. color.b=color.b/1.0f*(1.0f-ComposingBlackpoint)+ComposingBlackpoint;
  797. }else{
  798. color.rgb=vec3(ComposingBlackpoint,ComposingBlackpoint,ComposingBlackpoint);
  799. }
  800. }
  801. if(UseLut!=0){
  802. color.rgb = texture(TexLut,color.bgr).rgb;
  803. }
  804. if(UseHalftone>1e-6){ color=mix(color,halftone(color),UseHalftone); }
  805. outColor = color;
  806. outGPos = fGPos;
  807. })";
  808. extern "C" const char* LA_OBJECT_FRAGMENT_SHADER = R"(#version 310 es
  809. precision highp float;
  810. uniform int UseNormal;
  811. uniform float UseHalftone;
  812. uniform float HalftoneSize;
  813. uniform vec3 uObjectPos;
  814. uniform vec3 uViewPos;
  815. uniform vec3 uViewDir;
  816. in vec4 fColor;
  817. in vec2 fUV;
  818. flat in vec3 fNormal;
  819. in vec3 fGPos;
  820. layout(location = 0) out vec4 outColor;
  821. layout(location = 1) out vec3 outNormal;
  822. layout(location = 2) out vec3 outGPos;
  823. #with TNS_SHADER_COLOR_COMMON
  824. #with TNS_SHADER_LIBRARY
  825. void main(){
  826. vec4 color=fColor;
  827. {
  828. #with TNS_SHADER_MATERIAL
  829. }
  830. if(UseNormal!=0){
  831. color.a=HalftoneSingle(color.a,htsize,rad(7),0);
  832. if(color.a==0) discard;
  833. float light_factor=dot(fNormal,vec3(0,0,1));
  834. vec3 vd=uViewDir;
  835. if(len(uViewDir)<0.5){ vd=fGPos-uViewPos; }
  836. float view=dot(fNormal,vd);
  837. float factor=abs(light_factor);
  838. if(light_factor*view>0){ factor=0; }
  839. color=vec4(color.rgb*mix(0.2,1.,factor),color.a);
  840. vec3 oNormal=fNormal; if(view<0){ oNormal=-fNormal; }
  841. outNormal = oNormal;
  842. }
  843. if(UseHalftone>1e-6){ color=mix(color,halftone(color),UseHalftone); }
  844. outColor = color; outGPos = fGPos;
  845. })";
  846. extern "C" const char* LA_FLOOR_VERTEX_SHADER = R"(#version 310 es
  847. precision highp float;
  848. uniform mat4 mProjection;
  849. uniform mat4 mModel;
  850. uniform mat4 mView;
  851. in vec4 vVertex;
  852. in vec4 vColor;
  853. out vec3 fGPos;
  854. out vec4 fColor;
  855. void main(){
  856. gl_Position=mProjection * mView * mModel * vVertex;
  857. fGPos=vec3((mModel * vVertex).xyz);
  858. fColor=vColor;
  859. })";
  860. extern "C" const char* LA_FLOOR_FRAGMENT_SHADER = R"(#version 310 es
  861. precision highp float;
  862. uniform vec3 uViewPos;
  863. uniform float uFar;
  864. in vec4 fColor;
  865. in vec3 fGPos;
  866. layout(location = 0) out vec4 outColor;
  867. void main(){
  868. float fac=1.-pow(clamp(length(uViewPos-fGPos)/uFar,0.,1.),0.4);
  869. outColor=vec4(fColor.rgb,fColor.a*fac);
  870. })";
  871. extern "C" const char* LA_RAY_VERTEX_SHADER = R"(#version 310 es
  872. precision highp float;
  873. in vec3 vUV;
  874. in vec4 vVertex;
  875. out vec3 fViewDir;
  876. void main(){
  877. gl_Position=vVertex;
  878. fViewDir = vUV;
  879. })";
  880. extern "C" const char* LA_SHADER_LIB_FXAA = R"(
  881. #define DIFF_LUM_ABS_HOLD 0.0833
  882. #define DIFF_LUM_RES_HOLD 0.166
  883. float luminance(vec3 col) {
  884. return dot(col, vec3(0.2126729f, 0.7151522f, 0.0721750f));
  885. }
  886. vec4 fxaa(in sampler2D tex, vec2 uv, vec2 texsize) {
  887. vec3 e = vec3(-1., 1., 0.);
  888. vec2 offuv = uv;
  889. vec3 colnw = texture(tex, uv + e.xy / texsize).rgb;
  890. vec3 coln = texture(tex, uv + e.zy / texsize).rgb;
  891. vec3 colne = texture(tex, uv + e.yy / texsize).rgb;
  892. vec3 colw = texture(tex, uv + e.xz / texsize).rgb;
  893. vec4 colm4 = texture(tex, uv + e.zz / texsize);
  894. vec3 colm = colm4.rgb;
  895. vec3 cole = texture(tex, uv + e.yz / texsize).rgb;
  896. vec3 colsw = texture(tex, uv + e.xx / texsize).rgb;
  897. vec3 cols = texture(tex, uv + e.zx / texsize).rgb;
  898. vec3 colse = texture(tex, uv + e.yx / texsize).rgb;
  899. float lnw = luminance(colnw), ln = luminance(coln), lne = luminance(colne),
  900. lw = luminance(colw), lm = luminance(colm), le = luminance(cole),
  901. lsw = luminance(colsw), ls = luminance(cols), lse = luminance(colse);
  902. float maxl = max(ln, max(ls, max(lw, max(le, lm))));
  903. float minl = min(ln, min(ls, min(lw, min(le, lm))));
  904. float diff = maxl - minl;
  905. if (diff < max(DIFF_LUM_ABS_HOLD, DIFF_LUM_RES_HOLD * maxl)) return colm4;
  906. float filterfactor = 0.;
  907. filterfactor += 2. * (ln + lw + ls + le) + lnw + lne + lsw + lse;
  908. filterfactor /= 12.;
  909. filterfactor = abs(filterfactor - lm);
  910. filterfactor = clamp(filterfactor / diff, 0., 1.);
  911. float blend = smoothstep(0., 1., filterfactor);
  912. blend *= blend;
  913. float hedge = 2.*(ln + ls - 2.*lm) + (lne + lse - 2.*le) + (lnw + lsw - 2.*lw);
  914. float vedge = 2.*(le + lw - 2.*lm) + (lne + lnw - 2.*ln) + (lse + lsw - 2.*ls);
  915. float ish = step(vedge, hedge);
  916. float psoff = ish >= 1.0 ? 1./texsize.y : 1./texsize.x;
  917. float pleft = ish >= 1.0 ? ln : le;
  918. float pright = ish >= 1.0 ? ls : lw;
  919. if (abs(pleft - lm) < abs(pright - lm)) psoff = -psoff;
  920. if (ish >= 1.0) { offuv.y += psoff * blend; }else{ offuv.x += psoff * blend; }
  921. return vec4(texture(tex, offuv).rgb,colm4.a);
  922. })";
  923. extern "C" const char* LA_RAY_FRAGMENT_SHADER = R"(#version 310 es
  924. precision highp float;
  925. uniform vec3 uViewDir;
  926. uniform vec3 uViewPos;
  927. uniform float uFOV;
  928. in vec3 fViewDir;
  929. uniform highp sampler2D TexColor;
  930. uniform highp sampler2D TexNormal;
  931. uniform highp sampler2D TexGPos;
  932. #with LA_SHADER_LIB_FXAA
  933. out vec4 outColor;
  934. void main(){
  935. float d=dot(uViewDir,normalize(fViewDir));
  936. float target=cos(uFOV/2.);
  937. vec4 color=vec4(1.,1.,1.,1.); float mul=0.;
  938. //if(d<(target+0.005)&&d>target) mul=1.0;
  939. vec2 uv=gl_FragCoord.xy/vec2(textureSize(TexColor,0));
  940. vec4 buffer_color=fxaa(TexColor,uv,vec2(textureSize(TexColor,0)));
  941. //vec4 buffer_color=texture(TexColor,uv);
  942. outColor = mul*color+buffer_color;
  943. })";
  944. extern "C" const char* LA_SCENE_VERTEX_SHADER = R"(#version 310 es
  945. precision highp float;
  946. uniform mat4 mProjection;
  947. uniform mat4 mModel;
  948. uniform mat4 mView;
  949. uniform mat4 mShadow;
  950. in vec4 vVertex;
  951. in vec4 vColor;
  952. in vec4 vNormal;
  953. in vec2 vUV;
  954. out vec4 fColor;
  955. //out vec4 fNormal;
  956. out vec2 fUV;
  957. out vec4 fGPos;
  958. void main(){
  959. gl_Position= mProjection * mView * mModel * vVertex;
  960. fUV=vUV;
  961. //fNormal=vNormal;
  962. fColor=vColor;
  963. fGPos= mShadow * mModel * vVertex;\
  964. })";
  965. extern "C" const char* LA_SCENE_FRAGMENT_SHADER = R"(#version 310 es
  966. precision highp float;
  967. uniform highp sampler2D TexColor;
  968. uniform highp sampler2DMS TexColorMS;\
  969. uniform int TextureMode;
  970. uniform int SampleAmount;
  971. uniform int MultiplyColor;
  972. in vec4 fColor;
  973. //in vec4 fNormal;
  974. in vec2 fUV;
  975. in vec4 fGPos;
  976. out vec4 outColor;
  977. vec4 GetTexture(vec2 uv){
  978. vec4 color=vec4(1,0,1,1);
  979. if(TextureMode==1 || TextureMode==2){ return texture(TexColor,uv); }
  980. else if(TextureMode==3){
  981. ivec2 texSize = textureSize(TexColorMS);
  982. for(int i=0;i<SampleAmount;i++) color+=texelFetch(TexColorMS, ivec2(fUV * vec2(texSize)),i);
  983. color/=float(SampleAmount);
  984. if(MultiplyColor!=0){color*=fColor;}
  985. return color;
  986. }
  987. else return vec4(1,0,1,1);
  988. }
  989. float GetShadow(vec4 GPos){
  990. vec3 projCoords = GPos.xyz / GPos.w;
  991. projCoords = projCoords * 0.5 + 0.5;
  992. float closestDepth = GetTexture(projCoords.xy).r;
  993. float currentDepth = projCoords.z;
  994. float shadow = currentDepth > (closestDepth+0.001) ? 0.5 : 1.0;
  995. return shadow;
  996. }
  997. void main(){
  998. outColor=GetShadow(fGPos)*fColor;
  999. })";
  1000. extern "C" const char* LA_CASCADE_SHADOW_VERTEX_SHADER = R"(#version 310 es
  1001. precision highp float;
  1002. in vec4 vVertex;
  1003. uniform mat4 mModel;
  1004. uniform mat4 mShadow;
  1005. void main(){
  1006. gl_Position=mShadow*mModel*vVertex;
  1007. })";
  1008. extern "C" const char* LA_CASCADE_SHADOW_FRAGMENT_SHADER = "#version 310 es\n \
  1009. precision highp float;\nvoid main(){gl_FragDepth = gl_FragCoord.z;}";
  1010. extern "C" const char* LA_SELECTION_VERTEX_SHADER = R"(#version 310 es
  1011. precision highp float;
  1012. in vec4 vVertex;
  1013. in vec3 vColor;
  1014. uniform mat4 mProjection;
  1015. uniform mat4 mModel;
  1016. uniform mat4 mView;
  1017. uniform int DoOffset;
  1018. flat out vec3 fIdColor;
  1019. void main(){
  1020. vec4 pos = mProjection * mView * mModel * vVertex;
  1021. if(DoOffset!=0){ pos.xyw*=1.00005; }
  1022. gl_Position = pos;
  1023. fIdColor = vColor;
  1024. })";
  1025. extern "C" const char* LA_SELECTION_FRAGMENT_SHADER = R"(#version 310 es
  1026. precision highp float;
  1027. flat in vec3 fIdColor;
  1028. out vec4 outColor;
  1029. void main(){
  1030. outColor=vec4(fIdColor,1.);
  1031. })";