default.shader 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. include = [
  2. "core/shaders/common.shader"
  3. "core/shaders/lighting.shader"
  4. ]
  5. render_states = {
  6. debug_line = {
  7. inherit = "default"
  8. states = {
  9. blend_enable = true
  10. blend_src = "src_alpha"
  11. blend_dst = "inv_src_alpha"
  12. primitive_type = "pt_lines"
  13. "!defined(DEPTH_ENABLED)" = {
  14. depth_enable = false
  15. }
  16. }
  17. }
  18. gui = {
  19. inherit = "opacity"
  20. }
  21. blit = {
  22. inherit = "opacity"
  23. states = {
  24. "!defined(BLEND_ENABLED)" = {
  25. blend_enable = false;
  26. }
  27. }
  28. }
  29. sprite = {
  30. inherit = "default"
  31. states = {
  32. depth_func = "always"
  33. blend_enable = true
  34. blend_src = "src_alpha"
  35. blend_dst = "inv_src_alpha"
  36. blend_equation = "add"
  37. }
  38. }
  39. mesh = {
  40. inherit = "default"
  41. }
  42. noop = {
  43. inherit = "default"
  44. states = {
  45. rgb_write_enable = false
  46. alpha_write_enable = false
  47. depth_write_enable = false
  48. depth_enable = false
  49. blend_enable = false
  50. }
  51. }
  52. }
  53. bgfx_shaders = {
  54. debug_line = {
  55. includes = [ "common" ]
  56. varying = """
  57. vec4 v_color0 : COLOR0 = vec4(0.0, 0.0, 0.0, 0.0);
  58. vec3 a_position : POSITION;
  59. vec4 a_color0 : COLOR0;
  60. """
  61. vs_input_output = """
  62. $input a_position, a_color0
  63. $output v_color0
  64. """
  65. vs_code = """
  66. void main()
  67. {
  68. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  69. v_color0 = a_color0;
  70. }
  71. """
  72. fs_input_output = """
  73. $input v_color0
  74. """
  75. fs_code = """
  76. void main()
  77. {
  78. gl_FragColor = v_color0;
  79. }
  80. """
  81. }
  82. gui = {
  83. includes = [ "common" ]
  84. samplers = {
  85. u_albedo_map = { sampler_state = "clamp_anisotropic" }
  86. }
  87. varying = """
  88. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  89. vec4 v_color0 : COLOR0 = vec4(0.0, 0.0, 0.0, 0.0);
  90. vec3 a_position : POSITION;
  91. vec2 a_texcoord0 : TEXCOORD0;
  92. vec4 a_color0 : COLOR0;
  93. """
  94. vs_input_output = """
  95. $input a_position, a_texcoord0, a_color0
  96. $output v_texcoord0, v_color0
  97. """
  98. vs_code = """
  99. void main()
  100. {
  101. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  102. #if defined(DIFFUSE_MAP)
  103. v_texcoord0 = a_texcoord0;
  104. #endif // DIFFUSE_MAP
  105. v_color0 = a_color0;
  106. }
  107. """
  108. fs_input_output = """
  109. $input v_texcoord0, v_color0
  110. """
  111. fs_code = """
  112. #if defined(DIFFUSE_MAP)
  113. SAMPLER2D(u_albedo_map, 0);
  114. #endif // DIFFUSE_MAP
  115. void main()
  116. {
  117. #if defined(DIFFUSE_MAP)
  118. gl_FragColor = toGammaAccurate(texture2D(u_albedo_map, v_texcoord0) * toLinearAccurate(v_color0));
  119. #else
  120. gl_FragColor = v_color0;
  121. #endif // DIFFUSE_MAP
  122. }
  123. """
  124. }
  125. sprite = {
  126. includes = [ "common" ]
  127. samplers = {
  128. u_albedo_map = { sampler_state = "clamp_point" }
  129. }
  130. varying = """
  131. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  132. vec3 a_position : POSITION;
  133. vec2 a_texcoord0 : TEXCOORD0;
  134. """
  135. vs_input_output = """
  136. $input a_position, a_texcoord0
  137. $output v_texcoord0
  138. """
  139. vs_code = """
  140. void main()
  141. {
  142. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  143. v_texcoord0 = a_texcoord0;
  144. }
  145. """
  146. fs_input_output = """
  147. $input v_texcoord0
  148. """
  149. fs_code = """
  150. SAMPLER2D(u_albedo_map, 0);
  151. uniform vec4 u_color;
  152. void main()
  153. {
  154. gl_FragColor = toGammaAccurate(texture2D(u_albedo_map, v_texcoord0) * toLinearAccurate(u_color));
  155. }
  156. """
  157. }
  158. mesh = {
  159. includes = [ "common" "lighting" ]
  160. samplers = {
  161. u_albedo_map = { sampler_state = "mirror_anisotropic" }
  162. u_normal_map = { sampler_state = "mirror_anisotropic" }
  163. u_metallic_map = { sampler_state = "mirror_anisotropic" }
  164. u_roughness_map = { sampler_state = "mirror_anisotropic" }
  165. u_ao_map = { sampler_state = "mirror_anisotropic" }
  166. }
  167. varying = """
  168. vec3 v_normal : NORMAL = vec3(0.0, 0.0, 0.0);
  169. vec3 v_tangent : TANGENT = vec3(0.0, 0.0, 0.0);
  170. vec3 v_bitangent : BITANGENT = vec3(0.0, 0.0, 0.0);
  171. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  172. vec3 v_position : TEXCOORD1 = vec3(0.0, 0.0, 0.0);
  173. vec3 v_camera : TEXCOORD2 = vec3(0.0, 0.0, 0.0);
  174. vec4 v_shadow0 : TEXCOORD3 = vec4(0.0, 0.0, 0.0, 0.0);
  175. vec4 v_shadow1 : TEXCOORD4 = vec4(0.0, 0.0, 0.0, 0.0);
  176. vec4 v_shadow2 : TEXCOORD5 = vec4(0.0, 0.0, 0.0, 0.0);
  177. vec4 v_shadow3 : TEXCOORD6 = vec4(0.0, 0.0, 0.0, 0.0);
  178. vec3 a_position : POSITION;
  179. vec3 a_normal : NORMAL;
  180. vec3 a_tangent : TANGENT;
  181. vec3 a_bitangent : BITANGENT;
  182. vec4 a_indices : BLENDINDICES;
  183. vec4 a_weight : BLENDWEIGHT;
  184. vec2 a_texcoord0 : TEXCOORD0;
  185. """
  186. vs_input_output = """
  187. #if defined(SKINNING)
  188. $input a_position, a_normal, a_tangent, a_bitangent, a_texcoord0, a_indices, a_weight
  189. #else
  190. $input a_position, a_normal, a_tangent, a_bitangent, a_texcoord0
  191. #endif
  192. $output v_normal, v_tangent, v_bitangent, v_texcoord0, v_position, v_camera, v_shadow0, v_shadow1, v_shadow2, v_shadow3
  193. """
  194. vs_code = """
  195. uniform vec4 u_use_normal_map;
  196. void main()
  197. {
  198. #if defined(SKINNING)
  199. mat4 model;
  200. model = a_weight.x * u_model[int(a_indices.x)];
  201. model += a_weight.y * u_model[int(a_indices.y)];
  202. model += a_weight.z * u_model[int(a_indices.z)];
  203. model += a_weight.w * u_model[int(a_indices.w)];
  204. gl_Position = mul(mul(u_modelViewProj, model), vec4(a_position, 1.0));
  205. model = mul(u_model[0], model);
  206. #else
  207. gl_Position = mul(mul(u_viewProj, u_model[0]), vec4(a_position, 1.0));
  208. mat4 model = u_model[0];
  209. #endif
  210. v_position = mul(model, vec4(a_position, 1.0)).xyz;
  211. v_normal = normalize(mul(model, vec4(a_normal, 0.0))).xyz;
  212. v_tangent = normalize(mul(model, vec4(a_tangent, 0.0))).xyz;
  213. v_bitangent = normalize(mul(model, vec4(a_bitangent, 0.0))).xyz;
  214. mat3 tbn;
  215. if (u_use_normal_map.r == 1.0)
  216. tbn = mtxFromCols(v_tangent, v_bitangent, v_normal);
  217. else
  218. tbn = mtxFromCols(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
  219. v_camera = mul(u_invView, vec4(0.0, 0.0, 0.0, 1.0)).xyz;
  220. v_camera = mul(v_camera - v_position, tbn);
  221. v_position = mul(v_position, tbn);
  222. v_texcoord0 = a_texcoord0;
  223. #if !defined(NO_LIGHT)
  224. vec3 pos_offset = a_position + a_normal * 0.01;
  225. v_shadow0 = mul(mul(u_cascaded_lights[0], model), vec4(pos_offset, 1.0));
  226. v_shadow1 = mul(mul(u_cascaded_lights[1], model), vec4(pos_offset, 1.0));
  227. v_shadow2 = mul(mul(u_cascaded_lights[2], model), vec4(pos_offset, 1.0));
  228. v_shadow3 = mul(mul(u_cascaded_lights[3], model), vec4(pos_offset, 1.0));
  229. #endif
  230. }
  231. """
  232. fs_input_output = """
  233. $input v_normal, v_tangent, v_bitangent, v_texcoord0, v_position, v_camera, v_shadow0, v_shadow1, v_shadow2, v_shadow3
  234. """
  235. code = """
  236. """
  237. fs_code = """
  238. SAMPLER2D(u_albedo_map, 0);
  239. SAMPLER2D(u_normal_map, 1);
  240. SAMPLER2D(u_metallic_map, 2);
  241. SAMPLER2D(u_roughness_map, 3);
  242. SAMPLER2D(u_ao_map, 4);
  243. uniform vec4 u_albedo;
  244. uniform vec4 u_metallic;
  245. uniform vec4 u_roughness;
  246. uniform vec4 u_use_albedo_map;
  247. uniform vec4 u_use_normal_map;
  248. uniform vec4 u_use_metallic_map;
  249. uniform vec4 u_use_roughness_map;
  250. uniform vec4 u_use_ao_map;
  251. void main()
  252. {
  253. vec3 albedo = u_use_albedo_map.r == 1.0 ? texture2D(u_albedo_map, v_texcoord0).rgb : u_albedo.rgb;
  254. #if defined(NO_LIGHT)
  255. vec3 radiance = albedo;
  256. #else
  257. vec3 normal = u_use_normal_map.r == 1.0 ? decodeNormalUint(texture2D(u_normal_map, v_texcoord0).rgb) : v_normal;
  258. float metallic = u_use_metallic_map.r == 1.0 ? texture2D(u_metallic_map, v_texcoord0).r : u_metallic.r;
  259. float roughness = u_use_roughness_map.r == 1.0 ? texture2D(u_roughness_map, v_texcoord0).r: u_roughness.r;
  260. float ao = u_use_ao_map.r == 1.0 ? texture2D(u_ao_map, v_texcoord0).r : 0.0;
  261. mat3 tbn;
  262. if (u_use_normal_map.r == 1.0)
  263. tbn = mtxFromCols(v_tangent, v_bitangent, v_normal);
  264. else
  265. tbn = mtxFromCols(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
  266. vec3 n = normalize(normal); // Fragment normal.
  267. vec3 v = normalize(v_camera); // Versor from fragment to camera pos.
  268. vec3 f0 = mix(vec3_splat(0.04), albedo, metallic);
  269. vec3 radiance = calc_lighting(tbn, n, v, v_position, v_shadow0, v_shadow1, v_shadow2, v_shadow3, albedo, metallic, roughness, f0);
  270. radiance = radiance / (radiance + vec3_splat(1.0)); // Tone-mapping.
  271. #endif // !defined(NO_LIGHT)
  272. gl_FragColor = vec4(toGammaAccurate(radiance), 1.0);
  273. }
  274. """
  275. }
  276. blit = {
  277. includes = [ "common" ]
  278. varying = """
  279. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  280. vec3 a_position : POSITION;
  281. vec2 a_texcoord0 : TEXCOORD0;
  282. """
  283. vs_input_output = """
  284. $input a_position, a_texcoord0
  285. $output v_texcoord0
  286. """
  287. vs_code = """
  288. void main()
  289. {
  290. gl_Position = mul(u_viewProj, vec4(a_position.xy, 0.0, 1.0) );
  291. v_texcoord0 = a_texcoord0;
  292. }
  293. """
  294. fs_input_output = """
  295. $input v_texcoord0
  296. """
  297. fs_code = """
  298. SAMPLER2D(s_color, 0);
  299. void main()
  300. {
  301. gl_FragColor = texture2D(s_color, v_texcoord0);
  302. }
  303. """
  304. }
  305. fallback = {
  306. includes = [ "common" ]
  307. varying = """
  308. vec3 a_position : POSITION;
  309. """
  310. vs_input_output = """
  311. $input a_position
  312. """
  313. vs_code = """
  314. void main()
  315. {
  316. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  317. }
  318. """
  319. fs_input_output = """
  320. """
  321. fs_code = """
  322. void main()
  323. {
  324. gl_FragColor = toGammaAccurate(vec4(1.0, 0.0, 1.0, 1.0));
  325. }
  326. """
  327. }
  328. noop = {
  329. includes = [ "common" ]
  330. varying = """
  331. """
  332. vs_input_output = """
  333. """
  334. vs_code = """
  335. void main()
  336. {
  337. gl_Position = vec4_splat(0.0);
  338. }
  339. """
  340. fs_input_output = """
  341. """
  342. fs_code = """
  343. void main()
  344. {
  345. discard;
  346. }
  347. """
  348. }
  349. }
  350. shaders = {
  351. debug_line = {
  352. bgfx_shader = "debug_line"
  353. render_state = "debug_line"
  354. }
  355. gui = {
  356. bgfx_shader = "gui"
  357. render_state = "gui"
  358. }
  359. sprite = {
  360. bgfx_shader = "sprite"
  361. render_state = "sprite"
  362. }
  363. mesh = {
  364. bgfx_shader = "mesh"
  365. render_state = "mesh"
  366. }
  367. blit = {
  368. bgfx_shader = "blit"
  369. render_state = "blit"
  370. }
  371. fallback = {
  372. bgfx_shader = "fallback"
  373. render_state = "mesh"
  374. }
  375. noop = {
  376. bgfx_shader = "noop"
  377. render_state = "noop"
  378. }
  379. }
  380. static_compile = [
  381. { shader = "debug_line" defines = [] }
  382. { shader = "debug_line" defines = ["DEPTH_ENABLED"] }
  383. { shader = "gui" defines = [] }
  384. { shader = "gui" defines = ["DIFFUSE_MAP"]}
  385. { shader = "gui" defines = ["DEPTH_ENABLED"]}
  386. { shader = "gui" defines = ["DIFFUSE_MAP" "DEPTH_ENABLED"]}
  387. { shader = "sprite" defines = [] }
  388. { shader = "mesh" defines = [] }
  389. { shader = "mesh" defines = ["DIFFUSE_MAP"] }
  390. { shader = "mesh" defines = ["SKINNING"] }
  391. { shader = "mesh" defines = ["NO_LIGHT"] }
  392. { shader = "mesh" defines = ["DIFFUSE_MAP" "SKINNING"] }
  393. { shader = "mesh" defines = ["DIFFUSE_MAP" "NO_LIGHT"] }
  394. { shader = "blit" defines = [] }
  395. { shader = "blit" defines = ["BLEND_ENABLED"] }
  396. { shader = "fallback" defines = [] }
  397. { shader = "noop" defines = [] }
  398. ]