canvas.glsl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 highp float;
  9. precision highp 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. vec2 uv;
  82. #ifdef USE_INSTANCING
  83. mat4 extra_matrix_instance = extra_matrix * transpose(mat4(instance_xform0, instance_xform1, instance_xform2, vec4(0.0, 0.0, 0.0, 1.0)));
  84. color *= instance_color;
  85. vec4 instance_custom = instance_custom_data;
  86. #else
  87. mat4 extra_matrix_instance = extra_matrix;
  88. vec4 instance_custom = vec4(0.0);
  89. #endif
  90. #ifdef USE_TEXTURE_RECT
  91. if (dst_rect.z < 0.0) { // Transpose is encoded as negative dst_rect.z
  92. uv = src_rect.xy + abs(src_rect.zw) * vertex.yx;
  93. } else {
  94. uv = src_rect.xy + abs(src_rect.zw) * vertex;
  95. }
  96. vec4 outvec = vec4(0.0, 0.0, 0.0, 1.0);
  97. // This is what is done in the GLES 3 bindings and should
  98. // take care of flipped rects.
  99. //
  100. // But it doesn't.
  101. // I don't know why, will need to investigate further.
  102. 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)));
  103. // outvec.xy = dst_rect.xy + abs(dst_rect.zw) * vertex;
  104. #else
  105. vec4 outvec = vec4(vertex.xy, 0.0, 1.0);
  106. uv = uv_attrib;
  107. #endif
  108. {
  109. vec2 src_vtx = outvec.xy;
  110. /* clang-format off */
  111. VERTEX_SHADER_CODE
  112. /* clang-format on */
  113. }
  114. #if !defined(SKIP_TRANSFORM_USED)
  115. outvec = extra_matrix_instance * outvec;
  116. outvec = modelview_matrix * outvec;
  117. #endif
  118. color_interp = color;
  119. #ifdef USE_PIXEL_SNAP
  120. outvec.xy = floor(outvec + 0.5).xy;
  121. // precision issue on some hardware creates artifacts within texture
  122. // offset uv by a small amount to avoid
  123. uv += 1e-5;
  124. #endif
  125. #ifdef USE_SKELETON
  126. // look up transform from the "pose texture"
  127. if (bone_weights != vec4(0.0)) {
  128. highp mat4 bone_transform = mat4(0.0);
  129. for (int i = 0; i < 4; i++) {
  130. ivec2 tex_ofs = ivec2(int(bone_indices[i]) * 2, 0);
  131. highp mat4 b = mat4(
  132. texel2DFetch(skeleton_texture, skeleton_texture_size, tex_ofs + ivec2(0, 0)),
  133. texel2DFetch(skeleton_texture, skeleton_texture_size, tex_ofs + ivec2(1, 0)),
  134. vec4(0.0, 0.0, 1.0, 0.0),
  135. vec4(0.0, 0.0, 0.0, 1.0));
  136. bone_transform += b * bone_weights[i];
  137. }
  138. mat4 bone_matrix = skeleton_transform * transpose(bone_transform) * skeleton_transform_inverse;
  139. outvec = bone_matrix * outvec;
  140. }
  141. #endif
  142. uv_interp = uv;
  143. gl_Position = projection_matrix * outvec;
  144. #ifdef USE_LIGHTING
  145. light_uv_interp.xy = (light_matrix * outvec).xy;
  146. light_uv_interp.zw = (light_local_matrix * outvec).xy;
  147. transformed_light_uv = (mat3(light_matrix_inverse) * vec3(light_uv_interp.zw, 0.0)).xy; //for normal mapping
  148. #ifdef USE_SHADOWS
  149. pos = outvec.xy;
  150. #endif
  151. local_rot.xy = normalize((modelview_matrix * (extra_matrix_instance * vec4(1.0, 0.0, 0.0, 0.0))).xy);
  152. local_rot.zw = normalize((modelview_matrix * (extra_matrix_instance * vec4(0.0, 1.0, 0.0, 0.0))).xy);
  153. #ifdef USE_TEXTURE_RECT
  154. local_rot.xy *= sign(src_rect.z);
  155. local_rot.zw *= sign(src_rect.w);
  156. #endif
  157. #endif
  158. }
  159. /* clang-format off */
  160. [fragment]
  161. #ifndef USE_GLES_OVER_GL
  162. #ifdef GL_EXT_shader_texture_lod
  163. #extension GL_EXT_shader_texture_lod : enable
  164. #define texture2DLod(img, coord, lod) texture2DLodEXT(img, coord, lod)
  165. #define textureCubeLod(img, coord, lod) textureCubeLodEXT(img, coord, lod)
  166. #endif
  167. #endif
  168. #ifdef GL_ARB_shader_texture_lod
  169. #extension GL_ARB_shader_texture_lod : enable
  170. #endif
  171. #if !defined(GL_EXT_shader_texture_lod) && !defined(GL_ARB_shader_texture_lod)
  172. #define texture2DLod(img, coord, lod) texture2D(img, coord, lod)
  173. #define textureCubeLod(img, coord, lod) textureCube(img, coord, lod)
  174. #endif
  175. #ifdef USE_GLES_OVER_GL
  176. #define lowp
  177. #define mediump
  178. #define highp
  179. #else
  180. #if defined(USE_HIGHP_PRECISION)
  181. precision highp float;
  182. precision highp int;
  183. #else
  184. precision mediump float;
  185. precision mediump int;
  186. #endif
  187. #endif
  188. uniform sampler2D color_texture; // texunit:-1
  189. /* clang-format on */
  190. uniform highp vec2 color_texpixel_size;
  191. uniform mediump sampler2D normal_texture; // texunit:-2
  192. varying mediump vec2 uv_interp;
  193. varying mediump vec4 color_interp;
  194. uniform highp float time;
  195. uniform vec4 final_modulate;
  196. #ifdef SCREEN_TEXTURE_USED
  197. uniform sampler2D screen_texture; // texunit:-4
  198. #endif
  199. #ifdef SCREEN_UV_USED
  200. uniform vec2 screen_pixel_size;
  201. #endif
  202. #ifdef USE_LIGHTING
  203. uniform highp mat4 light_matrix;
  204. uniform highp mat4 light_local_matrix;
  205. uniform highp mat4 shadow_matrix;
  206. uniform highp vec4 light_color;
  207. uniform highp vec4 light_shadow_color;
  208. uniform highp vec2 light_pos;
  209. uniform highp float shadowpixel_size;
  210. uniform highp float shadow_gradient;
  211. uniform highp float light_height;
  212. uniform highp float light_outside_alpha;
  213. uniform highp float shadow_distance_mult;
  214. uniform lowp sampler2D light_texture; // texunit:-4
  215. varying vec4 light_uv_interp;
  216. varying vec2 transformed_light_uv;
  217. varying vec4 local_rot;
  218. #ifdef USE_SHADOWS
  219. uniform highp sampler2D shadow_texture; // texunit:-5
  220. varying highp vec2 pos;
  221. #endif
  222. const bool at_light_pass = true;
  223. #else
  224. const bool at_light_pass = false;
  225. #endif
  226. uniform bool use_default_normal;
  227. /* clang-format off */
  228. FRAGMENT_SHADER_GLOBALS
  229. /* clang-format on */
  230. void light_compute(
  231. inout vec4 light,
  232. inout vec2 light_vec,
  233. inout float light_height,
  234. inout vec4 light_color,
  235. vec2 light_uv,
  236. inout vec4 shadow_color,
  237. vec3 normal,
  238. vec2 uv,
  239. #if defined(SCREEN_UV_USED)
  240. vec2 screen_uv,
  241. #endif
  242. vec4 color) {
  243. #if defined(USE_LIGHT_SHADER_CODE)
  244. /* clang-format off */
  245. LIGHT_SHADER_CODE
  246. /* clang-format on */
  247. #endif
  248. }
  249. void main() {
  250. vec4 color = color_interp;
  251. vec2 uv = uv_interp;
  252. #ifdef USE_FORCE_REPEAT
  253. //needs to use this to workaround GLES2/WebGL1 forcing tiling that textures that dont support it
  254. uv = mod(uv, vec2(1.0, 1.0));
  255. #endif
  256. #if !defined(COLOR_USED)
  257. //default behavior, texture by color
  258. color *= texture2D(color_texture, uv);
  259. #endif
  260. #ifdef SCREEN_UV_USED
  261. vec2 screen_uv = gl_FragCoord.xy * screen_pixel_size;
  262. #endif
  263. vec3 normal;
  264. #if defined(NORMAL_USED)
  265. bool normal_used = true;
  266. #else
  267. bool normal_used = false;
  268. #endif
  269. if (use_default_normal) {
  270. normal.xy = texture2D(normal_texture, uv).xy * 2.0 - 1.0;
  271. normal.z = sqrt(1.0 - dot(normal.xy, normal.xy));
  272. normal_used = true;
  273. } else {
  274. normal = vec3(0.0, 0.0, 1.0);
  275. }
  276. {
  277. float normal_depth = 1.0;
  278. #if defined(NORMALMAP_USED)
  279. vec3 normal_map = vec3(0.0, 0.0, 1.0);
  280. normal_used = true;
  281. #endif
  282. /* clang-format off */
  283. FRAGMENT_SHADER_CODE
  284. /* clang-format on */
  285. #if defined(NORMALMAP_USED)
  286. 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);
  287. #endif
  288. }
  289. color *= final_modulate;
  290. #ifdef USE_LIGHTING
  291. vec2 light_vec = transformed_light_uv;
  292. if (normal_used) {
  293. normal.xy = mat2(local_rot.xy, local_rot.zw) * normal.xy;
  294. }
  295. float att = 1.0;
  296. vec2 light_uv = light_uv_interp.xy;
  297. vec4 light = texture2D(light_texture, light_uv);
  298. if (any(lessThan(light_uv_interp.xy, vec2(0.0, 0.0))) || any(greaterThanEqual(light_uv_interp.xy, vec2(1.0, 1.0)))) {
  299. color.a *= light_outside_alpha; //invisible
  300. } else {
  301. float real_light_height = light_height;
  302. vec4 real_light_color = light_color;
  303. vec4 real_light_shadow_color = light_shadow_color;
  304. #if defined(USE_LIGHT_SHADER_CODE)
  305. //light is written by the light shader
  306. light_compute(
  307. light,
  308. light_vec,
  309. real_light_height,
  310. real_light_color,
  311. light_uv,
  312. real_light_shadow_color,
  313. normal,
  314. uv,
  315. #if defined(SCREEN_UV_USED)
  316. screen_uv,
  317. #endif
  318. color);
  319. #endif
  320. light *= real_light_color;
  321. if (normal_used) {
  322. vec3 light_normal = normalize(vec3(light_vec, -real_light_height));
  323. light *= max(dot(-light_normal, normal), 0.0);
  324. }
  325. color *= light;
  326. #ifdef USE_SHADOWS
  327. // Reset light_vec to compute shadows, the shadow map is created from the light origin, so it only
  328. // makes sense to compute shadows from there.
  329. light_vec = light_uv_interp.zw;
  330. float angle_to_light = -atan(light_vec.x, light_vec.y);
  331. float PI = 3.14159265358979323846264;
  332. /*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
  333. float ang*/
  334. float su, sz;
  335. float abs_angle = abs(angle_to_light);
  336. vec2 point;
  337. float sh;
  338. if (abs_angle < 45.0 * PI / 180.0) {
  339. point = light_vec;
  340. sh = 0.0 + (1.0 / 8.0);
  341. } else if (abs_angle > 135.0 * PI / 180.0) {
  342. point = -light_vec;
  343. sh = 0.5 + (1.0 / 8.0);
  344. } else if (angle_to_light > 0.0) {
  345. point = vec2(light_vec.y, -light_vec.x);
  346. sh = 0.25 + (1.0 / 8.0);
  347. } else {
  348. point = vec2(-light_vec.y, light_vec.x);
  349. sh = 0.75 + (1.0 / 8.0);
  350. }
  351. highp vec4 s = shadow_matrix * vec4(point, 0.0, 1.0);
  352. s.xyz /= s.w;
  353. su = s.x * 0.5 + 0.5;
  354. sz = s.z * 0.5 + 0.5;
  355. //sz=lightlength(light_vec);
  356. highp float shadow_attenuation = 0.0;
  357. #ifdef USE_RGBA_SHADOWS
  358. #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.0))
  359. #else
  360. #define SHADOW_DEPTH(m_tex, m_uv) (texture2D((m_tex), (m_uv)).r)
  361. #endif
  362. #ifdef SHADOW_USE_GRADIENT
  363. /* clang-format off */
  364. /* GLSL es 100 doesn't support line continuation characters(backslashes) */
  365. #define SHADOW_TEST(m_ofs) { highp float sd = SHADOW_DEPTH(shadow_texture, vec2(m_ofs, sh)); shadow_attenuation += 1.0 - smoothstep(sd, sd + shadow_gradient, sz); }
  366. #else
  367. #define SHADOW_TEST(m_ofs) { highp float sd = SHADOW_DEPTH(shadow_texture, vec2(m_ofs, sh)); shadow_attenuation += step(sz, sd); }
  368. /* clang-format on */
  369. #endif
  370. #ifdef SHADOW_FILTER_NEAREST
  371. SHADOW_TEST(su);
  372. #endif
  373. #ifdef SHADOW_FILTER_PCF3
  374. SHADOW_TEST(su + shadowpixel_size);
  375. SHADOW_TEST(su);
  376. SHADOW_TEST(su - shadowpixel_size);
  377. shadow_attenuation /= 3.0;
  378. #endif
  379. #ifdef SHADOW_FILTER_PCF5
  380. SHADOW_TEST(su + shadowpixel_size * 2.0);
  381. SHADOW_TEST(su + shadowpixel_size);
  382. SHADOW_TEST(su);
  383. SHADOW_TEST(su - shadowpixel_size);
  384. SHADOW_TEST(su - shadowpixel_size * 2.0);
  385. shadow_attenuation /= 5.0;
  386. #endif
  387. #ifdef SHADOW_FILTER_PCF7
  388. SHADOW_TEST(su + shadowpixel_size * 3.0);
  389. SHADOW_TEST(su + shadowpixel_size * 2.0);
  390. SHADOW_TEST(su + shadowpixel_size);
  391. SHADOW_TEST(su);
  392. SHADOW_TEST(su - shadowpixel_size);
  393. SHADOW_TEST(su - shadowpixel_size * 2.0);
  394. SHADOW_TEST(su - shadowpixel_size * 3.0);
  395. shadow_attenuation /= 7.0;
  396. #endif
  397. #ifdef SHADOW_FILTER_PCF9
  398. SHADOW_TEST(su + shadowpixel_size * 4.0);
  399. SHADOW_TEST(su + shadowpixel_size * 3.0);
  400. SHADOW_TEST(su + shadowpixel_size * 2.0);
  401. SHADOW_TEST(su + shadowpixel_size);
  402. SHADOW_TEST(su);
  403. SHADOW_TEST(su - shadowpixel_size);
  404. SHADOW_TEST(su - shadowpixel_size * 2.0);
  405. SHADOW_TEST(su - shadowpixel_size * 3.0);
  406. SHADOW_TEST(su - shadowpixel_size * 4.0);
  407. shadow_attenuation /= 9.0;
  408. #endif
  409. #ifdef SHADOW_FILTER_PCF13
  410. SHADOW_TEST(su + shadowpixel_size * 6.0);
  411. SHADOW_TEST(su + shadowpixel_size * 5.0);
  412. SHADOW_TEST(su + shadowpixel_size * 4.0);
  413. SHADOW_TEST(su + shadowpixel_size * 3.0);
  414. SHADOW_TEST(su + shadowpixel_size * 2.0);
  415. SHADOW_TEST(su + shadowpixel_size);
  416. SHADOW_TEST(su);
  417. SHADOW_TEST(su - shadowpixel_size);
  418. SHADOW_TEST(su - shadowpixel_size * 2.0);
  419. SHADOW_TEST(su - shadowpixel_size * 3.0);
  420. SHADOW_TEST(su - shadowpixel_size * 4.0);
  421. SHADOW_TEST(su - shadowpixel_size * 5.0);
  422. SHADOW_TEST(su - shadowpixel_size * 6.0);
  423. shadow_attenuation /= 13.0;
  424. #endif
  425. //color *= shadow_attenuation;
  426. color = mix(real_light_shadow_color, color, shadow_attenuation);
  427. //use shadows
  428. #endif
  429. }
  430. //use lighting
  431. #endif
  432. gl_FragColor = color;
  433. }