2
0

scene.glsl 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161
  1. /* clang-format off */
  2. [vertex]
  3. #define M_PI 3.14159265359
  4. #define SHADER_IS_SRGB false
  5. /*
  6. from VisualServer:
  7. ARRAY_VERTEX=0,
  8. ARRAY_NORMAL=1,
  9. ARRAY_TANGENT=2,
  10. ARRAY_COLOR=3,
  11. ARRAY_TEX_UV=4,
  12. ARRAY_TEX_UV2=5,
  13. ARRAY_BONES=6,
  14. ARRAY_WEIGHTS=7,
  15. ARRAY_INDEX=8,
  16. */
  17. // hack to use uv if no uv present so it works with lightmap
  18. /* INPUT ATTRIBS */
  19. layout(location = 0) in highp vec4 vertex_attrib;
  20. /* clang-format on */
  21. layout(location = 1) in vec3 normal_attrib;
  22. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
  23. layout(location = 2) in vec4 tangent_attrib;
  24. #endif
  25. #if defined(ENABLE_COLOR_INTERP)
  26. layout(location = 3) in vec4 color_attrib;
  27. #endif
  28. #if defined(ENABLE_UV_INTERP)
  29. layout(location = 4) in vec2 uv_attrib;
  30. #endif
  31. #if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP)
  32. layout(location = 5) in vec2 uv2_attrib;
  33. #endif
  34. #ifdef USE_SKELETON
  35. layout(location = 6) in uvec4 bone_indices; // attrib:6
  36. layout(location = 7) in highp vec4 bone_weights; // attrib:7
  37. #endif
  38. #ifdef USE_INSTANCING
  39. layout(location = 8) in highp vec4 instance_xform0;
  40. layout(location = 9) in highp vec4 instance_xform1;
  41. layout(location = 10) in highp vec4 instance_xform2;
  42. layout(location = 11) in lowp vec4 instance_color;
  43. #if defined(ENABLE_INSTANCE_CUSTOM)
  44. layout(location = 12) in highp vec4 instance_custom_data;
  45. #endif
  46. #endif
  47. layout(std140) uniform SceneData { // ubo:0
  48. highp mat4 projection_matrix;
  49. highp mat4 inv_projection_matrix;
  50. highp mat4 camera_inverse_matrix;
  51. highp mat4 camera_matrix;
  52. mediump vec4 ambient_light_color;
  53. mediump vec4 bg_color;
  54. mediump vec4 fog_color_enabled;
  55. mediump vec4 fog_sun_color_amount;
  56. mediump float ambient_energy;
  57. mediump float bg_energy;
  58. mediump float z_offset;
  59. mediump float z_slope_scale;
  60. highp float shadow_dual_paraboloid_render_zfar;
  61. highp float shadow_dual_paraboloid_render_side;
  62. highp vec2 viewport_size;
  63. highp vec2 screen_pixel_size;
  64. highp vec2 shadow_atlas_pixel_size;
  65. highp vec2 directional_shadow_pixel_size;
  66. highp float time;
  67. highp float z_far;
  68. mediump float reflection_multiplier;
  69. mediump float subsurface_scatter_width;
  70. mediump float ambient_occlusion_affect_light;
  71. mediump float ambient_occlusion_affect_ao_channel;
  72. mediump float opaque_prepass_threshold;
  73. bool fog_depth_enabled;
  74. highp float fog_depth_begin;
  75. highp float fog_depth_end;
  76. mediump float fog_density;
  77. highp float fog_depth_curve;
  78. bool fog_transmit_enabled;
  79. highp float fog_transmit_curve;
  80. bool fog_height_enabled;
  81. highp float fog_height_min;
  82. highp float fog_height_max;
  83. highp float fog_height_curve;
  84. };
  85. uniform highp mat4 world_transform;
  86. #ifdef USE_LIGHT_DIRECTIONAL
  87. layout(std140) uniform DirectionalLightData { //ubo:3
  88. highp vec4 light_pos_inv_radius;
  89. mediump vec4 light_direction_attenuation;
  90. mediump vec4 light_color_energy;
  91. mediump vec4 light_params; // cone attenuation, angle, specular, shadow enabled,
  92. mediump vec4 light_clamp;
  93. mediump vec4 shadow_color_contact;
  94. highp mat4 shadow_matrix1;
  95. highp mat4 shadow_matrix2;
  96. highp mat4 shadow_matrix3;
  97. highp mat4 shadow_matrix4;
  98. mediump vec4 shadow_split_offsets;
  99. };
  100. #endif
  101. #ifdef USE_VERTEX_LIGHTING
  102. //omni and spot
  103. struct LightData {
  104. highp vec4 light_pos_inv_radius;
  105. mediump vec4 light_direction_attenuation;
  106. mediump vec4 light_color_energy;
  107. mediump vec4 light_params; // cone attenuation, angle, specular, shadow enabled,
  108. mediump vec4 light_clamp;
  109. mediump vec4 shadow_color_contact;
  110. highp mat4 shadow_matrix;
  111. };
  112. layout(std140) uniform OmniLightData { //ubo:4
  113. LightData omni_lights[MAX_LIGHT_DATA_STRUCTS];
  114. };
  115. layout(std140) uniform SpotLightData { //ubo:5
  116. LightData spot_lights[MAX_LIGHT_DATA_STRUCTS];
  117. };
  118. #ifdef USE_FORWARD_LIGHTING
  119. uniform int omni_light_indices[MAX_FORWARD_LIGHTS];
  120. uniform int omni_light_count;
  121. uniform int spot_light_indices[MAX_FORWARD_LIGHTS];
  122. uniform int spot_light_count;
  123. #endif
  124. out vec4 diffuse_light_interp;
  125. out vec4 specular_light_interp;
  126. void light_compute(vec3 N, vec3 L, vec3 V, vec3 light_color, float roughness, inout vec3 diffuse, inout vec3 specular) {
  127. float NdotL = dot(N, L);
  128. float cNdotL = max(NdotL, 0.0); // clamped NdotL
  129. float NdotV = dot(N, V);
  130. float cNdotV = max(NdotV, 0.0);
  131. #if defined(DIFFUSE_OREN_NAYAR)
  132. vec3 diffuse_brdf_NL;
  133. #else
  134. float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance
  135. #endif
  136. #if defined(DIFFUSE_LAMBERT_WRAP)
  137. // energy conserving lambert wrap shader
  138. diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness)));
  139. #elif defined(DIFFUSE_OREN_NAYAR)
  140. {
  141. // see http://mimosa-pudica.net/improved-oren-nayar.html
  142. float LdotV = dot(L, V);
  143. float s = LdotV - NdotL * NdotV;
  144. float t = mix(1.0, max(NdotL, NdotV), step(0.0, s));
  145. float sigma2 = roughness * roughness; // TODO: this needs checking
  146. vec3 A = 1.0 + sigma2 * (-0.5 / (sigma2 + 0.33) + 0.17 * diffuse_color / (sigma2 + 0.13));
  147. float B = 0.45 * sigma2 / (sigma2 + 0.09);
  148. diffuse_brdf_NL = cNdotL * (A + vec3(B) * s / t) * (1.0 / M_PI);
  149. }
  150. #else
  151. // lambert by default for everything else
  152. diffuse_brdf_NL = cNdotL * (1.0 / M_PI);
  153. #endif
  154. diffuse += light_color * diffuse_brdf_NL;
  155. if (roughness > 0.0) {
  156. // D
  157. float specular_brdf_NL = 0.0;
  158. #if !defined(SPECULAR_DISABLED)
  159. //normalized blinn always unless disabled
  160. vec3 H = normalize(V + L);
  161. float cNdotH = max(dot(N, H), 0.0);
  162. float cVdotH = max(dot(V, H), 0.0);
  163. float cLdotH = max(dot(L, H), 0.0);
  164. float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25;
  165. float blinn = pow(cNdotH, shininess);
  166. blinn *= (shininess + 8.0) * (1.0 / (8.0 * M_PI));
  167. specular_brdf_NL = (blinn) / max(4.0 * cNdotV * cNdotL, 0.75);
  168. #endif
  169. specular += specular_brdf_NL * light_color * (1.0 / M_PI);
  170. }
  171. }
  172. void light_process_omni(int idx, vec3 vertex, vec3 eye_vec, vec3 normal, float roughness, inout vec3 diffuse, inout vec3 specular) {
  173. vec3 light_rel_vec = omni_lights[idx].light_pos_inv_radius.xyz - vertex;
  174. float light_length = length(light_rel_vec);
  175. float normalized_distance = light_length * omni_lights[idx].light_pos_inv_radius.w;
  176. vec3 light_attenuation = vec3(pow(max(1.0 - normalized_distance, 0.0), omni_lights[idx].light_direction_attenuation.w));
  177. light_compute(normal, normalize(light_rel_vec), eye_vec, omni_lights[idx].light_color_energy.rgb * light_attenuation, roughness, diffuse, specular);
  178. }
  179. void light_process_spot(int idx, vec3 vertex, vec3 eye_vec, vec3 normal, float roughness, inout vec3 diffuse, inout vec3 specular) {
  180. vec3 light_rel_vec = spot_lights[idx].light_pos_inv_radius.xyz - vertex;
  181. float light_length = length(light_rel_vec);
  182. float normalized_distance = light_length * spot_lights[idx].light_pos_inv_radius.w;
  183. vec3 light_attenuation = vec3(pow(max(1.0 - normalized_distance, 0.001), spot_lights[idx].light_direction_attenuation.w));
  184. vec3 spot_dir = spot_lights[idx].light_direction_attenuation.xyz;
  185. float spot_cutoff = spot_lights[idx].light_params.y;
  186. float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_cutoff);
  187. float spot_rim = (1.0 - scos) / (1.0 - spot_cutoff);
  188. light_attenuation *= 1.0 - pow(max(spot_rim, 0.001), spot_lights[idx].light_params.x);
  189. light_compute(normal, normalize(light_rel_vec), eye_vec, spot_lights[idx].light_color_energy.rgb * light_attenuation, roughness, diffuse, specular);
  190. }
  191. #endif
  192. /* Varyings */
  193. out highp vec3 vertex_interp;
  194. out vec3 normal_interp;
  195. #if defined(ENABLE_COLOR_INTERP)
  196. out vec4 color_interp;
  197. #endif
  198. #if defined(ENABLE_UV_INTERP)
  199. out vec2 uv_interp;
  200. #endif
  201. #if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP)
  202. out vec2 uv2_interp;
  203. #endif
  204. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
  205. out vec3 tangent_interp;
  206. out vec3 binormal_interp;
  207. #endif
  208. #if defined(USE_MATERIAL)
  209. /* clang-format off */
  210. layout(std140) uniform UniformData { // ubo:1
  211. MATERIAL_UNIFORMS
  212. };
  213. /* clang-format on */
  214. #endif
  215. /* clang-format off */
  216. VERTEX_SHADER_GLOBALS
  217. /* clang-format on */
  218. #ifdef RENDER_DEPTH_DUAL_PARABOLOID
  219. out highp float dp_clip;
  220. #endif
  221. #define SKELETON_TEXTURE_WIDTH 256
  222. #ifdef USE_SKELETON
  223. uniform highp sampler2D skeleton_texture; // texunit:-1
  224. uniform highp mat4 skeleton_transform;
  225. uniform bool skeleton_in_world_coords;
  226. #endif
  227. out highp vec4 position_interp;
  228. // FIXME: This triggers a Mesa bug that breaks rendering, so disabled for now.
  229. // See GH-13450 and https://bugs.freedesktop.org/show_bug.cgi?id=100316
  230. //invariant gl_Position;
  231. void main() {
  232. highp vec4 vertex = vertex_attrib; // vec4(vertex_attrib.xyz * data_attrib.x,1.0);
  233. highp mat4 world_matrix = world_transform;
  234. #ifdef USE_INSTANCING
  235. {
  236. highp mat4 m = mat4(instance_xform0, instance_xform1, instance_xform2, vec4(0.0, 0.0, 0.0, 1.0));
  237. world_matrix = world_matrix * transpose(m);
  238. }
  239. #endif
  240. vec3 normal = normal_attrib;
  241. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
  242. vec3 tangent = tangent_attrib.xyz;
  243. float binormalf = tangent_attrib.a;
  244. #endif
  245. #if defined(ENABLE_COLOR_INTERP)
  246. color_interp = color_attrib;
  247. #if defined(USE_INSTANCING)
  248. color_interp *= instance_color;
  249. #endif
  250. #endif
  251. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
  252. vec3 binormal = normalize(cross(normal, tangent) * binormalf);
  253. #endif
  254. #if defined(ENABLE_UV_INTERP)
  255. uv_interp = uv_attrib;
  256. #endif
  257. #if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP)
  258. uv2_interp = uv2_attrib;
  259. #endif
  260. #ifdef OVERRIDE_POSITION
  261. highp vec4 position;
  262. #endif
  263. #if defined(USE_INSTANCING) && defined(ENABLE_INSTANCE_CUSTOM)
  264. vec4 instance_custom = instance_custom_data;
  265. #else
  266. vec4 instance_custom = vec4(0.0);
  267. #endif
  268. highp mat4 local_projection = projection_matrix;
  269. //using world coordinates
  270. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  271. vertex = world_matrix * vertex;
  272. #if defined(ENSURE_CORRECT_NORMALS)
  273. mat3 normal_matrix = mat3(transpose(inverse(world_matrix)));
  274. normal = normal_matrix * normal;
  275. #else
  276. normal = normalize((world_matrix * vec4(normal, 0.0)).xyz);
  277. #endif
  278. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
  279. tangent = normalize((world_matrix * vec4(tangent, 0.0)).xyz);
  280. binormal = normalize((world_matrix * vec4(binormal, 0.0)).xyz);
  281. #endif
  282. #endif
  283. float roughness = 1.0;
  284. //defines that make writing custom shaders easier
  285. #define projection_matrix local_projection
  286. #define world_transform world_matrix
  287. #ifdef USE_SKELETON
  288. {
  289. //skeleton transform
  290. ivec4 bone_indicesi = ivec4(bone_indices); // cast to signed int
  291. ivec2 tex_ofs = ivec2(bone_indicesi.x % 256, (bone_indicesi.x / 256) * 3);
  292. highp mat4 m;
  293. m = mat4(
  294. texelFetch(skeleton_texture, tex_ofs, 0),
  295. texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0),
  296. texelFetch(skeleton_texture, tex_ofs + ivec2(0, 2), 0),
  297. vec4(0.0, 0.0, 0.0, 1.0)) *
  298. bone_weights.x;
  299. tex_ofs = ivec2(bone_indicesi.y % 256, (bone_indicesi.y / 256) * 3);
  300. m += mat4(
  301. texelFetch(skeleton_texture, tex_ofs, 0),
  302. texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0),
  303. texelFetch(skeleton_texture, tex_ofs + ivec2(0, 2), 0),
  304. vec4(0.0, 0.0, 0.0, 1.0)) *
  305. bone_weights.y;
  306. tex_ofs = ivec2(bone_indicesi.z % 256, (bone_indicesi.z / 256) * 3);
  307. m += mat4(
  308. texelFetch(skeleton_texture, tex_ofs, 0),
  309. texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0),
  310. texelFetch(skeleton_texture, tex_ofs + ivec2(0, 2), 0),
  311. vec4(0.0, 0.0, 0.0, 1.0)) *
  312. bone_weights.z;
  313. tex_ofs = ivec2(bone_indicesi.w % 256, (bone_indicesi.w / 256) * 3);
  314. m += mat4(
  315. texelFetch(skeleton_texture, tex_ofs, 0),
  316. texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0),
  317. texelFetch(skeleton_texture, tex_ofs + ivec2(0, 2), 0),
  318. vec4(0.0, 0.0, 0.0, 1.0)) *
  319. bone_weights.w;
  320. if (skeleton_in_world_coords) {
  321. highp mat4 bone_matrix = skeleton_transform * (transpose(m) * inverse(skeleton_transform));
  322. world_matrix = bone_matrix * world_matrix;
  323. } else {
  324. world_matrix = world_matrix * transpose(m);
  325. }
  326. }
  327. #endif
  328. highp mat4 modelview = camera_inverse_matrix * world_matrix;
  329. {
  330. /* clang-format off */
  331. VERTEX_SHADER_CODE
  332. /* clang-format on */
  333. }
  334. // using local coordinates (default)
  335. #if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
  336. vertex = modelview * vertex;
  337. #if defined(ENSURE_CORRECT_NORMALS)
  338. mat3 normal_matrix = mat3(transpose(inverse(modelview)));
  339. normal = normal_matrix * normal;
  340. #else
  341. normal = normalize((modelview * vec4(normal, 0.0)).xyz);
  342. #endif
  343. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
  344. tangent = normalize((modelview * vec4(tangent, 0.0)).xyz);
  345. binormal = normalize((modelview * vec4(binormal, 0.0)).xyz);
  346. #endif
  347. #endif
  348. //using world coordinates
  349. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  350. vertex = camera_inverse_matrix * vertex;
  351. normal = normalize((camera_inverse_matrix * vec4(normal, 0.0)).xyz);
  352. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
  353. tangent = normalize((camera_inverse_matrix * vec4(tangent, 0.0)).xyz);
  354. binormal = normalize((camera_inverse_matrix * vec4(binormal, 0.0)).xyz);
  355. #endif
  356. #endif
  357. vertex_interp = vertex.xyz;
  358. normal_interp = normal;
  359. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
  360. tangent_interp = tangent;
  361. binormal_interp = binormal;
  362. #endif
  363. #ifdef RENDER_DEPTH
  364. #ifdef RENDER_DEPTH_DUAL_PARABOLOID
  365. vertex_interp.z *= shadow_dual_paraboloid_render_side;
  366. normal_interp.z *= shadow_dual_paraboloid_render_side;
  367. dp_clip = vertex_interp.z; //this attempts to avoid noise caused by objects sent to the other parabolloid side due to bias
  368. //for dual paraboloid shadow mapping, this is the fastest but least correct way, as it curves straight edges
  369. highp vec3 vtx = vertex_interp + normalize(vertex_interp) * z_offset;
  370. highp float distance = length(vtx);
  371. vtx = normalize(vtx);
  372. vtx.xy /= 1.0 - vtx.z;
  373. vtx.z = (distance / shadow_dual_paraboloid_render_zfar);
  374. vtx.z = vtx.z * 2.0 - 1.0;
  375. vertex_interp = vtx;
  376. #else
  377. float z_ofs = z_offset;
  378. z_ofs += (1.0 - abs(normal_interp.z)) * z_slope_scale;
  379. vertex_interp.z -= z_ofs;
  380. #endif //RENDER_DEPTH_DUAL_PARABOLOID
  381. #endif //RENDER_DEPTH
  382. #ifdef OVERRIDE_POSITION
  383. gl_Position = position;
  384. #else
  385. gl_Position = projection_matrix * vec4(vertex_interp, 1.0);
  386. #endif
  387. position_interp = gl_Position;
  388. #ifdef USE_VERTEX_LIGHTING
  389. diffuse_light_interp = vec4(0.0);
  390. specular_light_interp = vec4(0.0);
  391. #ifdef USE_FORWARD_LIGHTING
  392. for (int i = 0; i < omni_light_count; i++) {
  393. light_process_omni(omni_light_indices[i], vertex_interp, -normalize(vertex_interp), normal_interp, roughness, diffuse_light_interp.rgb, specular_light_interp.rgb);
  394. }
  395. for (int i = 0; i < spot_light_count; i++) {
  396. light_process_spot(spot_light_indices[i], vertex_interp, -normalize(vertex_interp), normal_interp, roughness, diffuse_light_interp.rgb, specular_light_interp.rgb);
  397. }
  398. #endif
  399. #ifdef USE_LIGHT_DIRECTIONAL
  400. vec3 directional_diffuse = vec3(0.0);
  401. vec3 directional_specular = vec3(0.0);
  402. light_compute(normal_interp, -light_direction_attenuation.xyz, -normalize(vertex_interp), light_color_energy.rgb, roughness, directional_diffuse, directional_specular);
  403. float diff_avg = dot(diffuse_light_interp.rgb, vec3(0.33333));
  404. float diff_dir_avg = dot(directional_diffuse, vec3(0.33333));
  405. if (diff_avg > 0.0) {
  406. diffuse_light_interp.a = diff_dir_avg / (diff_avg + diff_dir_avg);
  407. } else {
  408. diffuse_light_interp.a = 1.0;
  409. }
  410. diffuse_light_interp.rgb += directional_diffuse;
  411. float spec_avg = dot(specular_light_interp.rgb, vec3(0.33333));
  412. float spec_dir_avg = dot(directional_specular, vec3(0.33333));
  413. if (spec_avg > 0.0) {
  414. specular_light_interp.a = spec_dir_avg / (spec_avg + spec_dir_avg);
  415. } else {
  416. specular_light_interp.a = 1.0;
  417. }
  418. specular_light_interp.rgb += directional_specular;
  419. #endif //USE_LIGHT_DIRECTIONAL
  420. #endif // USE_VERTEX_LIGHTING
  421. }
  422. /* clang-format off */
  423. [fragment]
  424. /* texture unit usage, N is max_texture_unity-N
  425. 1-skeleton
  426. 2-radiance
  427. 3-reflection_atlas
  428. 4-directional_shadow
  429. 5-shadow_atlas
  430. 6-decal_atlas
  431. 7-screen
  432. 8-depth
  433. 9-probe1
  434. 10-probe2
  435. */
  436. uniform highp mat4 world_transform;
  437. /* clang-format on */
  438. #define M_PI 3.14159265359
  439. #define SHADER_IS_SRGB false
  440. /* Varyings */
  441. #if defined(ENABLE_COLOR_INTERP)
  442. in vec4 color_interp;
  443. #endif
  444. #if defined(ENABLE_UV_INTERP)
  445. in vec2 uv_interp;
  446. #endif
  447. #if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP)
  448. in vec2 uv2_interp;
  449. #endif
  450. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
  451. in vec3 tangent_interp;
  452. in vec3 binormal_interp;
  453. #endif
  454. in highp vec3 vertex_interp;
  455. in vec3 normal_interp;
  456. /* PBR CHANNELS */
  457. #ifdef USE_RADIANCE_MAP
  458. layout(std140) uniform Radiance { // ubo:2
  459. mat4 radiance_inverse_xform;
  460. float radiance_ambient_contribution;
  461. };
  462. #define RADIANCE_MAX_LOD 5.0
  463. #ifdef USE_RADIANCE_MAP_ARRAY
  464. uniform sampler2DArray radiance_map; // texunit:-2
  465. vec3 textureDualParaboloid(sampler2DArray p_tex, vec3 p_vec, float p_roughness) {
  466. vec3 norm = normalize(p_vec);
  467. norm.xy /= 1.0 + abs(norm.z);
  468. norm.xy = norm.xy * vec2(0.5, 0.25) + vec2(0.5, 0.25);
  469. // we need to lie the derivatives (normg) and assume that DP side is always the same
  470. // to get proper texture filtering
  471. vec2 normg = norm.xy;
  472. if (norm.z > 0.0) {
  473. norm.y = 0.5 - norm.y + 0.5;
  474. }
  475. // thanks to OpenGL spec using floor(layer + 0.5) for texture arrays,
  476. // it's easy to have precision errors using fract() to interpolate layers
  477. // as such, using fixed point to ensure it works.
  478. float index = p_roughness * RADIANCE_MAX_LOD;
  479. int indexi = int(index * 256.0);
  480. vec3 base = textureGrad(p_tex, vec3(norm.xy, float(indexi / 256)), dFdx(normg), dFdy(normg)).xyz;
  481. vec3 next = textureGrad(p_tex, vec3(norm.xy, float(indexi / 256 + 1)), dFdx(normg), dFdy(normg)).xyz;
  482. return mix(base, next, float(indexi % 256) / 256.0);
  483. }
  484. #else
  485. uniform sampler2D radiance_map; // texunit:-2
  486. vec3 textureDualParaboloid(sampler2D p_tex, vec3 p_vec, float p_roughness) {
  487. vec3 norm = normalize(p_vec);
  488. norm.xy /= 1.0 + abs(norm.z);
  489. norm.xy = norm.xy * vec2(0.5, 0.25) + vec2(0.5, 0.25);
  490. if (norm.z > 0.0) {
  491. norm.y = 0.5 - norm.y + 0.5;
  492. }
  493. return textureLod(p_tex, norm.xy, p_roughness * RADIANCE_MAX_LOD).xyz;
  494. }
  495. #endif
  496. #endif
  497. /* Material Uniforms */
  498. #if defined(USE_MATERIAL)
  499. /* clang-format off */
  500. layout(std140) uniform UniformData {
  501. MATERIAL_UNIFORMS
  502. };
  503. /* clang-format on */
  504. #endif
  505. /* clang-format off */
  506. FRAGMENT_SHADER_GLOBALS
  507. /* clang-format on */
  508. layout(std140) uniform SceneData {
  509. highp mat4 projection_matrix;
  510. highp mat4 inv_projection_matrix;
  511. highp mat4 camera_inverse_matrix;
  512. highp mat4 camera_matrix;
  513. mediump vec4 ambient_light_color;
  514. mediump vec4 bg_color;
  515. mediump vec4 fog_color_enabled;
  516. mediump vec4 fog_sun_color_amount;
  517. mediump float ambient_energy;
  518. mediump float bg_energy;
  519. mediump float z_offset;
  520. mediump float z_slope_scale;
  521. highp float shadow_dual_paraboloid_render_zfar;
  522. highp float shadow_dual_paraboloid_render_side;
  523. highp vec2 viewport_size;
  524. highp vec2 screen_pixel_size;
  525. highp vec2 shadow_atlas_pixel_size;
  526. highp vec2 directional_shadow_pixel_size;
  527. highp float time;
  528. highp float z_far;
  529. mediump float reflection_multiplier;
  530. mediump float subsurface_scatter_width;
  531. mediump float ambient_occlusion_affect_light;
  532. mediump float ambient_occlusion_affect_ao_channel;
  533. mediump float opaque_prepass_threshold;
  534. bool fog_depth_enabled;
  535. highp float fog_depth_begin;
  536. highp float fog_depth_end;
  537. mediump float fog_density;
  538. highp float fog_depth_curve;
  539. bool fog_transmit_enabled;
  540. highp float fog_transmit_curve;
  541. bool fog_height_enabled;
  542. highp float fog_height_min;
  543. highp float fog_height_max;
  544. highp float fog_height_curve;
  545. };
  546. //directional light data
  547. #ifdef USE_LIGHT_DIRECTIONAL
  548. layout(std140) uniform DirectionalLightData {
  549. highp vec4 light_pos_inv_radius;
  550. mediump vec4 light_direction_attenuation;
  551. mediump vec4 light_color_energy;
  552. mediump vec4 light_params; // cone attenuation, angle, specular, shadow enabled,
  553. mediump vec4 light_clamp;
  554. mediump vec4 shadow_color_contact;
  555. highp mat4 shadow_matrix1;
  556. highp mat4 shadow_matrix2;
  557. highp mat4 shadow_matrix3;
  558. highp mat4 shadow_matrix4;
  559. mediump vec4 shadow_split_offsets;
  560. };
  561. uniform highp sampler2DShadow directional_shadow; // texunit:-4
  562. #endif
  563. #ifdef USE_VERTEX_LIGHTING
  564. in vec4 diffuse_light_interp;
  565. in vec4 specular_light_interp;
  566. #endif
  567. // omni and spot
  568. struct LightData {
  569. highp vec4 light_pos_inv_radius;
  570. mediump vec4 light_direction_attenuation;
  571. mediump vec4 light_color_energy;
  572. mediump vec4 light_params; // cone attenuation, angle, specular, shadow enabled,
  573. mediump vec4 light_clamp;
  574. mediump vec4 shadow_color_contact;
  575. highp mat4 shadow_matrix;
  576. };
  577. layout(std140) uniform OmniLightData { // ubo:4
  578. LightData omni_lights[MAX_LIGHT_DATA_STRUCTS];
  579. };
  580. layout(std140) uniform SpotLightData { // ubo:5
  581. LightData spot_lights[MAX_LIGHT_DATA_STRUCTS];
  582. };
  583. uniform highp sampler2DShadow shadow_atlas; // texunit:-5
  584. struct ReflectionData {
  585. mediump vec4 box_extents;
  586. mediump vec4 box_offset;
  587. mediump vec4 params; // intensity, 0, interior , boxproject
  588. mediump vec4 ambient; // ambient color, energy
  589. mediump vec4 atlas_clamp;
  590. highp mat4 local_matrix; // up to here for spot and omni, rest is for directional
  591. // notes: for ambientblend, use distance to edge to blend between already existing global environment
  592. };
  593. layout(std140) uniform ReflectionProbeData { //ubo:6
  594. ReflectionData reflections[MAX_REFLECTION_DATA_STRUCTS];
  595. };
  596. uniform mediump sampler2D reflection_atlas; // texunit:-3
  597. #ifdef USE_FORWARD_LIGHTING
  598. uniform int omni_light_indices[MAX_FORWARD_LIGHTS];
  599. uniform int omni_light_count;
  600. uniform int spot_light_indices[MAX_FORWARD_LIGHTS];
  601. uniform int spot_light_count;
  602. uniform int reflection_indices[MAX_FORWARD_LIGHTS];
  603. uniform int reflection_count;
  604. #endif
  605. #if defined(SCREEN_TEXTURE_USED)
  606. uniform highp sampler2D screen_texture; // texunit:-7
  607. #endif
  608. #ifdef USE_MULTIPLE_RENDER_TARGETS
  609. layout(location = 0) out vec4 diffuse_buffer;
  610. layout(location = 1) out vec4 specular_buffer;
  611. layout(location = 2) out vec4 normal_mr_buffer;
  612. #if defined(ENABLE_SSS)
  613. layout(location = 3) out float sss_buffer;
  614. #endif
  615. #else
  616. layout(location = 0) out vec4 frag_color;
  617. #endif
  618. in highp vec4 position_interp;
  619. uniform highp sampler2D depth_buffer; // texunit:-8
  620. #ifdef USE_CONTACT_SHADOWS
  621. float contact_shadow_compute(vec3 pos, vec3 dir, float max_distance) {
  622. if (abs(dir.z) > 0.99)
  623. return 1.0;
  624. vec3 endpoint = pos + dir * max_distance;
  625. vec4 source = position_interp;
  626. vec4 dest = projection_matrix * vec4(endpoint, 1.0);
  627. vec2 from_screen = (source.xy / source.w) * 0.5 + 0.5;
  628. vec2 to_screen = (dest.xy / dest.w) * 0.5 + 0.5;
  629. vec2 screen_rel = to_screen - from_screen;
  630. if (length(screen_rel) < 0.00001)
  631. return 1.0; // too small, don't do anything
  632. /*
  633. float pixel_size; // approximate pixel size
  634. if (screen_rel.x > screen_rel.y) {
  635. pixel_size = abs((pos.x - endpoint.x) / (screen_rel.x / screen_pixel_size.x));
  636. } else {
  637. pixel_size = abs((pos.y - endpoint.y) / (screen_rel.y / screen_pixel_size.y));
  638. }
  639. */
  640. vec4 bias = projection_matrix * vec4(pos + vec3(0.0, 0.0, max_distance * 0.5), 1.0);
  641. vec2 pixel_incr = normalize(screen_rel) * screen_pixel_size;
  642. float steps = length(screen_rel) / length(pixel_incr);
  643. steps = min(2000.0, steps); // put a limit to avoid freezing in some strange situation
  644. //steps = 10.0;
  645. vec4 incr = (dest - source) / steps;
  646. float ratio = 0.0;
  647. float ratio_incr = 1.0 / steps;
  648. while (steps > 0.0) {
  649. source += incr * 2.0;
  650. bias += incr * 2.0;
  651. vec3 uv_depth = (source.xyz / source.w) * 0.5 + 0.5;
  652. float depth = texture(depth_buffer, uv_depth.xy).r;
  653. if (depth < uv_depth.z) {
  654. if (depth > (bias.z / bias.w) * 0.5 + 0.5) {
  655. return min(pow(ratio, 4.0), 1.0);
  656. } else {
  657. return 1.0;
  658. }
  659. }
  660. ratio += ratio_incr;
  661. steps -= 1.0;
  662. }
  663. return 1.0;
  664. }
  665. #endif
  666. // This returns the G_GGX function divided by 2 cos_theta_m, where in practice cos_theta_m is either N.L or N.V.
  667. // We're dividing this factor off because the overall term we'll end up looks like
  668. // (see, for example, the first unnumbered equation in B. Burley, "Physically Based Shading at Disney", SIGGRAPH 2012):
  669. //
  670. // F(L.V) D(N.H) G(N.L) G(N.V) / (4 N.L N.V)
  671. //
  672. // We're basically regouping this as
  673. //
  674. // F(L.V) D(N.H) [G(N.L)/(2 N.L)] [G(N.V) / (2 N.V)]
  675. //
  676. // and thus, this function implements the [G(N.m)/(2 N.m)] part with m = L or V.
  677. //
  678. // The contents of the D and G (G1) functions (GGX) are taken from
  679. // E. Heitz, "Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs", J. Comp. Graph. Tech. 3 (2) (2014).
  680. // Eqns 71-72 and 85-86 (see also Eqns 43 and 80).
  681. float G_GGX_2cos(float cos_theta_m, float alpha) {
  682. // Schlick's approximation
  683. // C. Schlick, "An Inexpensive BRDF Model for Physically-based Rendering", Computer Graphics Forum. 13 (3): 233 (1994)
  684. // Eq. (19), although see Heitz (2014) the about the problems with his derivation.
  685. // It nevertheless approximates GGX well with k = alpha/2.
  686. float k = 0.5 * alpha;
  687. return 0.5 / (cos_theta_m * (1.0 - k) + k);
  688. // float cos2 = cos_theta_m * cos_theta_m;
  689. // float sin2 = (1.0 - cos2);
  690. // return 1.0 / (cos_theta_m + sqrt(cos2 + alpha * alpha * sin2));
  691. }
  692. float D_GGX(float cos_theta_m, float alpha) {
  693. float alpha2 = alpha * alpha;
  694. float d = 1.0 + (alpha2 - 1.0) * cos_theta_m * cos_theta_m;
  695. return alpha2 / (M_PI * d * d);
  696. }
  697. float G_GGX_anisotropic_2cos(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) {
  698. float cos2 = cos_theta_m * cos_theta_m;
  699. float sin2 = (1.0 - cos2);
  700. float s_x = alpha_x * cos_phi;
  701. float s_y = alpha_y * sin_phi;
  702. return 1.0 / max(cos_theta_m + sqrt(cos2 + (s_x * s_x + s_y * s_y) * sin2), 0.001);
  703. }
  704. float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) {
  705. float cos2 = cos_theta_m * cos_theta_m;
  706. float sin2 = (1.0 - cos2);
  707. float r_x = cos_phi / alpha_x;
  708. float r_y = sin_phi / alpha_y;
  709. float d = cos2 + sin2 * (r_x * r_x + r_y * r_y);
  710. return 1.0 / max(M_PI * alpha_x * alpha_y * d * d, 0.001);
  711. }
  712. float SchlickFresnel(float u) {
  713. float m = 1.0 - u;
  714. float m2 = m * m;
  715. return m2 * m2 * m; // pow(m,5)
  716. }
  717. float GTR1(float NdotH, float a) {
  718. if (a >= 1.0) return 1.0 / M_PI;
  719. float a2 = a * a;
  720. float t = 1.0 + (a2 - 1.0) * NdotH * NdotH;
  721. return (a2 - 1.0) / (M_PI * log(a2) * t);
  722. }
  723. vec3 F0(float metallic, float specular, vec3 albedo) {
  724. float dielectric = 0.16 * specular * specular;
  725. // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials;
  726. // see https://google.github.io/filament/Filament.md.html
  727. return mix(vec3(dielectric), albedo, vec3(metallic));
  728. }
  729. void light_compute(vec3 N, vec3 L, vec3 V, vec3 B, vec3 T, vec3 light_color, vec3 attenuation, vec3 diffuse_color, vec3 transmission, float specular_blob_intensity, float roughness, float metallic, float specular, float rim, float rim_tint, float clearcoat, float clearcoat_gloss, float anisotropy, inout vec3 diffuse_light, inout vec3 specular_light) {
  730. #if defined(USE_LIGHT_SHADER_CODE)
  731. // light is written by the light shader
  732. vec3 normal = N;
  733. vec3 albedo = diffuse_color;
  734. vec3 light = L;
  735. vec3 view = V;
  736. /* clang-format off */
  737. LIGHT_SHADER_CODE
  738. /* clang-format on */
  739. #else
  740. float NdotL = dot(N, L);
  741. float cNdotL = max(NdotL, 0.0); // clamped NdotL
  742. float NdotV = dot(N, V);
  743. float cNdotV = max(NdotV, 0.0);
  744. #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_BLINN) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_USE_CLEARCOAT)
  745. vec3 H = normalize(V + L);
  746. #endif
  747. #if defined(SPECULAR_BLINN) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_USE_CLEARCOAT)
  748. float cNdotH = max(dot(N, H), 0.0);
  749. #endif
  750. #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_USE_CLEARCOAT)
  751. float cLdotH = max(dot(L, H), 0.0);
  752. #endif
  753. if (metallic < 1.0) {
  754. #if defined(DIFFUSE_OREN_NAYAR)
  755. vec3 diffuse_brdf_NL;
  756. #else
  757. float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance
  758. #endif
  759. #if defined(DIFFUSE_LAMBERT_WRAP)
  760. // energy conserving lambert wrap shader
  761. diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness)));
  762. #elif defined(DIFFUSE_OREN_NAYAR)
  763. {
  764. // see http://mimosa-pudica.net/improved-oren-nayar.html
  765. float LdotV = dot(L, V);
  766. float s = LdotV - NdotL * NdotV;
  767. float t = mix(1.0, max(NdotL, NdotV), step(0.0, s));
  768. float sigma2 = roughness * roughness; // TODO: this needs checking
  769. vec3 A = 1.0 + sigma2 * (-0.5 / (sigma2 + 0.33) + 0.17 * diffuse_color / (sigma2 + 0.13));
  770. float B = 0.45 * sigma2 / (sigma2 + 0.09);
  771. diffuse_brdf_NL = cNdotL * (A + vec3(B) * s / t) * (1.0 / M_PI);
  772. }
  773. #elif defined(DIFFUSE_TOON)
  774. diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL);
  775. #elif defined(DIFFUSE_BURLEY)
  776. {
  777. float FD90_minus_1 = 2.0 * cLdotH * cLdotH * roughness - 0.5;
  778. float FdV = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotV);
  779. float FdL = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotL);
  780. diffuse_brdf_NL = (1.0 / M_PI) * FdV * FdL * cNdotL;
  781. /*
  782. float energyBias = mix(roughness, 0.0, 0.5);
  783. float energyFactor = mix(roughness, 1.0, 1.0 / 1.51);
  784. float fd90 = energyBias + 2.0 * VoH * VoH * roughness;
  785. float f0 = 1.0;
  786. float lightScatter = f0 + (fd90 - f0) * pow(1.0 - cNdotL, 5.0);
  787. float viewScatter = f0 + (fd90 - f0) * pow(1.0 - cNdotV, 5.0);
  788. diffuse_brdf_NL = lightScatter * viewScatter * energyFactor;
  789. */
  790. }
  791. #else
  792. // lambert
  793. diffuse_brdf_NL = cNdotL * (1.0 / M_PI);
  794. #endif
  795. diffuse_light += light_color * diffuse_color * diffuse_brdf_NL * attenuation;
  796. #if defined(TRANSMISSION_USED)
  797. diffuse_light += light_color * diffuse_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * transmission * attenuation;
  798. #endif
  799. #if defined(LIGHT_USE_RIM)
  800. float rim_light = pow(max(0.0, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0));
  801. diffuse_light += rim_light * rim * mix(vec3(1.0), diffuse_color, rim_tint) * light_color;
  802. #endif
  803. }
  804. if (roughness > 0.0) { // FIXME: roughness == 0 should not disable specular light entirely
  805. // D
  806. #if defined(SPECULAR_BLINN)
  807. //normalized blinn
  808. float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25;
  809. float blinn = pow(cNdotH, shininess);
  810. blinn *= (shininess + 8.0) * (1.0 / (8.0 * M_PI));
  811. float intensity = (blinn) / max(4.0 * cNdotV * cNdotL, 0.75);
  812. specular_light += light_color * intensity * specular_blob_intensity * attenuation;
  813. #elif defined(SPECULAR_PHONG)
  814. vec3 R = normalize(-reflect(L, N));
  815. float cRdotV = max(0.0, dot(R, V));
  816. float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25;
  817. float phong = pow(cRdotV, shininess);
  818. phong *= (shininess + 8.0) * (1.0 / (8.0 * M_PI));
  819. float intensity = (phong) / max(4.0 * cNdotV * cNdotL, 0.75);
  820. specular_light += light_color * intensity * specular_blob_intensity * attenuation;
  821. #elif defined(SPECULAR_TOON)
  822. vec3 R = normalize(-reflect(L, N));
  823. float RdotV = dot(R, V);
  824. float mid = 1.0 - roughness;
  825. mid *= mid;
  826. float intensity = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid;
  827. diffuse_light += light_color * intensity * specular_blob_intensity * attenuation; // write to diffuse_light, as in toon shading you generally want no reflection
  828. #elif defined(SPECULAR_DISABLED)
  829. // none..
  830. #elif defined(SPECULAR_SCHLICK_GGX)
  831. // shlick+ggx as default
  832. #if defined(LIGHT_USE_ANISOTROPY)
  833. float alpha = roughness * roughness;
  834. float aspect = sqrt(1.0 - anisotropy * 0.9);
  835. float ax = alpha / aspect;
  836. float ay = alpha * aspect;
  837. float XdotH = dot(T, H);
  838. float YdotH = dot(B, H);
  839. float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH);
  840. float G = G_GGX_anisotropic_2cos(cNdotL, ax, ay, XdotH, YdotH) * G_GGX_anisotropic_2cos(cNdotV, ax, ay, XdotH, YdotH);
  841. #else
  842. float alpha = roughness * roughness;
  843. float D = D_GGX(cNdotH, alpha);
  844. float G = G_GGX_2cos(cNdotL, alpha) * G_GGX_2cos(cNdotV, alpha);
  845. #endif
  846. // F
  847. vec3 f0 = F0(metallic, specular, diffuse_color);
  848. float cLdotH5 = SchlickFresnel(cLdotH);
  849. vec3 F = mix(vec3(cLdotH5), vec3(1.0), f0);
  850. vec3 specular_brdf_NL = cNdotL * D * F * G;
  851. specular_light += specular_brdf_NL * light_color * specular_blob_intensity * attenuation;
  852. #endif
  853. #if defined(LIGHT_USE_CLEARCOAT)
  854. #if !defined(SPECULAR_SCHLICK_GGX)
  855. float cLdotH5 = SchlickFresnel(cLdotH);
  856. #endif
  857. float Dr = GTR1(cNdotH, mix(.1, .001, clearcoat_gloss));
  858. float Fr = mix(.04, 1.0, cLdotH5);
  859. float Gr = G_GGX_2cos(cNdotL, .25) * G_GGX_2cos(cNdotV, .25);
  860. float clearcoat_specular_brdf_NL = 0.25 * clearcoat * Gr * Fr * Dr * cNdotL;
  861. specular_light += clearcoat_specular_brdf_NL * light_color * specular_blob_intensity * attenuation;
  862. #endif
  863. }
  864. #endif //defined(USE_LIGHT_SHADER_CODE)
  865. }
  866. float sample_shadow(highp sampler2DShadow shadow, vec2 shadow_pixel_size, vec2 pos, float depth, vec4 clamp_rect) {
  867. #ifdef SHADOW_MODE_PCF_13
  868. float avg = textureProj(shadow, vec4(pos, depth, 1.0));
  869. avg += textureProj(shadow, vec4(pos + vec2(shadow_pixel_size.x, 0.0), depth, 1.0));
  870. avg += textureProj(shadow, vec4(pos + vec2(-shadow_pixel_size.x, 0.0), depth, 1.0));
  871. avg += textureProj(shadow, vec4(pos + vec2(0.0, shadow_pixel_size.y), depth, 1.0));
  872. avg += textureProj(shadow, vec4(pos + vec2(0.0, -shadow_pixel_size.y), depth, 1.0));
  873. avg += textureProj(shadow, vec4(pos + vec2(shadow_pixel_size.x, shadow_pixel_size.y), depth, 1.0));
  874. avg += textureProj(shadow, vec4(pos + vec2(-shadow_pixel_size.x, shadow_pixel_size.y), depth, 1.0));
  875. avg += textureProj(shadow, vec4(pos + vec2(shadow_pixel_size.x, -shadow_pixel_size.y), depth, 1.0));
  876. avg += textureProj(shadow, vec4(pos + vec2(-shadow_pixel_size.x, -shadow_pixel_size.y), depth, 1.0));
  877. avg += textureProj(shadow, vec4(pos + vec2(shadow_pixel_size.x * 2.0, 0.0), depth, 1.0));
  878. avg += textureProj(shadow, vec4(pos + vec2(-shadow_pixel_size.x * 2.0, 0.0), depth, 1.0));
  879. avg += textureProj(shadow, vec4(pos + vec2(0.0, shadow_pixel_size.y * 2.0), depth, 1.0));
  880. avg += textureProj(shadow, vec4(pos + vec2(0.0, -shadow_pixel_size.y * 2.0), depth, 1.0));
  881. return avg * (1.0 / 13.0);
  882. #endif
  883. #ifdef SHADOW_MODE_PCF_5
  884. float avg = textureProj(shadow, vec4(pos, depth, 1.0));
  885. avg += textureProj(shadow, vec4(pos + vec2(shadow_pixel_size.x, 0.0), depth, 1.0));
  886. avg += textureProj(shadow, vec4(pos + vec2(-shadow_pixel_size.x, 0.0), depth, 1.0));
  887. avg += textureProj(shadow, vec4(pos + vec2(0.0, shadow_pixel_size.y), depth, 1.0));
  888. avg += textureProj(shadow, vec4(pos + vec2(0.0, -shadow_pixel_size.y), depth, 1.0));
  889. return avg * (1.0 / 5.0);
  890. #endif
  891. #if !defined(SHADOW_MODE_PCF_5) || !defined(SHADOW_MODE_PCF_13)
  892. return textureProj(shadow, vec4(pos, depth, 1.0));
  893. #endif
  894. }
  895. #ifdef RENDER_DEPTH_DUAL_PARABOLOID
  896. in highp float dp_clip;
  897. #endif
  898. #if 0
  899. // need to save texture depth for this
  900. vec3 light_transmittance(float translucency,vec3 light_vec, vec3 normal, vec3 pos, float distance) {
  901. float scale = 8.25 * (1.0 - translucency) / subsurface_scatter_width;
  902. float d = scale * distance;
  903. /**
  904. * Armed with the thickness, we can now calculate the color by means of the
  905. * precalculated transmittance profile.
  906. * (It can be precomputed into a texture, for maximum performance):
  907. */
  908. float dd = -d * d;
  909. vec3 profile =
  910. vec3(0.233, 0.455, 0.649) * exp(dd / 0.0064) +
  911. vec3(0.1, 0.336, 0.344) * exp(dd / 0.0484) +
  912. vec3(0.118, 0.198, 0.0) * exp(dd / 0.187) +
  913. vec3(0.113, 0.007, 0.007) * exp(dd / 0.567) +
  914. vec3(0.358, 0.004, 0.0) * exp(dd / 1.99) +
  915. vec3(0.078, 0.0, 0.0) * exp(dd / 7.41);
  916. /**
  917. * Using the profile, we finally approximate the transmitted lighting from
  918. * the back of the object:
  919. */
  920. return profile * clamp(0.3 + dot(light_vec, normal),0.0,1.0);
  921. }
  922. #endif
  923. void light_process_omni(int idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 binormal, vec3 tangent, vec3 albedo, vec3 transmission, float roughness, float metallic, float specular, float rim, float rim_tint, float clearcoat, float clearcoat_gloss, float anisotropy, float p_blob_intensity, inout vec3 diffuse_light, inout vec3 specular_light) {
  924. vec3 light_rel_vec = omni_lights[idx].light_pos_inv_radius.xyz - vertex;
  925. float light_length = length(light_rel_vec);
  926. float normalized_distance = light_length * omni_lights[idx].light_pos_inv_radius.w;
  927. float omni_attenuation = pow(max(1.0 - normalized_distance, 0.0), omni_lights[idx].light_direction_attenuation.w);
  928. vec3 light_attenuation = vec3(omni_attenuation);
  929. #if !defined(SHADOWS_DISABLED)
  930. if (omni_lights[idx].light_params.w > 0.5) {
  931. // there is a shadowmap
  932. highp vec3 splane = (omni_lights[idx].shadow_matrix * vec4(vertex, 1.0)).xyz;
  933. float shadow_len = length(splane);
  934. splane = normalize(splane);
  935. vec4 clamp_rect = omni_lights[idx].light_clamp;
  936. if (splane.z >= 0.0) {
  937. splane.z += 1.0;
  938. clamp_rect.y += clamp_rect.w;
  939. } else {
  940. splane.z = 1.0 - splane.z;
  941. /*
  942. if (clamp_rect.z < clamp_rect.w) {
  943. clamp_rect.x += clamp_rect.z;
  944. } else {
  945. clamp_rect.y += clamp_rect.w;
  946. }
  947. */
  948. }
  949. splane.xy /= splane.z;
  950. splane.xy = splane.xy * 0.5 + 0.5;
  951. splane.z = shadow_len * omni_lights[idx].light_pos_inv_radius.w;
  952. splane.xy = clamp_rect.xy + splane.xy * clamp_rect.zw;
  953. float shadow = sample_shadow(shadow_atlas, shadow_atlas_pixel_size, splane.xy, splane.z, clamp_rect);
  954. #ifdef USE_CONTACT_SHADOWS
  955. if (shadow > 0.01 && omni_lights[idx].shadow_color_contact.a > 0.0) {
  956. float contact_shadow = contact_shadow_compute(vertex, normalize(light_rel_vec), min(light_length, omni_lights[idx].shadow_color_contact.a));
  957. shadow = min(shadow, contact_shadow);
  958. }
  959. #endif
  960. light_attenuation *= mix(omni_lights[idx].shadow_color_contact.rgb, vec3(1.0), shadow);
  961. }
  962. #endif //SHADOWS_DISABLED
  963. light_compute(normal, normalize(light_rel_vec), eye_vec, binormal, tangent, omni_lights[idx].light_color_energy.rgb, light_attenuation, albedo, transmission, omni_lights[idx].light_params.z * p_blob_intensity, roughness, metallic, specular, rim * omni_attenuation, rim_tint, clearcoat, clearcoat_gloss, anisotropy, diffuse_light, specular_light);
  964. }
  965. void light_process_spot(int idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 binormal, vec3 tangent, vec3 albedo, vec3 transmission, float roughness, float metallic, float specular, float rim, float rim_tint, float clearcoat, float clearcoat_gloss, float anisotropy, float p_blob_intensity, inout vec3 diffuse_light, inout vec3 specular_light) {
  966. vec3 light_rel_vec = spot_lights[idx].light_pos_inv_radius.xyz - vertex;
  967. float light_length = length(light_rel_vec);
  968. float normalized_distance = light_length * spot_lights[idx].light_pos_inv_radius.w;
  969. float spot_attenuation = pow(max(1.0 - normalized_distance, 0.001), spot_lights[idx].light_direction_attenuation.w);
  970. vec3 spot_dir = spot_lights[idx].light_direction_attenuation.xyz;
  971. float spot_cutoff = spot_lights[idx].light_params.y;
  972. float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_cutoff);
  973. float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_cutoff));
  974. spot_attenuation *= 1.0 - pow(spot_rim, spot_lights[idx].light_params.x);
  975. vec3 light_attenuation = vec3(spot_attenuation);
  976. #if !defined(SHADOWS_DISABLED)
  977. if (spot_lights[idx].light_params.w > 0.5) {
  978. //there is a shadowmap
  979. highp vec4 splane = (spot_lights[idx].shadow_matrix * vec4(vertex, 1.0));
  980. splane.xyz /= splane.w;
  981. float shadow = sample_shadow(shadow_atlas, shadow_atlas_pixel_size, splane.xy, splane.z, spot_lights[idx].light_clamp);
  982. #ifdef USE_CONTACT_SHADOWS
  983. if (shadow > 0.01 && spot_lights[idx].shadow_color_contact.a > 0.0) {
  984. float contact_shadow = contact_shadow_compute(vertex, normalize(light_rel_vec), min(light_length, spot_lights[idx].shadow_color_contact.a));
  985. shadow = min(shadow, contact_shadow);
  986. }
  987. #endif
  988. light_attenuation *= mix(spot_lights[idx].shadow_color_contact.rgb, vec3(1.0), shadow);
  989. }
  990. #endif //SHADOWS_DISABLED
  991. light_compute(normal, normalize(light_rel_vec), eye_vec, binormal, tangent, spot_lights[idx].light_color_energy.rgb, light_attenuation, albedo, transmission, spot_lights[idx].light_params.z * p_blob_intensity, roughness, metallic, specular, rim * spot_attenuation, rim_tint, clearcoat, clearcoat_gloss, anisotropy, diffuse_light, specular_light);
  992. }
  993. void reflection_process(int idx, vec3 vertex, vec3 normal, vec3 binormal, vec3 tangent, float roughness, float anisotropy, vec3 ambient, vec3 skybox, inout highp vec4 reflection_accum, inout highp vec4 ambient_accum) {
  994. vec3 ref_vec = normalize(reflect(vertex, normal));
  995. vec3 local_pos = (reflections[idx].local_matrix * vec4(vertex, 1.0)).xyz;
  996. vec3 box_extents = reflections[idx].box_extents.xyz;
  997. if (any(greaterThan(abs(local_pos), box_extents))) { //out of the reflection box
  998. return;
  999. }
  1000. vec3 inner_pos = abs(local_pos / box_extents);
  1001. float blend = max(inner_pos.x, max(inner_pos.y, inner_pos.z));
  1002. //make blend more rounded
  1003. blend = mix(length(inner_pos), blend, blend);
  1004. blend *= blend;
  1005. blend = max(0.0, 1.0 - blend);
  1006. if (reflections[idx].params.x > 0.0) { // compute reflection
  1007. vec3 local_ref_vec = (reflections[idx].local_matrix * vec4(ref_vec, 0.0)).xyz;
  1008. if (reflections[idx].params.w > 0.5) { //box project
  1009. vec3 nrdir = normalize(local_ref_vec);
  1010. vec3 rbmax = (box_extents - local_pos) / nrdir;
  1011. vec3 rbmin = (-box_extents - local_pos) / nrdir;
  1012. vec3 rbminmax = mix(rbmin, rbmax, greaterThan(nrdir, vec3(0.0, 0.0, 0.0)));
  1013. float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z);
  1014. vec3 posonbox = local_pos + nrdir * fa;
  1015. local_ref_vec = posonbox - reflections[idx].box_offset.xyz;
  1016. }
  1017. vec4 clamp_rect = reflections[idx].atlas_clamp;
  1018. vec3 norm = normalize(local_ref_vec);
  1019. norm.xy /= 1.0 + abs(norm.z);
  1020. norm.xy = norm.xy * vec2(0.5, 0.25) + vec2(0.5, 0.25);
  1021. if (norm.z > 0.0) {
  1022. norm.y = 0.5 - norm.y + 0.5;
  1023. }
  1024. vec2 atlas_uv = norm.xy * clamp_rect.zw + clamp_rect.xy;
  1025. atlas_uv = clamp(atlas_uv, clamp_rect.xy, clamp_rect.xy + clamp_rect.zw);
  1026. highp vec4 reflection;
  1027. reflection.rgb = textureLod(reflection_atlas, atlas_uv, roughness * 5.0).rgb;
  1028. if (reflections[idx].params.z < 0.5) {
  1029. reflection.rgb = mix(skybox, reflection.rgb, blend);
  1030. }
  1031. reflection.rgb *= reflections[idx].params.x;
  1032. reflection.a = blend;
  1033. reflection.rgb *= reflection.a;
  1034. reflection_accum += reflection;
  1035. }
  1036. #if !defined(USE_LIGHTMAP) && !defined(USE_LIGHTMAP_CAPTURE)
  1037. if (reflections[idx].ambient.a > 0.0) { //compute ambient using skybox
  1038. vec3 local_amb_vec = (reflections[idx].local_matrix * vec4(normal, 0.0)).xyz;
  1039. vec3 splane = normalize(local_amb_vec);
  1040. vec4 clamp_rect = reflections[idx].atlas_clamp;
  1041. splane.z *= -1.0;
  1042. if (splane.z >= 0.0) {
  1043. splane.z += 1.0;
  1044. clamp_rect.y += clamp_rect.w;
  1045. } else {
  1046. splane.z = 1.0 - splane.z;
  1047. splane.y = -splane.y;
  1048. }
  1049. splane.xy /= splane.z;
  1050. splane.xy = splane.xy * 0.5 + 0.5;
  1051. splane.xy = splane.xy * clamp_rect.zw + clamp_rect.xy;
  1052. splane.xy = clamp(splane.xy, clamp_rect.xy, clamp_rect.xy + clamp_rect.zw);
  1053. highp vec4 ambient_out;
  1054. ambient_out.a = blend;
  1055. ambient_out.rgb = textureLod(reflection_atlas, splane.xy, 5.0).rgb;
  1056. ambient_out.rgb = mix(reflections[idx].ambient.rgb, ambient_out.rgb, reflections[idx].ambient.a);
  1057. if (reflections[idx].params.z < 0.5) {
  1058. ambient_out.rgb = mix(ambient, ambient_out.rgb, blend);
  1059. }
  1060. ambient_out.rgb *= ambient_out.a;
  1061. ambient_accum += ambient_out;
  1062. } else {
  1063. highp vec4 ambient_out;
  1064. ambient_out.a = blend;
  1065. ambient_out.rgb = reflections[idx].ambient.rgb;
  1066. if (reflections[idx].params.z < 0.5) {
  1067. ambient_out.rgb = mix(ambient, ambient_out.rgb, blend);
  1068. }
  1069. ambient_out.rgb *= ambient_out.a;
  1070. ambient_accum += ambient_out;
  1071. }
  1072. #endif
  1073. }
  1074. #ifdef USE_LIGHTMAP
  1075. uniform mediump sampler2D lightmap; //texunit:-9
  1076. uniform mediump float lightmap_energy;
  1077. #endif
  1078. #ifdef USE_LIGHTMAP_CAPTURE
  1079. uniform mediump vec4[12] lightmap_captures;
  1080. uniform bool lightmap_capture_sky;
  1081. #endif
  1082. #ifdef USE_GI_PROBES
  1083. uniform mediump sampler3D gi_probe1; //texunit:-9
  1084. uniform highp mat4 gi_probe_xform1;
  1085. uniform highp vec3 gi_probe_bounds1;
  1086. uniform highp vec3 gi_probe_cell_size1;
  1087. uniform highp float gi_probe_multiplier1;
  1088. uniform highp float gi_probe_bias1;
  1089. uniform highp float gi_probe_normal_bias1;
  1090. uniform bool gi_probe_blend_ambient1;
  1091. uniform mediump sampler3D gi_probe2; //texunit:-10
  1092. uniform highp mat4 gi_probe_xform2;
  1093. uniform highp vec3 gi_probe_bounds2;
  1094. uniform highp vec3 gi_probe_cell_size2;
  1095. uniform highp float gi_probe_multiplier2;
  1096. uniform highp float gi_probe_bias2;
  1097. uniform highp float gi_probe_normal_bias2;
  1098. uniform bool gi_probe2_enabled;
  1099. uniform bool gi_probe_blend_ambient2;
  1100. vec3 voxel_cone_trace(mediump sampler3D probe, vec3 cell_size, vec3 pos, vec3 ambient, bool blend_ambient, vec3 direction, float tan_half_angle, float max_distance, float p_bias) {
  1101. float dist = p_bias; //1.0; //dot(direction,mix(vec3(-1.0),vec3(1.0),greaterThan(direction,vec3(0.0))))*2.0;
  1102. float alpha = 0.0;
  1103. vec3 color = vec3(0.0);
  1104. while (dist < max_distance && alpha < 0.95) {
  1105. float diameter = max(1.0, 2.0 * tan_half_angle * dist);
  1106. vec4 scolor = textureLod(probe, (pos + dist * direction) * cell_size, log2(diameter));
  1107. float a = (1.0 - alpha);
  1108. color += scolor.rgb * a;
  1109. alpha += a * scolor.a;
  1110. dist += diameter * 0.5;
  1111. }
  1112. if (blend_ambient) {
  1113. color.rgb = mix(ambient, color.rgb, min(1.0, alpha / 0.95));
  1114. }
  1115. return color;
  1116. }
  1117. void gi_probe_compute(mediump sampler3D probe, mat4 probe_xform, vec3 bounds, vec3 cell_size, vec3 pos, vec3 ambient, vec3 environment, bool blend_ambient, float multiplier, mat3 normal_mtx, vec3 ref_vec, float roughness, float p_bias, float p_normal_bias, inout vec4 out_spec, inout vec4 out_diff) {
  1118. vec3 probe_pos = (probe_xform * vec4(pos, 1.0)).xyz;
  1119. vec3 ref_pos = (probe_xform * vec4(pos + ref_vec, 1.0)).xyz;
  1120. ref_vec = normalize(ref_pos - probe_pos);
  1121. probe_pos += (probe_xform * vec4(normal_mtx[2], 0.0)).xyz * p_normal_bias;
  1122. /* out_diff.rgb = voxel_cone_trace(probe,cell_size,probe_pos,normalize((probe_xform * vec4(ref_vec,0.0)).xyz),0.0 ,100.0);
  1123. out_diff.a = 1.0;
  1124. return;*/
  1125. //out_diff = vec4(textureLod(probe,probe_pos*cell_size,3.0).rgb,1.0);
  1126. //return;
  1127. //this causes corrupted pixels, i have no idea why..
  1128. if (any(bvec2(any(lessThan(probe_pos, vec3(0.0))), any(greaterThan(probe_pos, bounds))))) {
  1129. return;
  1130. }
  1131. vec3 blendv = abs(probe_pos / bounds * 2.0 - 1.0);
  1132. float blend = clamp(1.0 - max(blendv.x, max(blendv.y, blendv.z)), 0.0, 1.0);
  1133. //float blend=1.0;
  1134. float max_distance = length(bounds);
  1135. //radiance
  1136. #ifdef VCT_QUALITY_HIGH
  1137. #define MAX_CONE_DIRS 6
  1138. vec3 cone_dirs[MAX_CONE_DIRS] = vec3[](
  1139. vec3(0.0, 0.0, 1.0),
  1140. vec3(0.866025, 0.0, 0.5),
  1141. vec3(0.267617, 0.823639, 0.5),
  1142. vec3(-0.700629, 0.509037, 0.5),
  1143. vec3(-0.700629, -0.509037, 0.5),
  1144. vec3(0.267617, -0.823639, 0.5));
  1145. float cone_weights[MAX_CONE_DIRS] = float[](0.25, 0.15, 0.15, 0.15, 0.15, 0.15);
  1146. float cone_angle_tan = 0.577;
  1147. float min_ref_tan = 0.0;
  1148. #else
  1149. #define MAX_CONE_DIRS 4
  1150. vec3 cone_dirs[MAX_CONE_DIRS] = vec3[](
  1151. vec3(0.707107, 0.0, 0.707107),
  1152. vec3(0.0, 0.707107, 0.707107),
  1153. vec3(-0.707107, 0.0, 0.707107),
  1154. vec3(0.0, -0.707107, 0.707107));
  1155. float cone_weights[MAX_CONE_DIRS] = float[](0.25, 0.25, 0.25, 0.25);
  1156. float cone_angle_tan = 0.98269;
  1157. max_distance *= 0.5;
  1158. float min_ref_tan = 0.2;
  1159. #endif
  1160. vec3 light = vec3(0.0);
  1161. for (int i = 0; i < MAX_CONE_DIRS; i++) {
  1162. vec3 dir = normalize((probe_xform * vec4(pos + normal_mtx * cone_dirs[i], 1.0)).xyz - probe_pos);
  1163. light += cone_weights[i] * voxel_cone_trace(probe, cell_size, probe_pos, ambient, blend_ambient, dir, cone_angle_tan, max_distance, p_bias);
  1164. }
  1165. light *= multiplier;
  1166. out_diff += vec4(light * blend, blend);
  1167. //irradiance
  1168. vec3 irr_light = voxel_cone_trace(probe, cell_size, probe_pos, environment, blend_ambient, ref_vec, max(min_ref_tan, tan(roughness * 0.5 * M_PI * 0.99)), max_distance, p_bias);
  1169. irr_light *= multiplier;
  1170. //irr_light=vec3(0.0);
  1171. out_spec += vec4(irr_light * blend, blend);
  1172. }
  1173. void gi_probes_compute(vec3 pos, vec3 normal, float roughness, inout vec3 out_specular, inout vec3 out_ambient) {
  1174. roughness = roughness * roughness;
  1175. vec3 ref_vec = normalize(reflect(normalize(pos), normal));
  1176. //find arbitrary tangent and bitangent, then build a matrix
  1177. vec3 v0 = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);
  1178. vec3 tangent = normalize(cross(v0, normal));
  1179. vec3 bitangent = normalize(cross(tangent, normal));
  1180. mat3 normal_mat = mat3(tangent, bitangent, normal);
  1181. vec4 diff_accum = vec4(0.0);
  1182. vec4 spec_accum = vec4(0.0);
  1183. vec3 ambient = out_ambient;
  1184. out_ambient = vec3(0.0);
  1185. vec3 environment = out_specular;
  1186. out_specular = vec3(0.0);
  1187. gi_probe_compute(gi_probe1, gi_probe_xform1, gi_probe_bounds1, gi_probe_cell_size1, pos, ambient, environment, gi_probe_blend_ambient1, gi_probe_multiplier1, normal_mat, ref_vec, roughness, gi_probe_bias1, gi_probe_normal_bias1, spec_accum, diff_accum);
  1188. if (gi_probe2_enabled) {
  1189. gi_probe_compute(gi_probe2, gi_probe_xform2, gi_probe_bounds2, gi_probe_cell_size2, pos, ambient, environment, gi_probe_blend_ambient2, gi_probe_multiplier2, normal_mat, ref_vec, roughness, gi_probe_bias2, gi_probe_normal_bias2, spec_accum, diff_accum);
  1190. }
  1191. if (diff_accum.a > 0.0) {
  1192. diff_accum.rgb /= diff_accum.a;
  1193. }
  1194. if (spec_accum.a > 0.0) {
  1195. spec_accum.rgb /= spec_accum.a;
  1196. }
  1197. out_specular += spec_accum.rgb;
  1198. out_ambient += diff_accum.rgb;
  1199. }
  1200. #endif
  1201. void main() {
  1202. #ifdef RENDER_DEPTH_DUAL_PARABOLOID
  1203. if (dp_clip > 0.0)
  1204. discard;
  1205. #endif
  1206. //lay out everything, whathever is unused is optimized away anyway
  1207. highp vec3 vertex = vertex_interp;
  1208. vec3 view = -normalize(vertex_interp);
  1209. vec3 albedo = vec3(1.0);
  1210. vec3 transmission = vec3(0.0);
  1211. float metallic = 0.0;
  1212. float specular = 0.5;
  1213. vec3 emission = vec3(0.0);
  1214. float roughness = 1.0;
  1215. float rim = 0.0;
  1216. float rim_tint = 0.0;
  1217. float clearcoat = 0.0;
  1218. float clearcoat_gloss = 0.0;
  1219. float anisotropy = 0.0;
  1220. vec2 anisotropy_flow = vec2(1.0, 0.0);
  1221. #if defined(ENABLE_AO)
  1222. float ao = 1.0;
  1223. float ao_light_affect = 0.0;
  1224. #endif
  1225. float alpha = 1.0;
  1226. #if defined(ALPHA_SCISSOR_USED)
  1227. float alpha_scissor = 0.5;
  1228. #endif
  1229. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
  1230. vec3 binormal = normalize(binormal_interp);
  1231. vec3 tangent = normalize(tangent_interp);
  1232. #else
  1233. vec3 binormal = vec3(0.0);
  1234. vec3 tangent = vec3(0.0);
  1235. #endif
  1236. vec3 normal = normalize(normal_interp);
  1237. #if defined(DO_SIDE_CHECK)
  1238. if (!gl_FrontFacing) {
  1239. normal = -normal;
  1240. }
  1241. #endif
  1242. #if defined(ENABLE_UV_INTERP)
  1243. vec2 uv = uv_interp;
  1244. #endif
  1245. #if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP)
  1246. vec2 uv2 = uv2_interp;
  1247. #endif
  1248. #if defined(ENABLE_COLOR_INTERP)
  1249. vec4 color = color_interp;
  1250. #endif
  1251. #if defined(ENABLE_NORMALMAP)
  1252. vec3 normalmap = vec3(0.5);
  1253. #endif
  1254. float normaldepth = 1.0;
  1255. #if defined(SCREEN_UV_USED)
  1256. vec2 screen_uv = gl_FragCoord.xy * screen_pixel_size;
  1257. #endif
  1258. #if defined(ENABLE_SSS)
  1259. float sss_strength = 0.0;
  1260. #endif
  1261. {
  1262. /* clang-format off */
  1263. FRAGMENT_SHADER_CODE
  1264. /* clang-format on */
  1265. }
  1266. #if defined(ALPHA_SCISSOR_USED)
  1267. if (alpha < alpha_scissor) {
  1268. discard;
  1269. }
  1270. #endif
  1271. #ifdef USE_OPAQUE_PREPASS
  1272. if (alpha < opaque_prepass_threshold) {
  1273. discard;
  1274. }
  1275. #endif
  1276. #if defined(ENABLE_NORMALMAP)
  1277. normalmap.xy = normalmap.xy * 2.0 - 1.0;
  1278. normalmap.z = sqrt(max(0.0, 1.0 - dot(normalmap.xy, normalmap.xy))); //always ignore Z, as it can be RG packed, Z may be pos/neg, etc.
  1279. normal = normalize(mix(normal, tangent * normalmap.x + binormal * normalmap.y + normal * normalmap.z, normaldepth));
  1280. #endif
  1281. #if defined(LIGHT_USE_ANISOTROPY)
  1282. if (anisotropy > 0.01) {
  1283. //rotation matrix
  1284. mat3 rot = mat3(tangent, binormal, normal);
  1285. //make local to space
  1286. tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0));
  1287. binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0));
  1288. }
  1289. #endif
  1290. #ifdef ENABLE_CLIP_ALPHA
  1291. if (albedo.a < 0.99) {
  1292. //used for doublepass and shadowmapping
  1293. discard;
  1294. }
  1295. #endif
  1296. /////////////////////// LIGHTING //////////////////////////////
  1297. //apply energy conservation
  1298. #ifdef USE_VERTEX_LIGHTING
  1299. vec3 specular_light = specular_light_interp.rgb;
  1300. vec3 diffuse_light = diffuse_light_interp.rgb;
  1301. #else
  1302. vec3 specular_light = vec3(0.0, 0.0, 0.0);
  1303. vec3 diffuse_light = vec3(0.0, 0.0, 0.0);
  1304. #endif
  1305. vec3 ambient_light;
  1306. vec3 env_reflection_light = vec3(0.0, 0.0, 0.0);
  1307. vec3 eye_vec = view;
  1308. #ifdef USE_RADIANCE_MAP
  1309. #ifdef AMBIENT_LIGHT_DISABLED
  1310. ambient_light = vec3(0.0, 0.0, 0.0);
  1311. #else
  1312. {
  1313. { //read radiance from dual paraboloid
  1314. vec3 ref_vec = reflect(-eye_vec, normal); //2.0 * ndotv * normal - view; // reflect(v, n);
  1315. ref_vec = normalize((radiance_inverse_xform * vec4(ref_vec, 0.0)).xyz);
  1316. vec3 radiance = textureDualParaboloid(radiance_map, ref_vec, roughness) * bg_energy;
  1317. env_reflection_light = radiance;
  1318. }
  1319. //no longer a cubemap
  1320. //vec3 radiance = textureLod(radiance_cube, r, lod).xyz * ( brdf.x + brdf.y);
  1321. }
  1322. #ifndef USE_LIGHTMAP
  1323. {
  1324. vec3 ambient_dir = normalize((radiance_inverse_xform * vec4(normal, 0.0)).xyz);
  1325. vec3 env_ambient = textureDualParaboloid(radiance_map, ambient_dir, 1.0) * bg_energy;
  1326. ambient_light = mix(ambient_light_color.rgb, env_ambient, radiance_ambient_contribution);
  1327. //ambient_light=vec3(0.0,0.0,0.0);
  1328. }
  1329. #endif
  1330. #endif //AMBIENT_LIGHT_DISABLED
  1331. #else
  1332. #ifdef AMBIENT_LIGHT_DISABLED
  1333. ambient_light = vec3(0.0, 0.0, 0.0);
  1334. #else
  1335. ambient_light = ambient_light_color.rgb;
  1336. #endif //AMBIENT_LIGHT_DISABLED
  1337. #endif
  1338. ambient_light *= ambient_energy;
  1339. float specular_blob_intensity = 1.0;
  1340. #if defined(SPECULAR_TOON)
  1341. specular_blob_intensity *= specular * 2.0;
  1342. #endif
  1343. #if defined(USE_LIGHT_DIRECTIONAL)
  1344. vec3 light_attenuation = vec3(1.0);
  1345. float depth_z = -vertex.z;
  1346. #ifdef LIGHT_DIRECTIONAL_SHADOW
  1347. #if !defined(SHADOWS_DISABLED)
  1348. #ifdef LIGHT_USE_PSSM4
  1349. if (depth_z < shadow_split_offsets.w) {
  1350. #elif defined(LIGHT_USE_PSSM2)
  1351. if (depth_z < shadow_split_offsets.y) {
  1352. #else
  1353. if (depth_z < shadow_split_offsets.x) {
  1354. #endif //LIGHT_USE_PSSM4
  1355. vec3 pssm_coord;
  1356. float pssm_fade = 0.0;
  1357. #ifdef LIGHT_USE_PSSM_BLEND
  1358. float pssm_blend;
  1359. vec3 pssm_coord2;
  1360. bool use_blend = true;
  1361. #endif
  1362. #ifdef LIGHT_USE_PSSM4
  1363. if (depth_z < shadow_split_offsets.y) {
  1364. if (depth_z < shadow_split_offsets.x) {
  1365. highp vec4 splane = (shadow_matrix1 * vec4(vertex, 1.0));
  1366. pssm_coord = splane.xyz / splane.w;
  1367. #if defined(LIGHT_USE_PSSM_BLEND)
  1368. splane = (shadow_matrix2 * vec4(vertex, 1.0));
  1369. pssm_coord2 = splane.xyz / splane.w;
  1370. pssm_blend = smoothstep(0.0, shadow_split_offsets.x, depth_z);
  1371. #endif
  1372. } else {
  1373. highp vec4 splane = (shadow_matrix2 * vec4(vertex, 1.0));
  1374. pssm_coord = splane.xyz / splane.w;
  1375. #if defined(LIGHT_USE_PSSM_BLEND)
  1376. splane = (shadow_matrix3 * vec4(vertex, 1.0));
  1377. pssm_coord2 = splane.xyz / splane.w;
  1378. pssm_blend = smoothstep(shadow_split_offsets.x, shadow_split_offsets.y, depth_z);
  1379. #endif
  1380. }
  1381. } else {
  1382. if (depth_z < shadow_split_offsets.z) {
  1383. highp vec4 splane = (shadow_matrix3 * vec4(vertex, 1.0));
  1384. pssm_coord = splane.xyz / splane.w;
  1385. #if defined(LIGHT_USE_PSSM_BLEND)
  1386. splane = (shadow_matrix4 * vec4(vertex, 1.0));
  1387. pssm_coord2 = splane.xyz / splane.w;
  1388. pssm_blend = smoothstep(shadow_split_offsets.y, shadow_split_offsets.z, depth_z);
  1389. #endif
  1390. } else {
  1391. highp vec4 splane = (shadow_matrix4 * vec4(vertex, 1.0));
  1392. pssm_coord = splane.xyz / splane.w;
  1393. pssm_fade = smoothstep(shadow_split_offsets.z, shadow_split_offsets.w, depth_z);
  1394. #if defined(LIGHT_USE_PSSM_BLEND)
  1395. use_blend = false;
  1396. #endif
  1397. }
  1398. }
  1399. #endif //LIGHT_USE_PSSM4
  1400. #ifdef LIGHT_USE_PSSM2
  1401. if (depth_z < shadow_split_offsets.x) {
  1402. highp vec4 splane = (shadow_matrix1 * vec4(vertex, 1.0));
  1403. pssm_coord = splane.xyz / splane.w;
  1404. #if defined(LIGHT_USE_PSSM_BLEND)
  1405. splane = (shadow_matrix2 * vec4(vertex, 1.0));
  1406. pssm_coord2 = splane.xyz / splane.w;
  1407. pssm_blend = smoothstep(0.0, shadow_split_offsets.x, depth_z);
  1408. #endif
  1409. } else {
  1410. highp vec4 splane = (shadow_matrix2 * vec4(vertex, 1.0));
  1411. pssm_coord = splane.xyz / splane.w;
  1412. pssm_fade = smoothstep(shadow_split_offsets.x, shadow_split_offsets.y, depth_z);
  1413. #if defined(LIGHT_USE_PSSM_BLEND)
  1414. use_blend = false;
  1415. #endif
  1416. }
  1417. #endif //LIGHT_USE_PSSM2
  1418. #if !defined(LIGHT_USE_PSSM4) && !defined(LIGHT_USE_PSSM2)
  1419. { //regular orthogonal
  1420. highp vec4 splane = (shadow_matrix1 * vec4(vertex, 1.0));
  1421. pssm_coord = splane.xyz / splane.w;
  1422. }
  1423. #endif
  1424. //one one sample
  1425. float shadow = sample_shadow(directional_shadow, directional_shadow_pixel_size, pssm_coord.xy, pssm_coord.z, light_clamp);
  1426. #if defined(LIGHT_USE_PSSM_BLEND)
  1427. if (use_blend) {
  1428. shadow = mix(shadow, sample_shadow(directional_shadow, directional_shadow_pixel_size, pssm_coord2.xy, pssm_coord2.z, light_clamp), pssm_blend);
  1429. }
  1430. #endif
  1431. #ifdef USE_CONTACT_SHADOWS
  1432. if (shadow > 0.01 && shadow_color_contact.a > 0.0) {
  1433. float contact_shadow = contact_shadow_compute(vertex, -light_direction_attenuation.xyz, shadow_color_contact.a);
  1434. shadow = min(shadow, contact_shadow);
  1435. }
  1436. #endif
  1437. light_attenuation = mix(mix(shadow_color_contact.rgb, vec3(1.0), shadow), vec3(1.0), pssm_fade);
  1438. }
  1439. #endif // !defined(SHADOWS_DISABLED)
  1440. #endif //LIGHT_DIRECTIONAL_SHADOW
  1441. #ifdef USE_VERTEX_LIGHTING
  1442. diffuse_light *= mix(vec3(1.0), light_attenuation, diffuse_light_interp.a);
  1443. specular_light *= mix(vec3(1.0), light_attenuation, specular_light_interp.a);
  1444. #else
  1445. light_compute(normal, -light_direction_attenuation.xyz, eye_vec, binormal, tangent, light_color_energy.rgb, light_attenuation, albedo, transmission, light_params.z * specular_blob_intensity, roughness, metallic, specular, rim, rim_tint, clearcoat, clearcoat_gloss, anisotropy, diffuse_light, specular_light);
  1446. #endif
  1447. #endif //#USE_LIGHT_DIRECTIONAL
  1448. #ifdef USE_GI_PROBES
  1449. gi_probes_compute(vertex, normal, roughness, env_reflection_light, ambient_light);
  1450. #endif
  1451. #ifdef USE_LIGHTMAP
  1452. ambient_light = texture(lightmap, uv2).rgb * lightmap_energy;
  1453. #endif
  1454. #ifdef USE_LIGHTMAP_CAPTURE
  1455. {
  1456. vec3 cone_dirs[12] = vec3[](
  1457. vec3(0.0, 0.0, 1.0),
  1458. vec3(0.866025, 0.0, 0.5),
  1459. vec3(0.267617, 0.823639, 0.5),
  1460. vec3(-0.700629, 0.509037, 0.5),
  1461. vec3(-0.700629, -0.509037, 0.5),
  1462. vec3(0.267617, -0.823639, 0.5),
  1463. vec3(0.0, 0.0, -1.0),
  1464. vec3(0.866025, 0.0, -0.5),
  1465. vec3(0.267617, 0.823639, -0.5),
  1466. vec3(-0.700629, 0.509037, -0.5),
  1467. vec3(-0.700629, -0.509037, -0.5),
  1468. vec3(0.267617, -0.823639, -0.5));
  1469. vec3 local_normal = normalize(camera_matrix * vec4(normal, 0.0)).xyz;
  1470. vec4 captured = vec4(0.0);
  1471. float sum = 0.0;
  1472. for (int i = 0; i < 12; i++) {
  1473. float amount = max(0.0, dot(local_normal, cone_dirs[i])); //not correct, but creates a nice wrap around effect
  1474. captured += lightmap_captures[i] * amount;
  1475. sum += amount;
  1476. }
  1477. captured /= sum;
  1478. if (lightmap_capture_sky) {
  1479. ambient_light = mix(ambient_light, captured.rgb, captured.a);
  1480. } else {
  1481. ambient_light = captured.rgb;
  1482. }
  1483. }
  1484. #endif
  1485. #ifdef USE_FORWARD_LIGHTING
  1486. highp vec4 reflection_accum = vec4(0.0, 0.0, 0.0, 0.0);
  1487. highp vec4 ambient_accum = vec4(0.0, 0.0, 0.0, 0.0);
  1488. for (int i = 0; i < reflection_count; i++) {
  1489. reflection_process(reflection_indices[i], vertex, normal, binormal, tangent, roughness, anisotropy, ambient_light, env_reflection_light, reflection_accum, ambient_accum);
  1490. }
  1491. if (reflection_accum.a > 0.0) {
  1492. specular_light += reflection_accum.rgb / reflection_accum.a;
  1493. } else {
  1494. specular_light += env_reflection_light;
  1495. }
  1496. #if !defined(USE_LIGHTMAP) && !defined(USE_LIGHTMAP_CAPTURE)
  1497. if (ambient_accum.a > 0.0) {
  1498. ambient_light = ambient_accum.rgb / ambient_accum.a;
  1499. }
  1500. #endif
  1501. #ifdef USE_VERTEX_LIGHTING
  1502. diffuse_light *= albedo;
  1503. #else
  1504. for (int i = 0; i < omni_light_count; i++) {
  1505. light_process_omni(omni_light_indices[i], vertex, eye_vec, normal, binormal, tangent, albedo, transmission, roughness, metallic, specular, rim, rim_tint, clearcoat, clearcoat_gloss, anisotropy, specular_blob_intensity, diffuse_light, specular_light);
  1506. }
  1507. for (int i = 0; i < spot_light_count; i++) {
  1508. light_process_spot(spot_light_indices[i], vertex, eye_vec, normal, binormal, tangent, albedo, transmission, roughness, metallic, specular, rim, rim_tint, clearcoat, clearcoat_gloss, anisotropy, specular_blob_intensity, diffuse_light, specular_light);
  1509. }
  1510. #endif //USE_VERTEX_LIGHTING
  1511. #endif
  1512. #ifdef RENDER_DEPTH
  1513. //nothing happens, so a tree-ssa optimizer will result in no fragment shader :)
  1514. #else
  1515. specular_light *= reflection_multiplier;
  1516. ambient_light *= albedo; //ambient must be multiplied by albedo at the end
  1517. #if defined(ENABLE_AO)
  1518. ambient_light *= ao;
  1519. ao_light_affect = mix(1.0, ao, ao_light_affect);
  1520. specular_light *= ao_light_affect;
  1521. diffuse_light *= ao_light_affect;
  1522. #endif
  1523. // base color remapping
  1524. diffuse_light *= 1.0 - metallic; // TODO: avoid all diffuse and ambient light calculations when metallic == 1 up to this point
  1525. ambient_light *= 1.0 - metallic;
  1526. {
  1527. #if defined(DIFFUSE_TOON)
  1528. //simplify for toon, as
  1529. specular_light *= specular * metallic * albedo * 2.0;
  1530. #else
  1531. // Environment brdf approximation (Lazarov 2013)
  1532. // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
  1533. const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
  1534. const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);
  1535. vec4 r = roughness * c0 + c1;
  1536. float ndotv = clamp(dot(normal, eye_vec), 0.0, 1.0);
  1537. float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y;
  1538. vec2 env = vec2(-1.04, 1.04) * a004 + r.zw;
  1539. vec3 f0 = F0(metallic, specular, albedo);
  1540. specular_light *= env.x * f0 + env.y;
  1541. #endif
  1542. }
  1543. if (fog_color_enabled.a > 0.5) {
  1544. float fog_amount = 0.0;
  1545. #ifdef USE_LIGHT_DIRECTIONAL
  1546. vec3 fog_color = mix(fog_color_enabled.rgb, fog_sun_color_amount.rgb, fog_sun_color_amount.a * pow(max(dot(normalize(vertex), -light_direction_attenuation.xyz), 0.0), 8.0));
  1547. #else
  1548. vec3 fog_color = fog_color_enabled.rgb;
  1549. #endif
  1550. //apply fog
  1551. if (fog_depth_enabled) {
  1552. float fog_far = fog_depth_end > 0.0 ? fog_depth_end : z_far;
  1553. float fog_z = smoothstep(fog_depth_begin, fog_far, length(vertex));
  1554. fog_amount = pow(fog_z, fog_depth_curve) * fog_density;
  1555. if (fog_transmit_enabled) {
  1556. vec3 total_light = emission + ambient_light + specular_light + diffuse_light;
  1557. float transmit = pow(fog_z, fog_transmit_curve);
  1558. fog_color = mix(max(total_light, fog_color), fog_color, transmit);
  1559. }
  1560. }
  1561. if (fog_height_enabled) {
  1562. float y = (camera_matrix * vec4(vertex, 1.0)).y;
  1563. fog_amount = max(fog_amount, pow(smoothstep(fog_height_min, fog_height_max, y), fog_height_curve));
  1564. }
  1565. float rev_amount = 1.0 - fog_amount;
  1566. emission = emission * rev_amount + fog_color * fog_amount;
  1567. ambient_light *= rev_amount;
  1568. specular_light *= rev_amount;
  1569. diffuse_light *= rev_amount;
  1570. }
  1571. #ifdef USE_MULTIPLE_RENDER_TARGETS
  1572. #ifdef SHADELESS
  1573. diffuse_buffer = vec4(albedo.rgb, 0.0);
  1574. specular_buffer = vec4(0.0);
  1575. #else
  1576. //approximate ambient scale for SSAO, since we will lack full ambient
  1577. float max_emission = max(emission.r, max(emission.g, emission.b));
  1578. float max_ambient = max(ambient_light.r, max(ambient_light.g, ambient_light.b));
  1579. float max_diffuse = max(diffuse_light.r, max(diffuse_light.g, diffuse_light.b));
  1580. float total_ambient = max_ambient + max_diffuse + max_emission;
  1581. float ambient_scale = (total_ambient > 0.0) ? (max_ambient + ambient_occlusion_affect_light * max_diffuse) / total_ambient : 0.0;
  1582. #if defined(ENABLE_AO)
  1583. ambient_scale = mix(0.0, ambient_scale, ambient_occlusion_affect_ao_channel);
  1584. #endif
  1585. diffuse_buffer = vec4(emission + diffuse_light + ambient_light, ambient_scale);
  1586. specular_buffer = vec4(specular_light, metallic);
  1587. #endif //SHADELESS
  1588. normal_mr_buffer = vec4(normalize(normal) * 0.5 + 0.5, roughness);
  1589. #if defined(ENABLE_SSS)
  1590. sss_buffer = sss_strength;
  1591. #endif
  1592. #else //USE_MULTIPLE_RENDER_TARGETS
  1593. #ifdef SHADELESS
  1594. frag_color = vec4(albedo, alpha);
  1595. #else
  1596. frag_color = vec4(emission + ambient_light + diffuse_light + specular_light, alpha);
  1597. #endif //SHADELESS
  1598. #endif //USE_MULTIPLE_RENDER_TARGETS
  1599. #endif //RENDER_DEPTH
  1600. }