scene.glsl 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. /* clang-format off */
  2. #[modes]
  3. mode_color = #define BASE_PASS
  4. mode_color_instancing = #define BASE_PASS \n#define USE_INSTANCING
  5. mode_additive = #define USE_ADDITIVE_LIGHTING
  6. mode_additive_instancing = #define USE_ADDITIVE_LIGHTING \n#define USE_INSTANCING
  7. mode_depth = #define MODE_RENDER_DEPTH
  8. mode_depth_instancing = #define MODE_RENDER_DEPTH \n#define USE_INSTANCING
  9. #[specializations]
  10. DISABLE_LIGHTMAP = false
  11. DISABLE_LIGHT_DIRECTIONAL = false
  12. DISABLE_LIGHT_OMNI = false
  13. DISABLE_LIGHT_SPOT = false
  14. DISABLE_FOG = false
  15. USE_RADIANCE_MAP = true
  16. USE_MULTIVIEW = false
  17. #[vertex]
  18. #define M_PI 3.14159265359
  19. #define SHADER_IS_SRGB true
  20. #include "stdlib_inc.glsl"
  21. #if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED)
  22. #ifndef NORMAL_USED
  23. #define NORMAL_USED
  24. #endif
  25. #endif
  26. /*
  27. from RenderingServer:
  28. ARRAY_VERTEX = 0, // RG32F or RGB32F (depending on 2D bit)
  29. ARRAY_NORMAL = 1, // RG16 octahedral compression
  30. ARRAY_TANGENT = 2, // RG16 octahedral compression, sign stored in sign of G
  31. ARRAY_COLOR = 3, // RGBA8
  32. ARRAY_TEX_UV = 4, // RG32F
  33. ARRAY_TEX_UV2 = 5, // RG32F
  34. ARRAY_CUSTOM0 = 6, // Depends on ArrayCustomFormat.
  35. ARRAY_CUSTOM1 = 7,
  36. ARRAY_CUSTOM2 = 8,
  37. ARRAY_CUSTOM3 = 9,
  38. ARRAY_BONES = 10, // RGBA16UI (x2 if 8 weights)
  39. ARRAY_WEIGHTS = 11, // RGBA16UNORM (x2 if 8 weights)
  40. */
  41. /* INPUT ATTRIBS */
  42. layout(location = 0) in highp vec3 vertex_attrib;
  43. /* clang-format on */
  44. #ifdef NORMAL_USED
  45. layout(location = 1) in vec2 normal_attrib;
  46. #endif
  47. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  48. layout(location = 2) in vec2 tangent_attrib;
  49. #endif
  50. #if defined(COLOR_USED)
  51. layout(location = 3) in vec4 color_attrib;
  52. #endif
  53. #ifdef UV_USED
  54. layout(location = 4) in vec2 uv_attrib;
  55. #endif
  56. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  57. layout(location = 5) in vec2 uv2_attrib;
  58. #endif
  59. #if defined(CUSTOM0_USED)
  60. layout(location = 6) in vec4 custom0_attrib;
  61. #endif
  62. #if defined(CUSTOM1_USED)
  63. layout(location = 7) in vec4 custom1_attrib;
  64. #endif
  65. #if defined(CUSTOM2_USED)
  66. layout(location = 8) in vec4 custom2_attrib;
  67. #endif
  68. #if defined(CUSTOM3_USED)
  69. layout(location = 9) in vec4 custom3_attrib;
  70. #endif
  71. #if defined(BONES_USED)
  72. layout(location = 10) in uvec4 bone_attrib;
  73. #endif
  74. #if defined(WEIGHTS_USED)
  75. layout(location = 11) in vec4 weight_attrib;
  76. #endif
  77. vec3 oct_to_vec3(vec2 e) {
  78. vec3 v = vec3(e.xy, 1.0 - abs(e.x) - abs(e.y));
  79. float t = max(-v.z, 0.0);
  80. v.xy += t * -sign(v.xy);
  81. return normalize(v);
  82. }
  83. #ifdef USE_INSTANCING
  84. layout(location = 12) in highp vec4 instance_xform0;
  85. layout(location = 13) in highp vec4 instance_xform1;
  86. layout(location = 14) in highp vec4 instance_xform2;
  87. layout(location = 15) in highp uvec4 instance_color_custom_data; // Color packed into xy, Custom data into zw.
  88. #endif
  89. layout(std140) uniform GlobalShaderUniformData { //ubo:1
  90. vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS];
  91. };
  92. layout(std140) uniform SceneData { // ubo:2
  93. highp mat4 projection_matrix;
  94. highp mat4 inv_projection_matrix;
  95. highp mat4 inv_view_matrix;
  96. highp mat4 view_matrix;
  97. vec2 viewport_size;
  98. vec2 screen_pixel_size;
  99. mediump vec4 ambient_light_color_energy;
  100. mediump float ambient_color_sky_mix;
  101. bool material_uv2_mode;
  102. float emissive_exposure_normalization;
  103. bool use_ambient_light;
  104. bool use_ambient_cubemap;
  105. bool use_reflection_cubemap;
  106. float fog_aerial_perspective;
  107. float time;
  108. mat3 radiance_inverse_xform;
  109. uint directional_light_count;
  110. float z_far;
  111. float z_near;
  112. float IBL_exposure_normalization;
  113. bool fog_enabled;
  114. float fog_density;
  115. float fog_height;
  116. float fog_height_density;
  117. vec3 fog_light_color;
  118. float fog_sun_scatter;
  119. uint camera_visible_layers;
  120. uint pad3;
  121. uint pad4;
  122. uint pad5;
  123. }
  124. scene_data;
  125. #ifdef USE_MULTIVIEW
  126. layout(std140) uniform MultiviewData { // ubo:8
  127. highp mat4 projection_matrix_view[MAX_VIEWS];
  128. highp mat4 inv_projection_matrix_view[MAX_VIEWS];
  129. highp vec4 eye_offset[MAX_VIEWS];
  130. }
  131. multiview_data;
  132. #endif
  133. uniform highp mat4 world_transform;
  134. #ifdef USE_LIGHTMAP
  135. uniform highp vec4 lightmap_uv_rect;
  136. #endif
  137. /* Varyings */
  138. out highp vec3 vertex_interp;
  139. #ifdef NORMAL_USED
  140. out vec3 normal_interp;
  141. #endif
  142. #if defined(COLOR_USED)
  143. out vec4 color_interp;
  144. #endif
  145. #if defined(UV_USED)
  146. out vec2 uv_interp;
  147. #endif
  148. #if defined(UV2_USED)
  149. out vec2 uv2_interp;
  150. #else
  151. #ifdef USE_LIGHTMAP
  152. out vec2 uv2_interp;
  153. #endif
  154. #endif
  155. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  156. out vec3 tangent_interp;
  157. out vec3 binormal_interp;
  158. #endif
  159. #ifdef MATERIAL_UNIFORMS_USED
  160. /* clang-format off */
  161. layout(std140) uniform MaterialUniforms { // ubo:3
  162. #MATERIAL_UNIFORMS
  163. };
  164. /* clang-format on */
  165. #endif
  166. /* clang-format off */
  167. #GLOBALS
  168. /* clang-format on */
  169. invariant gl_Position;
  170. void main() {
  171. highp vec3 vertex = vertex_attrib;
  172. highp mat4 model_matrix = world_transform;
  173. #ifdef USE_INSTANCING
  174. highp mat4 m = mat4(instance_xform0, instance_xform1, instance_xform2, vec4(0.0, 0.0, 0.0, 1.0));
  175. model_matrix = model_matrix * transpose(m);
  176. #endif
  177. #ifdef NORMAL_USED
  178. vec3 normal = oct_to_vec3(normal_attrib * 2.0 - 1.0);
  179. #endif
  180. highp mat3 model_normal_matrix = mat3(model_matrix);
  181. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  182. vec2 signed_tangent_attrib = tangent_attrib * 2.0 - 1.0;
  183. vec3 tangent = oct_to_vec3(vec2(signed_tangent_attrib.x, abs(signed_tangent_attrib.y) * 2.0 - 1.0));
  184. float binormalf = sign(signed_tangent_attrib.y);
  185. vec3 binormal = normalize(cross(normal, tangent) * binormalf);
  186. #endif
  187. #if defined(COLOR_USED)
  188. color_interp = color_attrib;
  189. #ifdef USE_INSTANCING
  190. vec4 instance_color = vec4(unpackHalf2x16(instance_color_custom_data.x), unpackHalf2x16(instance_color_custom_data.y));
  191. color_interp *= instance_color;
  192. #endif
  193. #endif
  194. #if defined(UV_USED)
  195. uv_interp = uv_attrib;
  196. #endif
  197. #ifdef USE_LIGHTMAP
  198. uv2_interp = lightmap_uv_rect.zw * uv2_attrib + lightmap_uv_rect.xy;
  199. #else
  200. #if defined(UV2_USED)
  201. uv2_interp = uv2_attrib;
  202. #endif
  203. #endif
  204. #if defined(OVERRIDE_POSITION)
  205. highp vec4 position;
  206. #endif
  207. #ifdef USE_MULTIVIEW
  208. mat4 projection_matrix = multiview_data.projection_matrix_view[ViewIndex];
  209. mat4 inv_projection_matrix = multiview_data.inv_projection_matrix_view[ViewIndex];
  210. vec3 eye_offset = multiview_data.eye_offset[ViewIndex].xyz;
  211. #else
  212. mat4 projection_matrix = scene_data.projection_matrix;
  213. mat4 inv_projection_matrix = scene_data.inv_projection_matrix;
  214. vec3 eye_offset = vec3(0.0, 0.0, 0.0);
  215. #endif //USE_MULTIVIEW
  216. #ifdef USE_INSTANCING
  217. vec4 instance_custom = vec4(unpackHalf2x16(instance_color_custom_data.z), unpackHalf2x16(instance_color_custom_data.w));
  218. #else
  219. vec4 instance_custom = vec4(0.0);
  220. #endif
  221. // Using world coordinates
  222. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  223. vertex = (model_matrix * vec4(vertex, 1.0)).xyz;
  224. #ifdef NORMAL_USED
  225. normal = model_normal_matrix * normal;
  226. #endif
  227. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  228. tangent = model_normal_matrix * tangent;
  229. binormal = model_normal_matrix * binormal;
  230. #endif
  231. #endif
  232. float roughness = 1.0;
  233. highp mat4 modelview = scene_data.view_matrix * model_matrix;
  234. highp mat3 modelview_normal = mat3(scene_data.view_matrix) * model_normal_matrix;
  235. float point_size = 1.0;
  236. {
  237. #CODE : VERTEX
  238. }
  239. gl_PointSize = point_size;
  240. // Using local coordinates (default)
  241. #if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
  242. vertex = (modelview * vec4(vertex, 1.0)).xyz;
  243. #ifdef NORMAL_USED
  244. normal = modelview_normal * normal;
  245. #endif
  246. #endif
  247. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  248. binormal = modelview_normal * binormal;
  249. tangent = modelview_normal * tangent;
  250. #endif
  251. // Using world coordinates
  252. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  253. vertex = (scene_data.view_matrix * vec4(vertex, 1.0)).xyz;
  254. #ifdef NORMAL_USED
  255. normal = (scene_data.view_matrix * vec4(normal, 0.0)).xyz;
  256. #endif
  257. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  258. binormal = (scene_data.view_matrix * vec4(binormal, 0.0)).xyz;
  259. tangent = (scene_data.view_matrix * vec4(tangent, 0.0)).xyz;
  260. #endif
  261. #endif
  262. vertex_interp = vertex;
  263. #ifdef NORMAL_USED
  264. normal_interp = normal;
  265. #endif
  266. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  267. tangent_interp = tangent;
  268. binormal_interp = binormal;
  269. #endif
  270. #if defined(OVERRIDE_POSITION)
  271. gl_Position = position;
  272. #else
  273. gl_Position = projection_matrix * vec4(vertex_interp, 1.0);
  274. #endif
  275. }
  276. /* clang-format off */
  277. #[fragment]
  278. // Default to SPECULAR_SCHLICK_GGX.
  279. #if !defined(SPECULAR_DISABLED) && !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_TOON)
  280. #define SPECULAR_SCHLICK_GGX
  281. #endif
  282. #if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED)
  283. #ifndef NORMAL_USED
  284. #define NORMAL_USED
  285. #endif
  286. #endif
  287. #ifndef MODE_RENDER_DEPTH
  288. #include "tonemap_inc.glsl"
  289. #endif
  290. #include "stdlib_inc.glsl"
  291. /* texture unit usage, N is max_texture_unity-N
  292. 1-color correction // In tonemap_inc.glsl
  293. 2-radiance
  294. 3-directional_shadow
  295. 4-positional_shadow
  296. 5-screen
  297. 6-depth
  298. */
  299. #define M_PI 3.14159265359
  300. /* clang-format on */
  301. #define SHADER_IS_SRGB true
  302. /* Varyings */
  303. #if defined(COLOR_USED)
  304. in vec4 color_interp;
  305. #endif
  306. #if defined(UV_USED)
  307. in vec2 uv_interp;
  308. #endif
  309. #if defined(UV2_USED)
  310. in vec2 uv2_interp;
  311. #else
  312. #ifdef USE_LIGHTMAP
  313. in vec2 uv2_interp;
  314. #endif
  315. #endif
  316. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  317. in vec3 tangent_interp;
  318. in vec3 binormal_interp;
  319. #endif
  320. #ifdef NORMAL_USED
  321. in vec3 normal_interp;
  322. #endif
  323. in highp vec3 vertex_interp;
  324. #ifdef USE_RADIANCE_MAP
  325. #define RADIANCE_MAX_LOD 5.0
  326. uniform samplerCube radiance_map; // texunit:-2
  327. #endif
  328. layout(std140) uniform GlobalShaderUniformData { //ubo:1
  329. vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS];
  330. };
  331. /* Material Uniforms */
  332. #ifdef MATERIAL_UNIFORMS_USED
  333. /* clang-format off */
  334. layout(std140) uniform MaterialUniforms { // ubo:3
  335. #MATERIAL_UNIFORMS
  336. };
  337. /* clang-format on */
  338. #endif
  339. layout(std140) uniform SceneData { // ubo:2
  340. highp mat4 projection_matrix;
  341. highp mat4 inv_projection_matrix;
  342. highp mat4 inv_view_matrix;
  343. highp mat4 view_matrix;
  344. vec2 viewport_size;
  345. vec2 screen_pixel_size;
  346. mediump vec4 ambient_light_color_energy;
  347. mediump float ambient_color_sky_mix;
  348. bool material_uv2_mode;
  349. float emissive_exposure_normalization;
  350. bool use_ambient_light;
  351. bool use_ambient_cubemap;
  352. bool use_reflection_cubemap;
  353. float fog_aerial_perspective;
  354. float time;
  355. mat3 radiance_inverse_xform;
  356. uint directional_light_count;
  357. float z_far;
  358. float z_near;
  359. float IBL_exposure_normalization;
  360. bool fog_enabled;
  361. float fog_density;
  362. float fog_height;
  363. float fog_height_density;
  364. vec3 fog_light_color;
  365. float fog_sun_scatter;
  366. uint camera_visible_layers;
  367. uint pad3;
  368. uint pad4;
  369. uint pad5;
  370. }
  371. scene_data;
  372. #ifdef USE_MULTIVIEW
  373. layout(std140) uniform MultiviewData { // ubo:8
  374. highp mat4 projection_matrix_view[MAX_VIEWS];
  375. highp mat4 inv_projection_matrix_view[MAX_VIEWS];
  376. highp vec4 eye_offset[MAX_VIEWS];
  377. }
  378. multiview_data;
  379. #endif
  380. /* clang-format off */
  381. #GLOBALS
  382. /* clang-format on */
  383. // Directional light data.
  384. #ifndef DISABLE_LIGHT_DIRECTIONAL
  385. struct DirectionalLightData {
  386. mediump vec3 direction;
  387. mediump float energy;
  388. mediump vec3 color;
  389. mediump float size;
  390. mediump vec3 pad;
  391. mediump float specular;
  392. };
  393. layout(std140) uniform DirectionalLights { // ubo:7
  394. DirectionalLightData directional_lights[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
  395. };
  396. #endif // !DISABLE_LIGHT_DIRECTIONAL
  397. // Omni and spot light data.
  398. #if !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT)
  399. struct LightData { // This structure needs to be as packed as possible.
  400. highp vec3 position;
  401. highp float inv_radius;
  402. mediump vec3 direction;
  403. highp float size;
  404. mediump vec3 color;
  405. mediump float attenuation;
  406. mediump float cone_attenuation;
  407. mediump float cone_angle;
  408. mediump float specular_amount;
  409. mediump float shadow_opacity;
  410. };
  411. #ifndef DISABLE_LIGHT_OMNI
  412. layout(std140) uniform OmniLightData { // ubo:5
  413. LightData omni_lights[MAX_LIGHT_DATA_STRUCTS];
  414. };
  415. uniform uint omni_light_indices[MAX_FORWARD_LIGHTS];
  416. uniform uint omni_light_count;
  417. #endif
  418. #ifndef DISABLE_LIGHT_SPOT
  419. layout(std140) uniform SpotLightData { // ubo:6
  420. LightData spot_lights[MAX_LIGHT_DATA_STRUCTS];
  421. };
  422. uniform uint spot_light_indices[MAX_FORWARD_LIGHTS];
  423. uniform uint spot_light_count;
  424. #endif
  425. #ifdef USE_ADDITIVE_LIGHTING
  426. uniform highp samplerCubeShadow positional_shadow; // texunit:-4
  427. #endif
  428. #endif // !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT)
  429. #ifdef USE_MULTIVIEW
  430. uniform highp sampler2DArray depth_buffer; // texunit:-6
  431. uniform highp sampler2DArray color_buffer; // texunit:-5
  432. vec3 multiview_uv(vec2 uv) {
  433. return vec3(uv, ViewIndex);
  434. }
  435. #else
  436. uniform highp sampler2D depth_buffer; // texunit:-6
  437. uniform highp sampler2D color_buffer; // texunit:-5
  438. vec2 multiview_uv(vec2 uv) {
  439. return uv;
  440. }
  441. #endif
  442. uniform highp mat4 world_transform;
  443. uniform mediump float opaque_prepass_threshold;
  444. layout(location = 0) out vec4 frag_color;
  445. vec3 F0(float metallic, float specular, vec3 albedo) {
  446. float dielectric = 0.16 * specular * specular;
  447. // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials;
  448. // see https://google.github.io/filament/Filament.md.html
  449. return mix(vec3(dielectric), albedo, vec3(metallic));
  450. }
  451. #if !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT)
  452. float D_GGX(float cos_theta_m, float alpha) {
  453. float a = cos_theta_m * alpha;
  454. float k = alpha / (1.0 - cos_theta_m * cos_theta_m + a * a);
  455. return k * k * (1.0 / M_PI);
  456. }
  457. // From Earl Hammon, Jr. "PBR Diffuse Lighting for GGX+Smith Microsurfaces" https://www.gdcvault.com/play/1024478/PBR-Diffuse-Lighting-for-GGX
  458. float V_GGX(float NdotL, float NdotV, float alpha) {
  459. return 0.5 / mix(2.0 * NdotL * NdotV, NdotL + NdotV, alpha);
  460. }
  461. float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) {
  462. float alpha2 = alpha_x * alpha_y;
  463. highp vec3 v = vec3(alpha_y * cos_phi, alpha_x * sin_phi, alpha2 * cos_theta_m);
  464. highp float v2 = dot(v, v);
  465. float w2 = alpha2 / v2;
  466. float D = alpha2 * w2 * w2 * (1.0 / M_PI);
  467. return D;
  468. }
  469. float V_GGX_anisotropic(float alpha_x, float alpha_y, float TdotV, float TdotL, float BdotV, float BdotL, float NdotV, float NdotL) {
  470. float Lambda_V = NdotL * length(vec3(alpha_x * TdotV, alpha_y * BdotV, NdotV));
  471. float Lambda_L = NdotV * length(vec3(alpha_x * TdotL, alpha_y * BdotL, NdotL));
  472. return 0.5 / (Lambda_V + Lambda_L);
  473. }
  474. float SchlickFresnel(float u) {
  475. float m = 1.0 - u;
  476. float m2 = m * m;
  477. return m2 * m2 * m; // pow(m,5)
  478. }
  479. void light_compute(vec3 N, vec3 L, vec3 V, float A, vec3 light_color, float attenuation, vec3 f0, float roughness, float metallic, float specular_amount, vec3 albedo, inout float alpha,
  480. #ifdef LIGHT_BACKLIGHT_USED
  481. vec3 backlight,
  482. #endif
  483. #ifdef LIGHT_RIM_USED
  484. float rim, float rim_tint,
  485. #endif
  486. #ifdef LIGHT_CLEARCOAT_USED
  487. float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
  488. #endif
  489. #ifdef LIGHT_ANISOTROPY_USED
  490. vec3 B, vec3 T, float anisotropy,
  491. #endif
  492. inout vec3 diffuse_light, inout vec3 specular_light) {
  493. #if defined(USE_LIGHT_SHADER_CODE)
  494. // light is written by the light shader
  495. vec3 normal = N;
  496. vec3 light = L;
  497. vec3 view = V;
  498. /* clang-format off */
  499. #CODE : LIGHT
  500. /* clang-format on */
  501. #else
  502. float NdotL = min(A + dot(N, L), 1.0);
  503. float cNdotL = max(NdotL, 0.0); // clamped NdotL
  504. float NdotV = dot(N, V);
  505. float cNdotV = max(NdotV, 1e-4);
  506. #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED)
  507. vec3 H = normalize(V + L);
  508. #endif
  509. #if defined(SPECULAR_SCHLICK_GGX)
  510. float cNdotH = clamp(A + dot(N, H), 0.0, 1.0);
  511. #endif
  512. #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED)
  513. float cLdotH = clamp(A + dot(L, H), 0.0, 1.0);
  514. #endif
  515. if (metallic < 1.0) {
  516. float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance
  517. #if defined(DIFFUSE_LAMBERT_WRAP)
  518. // Energy conserving lambert wrap shader.
  519. // https://web.archive.org/web/20210228210901/http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/
  520. diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))) * (1.0 / M_PI);
  521. #elif defined(DIFFUSE_TOON)
  522. diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL) * (1.0 / M_PI);
  523. #elif defined(DIFFUSE_BURLEY)
  524. {
  525. float FD90_minus_1 = 2.0 * cLdotH * cLdotH * roughness - 0.5;
  526. float FdV = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotV);
  527. float FdL = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotL);
  528. diffuse_brdf_NL = (1.0 / M_PI) * FdV * FdL * cNdotL;
  529. }
  530. #else
  531. // Lambert
  532. diffuse_brdf_NL = cNdotL * (1.0 / M_PI);
  533. #endif
  534. diffuse_light += light_color * diffuse_brdf_NL * attenuation;
  535. #if defined(LIGHT_BACKLIGHT_USED)
  536. diffuse_light += light_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * backlight * attenuation;
  537. #endif
  538. #if defined(LIGHT_RIM_USED)
  539. // Epsilon min to prevent pow(0, 0) singularity which results in undefined behavior.
  540. float rim_light = pow(max(1e-4, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0));
  541. diffuse_light += rim_light * rim * mix(vec3(1.0), albedo, rim_tint) * light_color;
  542. #endif
  543. }
  544. if (roughness > 0.0) { // FIXME: roughness == 0 should not disable specular light entirely
  545. // D
  546. #if defined(SPECULAR_TOON)
  547. vec3 R = normalize(-reflect(L, N));
  548. float RdotV = dot(R, V);
  549. float mid = 1.0 - roughness;
  550. mid *= mid;
  551. float intensity = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid;
  552. diffuse_light += light_color * intensity * attenuation * specular_amount; // write to diffuse_light, as in toon shading you generally want no reflection
  553. #elif defined(SPECULAR_DISABLED)
  554. // none..
  555. #elif defined(SPECULAR_SCHLICK_GGX)
  556. // shlick+ggx as default
  557. float alpha_ggx = roughness * roughness;
  558. #if defined(LIGHT_ANISOTROPY_USED)
  559. float aspect = sqrt(1.0 - anisotropy * 0.9);
  560. float ax = alpha_ggx / aspect;
  561. float ay = alpha_ggx * aspect;
  562. float XdotH = dot(T, H);
  563. float YdotH = dot(B, H);
  564. float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH);
  565. float G = V_GGX_anisotropic(ax, ay, dot(T, V), dot(T, L), dot(B, V), dot(B, L), cNdotV, cNdotL);
  566. #else
  567. float D = D_GGX(cNdotH, alpha_ggx);
  568. float G = V_GGX(cNdotL, cNdotV, alpha_ggx);
  569. #endif // LIGHT_ANISOTROPY_USED
  570. // F
  571. float cLdotH5 = SchlickFresnel(cLdotH);
  572. // Calculate Fresnel using cheap approximate specular occlusion term from Filament:
  573. // https://google.github.io/filament/Filament.html#lighting/occlusion/specularocclusion
  574. float f90 = clamp(50.0 * f0.g, 0.0, 1.0);
  575. vec3 F = f0 + (f90 - f0) * cLdotH5;
  576. vec3 specular_brdf_NL = cNdotL * D * F * G;
  577. specular_light += specular_brdf_NL * light_color * attenuation * specular_amount;
  578. #endif
  579. #if defined(LIGHT_CLEARCOAT_USED)
  580. // Clearcoat ignores normal_map, use vertex normal instead
  581. float ccNdotL = max(min(A + dot(vertex_normal, L), 1.0), 0.0);
  582. float ccNdotH = clamp(A + dot(vertex_normal, H), 0.0, 1.0);
  583. float ccNdotV = max(dot(vertex_normal, V), 1e-4);
  584. #if !defined(SPECULAR_SCHLICK_GGX)
  585. float cLdotH5 = SchlickFresnel(cLdotH);
  586. #endif
  587. float Dr = D_GGX(ccNdotH, mix(0.001, 0.1, clearcoat_roughness));
  588. float Gr = 0.25 / (cLdotH * cLdotH);
  589. float Fr = mix(.04, 1.0, cLdotH5);
  590. float clearcoat_specular_brdf_NL = clearcoat * Gr * Fr * Dr * cNdotL;
  591. specular_light += clearcoat_specular_brdf_NL * light_color * attenuation * specular_amount;
  592. // TODO: Clearcoat adds light to the scene right now (it is non-energy conserving), both diffuse and specular need to be scaled by (1.0 - FR)
  593. // but to do so we need to rearrange this entire function
  594. #endif // LIGHT_CLEARCOAT_USED
  595. }
  596. #ifdef USE_SHADOW_TO_OPACITY
  597. alpha = min(alpha, clamp(1.0 - attenuation, 0.0, 1.0));
  598. #endif
  599. #endif // LIGHT_CODE_USED
  600. }
  601. float get_omni_spot_attenuation(float distance, float inv_range, float decay) {
  602. float nd = distance * inv_range;
  603. nd *= nd;
  604. nd *= nd; // nd^4
  605. nd = max(1.0 - nd, 0.0);
  606. nd *= nd; // nd^2
  607. return nd * pow(max(distance, 0.0001), -decay);
  608. }
  609. #ifndef DISABLE_LIGHT_OMNI
  610. void light_process_omni(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f0, float roughness, float metallic, float shadow, vec3 albedo, inout float alpha,
  611. #ifdef LIGHT_BACKLIGHT_USED
  612. vec3 backlight,
  613. #endif
  614. #ifdef LIGHT_RIM_USED
  615. float rim, float rim_tint,
  616. #endif
  617. #ifdef LIGHT_CLEARCOAT_USED
  618. float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
  619. #endif
  620. #ifdef LIGHT_ANISOTROPY_USED
  621. vec3 binormal, vec3 tangent, float anisotropy,
  622. #endif
  623. inout vec3 diffuse_light, inout vec3 specular_light) {
  624. vec3 light_rel_vec = omni_lights[idx].position - vertex;
  625. float light_length = length(light_rel_vec);
  626. float omni_attenuation = get_omni_spot_attenuation(light_length, omni_lights[idx].inv_radius, omni_lights[idx].attenuation);
  627. vec3 color = omni_lights[idx].color;
  628. float size_A = 0.0;
  629. if (omni_lights[idx].size > 0.0) {
  630. float t = omni_lights[idx].size / max(0.001, light_length);
  631. size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t));
  632. }
  633. light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, omni_attenuation, f0, roughness, metallic, omni_lights[idx].specular_amount, albedo, alpha,
  634. #ifdef LIGHT_BACKLIGHT_USED
  635. backlight,
  636. #endif
  637. #ifdef LIGHT_RIM_USED
  638. rim * omni_attenuation, rim_tint,
  639. #endif
  640. #ifdef LIGHT_CLEARCOAT_USED
  641. clearcoat, clearcoat_roughness, vertex_normal,
  642. #endif
  643. #ifdef LIGHT_ANISOTROPY_USED
  644. binormal, tangent, anisotropy,
  645. #endif
  646. diffuse_light,
  647. specular_light);
  648. }
  649. #endif // !DISABLE_LIGHT_OMNI
  650. #ifndef DISABLE_LIGHT_SPOT
  651. void light_process_spot(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f0, float roughness, float metallic, float shadow, vec3 albedo, inout float alpha,
  652. #ifdef LIGHT_BACKLIGHT_USED
  653. vec3 backlight,
  654. #endif
  655. #ifdef LIGHT_RIM_USED
  656. float rim, float rim_tint,
  657. #endif
  658. #ifdef LIGHT_CLEARCOAT_USED
  659. float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
  660. #endif
  661. #ifdef LIGHT_ANISOTROPY_USED
  662. vec3 binormal, vec3 tangent, float anisotropy,
  663. #endif
  664. inout vec3 diffuse_light,
  665. inout vec3 specular_light) {
  666. vec3 light_rel_vec = spot_lights[idx].position - vertex;
  667. float light_length = length(light_rel_vec);
  668. float spot_attenuation = get_omni_spot_attenuation(light_length, spot_lights[idx].inv_radius, spot_lights[idx].attenuation);
  669. vec3 spot_dir = spot_lights[idx].direction;
  670. float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights[idx].cone_angle);
  671. float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights[idx].cone_angle));
  672. spot_attenuation *= 1.0 - pow(spot_rim, spot_lights[idx].cone_attenuation);
  673. vec3 color = spot_lights[idx].color;
  674. float size_A = 0.0;
  675. if (spot_lights[idx].size > 0.0) {
  676. float t = spot_lights[idx].size / max(0.001, light_length);
  677. size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t));
  678. }
  679. light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, spot_attenuation, f0, roughness, metallic, spot_lights[idx].specular_amount, albedo, alpha,
  680. #ifdef LIGHT_BACKLIGHT_USED
  681. backlight,
  682. #endif
  683. #ifdef LIGHT_RIM_USED
  684. rim * spot_attenuation, rim_tint,
  685. #endif
  686. #ifdef LIGHT_CLEARCOAT_USED
  687. clearcoat, clearcoat_roughness, vertex_normal,
  688. #endif
  689. #ifdef LIGHT_ANISOTROPY_USED
  690. binormal, tangent, anisotropy,
  691. #endif
  692. diffuse_light, specular_light);
  693. }
  694. #endif // !DISABLE_LIGHT_SPOT
  695. #endif // !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT)
  696. #ifndef MODE_RENDER_DEPTH
  697. vec4 fog_process(vec3 vertex) {
  698. vec3 fog_color = scene_data.fog_light_color;
  699. #ifdef USE_RADIANCE_MAP
  700. /*
  701. if (scene_data.fog_aerial_perspective > 0.0) {
  702. vec3 sky_fog_color = vec3(0.0);
  703. vec3 cube_view = scene_data.radiance_inverse_xform * vertex;
  704. // mip_level always reads from the second mipmap and higher so the fog is always slightly blurred
  705. float mip_level = mix(1.0 / MAX_ROUGHNESS_LOD, 1.0, 1.0 - (abs(vertex.z) - scene_data.z_near) / (scene_data.z_far - scene_data.z_near));
  706. sky_fog_color = textureLod(radiance_map, cube_view, mip_level * RADIANCE_MAX_LOD).rgb;
  707. fog_color = mix(fog_color, sky_fog_color, scene_data.fog_aerial_perspective);
  708. }
  709. */
  710. #endif
  711. #ifndef DISABLE_LIGHT_DIRECTIONAL
  712. if (scene_data.fog_sun_scatter > 0.001) {
  713. vec4 sun_scatter = vec4(0.0);
  714. float sun_total = 0.0;
  715. vec3 view = normalize(vertex);
  716. for (uint i = uint(0); i < scene_data.directional_light_count; i++) {
  717. vec3 light_color = directional_lights[i].color * directional_lights[i].energy;
  718. float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0);
  719. fog_color += light_color * light_amount * scene_data.fog_sun_scatter;
  720. }
  721. }
  722. #endif // !DISABLE_LIGHT_DIRECTIONAL
  723. float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density));
  724. if (abs(scene_data.fog_height_density) >= 0.0001) {
  725. float y = (scene_data.inv_view_matrix * vec4(vertex, 1.0)).y;
  726. float y_dist = y - scene_data.fog_height;
  727. float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data.fog_height_density));
  728. fog_amount = max(vfog_amount, fog_amount);
  729. }
  730. return vec4(fog_color, fog_amount);
  731. }
  732. #endif // !MODE_RENDER_DEPTH
  733. void main() {
  734. //lay out everything, whatever is unused is optimized away anyway
  735. vec3 vertex = vertex_interp;
  736. #ifdef USE_MULTIVIEW
  737. vec3 eye_offset = multiview_data.eye_offset[ViewIndex].xyz;
  738. vec3 view = -normalize(vertex_interp - eye_offset);
  739. mat4 projection_matrix = multiview_data.projection_matrix_view[ViewIndex];
  740. mat4 inv_projection_matrix = multiview_data.inv_projection_matrix_view[ViewIndex];
  741. #else
  742. vec3 eye_offset = vec3(0.0, 0.0, 0.0);
  743. vec3 view = -normalize(vertex_interp);
  744. mat4 projection_matrix = scene_data.projection_matrix;
  745. mat4 inv_projection_matrix = scene_data.inv_projection_matrix;
  746. #endif
  747. highp mat4 model_matrix = world_transform;
  748. vec3 albedo = vec3(1.0);
  749. vec3 backlight = vec3(0.0);
  750. vec4 transmittance_color = vec4(0.0, 0.0, 0.0, 1.0);
  751. float transmittance_depth = 0.0;
  752. float transmittance_boost = 0.0;
  753. float metallic = 0.0;
  754. float specular = 0.5;
  755. vec3 emission = vec3(0.0);
  756. float roughness = 1.0;
  757. float rim = 0.0;
  758. float rim_tint = 0.0;
  759. float clearcoat = 0.0;
  760. float clearcoat_roughness = 0.0;
  761. float anisotropy = 0.0;
  762. vec2 anisotropy_flow = vec2(1.0, 0.0);
  763. vec4 fog = vec4(0.0);
  764. #if defined(CUSTOM_RADIANCE_USED)
  765. vec4 custom_radiance = vec4(0.0);
  766. #endif
  767. #if defined(CUSTOM_IRRADIANCE_USED)
  768. vec4 custom_irradiance = vec4(0.0);
  769. #endif
  770. float ao = 1.0;
  771. float ao_light_affect = 0.0;
  772. float alpha = 1.0;
  773. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  774. vec3 binormal = normalize(binormal_interp);
  775. vec3 tangent = normalize(tangent_interp);
  776. #else
  777. vec3 binormal = vec3(0.0);
  778. vec3 tangent = vec3(0.0);
  779. #endif
  780. #ifdef NORMAL_USED
  781. vec3 normal = normalize(normal_interp);
  782. #if defined(DO_SIDE_CHECK)
  783. if (!gl_FrontFacing) {
  784. normal = -normal;
  785. }
  786. #endif
  787. #endif //NORMAL_USED
  788. #ifdef UV_USED
  789. vec2 uv = uv_interp;
  790. #endif
  791. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  792. vec2 uv2 = uv2_interp;
  793. #endif
  794. #if defined(COLOR_USED)
  795. vec4 color = color_interp;
  796. #endif
  797. #if defined(NORMAL_MAP_USED)
  798. vec3 normal_map = vec3(0.5);
  799. #endif
  800. float normal_map_depth = 1.0;
  801. vec2 screen_uv = gl_FragCoord.xy * scene_data.screen_pixel_size;
  802. float sss_strength = 0.0;
  803. #ifdef ALPHA_SCISSOR_USED
  804. float alpha_scissor_threshold = 1.0;
  805. #endif // ALPHA_SCISSOR_USED
  806. #ifdef ALPHA_HASH_USED
  807. float alpha_hash_scale = 1.0;
  808. #endif // ALPHA_HASH_USED
  809. #ifdef ALPHA_ANTIALIASING_EDGE_USED
  810. float alpha_antialiasing_edge = 0.0;
  811. vec2 alpha_texture_coordinate = vec2(0.0, 0.0);
  812. #endif // ALPHA_ANTIALIASING_EDGE_USED
  813. {
  814. #CODE : FRAGMENT
  815. }
  816. #ifndef USE_SHADOW_TO_OPACITY
  817. #if defined(ALPHA_SCISSOR_USED)
  818. if (alpha < alpha_scissor_threshold) {
  819. discard;
  820. }
  821. #endif // ALPHA_SCISSOR_USED
  822. #ifdef USE_OPAQUE_PREPASS
  823. #if !defined(ALPHA_SCISSOR_USED)
  824. if (alpha < opaque_prepass_threshold) {
  825. discard;
  826. }
  827. #endif // not ALPHA_SCISSOR_USED
  828. #endif // USE_OPAQUE_PREPASS
  829. #endif // !USE_SHADOW_TO_OPACITY
  830. #ifdef NORMAL_MAP_USED
  831. normal_map.xy = normal_map.xy * 2.0 - 1.0;
  832. normal_map.z = sqrt(max(0.0, 1.0 - dot(normal_map.xy, normal_map.xy))); //always ignore Z, as it can be RG packed, Z may be pos/neg, etc.
  833. normal = normalize(mix(normal, tangent * normal_map.x + binormal * normal_map.y + normal * normal_map.z, normal_map_depth));
  834. #endif
  835. #ifdef LIGHT_ANISOTROPY_USED
  836. if (anisotropy > 0.01) {
  837. //rotation matrix
  838. mat3 rot = mat3(tangent, binormal, normal);
  839. //make local to space
  840. tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0));
  841. binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0));
  842. }
  843. #endif
  844. #ifndef MODE_RENDER_DEPTH
  845. #ifndef CUSTOM_FOG_USED
  846. #ifndef DISABLE_FOG
  847. // fog must be processed as early as possible and then packed.
  848. // to maximize VGPR usage
  849. if (scene_data.fog_enabled) {
  850. fog = fog_process(vertex);
  851. }
  852. #endif // !DISABLE_FOG
  853. #endif // !CUSTOM_FOG_USED
  854. uint fog_rg = packHalf2x16(fog.rg);
  855. uint fog_ba = packHalf2x16(fog.ba);
  856. // Convert colors to linear
  857. albedo = srgb_to_linear(albedo);
  858. emission = srgb_to_linear(emission);
  859. // TODO Backlight and transmittance when used
  860. #ifndef MODE_UNSHADED
  861. vec3 f0 = F0(metallic, specular, albedo);
  862. vec3 specular_light = vec3(0.0, 0.0, 0.0);
  863. vec3 diffuse_light = vec3(0.0, 0.0, 0.0);
  864. vec3 ambient_light = vec3(0.0, 0.0, 0.0);
  865. #ifdef BASE_PASS
  866. /////////////////////// LIGHTING //////////////////////////////
  867. // IBL precalculations
  868. float ndotv = clamp(dot(normal, view), 0.0, 1.0);
  869. vec3 F = f0 + (max(vec3(1.0 - roughness), f0) - f0) * pow(1.0 - ndotv, 5.0);
  870. #ifdef USE_RADIANCE_MAP
  871. if (scene_data.use_reflection_cubemap) {
  872. #ifdef LIGHT_ANISOTROPY_USED
  873. // https://google.github.io/filament/Filament.html#lighting/imagebasedlights/anisotropy
  874. vec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent;
  875. vec3 anisotropic_tangent = cross(anisotropic_direction, view);
  876. vec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction);
  877. vec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0)));
  878. vec3 ref_vec = reflect(-view, bent_normal);
  879. #else
  880. vec3 ref_vec = reflect(-view, normal);
  881. #endif
  882. ref_vec = mix(ref_vec, normal, roughness * roughness);
  883. float horizon = min(1.0 + dot(ref_vec, normal), 1.0);
  884. ref_vec = scene_data.radiance_inverse_xform * ref_vec;
  885. specular_light = textureLod(radiance_map, ref_vec, sqrt(roughness) * RADIANCE_MAX_LOD).rgb;
  886. specular_light = srgb_to_linear(specular_light);
  887. specular_light *= horizon * horizon;
  888. specular_light *= scene_data.ambient_light_color_energy.a;
  889. }
  890. #endif
  891. // Calculate Reflection probes
  892. // Calculate Lightmaps
  893. #if defined(CUSTOM_RADIANCE_USED)
  894. specular_light = mix(specular_light, custom_radiance.rgb, custom_radiance.a);
  895. #endif // CUSTOM_RADIANCE_USED
  896. #ifndef USE_LIGHTMAP
  897. //lightmap overrides everything
  898. if (scene_data.use_ambient_light) {
  899. ambient_light = scene_data.ambient_light_color_energy.rgb;
  900. #ifdef USE_RADIANCE_MAP
  901. if (scene_data.use_ambient_cubemap) {
  902. vec3 ambient_dir = scene_data.radiance_inverse_xform * normal;
  903. vec3 cubemap_ambient = textureLod(radiance_map, ambient_dir, RADIANCE_MAX_LOD).rgb;
  904. cubemap_ambient = srgb_to_linear(cubemap_ambient);
  905. ambient_light = mix(ambient_light, cubemap_ambient * scene_data.ambient_light_color_energy.a, scene_data.ambient_color_sky_mix);
  906. }
  907. #endif
  908. }
  909. #endif // USE_LIGHTMAP
  910. #if defined(CUSTOM_IRRADIANCE_USED)
  911. ambient_light = mix(ambient_light, custom_irradiance.rgb, custom_irradiance.a);
  912. #endif // CUSTOM_IRRADIANCE_USED
  913. {
  914. #if defined(AMBIENT_LIGHT_DISABLED)
  915. ambient_light = vec3(0.0, 0.0, 0.0);
  916. #else
  917. ambient_light *= albedo.rgb;
  918. ambient_light *= ao;
  919. #endif // AMBIENT_LIGHT_DISABLED
  920. }
  921. // convert ao to direct light ao
  922. ao = mix(1.0, ao, ao_light_affect);
  923. {
  924. #if defined(DIFFUSE_TOON)
  925. //simplify for toon, as
  926. specular_light *= specular * metallic * albedo * 2.0;
  927. #else
  928. // scales the specular reflections, needs to be be computed before lighting happens,
  929. // but after environment, GI, and reflection probes are added
  930. // Environment brdf approximation (Lazarov 2013)
  931. // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
  932. const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
  933. const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);
  934. vec4 r = roughness * c0 + c1;
  935. float ndotv = clamp(dot(normal, view), 0.0, 1.0);
  936. float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y;
  937. vec2 env = vec2(-1.04, 1.04) * a004 + r.zw;
  938. specular_light *= env.x * f0 + env.y * clamp(50.0 * f0.g, metallic, 1.0);
  939. #endif
  940. }
  941. #endif // BASE_PASS
  942. #ifndef DISABLE_LIGHT_DIRECTIONAL
  943. //diffuse_light = normal; //
  944. for (uint i = uint(0); i < scene_data.directional_light_count; i++) {
  945. light_compute(normal, normalize(directional_lights[i].direction), normalize(view), directional_lights[i].size, directional_lights[i].color * directional_lights[i].energy, 1.0, f0, roughness, metallic, 1.0, albedo, alpha,
  946. #ifdef LIGHT_BACKLIGHT_USED
  947. backlight,
  948. #endif
  949. #ifdef LIGHT_RIM_USED
  950. rim, rim_tint,
  951. #endif
  952. #ifdef LIGHT_CLEARCOAT_USED
  953. clearcoat, clearcoat_roughness, normalize(normal_interp),
  954. #endif
  955. #ifdef LIGHT_ANISOTROPY_USED
  956. binormal,
  957. tangent, anisotropy,
  958. #endif
  959. diffuse_light,
  960. specular_light);
  961. }
  962. #endif // !DISABLE_LIGHT_DIRECTIONAL
  963. #ifndef DISABLE_LIGHT_OMNI
  964. for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) {
  965. if (i >= omni_light_count) {
  966. break;
  967. }
  968. light_process_omni(omni_light_indices[i], vertex, view, normal, f0, roughness, metallic, 0.0, albedo, alpha,
  969. #ifdef LIGHT_BACKLIGHT_USED
  970. backlight,
  971. #endif
  972. #ifdef LIGHT_RIM_USED
  973. rim,
  974. rim_tint,
  975. #endif
  976. #ifdef LIGHT_CLEARCOAT_USED
  977. clearcoat, clearcoat_roughness, normalize(normal_interp),
  978. #endif
  979. #ifdef LIGHT_ANISOTROPY_USED
  980. binormal, tangent, anisotropy,
  981. #endif
  982. diffuse_light, specular_light);
  983. }
  984. #endif // !DISABLE_LIGHT_OMNI
  985. #ifndef DISABLE_LIGHT_SPOT
  986. for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) {
  987. if (i >= spot_light_count) {
  988. break;
  989. }
  990. light_process_spot(spot_light_indices[i], vertex, view, normal, f0, roughness, metallic, 0.0, albedo, alpha,
  991. #ifdef LIGHT_BACKLIGHT_USED
  992. backlight,
  993. #endif
  994. #ifdef LIGHT_RIM_USED
  995. rim,
  996. rim_tint,
  997. #endif
  998. #ifdef LIGHT_CLEARCOAT_USED
  999. clearcoat, clearcoat_roughness, normalize(normal_interp),
  1000. #endif
  1001. #ifdef LIGHT_ANISOTROPY_USED
  1002. tangent,
  1003. binormal, anisotropy,
  1004. #endif
  1005. diffuse_light, specular_light);
  1006. }
  1007. #endif // !DISABLE_LIGHT_SPOT
  1008. #endif // !MODE_UNSHADED
  1009. #endif // !MODE_RENDER_DEPTH
  1010. #if defined(USE_SHADOW_TO_OPACITY)
  1011. alpha = min(alpha, clamp(length(ambient_light), 0.0, 1.0));
  1012. #if defined(ALPHA_SCISSOR_USED)
  1013. if (alpha < alpha_scissor) {
  1014. discard;
  1015. }
  1016. #endif // ALPHA_SCISSOR_USED
  1017. #ifdef USE_OPAQUE_PREPASS
  1018. #if !defined(ALPHA_SCISSOR_USED)
  1019. if (alpha < opaque_prepass_threshold) {
  1020. discard;
  1021. }
  1022. #endif // not ALPHA_SCISSOR_USED
  1023. #endif // USE_OPAQUE_PREPASS
  1024. #endif // USE_SHADOW_TO_OPACITY
  1025. #ifdef MODE_RENDER_DEPTH
  1026. //nothing happens, so a tree-ssa optimizer will result in no fragment shader :)
  1027. #else // !MODE_RENDER_DEPTH
  1028. #ifdef MODE_UNSHADED
  1029. frag_color = vec4(albedo, alpha);
  1030. #else
  1031. diffuse_light *= albedo;
  1032. diffuse_light *= 1.0 - metallic;
  1033. ambient_light *= 1.0 - metallic;
  1034. frag_color = vec4(diffuse_light + specular_light, alpha);
  1035. #ifdef BASE_PASS
  1036. frag_color.rgb += emission + ambient_light;
  1037. #endif
  1038. #endif //MODE_UNSHADED
  1039. fog = vec4(unpackHalf2x16(fog_rg), unpackHalf2x16(fog_ba));
  1040. #ifndef DISABLE_FOG
  1041. if (scene_data.fog_enabled) {
  1042. #ifdef BASE_PASS
  1043. frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a);
  1044. #else
  1045. frag_color.rgb *= (1.0 - fog.a);
  1046. #endif // BASE_PASS
  1047. }
  1048. #endif
  1049. // Tonemap before writing as we are writing to an sRGB framebuffer
  1050. frag_color.rgb *= exposure;
  1051. frag_color.rgb = apply_tonemapping(frag_color.rgb, white);
  1052. frag_color.rgb = linear_to_srgb(frag_color.rgb);
  1053. #ifdef USE_BCS
  1054. frag_color.rgb = apply_bcs(frag_color.rgb, bcs);
  1055. #endif
  1056. #ifdef USE_COLOR_CORRECTION
  1057. frag_color.rgb = apply_color_correction(frag_color.rgb, color_correction);
  1058. #endif
  1059. #endif //!MODE_RENDER_DEPTH
  1060. }