canvas.glsl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /* clang-format off */
  2. [vertex]
  3. #ifdef USE_GLES_OVER_GL
  4. #define lowp
  5. #define mediump
  6. #define highp
  7. #else
  8. precision mediump float;
  9. precision mediump int;
  10. #endif
  11. uniform highp mat4 projection_matrix;
  12. /* clang-format on */
  13. #include "stdlib.glsl"
  14. uniform highp mat4 modelview_matrix;
  15. uniform highp mat4 extra_matrix;
  16. attribute highp vec2 vertex; // attrib:0
  17. attribute vec4 color_attrib; // attrib:3
  18. attribute vec2 uv_attrib; // attrib:4
  19. #ifdef USE_SKELETON
  20. attribute highp vec4 bone_indices; // attrib:6
  21. attribute highp vec4 bone_weights; // attrib:7
  22. #endif
  23. #ifdef USE_INSTANCING
  24. attribute highp vec4 instance_xform0; //attrib:8
  25. attribute highp vec4 instance_xform1; //attrib:9
  26. attribute highp vec4 instance_xform2; //attrib:10
  27. attribute highp vec4 instance_color; //attrib:11
  28. #ifdef USE_INSTANCE_CUSTOM
  29. attribute highp vec4 instance_custom_data; //attrib:12
  30. #endif
  31. #endif
  32. #ifdef USE_SKELETON
  33. uniform highp sampler2D skeleton_texture; // texunit:-3
  34. uniform highp ivec2 skeleton_texture_size;
  35. uniform highp mat4 skeleton_transform;
  36. uniform highp mat4 skeleton_transform_inverse;
  37. #endif
  38. varying vec2 uv_interp;
  39. varying vec4 color_interp;
  40. uniform highp vec2 color_texpixel_size;
  41. #ifdef USE_TEXTURE_RECT
  42. uniform vec4 dst_rect;
  43. uniform vec4 src_rect;
  44. #endif
  45. uniform highp float time;
  46. #ifdef USE_LIGHTING
  47. // light matrices
  48. uniform highp mat4 light_matrix;
  49. uniform highp mat4 light_matrix_inverse;
  50. uniform highp mat4 light_local_matrix;
  51. uniform highp mat4 shadow_matrix;
  52. uniform highp vec4 light_color;
  53. uniform highp vec4 light_shadow_color;
  54. uniform highp vec2 light_pos;
  55. uniform highp float shadowpixel_size;
  56. uniform highp float shadow_gradient;
  57. uniform highp float light_height;
  58. uniform highp float light_outside_alpha;
  59. uniform highp float shadow_distance_mult;
  60. varying vec4 light_uv_interp;
  61. varying vec2 transformed_light_uv;
  62. varying vec4 local_rot;
  63. #ifdef USE_SHADOWS
  64. varying highp vec2 pos;
  65. #endif
  66. const bool at_light_pass = true;
  67. #else
  68. const bool at_light_pass = false;
  69. #endif
  70. /* clang-format off */
  71. VERTEX_SHADER_GLOBALS
  72. /* clang-format on */
  73. vec2 select(vec2 a, vec2 b, bvec2 c) {
  74. vec2 ret;
  75. ret.x = c.x ? b.x : a.x;
  76. ret.y = c.y ? b.y : a.y;
  77. return ret;
  78. }
  79. void main() {
  80. vec4 color = color_attrib;
  81. #ifdef USE_INSTANCING
  82. mat4 extra_matrix_instance = extra_matrix * transpose(mat4(instance_xform0, instance_xform1, instance_xform2, vec4(0.0, 0.0, 0.0, 1.0)));
  83. color *= instance_color;
  84. vec4 instance_custom = instance_custom_data;
  85. #else
  86. mat4 extra_matrix_instance = extra_matrix;
  87. vec4 instance_custom = vec4(0.0);
  88. #endif
  89. #ifdef USE_TEXTURE_RECT
  90. if (dst_rect.z < 0.0) { // Transpose is encoded as negative dst_rect.z
  91. uv_interp = src_rect.xy + abs(src_rect.zw) * vertex.yx;
  92. } else {
  93. uv_interp = src_rect.xy + abs(src_rect.zw) * vertex;
  94. }
  95. vec4 outvec = vec4(0.0, 0.0, 0.0, 1.0);
  96. // This is what is done in the GLES 3 bindings and should
  97. // take care of flipped rects.
  98. //
  99. // But it doesn't.
  100. // I don't know why, will need to investigate further.
  101. outvec.xy = dst_rect.xy + abs(dst_rect.zw) * select(vertex, vec2(1.0, 1.0) - vertex, lessThan(src_rect.zw, vec2(0.0, 0.0)));
  102. // outvec.xy = dst_rect.xy + abs(dst_rect.zw) * vertex;
  103. #else
  104. vec4 outvec = vec4(vertex.xy, 0.0, 1.0);
  105. uv_interp = uv_attrib;
  106. #endif
  107. {
  108. vec2 src_vtx = outvec.xy;
  109. /* clang-format off */
  110. VERTEX_SHADER_CODE
  111. /* clang-format on */
  112. }
  113. #if !defined(SKIP_TRANSFORM_USED)
  114. outvec = extra_matrix_instance * outvec;
  115. outvec = modelview_matrix * outvec;
  116. #endif
  117. color_interp = color;
  118. #ifdef USE_PIXEL_SNAP
  119. outvec.xy = floor(outvec + 0.5).xy;
  120. #endif
  121. #ifdef USE_SKELETON
  122. // look up transform from the "pose texture"
  123. if (bone_weights != vec4(0.0)) {
  124. highp mat4 bone_transform = mat4(0.0);
  125. for (int i = 0; i < 4; i++) {
  126. ivec2 tex_ofs = ivec2(int(bone_indices[i]) * 2, 0);
  127. highp mat4 b = mat4(
  128. texel2DFetch(skeleton_texture, skeleton_texture_size, tex_ofs + ivec2(0, 0)),
  129. texel2DFetch(skeleton_texture, skeleton_texture_size, tex_ofs + ivec2(1, 0)),
  130. vec4(0.0, 0.0, 1.0, 0.0),
  131. vec4(0.0, 0.0, 0.0, 1.0));
  132. bone_transform += b * bone_weights[i];
  133. }
  134. mat4 bone_matrix = skeleton_transform * transpose(bone_transform) * skeleton_transform_inverse;
  135. outvec = bone_matrix * outvec;
  136. }
  137. #endif
  138. gl_Position = projection_matrix * outvec;
  139. #ifdef USE_LIGHTING
  140. light_uv_interp.xy = (light_matrix * outvec).xy;
  141. light_uv_interp.zw = (light_local_matrix * outvec).xy;
  142. transformed_light_uv = (mat3(light_matrix_inverse) * vec3(light_uv_interp.zw, 0.0)).xy; //for normal mapping
  143. #ifdef USE_SHADOWS
  144. pos = outvec.xy;
  145. #endif
  146. local_rot.xy = normalize((modelview_matrix * (extra_matrix_instance * vec4(1.0, 0.0, 0.0, 0.0))).xy);
  147. local_rot.zw = normalize((modelview_matrix * (extra_matrix_instance * vec4(0.0, 1.0, 0.0, 0.0))).xy);
  148. #ifdef USE_TEXTURE_RECT
  149. local_rot.xy *= sign(src_rect.z);
  150. local_rot.zw *= sign(src_rect.w);
  151. #endif
  152. #endif
  153. }
  154. /* clang-format off */
  155. [fragment]
  156. #ifdef USE_GLES_OVER_GL
  157. #define lowp
  158. #define mediump
  159. #define highp
  160. #else
  161. precision mediump float;
  162. precision mediump int;
  163. #endif
  164. uniform sampler2D color_texture; // texunit:-1
  165. /* clang-format on */
  166. uniform highp vec2 color_texpixel_size;
  167. uniform mediump sampler2D normal_texture; // texunit:-2
  168. varying mediump vec2 uv_interp;
  169. varying mediump vec4 color_interp;
  170. uniform highp float time;
  171. uniform vec4 final_modulate;
  172. #ifdef SCREEN_TEXTURE_USED
  173. uniform sampler2D screen_texture; // texunit:-4
  174. #endif
  175. #ifdef SCREEN_UV_USED
  176. uniform vec2 screen_pixel_size;
  177. #endif
  178. #ifdef USE_LIGHTING
  179. uniform highp mat4 light_matrix;
  180. uniform highp mat4 light_local_matrix;
  181. uniform highp mat4 shadow_matrix;
  182. uniform highp vec4 light_color;
  183. uniform highp vec4 light_shadow_color;
  184. uniform highp vec2 light_pos;
  185. uniform highp float shadowpixel_size;
  186. uniform highp float shadow_gradient;
  187. uniform highp float light_height;
  188. uniform highp float light_outside_alpha;
  189. uniform highp float shadow_distance_mult;
  190. uniform lowp sampler2D light_texture; // texunit:-4
  191. varying vec4 light_uv_interp;
  192. varying vec2 transformed_light_uv;
  193. varying vec4 local_rot;
  194. #ifdef USE_SHADOWS
  195. uniform highp sampler2D shadow_texture; // texunit:-5
  196. varying highp vec2 pos;
  197. #endif
  198. const bool at_light_pass = true;
  199. #else
  200. const bool at_light_pass = false;
  201. #endif
  202. uniform bool use_default_normal;
  203. /* clang-format off */
  204. FRAGMENT_SHADER_GLOBALS
  205. /* clang-format on */
  206. void main() {
  207. vec4 color = color_interp;
  208. #if !defined(COLOR_USED)
  209. //default behavior, texture by color
  210. color *= texture2D(color_texture, uv_interp);
  211. #endif
  212. #ifdef SCREEN_UV_USED
  213. vec2 screen_uv = gl_FragCoord.xy * screen_pixel_size;
  214. #endif
  215. vec3 normal;
  216. #if defined(NORMAL_USED)
  217. bool normal_used = true;
  218. #else
  219. bool normal_used = false;
  220. #endif
  221. if (use_default_normal) {
  222. normal.xy = texture2D(normal_texture, uv_interp).xy * 2.0 - 1.0;
  223. normal.z = sqrt(1.0 - dot(normal.xy, normal.xy));
  224. normal_used = true;
  225. } else {
  226. normal = vec3(0.0, 0.0, 1.0);
  227. }
  228. {
  229. float normal_depth = 1.0;
  230. #if defined(NORMALMAP_USED)
  231. vec3 normal_map = vec3(0.0, 0.0, 1.0);
  232. normal_used = true;
  233. #endif
  234. /* clang-format off */
  235. FRAGMENT_SHADER_CODE
  236. /* clang-format on */
  237. #if defined(NORMALMAP_USED)
  238. 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);
  239. #endif
  240. }
  241. color *= final_modulate;
  242. #ifdef USE_LIGHTING
  243. vec2 light_vec = transformed_light_uv;
  244. if (normal_used) {
  245. normal.xy = mat2(local_rot.xy, local_rot.zw) * normal.xy;
  246. }
  247. float att = 1.0;
  248. vec2 light_uv = light_uv_interp.xy;
  249. vec4 light = texture2D(light_texture, light_uv);
  250. if (any(lessThan(light_uv_interp.xy, vec2(0.0, 0.0))) || any(greaterThanEqual(light_uv_interp.xy, vec2(1.0, 1.0)))) {
  251. color.a *= light_outside_alpha; //invisible
  252. } else {
  253. float real_light_height = light_height;
  254. vec4 real_light_color = light_color;
  255. vec4 real_light_shadow_color = light_shadow_color;
  256. #if defined(USE_LIGHT_SHADER_CODE)
  257. //light is written by the light shader
  258. light_compute(
  259. light,
  260. light_vec,
  261. real_light_height,
  262. real_light_color,
  263. light_uv,
  264. real_light_shadow_color,
  265. normal,
  266. uv,
  267. #if defined(SCREEN_UV_USED)
  268. screen_uv,
  269. #endif
  270. color);
  271. #endif
  272. light *= real_light_color;
  273. if (normal_used) {
  274. vec3 light_normal = normalize(vec3(light_vec, -real_light_height));
  275. light *= max(dot(-light_normal, normal), 0.0);
  276. }
  277. color *= light;
  278. #ifdef USE_SHADOWS
  279. light_vec = light_uv_interp.zw; //for shadows
  280. float angle_to_light = -atan(light_vec.x, light_vec.y);
  281. float PI = 3.14159265358979323846264;
  282. /*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
  283. float ang*/
  284. float su, sz;
  285. float abs_angle = abs(angle_to_light);
  286. vec2 point;
  287. float sh;
  288. if (abs_angle < 45.0 * PI / 180.0) {
  289. point = light_vec;
  290. sh = 0.0 + (1.0 / 8.0);
  291. } else if (abs_angle > 135.0 * PI / 180.0) {
  292. point = -light_vec;
  293. sh = 0.5 + (1.0 / 8.0);
  294. } else if (angle_to_light > 0.0) {
  295. point = vec2(light_vec.y, -light_vec.x);
  296. sh = 0.25 + (1.0 / 8.0);
  297. } else {
  298. point = vec2(-light_vec.y, light_vec.x);
  299. sh = 0.75 + (1.0 / 8.0);
  300. }
  301. highp vec4 s = shadow_matrix * vec4(point, 0.0, 1.0);
  302. s.xyz /= s.w;
  303. su = s.x * 0.5 + 0.5;
  304. sz = s.z * 0.5 + 0.5;
  305. //sz=lightlength(light_vec);
  306. highp float shadow_attenuation = 0.0;
  307. #ifdef USE_RGBA_SHADOWS
  308. #define SHADOW_DEPTH(m_tex, m_uv) dot(texture2D((m_tex), (m_uv)), vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1))
  309. #else
  310. #define SHADOW_DEPTH(m_tex, m_uv) (texture2D((m_tex), (m_uv)).r)
  311. #endif
  312. #ifdef SHADOW_USE_GRADIENT
  313. #define SHADOW_TEST(m_ofs) \
  314. { \
  315. highp float sd = SHADOW_DEPTH(shadow_texture, vec2(m_ofs, sh)); \
  316. shadow_attenuation += 1.0 - smoothstep(sd, sd + shadow_gradient, sz); \
  317. }
  318. #else
  319. #define SHADOW_TEST(m_ofs) \
  320. { \
  321. highp float sd = SHADOW_DEPTH(shadow_texture, vec2(m_ofs, sh)); \
  322. shadow_attenuation += step(sz, sd); \
  323. }
  324. #endif
  325. #ifdef SHADOW_FILTER_NEAREST
  326. SHADOW_TEST(su);
  327. #endif
  328. #ifdef SHADOW_FILTER_PCF3
  329. SHADOW_TEST(su + shadowpixel_size);
  330. SHADOW_TEST(su);
  331. SHADOW_TEST(su - shadowpixel_size);
  332. shadow_attenuation /= 3.0;
  333. #endif
  334. #ifdef SHADOW_FILTER_PCF5
  335. SHADOW_TEST(su + shadowpixel_size * 2.0);
  336. SHADOW_TEST(su + shadowpixel_size);
  337. SHADOW_TEST(su);
  338. SHADOW_TEST(su - shadowpixel_size);
  339. SHADOW_TEST(su - shadowpixel_size * 2.0);
  340. shadow_attenuation /= 5.0;
  341. #endif
  342. #ifdef SHADOW_FILTER_PCF7
  343. SHADOW_TEST(su + shadowpixel_size * 3.0);
  344. SHADOW_TEST(su + shadowpixel_size * 2.0);
  345. SHADOW_TEST(su + shadowpixel_size);
  346. SHADOW_TEST(su);
  347. SHADOW_TEST(su - shadowpixel_size);
  348. SHADOW_TEST(su - shadowpixel_size * 2.0);
  349. SHADOW_TEST(su - shadowpixel_size * 3.0);
  350. shadow_attenuation /= 7.0;
  351. #endif
  352. #ifdef SHADOW_FILTER_PCF9
  353. SHADOW_TEST(su + shadowpixel_size * 4.0);
  354. SHADOW_TEST(su + shadowpixel_size * 3.0);
  355. SHADOW_TEST(su + shadowpixel_size * 2.0);
  356. SHADOW_TEST(su + shadowpixel_size);
  357. SHADOW_TEST(su);
  358. SHADOW_TEST(su - shadowpixel_size);
  359. SHADOW_TEST(su - shadowpixel_size * 2.0);
  360. SHADOW_TEST(su - shadowpixel_size * 3.0);
  361. SHADOW_TEST(su - shadowpixel_size * 4.0);
  362. shadow_attenuation /= 9.0;
  363. #endif
  364. #ifdef SHADOW_FILTER_PCF13
  365. SHADOW_TEST(su + shadowpixel_size * 6.0);
  366. SHADOW_TEST(su + shadowpixel_size * 5.0);
  367. SHADOW_TEST(su + shadowpixel_size * 4.0);
  368. SHADOW_TEST(su + shadowpixel_size * 3.0);
  369. SHADOW_TEST(su + shadowpixel_size * 2.0);
  370. SHADOW_TEST(su + shadowpixel_size);
  371. SHADOW_TEST(su);
  372. SHADOW_TEST(su - shadowpixel_size);
  373. SHADOW_TEST(su - shadowpixel_size * 2.0);
  374. SHADOW_TEST(su - shadowpixel_size * 3.0);
  375. SHADOW_TEST(su - shadowpixel_size * 4.0);
  376. SHADOW_TEST(su - shadowpixel_size * 5.0);
  377. SHADOW_TEST(su - shadowpixel_size * 6.0);
  378. shadow_attenuation /= 13.0;
  379. #endif
  380. //color *= shadow_attenuation;
  381. color = mix(real_light_shadow_color, color, shadow_attenuation);
  382. //use shadows
  383. #endif
  384. }
  385. //use lighting
  386. #endif
  387. gl_FragColor = color;
  388. }