default.shader 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. skydome = {
  43. inherit = "default"
  44. }
  45. noop = {
  46. inherit = "default"
  47. states = {
  48. rgb_write_enable = false
  49. alpha_write_enable = false
  50. depth_write_enable = false
  51. depth_enable = false
  52. blend_enable = false
  53. }
  54. }
  55. }
  56. bgfx_shaders = {
  57. debug_line = {
  58. includes = [ "common" ]
  59. varying = """
  60. vec4 v_color0 : COLOR0 = vec4(0.0, 0.0, 0.0, 0.0);
  61. vec3 a_position : POSITION;
  62. vec4 a_color0 : COLOR0;
  63. """
  64. vs_input_output = """
  65. $input a_position, a_color0
  66. $output v_color0
  67. """
  68. vs_code = """
  69. void main()
  70. {
  71. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  72. v_color0 = a_color0;
  73. }
  74. """
  75. fs_input_output = """
  76. $input v_color0
  77. """
  78. fs_code = """
  79. void main()
  80. {
  81. gl_FragColor = v_color0;
  82. }
  83. """
  84. }
  85. gui = {
  86. includes = [ "common" ]
  87. samplers = {
  88. u_albedo_map = { sampler_state = "clamp_anisotropic" }
  89. }
  90. varying = """
  91. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  92. vec4 v_color0 : COLOR0 = vec4(0.0, 0.0, 0.0, 0.0);
  93. vec3 a_position : POSITION;
  94. vec2 a_texcoord0 : TEXCOORD0;
  95. vec4 a_color0 : COLOR0;
  96. """
  97. vs_input_output = """
  98. $input a_position, a_texcoord0, a_color0
  99. $output v_texcoord0, v_color0
  100. """
  101. vs_code = """
  102. void main()
  103. {
  104. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  105. #if defined(DIFFUSE_MAP)
  106. v_texcoord0 = a_texcoord0;
  107. #endif // DIFFUSE_MAP
  108. v_color0 = a_color0;
  109. }
  110. """
  111. fs_input_output = """
  112. $input v_texcoord0, v_color0
  113. """
  114. fs_code = """
  115. #if defined(DIFFUSE_MAP)
  116. SAMPLER2D(u_albedo_map, 0);
  117. #endif // DIFFUSE_MAP
  118. void main()
  119. {
  120. #if defined(DIFFUSE_MAP)
  121. gl_FragColor = toGammaAccurate(texture2D(u_albedo_map, v_texcoord0) * toLinearAccurate(v_color0));
  122. #else
  123. gl_FragColor = v_color0;
  124. #endif // DIFFUSE_MAP
  125. }
  126. """
  127. }
  128. sprite = {
  129. includes = [ "common" ]
  130. samplers = {
  131. u_albedo_map = { sampler_state = "clamp_point" }
  132. }
  133. varying = """
  134. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  135. vec3 a_position : POSITION;
  136. vec2 a_texcoord0 : TEXCOORD0;
  137. """
  138. vs_input_output = """
  139. $input a_position, a_texcoord0
  140. $output v_texcoord0
  141. """
  142. vs_code = """
  143. void main()
  144. {
  145. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  146. v_texcoord0 = a_texcoord0;
  147. }
  148. """
  149. fs_input_output = """
  150. $input v_texcoord0
  151. """
  152. fs_code = """
  153. SAMPLER2D(u_albedo_map, 0);
  154. uniform vec4 u_color;
  155. void main()
  156. {
  157. gl_FragColor = toGammaAccurate(texture2D(u_albedo_map, v_texcoord0) * toLinearAccurate(u_color));
  158. }
  159. """
  160. }
  161. mesh = {
  162. includes = [ "common" "lighting" ]
  163. samplers = {
  164. u_albedo_map = { sampler_state = "mirror_anisotropic" }
  165. u_normal_map = { sampler_state = "mirror_anisotropic" }
  166. u_metallic_map = { sampler_state = "mirror_anisotropic" }
  167. u_roughness_map = { sampler_state = "mirror_anisotropic" }
  168. u_ao_map = { sampler_state = "mirror_anisotropic" }
  169. u_emission_map = { sampler_state = "mirror_anisotropic" }
  170. }
  171. varying = """
  172. vec3 v_normal : NORMAL = vec3(0.0, 0.0, 0.0);
  173. vec3 v_tangent : TANGENT = vec3(0.0, 0.0, 0.0);
  174. vec3 v_bitangent : BITANGENT = vec3(0.0, 0.0, 0.0);
  175. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  176. vec3 v_position : TEXCOORD1 = vec3(0.0, 0.0, 0.0);
  177. vec3 v_camera : TEXCOORD2 = vec3(0.0, 0.0, 0.0);
  178. vec4 v_shadow0 : TEXCOORD3 = vec4(0.0, 0.0, 0.0, 0.0);
  179. vec4 v_shadow1 : TEXCOORD4 = vec4(0.0, 0.0, 0.0, 0.0);
  180. vec4 v_shadow2 : TEXCOORD5 = vec4(0.0, 0.0, 0.0, 0.0);
  181. vec4 v_shadow3 : TEXCOORD6 = vec4(0.0, 0.0, 0.0, 0.0);
  182. vec4 v_shadow_local : TEXCOORD7 = vec4(0.0, 0.0, 0.0, 0.0);
  183. vec3 v_camera_pos : TEXCOORD8 = vec3(0.0, 0.0, 0.0);
  184. vec3 a_position : POSITION;
  185. vec3 a_normal : NORMAL;
  186. vec3 a_tangent : TANGENT;
  187. vec3 a_bitangent : BITANGENT;
  188. vec4 a_indices : BLENDINDICES;
  189. vec4 a_weight : BLENDWEIGHT;
  190. vec2 a_texcoord0 : TEXCOORD0;
  191. """
  192. vs_input_output = """
  193. #if defined(SKINNING)
  194. $input a_position, a_normal, a_tangent, a_bitangent, a_texcoord0, a_indices, a_weight
  195. #else
  196. $input a_position, a_normal, a_tangent, a_bitangent, a_texcoord0
  197. #endif
  198. $output v_normal, v_tangent, v_bitangent, v_texcoord0, v_position, v_camera, v_camera_pos, v_shadow0, v_shadow1, v_shadow2, v_shadow3, v_shadow_local
  199. """
  200. vs_code = """
  201. uniform vec4 u_use_normal_map;
  202. uniform vec4 u_uv_scale; // { val=[1 1] min=[0 0] }
  203. uniform vec4 u_uv_offset; // { val=[0 0] min=[0 0] }
  204. void main()
  205. {
  206. #if defined(SKINNING)
  207. mat4 model;
  208. model = a_weight.x * u_model[int(a_indices.x)];
  209. model += a_weight.y * u_model[int(a_indices.y)];
  210. model += a_weight.z * u_model[int(a_indices.z)];
  211. model += a_weight.w * u_model[int(a_indices.w)];
  212. gl_Position = mul(mul(u_modelViewProj, model), vec4(a_position, 1.0));
  213. model = mul(u_model[0], model);
  214. #else
  215. gl_Position = mul(mul(u_viewProj, u_model[0]), vec4(a_position, 1.0));
  216. mat4 model = u_model[0];
  217. #endif
  218. vec3 normal = decodeNormalUint(a_normal);
  219. vec3 tangent = decodeNormalUint(a_tangent);
  220. vec3 bitangent = decodeNormalUint(a_bitangent);
  221. mat3 normal_matrix = cofactor(model);
  222. v_position = mul(model, vec4(a_position, 1.0)).xyz;
  223. v_normal = normalize(mul(normal_matrix, normal)).xyz;
  224. v_tangent = normalize(mul(normal_matrix, tangent)).xyz;
  225. v_bitangent = normalize(mul(normal_matrix, bitangent)).xyz;
  226. mat3 tbn;
  227. if (u_use_normal_map.r == 1.0)
  228. tbn = mtxFromCols(v_tangent, v_bitangent, v_normal);
  229. else
  230. tbn = mtxFromCols(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
  231. v_camera_pos = mul(u_invView, vec4(0.0, 0.0, 0.0, 1.0)).xyz;
  232. v_camera = mul(v_camera_pos - v_position, tbn);
  233. v_position = mul(v_position, tbn);
  234. v_texcoord0 = (a_texcoord0 - vec2_splat(0.5))*u_uv_scale.xy + vec2_splat(0.5) + u_uv_offset.xy;
  235. #if !defined(NO_LIGHT)
  236. vec3 pos_offset = a_position + normal * 0.01;
  237. v_shadow0 = mul(mul(u_cascaded_lights[0], model), vec4(pos_offset, 1.0));
  238. v_shadow1 = mul(mul(u_cascaded_lights[1], model), vec4(pos_offset, 1.0));
  239. v_shadow2 = mul(mul(u_cascaded_lights[2], model), vec4(pos_offset, 1.0));
  240. v_shadow3 = mul(mul(u_cascaded_lights[3], model), vec4(pos_offset, 1.0));
  241. v_shadow_local = mul(model, vec4(pos_offset, 1.0));
  242. #endif
  243. }
  244. """
  245. fs_input_output = """
  246. $input v_normal, v_tangent, v_bitangent, v_texcoord0, v_position, v_camera, v_camera_pos, v_shadow0, v_shadow1, v_shadow2, v_shadow3, v_shadow_local
  247. """
  248. code = """
  249. """
  250. fs_code = """
  251. SAMPLER2D(u_albedo_map, 0);
  252. SAMPLER2D(u_normal_map, 1);
  253. SAMPLER2D(u_metallic_map, 2);
  254. SAMPLER2D(u_roughness_map, 3);
  255. SAMPLER2D(u_ao_map, 4);
  256. SAMPLER2D(u_emission_map, 5);
  257. uniform vec4 u_albedo;
  258. uniform vec4 u_metallic;
  259. uniform vec4 u_roughness;
  260. uniform vec4 u_emission_color;
  261. uniform vec4 u_emission_intensity;
  262. uniform vec4 u_use_albedo_map;
  263. uniform vec4 u_use_normal_map;
  264. uniform vec4 u_use_metallic_map;
  265. uniform vec4 u_use_roughness_map;
  266. uniform vec4 u_use_ao_map;
  267. uniform vec4 u_use_emission_map;
  268. void main()
  269. {
  270. vec3 albedo = u_use_albedo_map.r == 1.0 ? texture2D(u_albedo_map, v_texcoord0).rgb : u_albedo.rgb;
  271. #if defined(NO_LIGHT)
  272. vec3 radiance = albedo;
  273. #else
  274. vec3 normal;
  275. if (u_use_normal_map.r == 1.0) {
  276. normal.xy = texture2DBc5(u_normal_map, v_texcoord0) * 2.0 - 1.0;
  277. normal.z = sqrt(1.0 - dot(normal.xy, normal.xy));
  278. } else {
  279. normal = v_normal;
  280. }
  281. float metallic = u_use_metallic_map.r == 1.0 ? texture2D(u_metallic_map, v_texcoord0).r : u_metallic.r;
  282. float roughness = u_use_roughness_map.r == 1.0 ? texture2D(u_roughness_map, v_texcoord0).r: u_roughness.r;
  283. float ao = u_use_ao_map.r == 1.0 ? texture2D(u_ao_map, v_texcoord0).r : 1.0;
  284. vec3 emission = u_emission_intensity.r * (u_use_emission_map.r == 1.0 ? texture2D(u_emission_map, v_texcoord0).rgb : u_emission_color.rgb);
  285. mat3 tbn;
  286. if (u_use_normal_map.r == 1.0)
  287. tbn = mtxFromCols(v_tangent, v_bitangent, v_normal);
  288. else
  289. tbn = mtxFromCols(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
  290. vec3 n = normalize(normal); // Fragment normal.
  291. vec3 v = normalize(v_camera); // Versor from fragment to camera pos.
  292. vec3 f0 = mix(vec3_splat(0.04), albedo, metallic);
  293. vec3 radiance = calc_lighting(tbn, n, v, v_position, v_camera, v_camera_pos, v_shadow0, v_shadow1, v_shadow2, v_shadow3, v_shadow_local, albedo, metallic, roughness, ao, emission, f0);
  294. #endif // !defined(NO_LIGHT)
  295. gl_FragColor = vec4(radiance, 1.0);
  296. }
  297. """
  298. }
  299. skydome = {
  300. includes = [ "common" ]
  301. samplers = {
  302. u_skydome_map = { sampler_state = "clamp_anisotropic" }
  303. }
  304. varying = """
  305. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  306. vec3 a_position : POSITION;
  307. vec2 a_texcoord0 : TEXCOORD0;
  308. """
  309. vs_input_output = """
  310. $input a_position, a_texcoord0
  311. $output v_texcoord0
  312. """
  313. vs_code = """
  314. uniform mat4 u_persp; // Perspective proj.
  315. void main()
  316. {
  317. vec4 world_pos = mul(mul(u_persp, u_modelView), vec4(a_position, 1.0));
  318. world_pos.z = world_pos.w; // Project to far plane.
  319. gl_Position = world_pos;
  320. v_texcoord0 = a_texcoord0;
  321. }
  322. """
  323. fs_input_output = """
  324. $input v_texcoord0
  325. """
  326. fs_code = """
  327. SAMPLER2D(u_skydome_map, 0);
  328. uniform vec4 u_skydome_intensity;
  329. void main()
  330. {
  331. gl_FragColor = texture2D(u_skydome_map, v_texcoord0) * u_skydome_intensity.x;
  332. }
  333. """
  334. }
  335. blit = {
  336. includes = [ "common" ]
  337. varying = """
  338. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  339. vec3 a_position : POSITION;
  340. vec2 a_texcoord0 : TEXCOORD0;
  341. """
  342. vs_input_output = """
  343. $input a_position, a_texcoord0
  344. $output v_texcoord0
  345. """
  346. vs_code = """
  347. void main()
  348. {
  349. gl_Position = mul(u_viewProj, vec4(a_position.xy, 0.0, 1.0) );
  350. v_texcoord0 = a_texcoord0;
  351. }
  352. """
  353. fs_input_output = """
  354. $input v_texcoord0
  355. """
  356. fs_code = """
  357. SAMPLER2D(s_color_map, 0);
  358. void main()
  359. {
  360. gl_FragColor = texture2D(s_color_map, v_texcoord0);
  361. }
  362. """
  363. }
  364. fallback = {
  365. includes = [ "common" ]
  366. varying = """
  367. vec3 a_position : POSITION;
  368. """
  369. vs_input_output = """
  370. $input a_position
  371. """
  372. vs_code = """
  373. void main()
  374. {
  375. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  376. }
  377. """
  378. fs_input_output = """
  379. """
  380. fs_code = """
  381. void main()
  382. {
  383. gl_FragColor = toGammaAccurate(vec4(1.0, 0.0, 1.0, 1.0));
  384. }
  385. """
  386. }
  387. noop = {
  388. includes = [ "common" ]
  389. varying = """
  390. """
  391. vs_input_output = """
  392. """
  393. vs_code = """
  394. void main()
  395. {
  396. gl_Position = vec4_splat(0.0);
  397. }
  398. """
  399. fs_input_output = """
  400. """
  401. fs_code = """
  402. void main()
  403. {
  404. discard;
  405. }
  406. """
  407. }
  408. }
  409. shaders = {
  410. debug_line = {
  411. bgfx_shader = "debug_line"
  412. render_state = "debug_line"
  413. }
  414. gui = {
  415. bgfx_shader = "gui"
  416. render_state = "gui"
  417. }
  418. sprite = {
  419. bgfx_shader = "sprite"
  420. render_state = "sprite"
  421. }
  422. mesh = {
  423. bgfx_shader = "mesh"
  424. render_state = "mesh"
  425. }
  426. skydome = {
  427. bgfx_shader = "skydome"
  428. render_state = "skydome"
  429. }
  430. blit = {
  431. bgfx_shader = "blit"
  432. render_state = "blit"
  433. }
  434. fallback = {
  435. bgfx_shader = "fallback"
  436. render_state = "mesh"
  437. }
  438. noop = {
  439. bgfx_shader = "noop"
  440. render_state = "noop"
  441. }
  442. }
  443. static_compile = [
  444. { shader = "debug_line" defines = [] }
  445. { shader = "debug_line" defines = ["DEPTH_ENABLED"] }
  446. { shader = "gui" defines = [] }
  447. { shader = "gui" defines = ["DIFFUSE_MAP"]}
  448. { shader = "gui" defines = ["DEPTH_ENABLED"]}
  449. { shader = "gui" defines = ["DIFFUSE_MAP" "DEPTH_ENABLED"]}
  450. { shader = "sprite" defines = [] }
  451. { shader = "mesh" defines = [] }
  452. { shader = "mesh" defines = ["DIFFUSE_MAP"] }
  453. { shader = "mesh" defines = ["SKINNING"] }
  454. { shader = "mesh" defines = ["NO_LIGHT"] }
  455. { shader = "mesh" defines = ["DIFFUSE_MAP" "SKINNING"] }
  456. { shader = "mesh" defines = ["DIFFUSE_MAP" "NO_LIGHT"] }
  457. { shader = "skydome" defines = [] }
  458. { shader = "blit" defines = [] }
  459. { shader = "blit" defines = ["BLEND_ENABLED"] }
  460. { shader = "fallback" defines = [] }
  461. { shader = "noop" defines = [] }
  462. ]