scene.glsl 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629
  1. /* clang-format off */
  2. #[modes]
  3. mode_color =
  4. mode_color_instancing = \n#define USE_INSTANCING
  5. mode_depth = #define MODE_RENDER_DEPTH
  6. mode_depth_instancing = #define MODE_RENDER_DEPTH \n#define USE_INSTANCING
  7. #[specializations]
  8. DISABLE_LIGHTMAP = false
  9. DISABLE_LIGHT_DIRECTIONAL = false
  10. DISABLE_LIGHT_OMNI = false
  11. DISABLE_LIGHT_SPOT = false
  12. DISABLE_REFLECTION_PROBE = true
  13. DISABLE_FOG = false
  14. USE_DEPTH_FOG = false
  15. USE_RADIANCE_MAP = true
  16. USE_LIGHTMAP = false
  17. USE_SH_LIGHTMAP = false
  18. USE_LIGHTMAP_CAPTURE = false
  19. USE_MULTIVIEW = false
  20. RENDER_SHADOWS = false
  21. RENDER_SHADOWS_LINEAR = false
  22. SHADOW_MODE_PCF_5 = false
  23. SHADOW_MODE_PCF_13 = false
  24. LIGHT_USE_PSSM2 = false
  25. LIGHT_USE_PSSM4 = false
  26. LIGHT_USE_PSSM_BLEND = false
  27. BASE_PASS = true
  28. USE_ADDITIVE_LIGHTING = false
  29. APPLY_TONEMAPPING = true
  30. // We can only use one type of light per additive pass. This means that if USE_ADDITIVE_LIGHTING is defined, and
  31. // these are false, we are doing a directional light pass.
  32. ADDITIVE_OMNI = false
  33. ADDITIVE_SPOT = false
  34. RENDER_MATERIAL = false
  35. SECOND_REFLECTION_PROBE = false
  36. LIGHTMAP_BICUBIC_FILTER = false
  37. #[vertex]
  38. #define M_PI 3.14159265359
  39. #define SHADER_IS_SRGB true
  40. #define SHADER_SPACE_FAR -1.0
  41. #if defined(RENDER_SHADOWS) || defined(RENDER_SHADOWS_LINEAR)
  42. #define IN_SHADOW_PASS true
  43. #else
  44. #define IN_SHADOW_PASS false
  45. #endif
  46. #include "stdlib_inc.glsl"
  47. #if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED)
  48. #ifndef NORMAL_USED
  49. #define NORMAL_USED
  50. #endif
  51. #endif
  52. #ifdef MODE_UNSHADED
  53. #ifdef USE_ADDITIVE_LIGHTING
  54. #undef USE_ADDITIVE_LIGHTING
  55. #endif
  56. #endif // MODE_UNSHADED
  57. /*
  58. from RenderingServer:
  59. ARRAY_VERTEX = 0, // RGB32F or RGBA16
  60. ARRAY_NORMAL = 1, // RG16 octahedral compression or RGBA16 normal + angle
  61. ARRAY_TANGENT = 2, // RG16 octahedral compression, sign stored in sign of G
  62. ARRAY_COLOR = 3, // RGBA8
  63. ARRAY_TEX_UV = 4, // RG32F
  64. ARRAY_TEX_UV2 = 5, // RG32F
  65. ARRAY_CUSTOM0 = 6, // Depends on ArrayCustomFormat.
  66. ARRAY_CUSTOM1 = 7,
  67. ARRAY_CUSTOM2 = 8,
  68. ARRAY_CUSTOM3 = 9,
  69. ARRAY_BONES = 10, // RGBA16UI (x2 if 8 weights)
  70. ARRAY_WEIGHTS = 11, // RGBA16UNORM (x2 if 8 weights)
  71. */
  72. /* INPUT ATTRIBS */
  73. // Always contains vertex position in XYZ, can contain tangent angle in W.
  74. layout(location = 0) in highp vec4 vertex_angle_attrib;
  75. /* clang-format on */
  76. #ifdef NORMAL_USED
  77. // Contains Normal/Axis in RG, can contain tangent in BA.
  78. layout(location = 1) in vec4 axis_tangent_attrib;
  79. #endif
  80. // location 2 is unused.
  81. #if defined(COLOR_USED)
  82. layout(location = 3) in vec4 color_attrib;
  83. #endif
  84. #ifdef UV_USED
  85. layout(location = 4) in vec2 uv_attrib;
  86. #endif
  87. #if defined(UV2_USED) || defined(USE_LIGHTMAP) || defined(RENDER_MATERIAL)
  88. layout(location = 5) in vec2 uv2_attrib;
  89. #endif
  90. #if defined(CUSTOM0_USED)
  91. layout(location = 6) in vec4 custom0_attrib;
  92. #endif
  93. #if defined(CUSTOM1_USED)
  94. layout(location = 7) in vec4 custom1_attrib;
  95. #endif
  96. #if defined(CUSTOM2_USED)
  97. layout(location = 8) in vec4 custom2_attrib;
  98. #endif
  99. #if defined(CUSTOM3_USED)
  100. layout(location = 9) in vec4 custom3_attrib;
  101. #endif
  102. #if defined(BONES_USED)
  103. layout(location = 10) in uvec4 bone_attrib;
  104. #endif
  105. #if defined(WEIGHTS_USED)
  106. layout(location = 11) in vec4 weight_attrib;
  107. #endif
  108. vec3 oct_to_vec3(vec2 e) {
  109. vec3 v = vec3(e.xy, 1.0 - abs(e.x) - abs(e.y));
  110. float t = max(-v.z, 0.0);
  111. v.xy += t * -sign(v.xy);
  112. return normalize(v);
  113. }
  114. void axis_angle_to_tbn(vec3 axis, float angle, out vec3 tangent, out vec3 binormal, out vec3 normal) {
  115. float c = cos(angle);
  116. float s = sin(angle);
  117. vec3 omc_axis = (1.0 - c) * axis;
  118. vec3 s_axis = s * axis;
  119. tangent = omc_axis.xxx * axis + vec3(c, -s_axis.z, s_axis.y);
  120. binormal = omc_axis.yyy * axis + vec3(s_axis.z, c, -s_axis.x);
  121. normal = omc_axis.zzz * axis + vec3(-s_axis.y, s_axis.x, c);
  122. }
  123. #ifdef USE_INSTANCING
  124. layout(location = 12) in highp vec4 instance_xform0;
  125. layout(location = 13) in highp vec4 instance_xform1;
  126. layout(location = 14) in highp vec4 instance_xform2;
  127. layout(location = 15) in highp uvec4 instance_color_custom_data; // Color packed into xy, Custom data into zw.
  128. #endif
  129. #define FLAGS_NON_UNIFORM_SCALE (1 << 4)
  130. layout(std140) uniform GlobalShaderUniformData { //ubo:1
  131. vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS];
  132. };
  133. layout(std140) uniform SceneData { // ubo:2
  134. highp mat4 projection_matrix;
  135. highp mat4 inv_projection_matrix;
  136. highp mat4 inv_view_matrix;
  137. highp mat4 view_matrix;
  138. // Used for billboards to cast correct shadows.
  139. highp mat4 main_cam_inv_view_matrix;
  140. vec2 viewport_size;
  141. vec2 screen_pixel_size;
  142. mediump vec4 ambient_light_color_energy;
  143. mediump float ambient_color_sky_mix;
  144. float pad2;
  145. float emissive_exposure_normalization;
  146. bool use_ambient_light;
  147. bool use_ambient_cubemap;
  148. bool use_reflection_cubemap;
  149. float fog_aerial_perspective;
  150. float time;
  151. mat3 radiance_inverse_xform;
  152. uint directional_light_count;
  153. float z_far;
  154. float z_near;
  155. float IBL_exposure_normalization;
  156. bool fog_enabled;
  157. uint fog_mode;
  158. float fog_density;
  159. float fog_height;
  160. float fog_height_density;
  161. float fog_depth_curve;
  162. float fog_sun_scatter;
  163. float fog_depth_begin;
  164. vec3 fog_light_color;
  165. float fog_depth_end;
  166. float shadow_bias;
  167. float luminance_multiplier;
  168. uint camera_visible_layers;
  169. bool pancake_shadows;
  170. }
  171. scene_data;
  172. #ifdef USE_ADDITIVE_LIGHTING
  173. #if defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
  174. struct PositionalShadowData {
  175. highp mat4 shadow_matrix;
  176. highp vec3 light_position;
  177. highp float shadow_normal_bias;
  178. vec3 pad;
  179. highp float shadow_atlas_pixel_size;
  180. };
  181. layout(std140) uniform PositionalShadows { // ubo:9
  182. PositionalShadowData positional_shadows[MAX_LIGHT_DATA_STRUCTS];
  183. };
  184. uniform lowp uint positional_shadow_index;
  185. #else // ADDITIVE_DIRECTIONAL
  186. struct DirectionalShadowData {
  187. highp vec3 direction;
  188. highp float shadow_atlas_pixel_size;
  189. highp vec4 shadow_normal_bias;
  190. highp vec4 shadow_split_offsets;
  191. highp mat4 shadow_matrix1;
  192. highp mat4 shadow_matrix2;
  193. highp mat4 shadow_matrix3;
  194. highp mat4 shadow_matrix4;
  195. mediump float fade_from;
  196. mediump float fade_to;
  197. mediump vec2 pad;
  198. };
  199. layout(std140) uniform DirectionalShadows { // ubo:10
  200. DirectionalShadowData directional_shadows[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
  201. };
  202. uniform lowp uint directional_shadow_index;
  203. #endif // !(defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT))
  204. #endif // USE_ADDITIVE_LIGHTING
  205. #ifdef USE_VERTEX_LIGHTING
  206. out vec3 diffuse_light_interp;
  207. out vec3 specular_light_interp;
  208. #ifdef USE_ADDITIVE_LIGHTING
  209. out vec3 additive_diffuse_light_interp;
  210. out vec3 additive_specular_light_interp;
  211. #endif // USE_ADDITIVE_LIGHTING
  212. // Directional light data.
  213. #if !defined(DISABLE_LIGHT_DIRECTIONAL) || (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT) && defined(USE_ADDITIVE_LIGHTING))
  214. struct DirectionalLightData {
  215. mediump vec3 direction;
  216. mediump float energy;
  217. mediump vec3 color;
  218. mediump float size;
  219. lowp uint unused;
  220. lowp uint bake_mode;
  221. mediump float shadow_opacity;
  222. mediump float specular;
  223. };
  224. layout(std140) uniform DirectionalLights { // ubo:7
  225. DirectionalLightData directional_lights[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
  226. };
  227. #define LIGHT_BAKE_DISABLED 0u
  228. #define LIGHT_BAKE_STATIC 1u
  229. #define LIGHT_BAKE_DYNAMIC 2u
  230. #endif // !DISABLE_LIGHT_DIRECTIONAL
  231. // Omni and spot light data.
  232. #if !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) || (defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT) && defined(USE_ADDITIVE_LIGHTING))
  233. struct LightData { // This structure needs to be as packed as possible.
  234. highp vec3 position;
  235. highp float inv_radius;
  236. mediump vec3 direction;
  237. highp float size;
  238. mediump vec3 color;
  239. mediump float attenuation;
  240. mediump float cone_attenuation;
  241. mediump float cone_angle;
  242. mediump float specular_amount;
  243. mediump float shadow_opacity;
  244. lowp vec3 pad;
  245. lowp uint bake_mode;
  246. };
  247. #if !defined(DISABLE_LIGHT_OMNI) || defined(ADDITIVE_OMNI)
  248. layout(std140) uniform OmniLightData { // ubo:5
  249. LightData omni_lights[MAX_LIGHT_DATA_STRUCTS];
  250. };
  251. #ifdef BASE_PASS
  252. uniform uint omni_light_indices[MAX_FORWARD_LIGHTS];
  253. uniform uint omni_light_count;
  254. #endif // BASE_PASS
  255. #endif // DISABLE_LIGHT_OMNI
  256. #if !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_SPOT)
  257. layout(std140) uniform SpotLightData { // ubo:6
  258. LightData spot_lights[MAX_LIGHT_DATA_STRUCTS];
  259. };
  260. #ifdef BASE_PASS
  261. uniform uint spot_light_indices[MAX_FORWARD_LIGHTS];
  262. uniform uint spot_light_count;
  263. #endif // BASE_PASS
  264. #endif // DISABLE_LIGHT_SPOT
  265. #endif // !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) || (defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT) && defined(USE_ADDITIVE_LIGHTING))
  266. #ifdef USE_ADDITIVE_LIGHTING
  267. #ifdef ADDITIVE_OMNI
  268. uniform lowp uint omni_light_index;
  269. #endif
  270. #ifdef ADDITIVE_SPOT
  271. uniform lowp uint spot_light_index;
  272. #endif
  273. #endif // USE_ADDITIVE_LIGHTING
  274. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED) && defined(USE_VERTEX_LIGHTING)
  275. // Eyeballed approximation of `exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25`.
  276. // Uses slightly more FMA instructions (2x rate) to avoid special instructions (0.25x rate).
  277. // Range is reduced to [0.64,4977] from [068,2,221,528] which makes mediump feasible for the rest of the shader.
  278. mediump float roughness_to_shininess(mediump float roughness) {
  279. mediump float r = 1.2 - roughness;
  280. mediump float r2 = r * r;
  281. return r * r2 * r2 * 2000.0;
  282. }
  283. void light_compute(vec3 N, vec3 L, vec3 V, vec3 light_color, bool is_directional, float roughness,
  284. inout vec3 diffuse_light, inout vec3 specular_light) {
  285. float NdotL = min(dot(N, L), 1.0);
  286. float cNdotL = max(NdotL, 0.0); // clamped NdotL
  287. #if defined(DIFFUSE_LAMBERT_WRAP)
  288. // Energy conserving lambert wrap shader.
  289. // https://web.archive.org/web/20210228210901/http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/
  290. float diffuse_brdf_NL = max(0.0, (cNdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))) * (1.0 / M_PI);
  291. #else
  292. // lambert
  293. float diffuse_brdf_NL = cNdotL * (1.0 / M_PI);
  294. #endif
  295. diffuse_light += light_color * diffuse_brdf_NL;
  296. #if !defined(SPECULAR_DISABLED)
  297. float specular_brdf_NL = 0.0;
  298. // Normalized blinn always unless disabled.
  299. vec3 H = normalize(V + L);
  300. float cNdotH = clamp(dot(N, H), 0.0, 1.0);
  301. float shininess = roughness_to_shininess(roughness);
  302. float blinn = pow(cNdotH, shininess);
  303. blinn *= (shininess + 2.0) * (1.0 / (8.0 * M_PI)) * cNdotL;
  304. specular_brdf_NL = blinn;
  305. specular_light += specular_brdf_NL * light_color;
  306. #endif
  307. }
  308. float get_omni_spot_attenuation(float distance, float inv_range, float decay) {
  309. float nd = distance * inv_range;
  310. nd *= nd;
  311. nd *= nd; // nd^4
  312. nd = max(1.0 - nd, 0.0);
  313. nd *= nd; // nd^2
  314. return nd * pow(max(distance, 0.0001), -decay);
  315. }
  316. #if !defined(DISABLE_LIGHT_OMNI) || (defined(ADDITIVE_OMNI) && defined(USE_ADDITIVE_LIGHTING))
  317. void light_process_omni(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, float roughness,
  318. inout vec3 diffuse_light, inout vec3 specular_light) {
  319. vec3 light_rel_vec = omni_lights[idx].position - vertex;
  320. float light_length = length(light_rel_vec);
  321. float omni_attenuation = get_omni_spot_attenuation(light_length, omni_lights[idx].inv_radius, omni_lights[idx].attenuation);
  322. vec3 color = omni_lights[idx].color * omni_attenuation; // No light shaders here, so combine.
  323. light_compute(normal, normalize(light_rel_vec), eye_vec, color, false, roughness,
  324. diffuse_light,
  325. specular_light);
  326. }
  327. #endif // !defined(DISABLE_LIGHT_OMNI) || (defined(ADDITIVE_OMNI) && defined(USE_ADDITIVE_LIGHTING))
  328. #if !defined(DISABLE_LIGHT_SPOT) || (defined(ADDITIVE_SPOT) && defined(USE_ADDITIVE_LIGHTING))
  329. void light_process_spot(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, float roughness,
  330. inout vec3 diffuse_light,
  331. inout vec3 specular_light) {
  332. vec3 light_rel_vec = spot_lights[idx].position - vertex;
  333. float light_length = length(light_rel_vec);
  334. float spot_attenuation = get_omni_spot_attenuation(light_length, spot_lights[idx].inv_radius, spot_lights[idx].attenuation);
  335. vec3 spot_dir = spot_lights[idx].direction;
  336. float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights[idx].cone_angle);
  337. float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights[idx].cone_angle));
  338. mediump float cone_attenuation = spot_lights[idx].cone_attenuation;
  339. spot_attenuation *= 1.0 - pow(spot_rim, cone_attenuation);
  340. vec3 color = spot_lights[idx].color * spot_attenuation;
  341. light_compute(normal, normalize(light_rel_vec), eye_vec, color, false, roughness,
  342. diffuse_light, specular_light);
  343. }
  344. #endif // !defined(DISABLE_LIGHT_SPOT) || (defined(ADDITIVE_SPOT) && defined(USE_ADDITIVE_LIGHTING))
  345. #endif // !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED) && defined(USE_VERTEX_LIGHTING)
  346. #endif // USE_VERTEX_LIGHTING
  347. #ifdef USE_MULTIVIEW
  348. layout(std140) uniform MultiviewData { // ubo:8
  349. highp mat4 projection_matrix_view[MAX_VIEWS];
  350. highp mat4 inv_projection_matrix_view[MAX_VIEWS];
  351. highp vec4 eye_offset[MAX_VIEWS];
  352. }
  353. multiview_data;
  354. #endif
  355. uniform highp mat4 world_transform;
  356. uniform highp vec3 compressed_aabb_position;
  357. uniform highp vec3 compressed_aabb_size;
  358. uniform highp vec4 uv_scale;
  359. uniform highp uint instance_offset;
  360. uniform highp uint model_flags;
  361. #ifdef RENDER_MATERIAL
  362. uniform mediump vec2 uv_offset;
  363. #endif
  364. /* Varyings */
  365. out highp vec3 vertex_interp;
  366. #ifdef NORMAL_USED
  367. out vec3 normal_interp;
  368. #endif
  369. #if defined(COLOR_USED)
  370. out vec4 color_interp;
  371. #endif
  372. #if defined(UV_USED)
  373. out vec2 uv_interp;
  374. #endif
  375. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  376. out vec2 uv2_interp;
  377. #endif
  378. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  379. out vec3 tangent_interp;
  380. out vec3 binormal_interp;
  381. #endif
  382. #ifdef USE_ADDITIVE_LIGHTING
  383. out highp vec4 shadow_coord;
  384. #if defined(LIGHT_USE_PSSM2) || defined(LIGHT_USE_PSSM4)
  385. out highp vec4 shadow_coord2;
  386. #endif
  387. #ifdef LIGHT_USE_PSSM4
  388. out highp vec4 shadow_coord3;
  389. out highp vec4 shadow_coord4;
  390. #endif //LIGHT_USE_PSSM4
  391. #endif
  392. #ifdef MATERIAL_UNIFORMS_USED
  393. /* clang-format off */
  394. layout(std140) uniform MaterialUniforms { // ubo:3
  395. #MATERIAL_UNIFORMS
  396. };
  397. /* clang-format on */
  398. #endif
  399. /* clang-format off */
  400. #GLOBALS
  401. /* clang-format on */
  402. invariant gl_Position;
  403. void main() {
  404. highp vec3 vertex = vertex_angle_attrib.xyz * compressed_aabb_size + compressed_aabb_position;
  405. highp mat4 model_matrix = world_transform;
  406. #ifdef USE_INSTANCING
  407. highp mat4 m = mat4(instance_xform0, instance_xform1, instance_xform2, vec4(0.0, 0.0, 0.0, 1.0));
  408. model_matrix = model_matrix * transpose(m);
  409. #endif
  410. #ifdef NORMAL_USED
  411. vec3 normal = oct_to_vec3(axis_tangent_attrib.xy * 2.0 - 1.0);
  412. #endif
  413. highp mat3 model_normal_matrix;
  414. if (bool(model_flags & uint(FLAGS_NON_UNIFORM_SCALE))) {
  415. model_normal_matrix = transpose(inverse(mat3(model_matrix)));
  416. } else {
  417. model_normal_matrix = mat3(model_matrix);
  418. }
  419. #if defined(NORMAL_USED) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  420. vec3 binormal;
  421. float binormal_sign;
  422. vec3 tangent;
  423. if (axis_tangent_attrib.z > 0.0 || axis_tangent_attrib.w < 1.0) {
  424. // Uncompressed format.
  425. vec2 signed_tangent_attrib = axis_tangent_attrib.zw * 2.0 - 1.0;
  426. tangent = oct_to_vec3(vec2(signed_tangent_attrib.x, abs(signed_tangent_attrib.y) * 2.0 - 1.0));
  427. binormal_sign = sign(signed_tangent_attrib.y);
  428. binormal = normalize(cross(normal, tangent) * binormal_sign);
  429. } else {
  430. // Compressed format.
  431. float angle = vertex_angle_attrib.w;
  432. binormal_sign = angle > 0.5 ? 1.0 : -1.0; // 0.5 does not exist in UNORM16, so values are either greater or smaller.
  433. angle = abs(angle * 2.0 - 1.0) * M_PI; // 0.5 is basically zero, allowing to encode both signs reliably.
  434. vec3 axis = normal;
  435. axis_angle_to_tbn(axis, angle, tangent, binormal, normal);
  436. binormal *= binormal_sign;
  437. }
  438. #endif
  439. #if defined(COLOR_USED)
  440. color_interp = color_attrib;
  441. #ifdef USE_INSTANCING
  442. vec4 instance_color;
  443. instance_color.xy = unpackHalf2x16(instance_color_custom_data.x);
  444. instance_color.zw = unpackHalf2x16(instance_color_custom_data.y);
  445. color_interp *= instance_color;
  446. #endif
  447. #endif
  448. #if defined(UV_USED)
  449. uv_interp = uv_attrib;
  450. #endif
  451. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  452. uv2_interp = uv2_attrib;
  453. #endif
  454. if (uv_scale != vec4(0.0)) { // Compression enabled
  455. #ifdef UV_USED
  456. uv_interp = (uv_interp - 0.5) * uv_scale.xy;
  457. #endif
  458. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  459. uv2_interp = (uv2_interp - 0.5) * uv_scale.zw;
  460. #endif
  461. }
  462. #if defined(OVERRIDE_POSITION)
  463. highp vec4 position;
  464. #endif
  465. #ifdef USE_MULTIVIEW
  466. mat4 projection_matrix = multiview_data.projection_matrix_view[ViewIndex];
  467. mat4 inv_projection_matrix = multiview_data.inv_projection_matrix_view[ViewIndex];
  468. vec3 eye_offset = multiview_data.eye_offset[ViewIndex].xyz;
  469. #else
  470. mat4 projection_matrix = scene_data.projection_matrix;
  471. mat4 inv_projection_matrix = scene_data.inv_projection_matrix;
  472. vec3 eye_offset = vec3(0.0, 0.0, 0.0);
  473. #endif //USE_MULTIVIEW
  474. #ifdef USE_INSTANCING
  475. vec4 instance_custom;
  476. instance_custom.xy = unpackHalf2x16(instance_color_custom_data.z);
  477. instance_custom.zw = unpackHalf2x16(instance_color_custom_data.w);
  478. #else
  479. vec4 instance_custom = vec4(0.0);
  480. #endif
  481. // Using world coordinates
  482. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  483. vertex = (model_matrix * vec4(vertex, 1.0)).xyz;
  484. #ifdef NORMAL_USED
  485. normal = model_normal_matrix * normal;
  486. #endif
  487. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  488. tangent = model_normal_matrix * tangent;
  489. binormal = model_normal_matrix * binormal;
  490. #endif
  491. #endif
  492. #ifdef Z_CLIP_SCALE_USED
  493. float z_clip_scale = 1.0;
  494. #endif
  495. float roughness = 1.0;
  496. highp mat4 modelview = scene_data.view_matrix * model_matrix;
  497. highp mat3 modelview_normal = mat3(scene_data.view_matrix) * model_normal_matrix;
  498. float point_size = 1.0;
  499. {
  500. #CODE : VERTEX
  501. }
  502. gl_PointSize = point_size;
  503. // Using local coordinates (default)
  504. #if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
  505. vertex = (modelview * vec4(vertex, 1.0)).xyz;
  506. #ifdef NORMAL_USED
  507. normal = modelview_normal * normal;
  508. #endif
  509. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  510. binormal = modelview_normal * binormal;
  511. tangent = modelview_normal * tangent;
  512. #endif
  513. #endif // !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
  514. // Using world coordinates
  515. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  516. vertex = (scene_data.view_matrix * vec4(vertex, 1.0)).xyz;
  517. #ifdef NORMAL_USED
  518. normal = (scene_data.view_matrix * vec4(normal, 0.0)).xyz;
  519. #endif
  520. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  521. binormal = (scene_data.view_matrix * vec4(binormal, 0.0)).xyz;
  522. tangent = (scene_data.view_matrix * vec4(tangent, 0.0)).xyz;
  523. #endif
  524. #endif
  525. vertex_interp = vertex;
  526. // Normalize TBN vectors before interpolation, per MikkTSpace.
  527. // See: http://www.mikktspace.com/
  528. #ifdef NORMAL_USED
  529. normal_interp = normalize(normal);
  530. #endif
  531. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  532. tangent_interp = normalize(tangent);
  533. binormal_interp = normalize(binormal);
  534. #endif
  535. // Calculate shadows.
  536. #ifdef USE_ADDITIVE_LIGHTING
  537. #if defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
  538. // Apply normal bias at draw time to avoid issues with scaling non-fused geometry.
  539. vec3 light_rel_vec = positional_shadows[positional_shadow_index].light_position - vertex_interp;
  540. float light_length = length(light_rel_vec);
  541. float aNdotL = abs(dot(normalize(normal_interp), normalize(light_rel_vec)));
  542. vec3 normal_offset = (1.0 - aNdotL) * positional_shadows[positional_shadow_index].shadow_normal_bias * light_length * normal_interp;
  543. #ifdef ADDITIVE_SPOT
  544. // Calculate coord here so we can take advantage of prefetch.
  545. shadow_coord = positional_shadows[positional_shadow_index].shadow_matrix * vec4(vertex_interp + normal_offset, 1.0);
  546. #endif
  547. #ifdef ADDITIVE_OMNI
  548. // Can't interpolate unit direction nicely, so forget about prefetch.
  549. shadow_coord = vec4(vertex_interp + normal_offset, 1.0);
  550. #endif
  551. #else // ADDITIVE_DIRECTIONAL
  552. vec3 base_normal_bias = normalize(normal_interp) * (1.0 - max(0.0, dot(directional_shadows[directional_shadow_index].direction, -normalize(normal_interp))));
  553. vec3 normal_offset = base_normal_bias * directional_shadows[directional_shadow_index].shadow_normal_bias.x;
  554. shadow_coord = directional_shadows[directional_shadow_index].shadow_matrix1 * vec4(vertex_interp + normal_offset, 1.0);
  555. #if defined(LIGHT_USE_PSSM2) || defined(LIGHT_USE_PSSM4)
  556. normal_offset = base_normal_bias * directional_shadows[directional_shadow_index].shadow_normal_bias.y;
  557. shadow_coord2 = directional_shadows[directional_shadow_index].shadow_matrix2 * vec4(vertex_interp + normal_offset, 1.0);
  558. #endif
  559. #ifdef LIGHT_USE_PSSM4
  560. normal_offset = base_normal_bias * directional_shadows[directional_shadow_index].shadow_normal_bias.z;
  561. shadow_coord3 = directional_shadows[directional_shadow_index].shadow_matrix3 * vec4(vertex_interp + normal_offset, 1.0);
  562. normal_offset = base_normal_bias * directional_shadows[directional_shadow_index].shadow_normal_bias.w;
  563. shadow_coord4 = directional_shadows[directional_shadow_index].shadow_matrix4 * vec4(vertex_interp + normal_offset, 1.0);
  564. #endif //LIGHT_USE_PSSM4
  565. #endif // !(defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT))
  566. #endif // USE_ADDITIVE_LIGHTING
  567. #if defined(RENDER_SHADOWS) && !defined(RENDER_SHADOWS_LINEAR)
  568. // This is an optimized version of normalize(vertex_interp) * scene_data.shadow_bias / length(vertex_interp).
  569. float light_length_sq = dot(vertex_interp, vertex_interp);
  570. vertex_interp += vertex_interp * scene_data.shadow_bias / light_length_sq;
  571. #endif
  572. #if defined(OVERRIDE_POSITION)
  573. gl_Position = position;
  574. #else
  575. gl_Position = projection_matrix * vec4(vertex_interp, 1.0);
  576. #endif
  577. #if !defined(RENDER_SHADOWS) && !defined(RENDER_SHADOWS_LINEAR)
  578. #ifdef Z_CLIP_SCALE_USED
  579. gl_Position.z = mix(gl_Position.w, gl_Position.z, z_clip_scale);
  580. #endif
  581. #endif
  582. #ifdef RENDER_MATERIAL
  583. vec2 uv_dest_attrib;
  584. if (uv_scale != vec4(0.0)) {
  585. uv_dest_attrib = (uv2_attrib.xy - 0.5) * uv_scale.zw;
  586. } else {
  587. uv_dest_attrib = uv2_attrib.xy;
  588. }
  589. gl_Position.xy = (uv_dest_attrib + uv_offset) * 2.0 - 1.0;
  590. gl_Position.z = 0.00001;
  591. gl_Position.w = 1.0;
  592. #endif
  593. #ifdef USE_VERTEX_LIGHTING
  594. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  595. #ifdef USE_MULTIVIEW
  596. vec3 view = -normalize(vertex_interp - eye_offset);
  597. #else
  598. vec3 view = -normalize(vertex_interp);
  599. #endif
  600. diffuse_light_interp = vec3(0.0);
  601. specular_light_interp = vec3(0.0);
  602. #ifdef BASE_PASS
  603. #ifndef DISABLE_LIGHT_DIRECTIONAL
  604. for (uint i = uint(0); i < scene_data.directional_light_count; i++) {
  605. #if defined(USE_LIGHTMAP) && !defined(DISABLE_LIGHTMAP)
  606. if (directional_lights[i].bake_mode == LIGHT_BAKE_STATIC) {
  607. continue;
  608. }
  609. #endif
  610. light_compute(normal_interp, normalize(directional_lights[i].direction), normalize(view), directional_lights[i].color * directional_lights[i].energy, true, roughness,
  611. diffuse_light_interp.rgb,
  612. specular_light_interp.rgb);
  613. }
  614. #endif // !DISABLE_LIGHT_DIRECTIONAL
  615. #ifndef DISABLE_LIGHT_OMNI
  616. for (uint i = 0u; i < omni_light_count; i++) {
  617. light_process_omni(omni_light_indices[i], vertex_interp, view, normal_interp, roughness,
  618. diffuse_light_interp.rgb, specular_light_interp.rgb);
  619. }
  620. #endif // !DISABLE_LIGHT_OMNI
  621. #ifndef DISABLE_LIGHT_SPOT
  622. for (uint i = 0u; i < spot_light_count; i++) {
  623. light_process_spot(spot_light_indices[i], vertex_interp, view, normal_interp, roughness,
  624. diffuse_light_interp.rgb, specular_light_interp.rgb);
  625. }
  626. #endif // !DISABLE_LIGHT_SPOT
  627. #endif // BASE_PASS
  628. /* ADDITIVE LIGHTING PASS */
  629. #ifdef USE_ADDITIVE_LIGHTING
  630. additive_diffuse_light_interp = vec3(0.0);
  631. additive_specular_light_interp = vec3(0.0);
  632. #if !defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)
  633. light_compute(normal_interp, normalize(directional_lights[directional_shadow_index].direction), normalize(view), directional_lights[directional_shadow_index].color * directional_lights[directional_shadow_index].energy, true, roughness,
  634. additive_diffuse_light_interp.rgb,
  635. additive_specular_light_interp.rgb);
  636. #endif // !defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)
  637. #ifdef ADDITIVE_OMNI
  638. light_process_omni(omni_light_index, vertex_interp, view, normal_interp, roughness,
  639. additive_diffuse_light_interp.rgb, additive_specular_light_interp.rgb);
  640. #endif // ADDITIVE_OMNI
  641. #ifdef ADDITIVE_SPOT
  642. light_process_spot(spot_light_index, vertex_interp, view, normal_interp, roughness,
  643. additive_diffuse_light_interp.rgb, additive_specular_light_interp.rgb);
  644. #endif // ADDITIVE_SPOT
  645. #endif // USE_ADDITIVE_LIGHTING
  646. #endif // !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  647. #endif // USE_VERTEX_LIGHTING
  648. }
  649. /* clang-format off */
  650. #[fragment]
  651. // Default to SPECULAR_SCHLICK_GGX.
  652. #if !defined(SPECULAR_DISABLED) && !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_TOON)
  653. #define SPECULAR_SCHLICK_GGX
  654. #endif
  655. #if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED)
  656. #ifndef NORMAL_USED
  657. #define NORMAL_USED
  658. #endif
  659. #endif
  660. #ifdef MODE_UNSHADED
  661. #ifdef USE_ADDITIVE_LIGHTING
  662. #undef USE_ADDITIVE_LIGHTING
  663. #endif
  664. #endif // MODE_UNSHADED
  665. #ifndef MODE_RENDER_DEPTH
  666. #include "tonemap_inc.glsl"
  667. #endif
  668. #include "stdlib_inc.glsl"
  669. /* texture unit usage, N is max_texture_unit-N
  670. 1-color correction // In tonemap_inc.glsl
  671. 2-radiance
  672. 3-shadow
  673. 4-lightmap textures
  674. 5-shadowmask textures
  675. 6-screen
  676. 7-depth
  677. 8-reflection probe 1
  678. 9-reflection probe 2
  679. */
  680. #define M_PI 3.14159265359
  681. /* clang-format on */
  682. #define SHADER_IS_SRGB true
  683. #define SHADER_SPACE_FAR -1.0
  684. #if defined(RENDER_SHADOWS) || defined(RENDER_SHADOWS_LINEAR)
  685. #define IN_SHADOW_PASS true
  686. #else
  687. #define IN_SHADOW_PASS false
  688. #endif
  689. #define FLAGS_NON_UNIFORM_SCALE (1 << 4)
  690. /* Varyings */
  691. #if defined(COLOR_USED)
  692. in vec4 color_interp;
  693. #endif
  694. #if defined(UV_USED)
  695. in vec2 uv_interp;
  696. #endif
  697. #if defined(UV2_USED)
  698. in vec2 uv2_interp;
  699. #else
  700. #ifdef USE_LIGHTMAP
  701. in vec2 uv2_interp;
  702. #endif
  703. #endif
  704. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  705. in vec3 tangent_interp;
  706. in vec3 binormal_interp;
  707. #endif
  708. #ifdef NORMAL_USED
  709. in vec3 normal_interp;
  710. #endif
  711. in highp vec3 vertex_interp;
  712. #ifdef USE_ADDITIVE_LIGHTING
  713. in highp vec4 shadow_coord;
  714. #if defined(LIGHT_USE_PSSM2) || defined(LIGHT_USE_PSSM4)
  715. in highp vec4 shadow_coord2;
  716. #endif
  717. #ifdef LIGHT_USE_PSSM4
  718. in highp vec4 shadow_coord3;
  719. in highp vec4 shadow_coord4;
  720. #endif //LIGHT_USE_PSSM4
  721. #endif
  722. #ifdef USE_RADIANCE_MAP
  723. #define RADIANCE_MAX_LOD 5.0
  724. uniform samplerCube radiance_map; // texunit:-2
  725. #endif // USE_RADIANCE_MAP
  726. #ifndef DISABLE_REFLECTION_PROBE
  727. #define REFLECTION_PROBE_MAX_LOD 8.0
  728. uniform bool refprobe1_use_box_project;
  729. uniform highp vec3 refprobe1_box_extents;
  730. uniform vec3 refprobe1_box_offset;
  731. uniform highp mat4 refprobe1_local_matrix;
  732. uniform bool refprobe1_exterior;
  733. uniform float refprobe1_intensity;
  734. uniform float refprobe1_blend_distance;
  735. uniform int refprobe1_ambient_mode;
  736. uniform vec4 refprobe1_ambient_color;
  737. uniform samplerCube refprobe1_texture; // texunit:-8
  738. #ifdef SECOND_REFLECTION_PROBE
  739. uniform bool refprobe2_use_box_project;
  740. uniform highp vec3 refprobe2_box_extents;
  741. uniform vec3 refprobe2_box_offset;
  742. uniform highp mat4 refprobe2_local_matrix;
  743. uniform bool refprobe2_exterior;
  744. uniform float refprobe2_intensity;
  745. uniform float refprobe2_blend_distance;
  746. uniform int refprobe2_ambient_mode;
  747. uniform vec4 refprobe2_ambient_color;
  748. uniform samplerCube refprobe2_texture; // texunit:-9
  749. #endif // SECOND_REFLECTION_PROBE
  750. #endif // DISABLE_REFLECTION_PROBE
  751. layout(std140) uniform GlobalShaderUniformData { //ubo:1
  752. vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS];
  753. };
  754. /* Material Uniforms */
  755. #ifdef MATERIAL_UNIFORMS_USED
  756. /* clang-format off */
  757. layout(std140) uniform MaterialUniforms { // ubo:3
  758. #MATERIAL_UNIFORMS
  759. };
  760. /* clang-format on */
  761. #endif
  762. layout(std140) uniform SceneData { // ubo:2
  763. highp mat4 projection_matrix;
  764. highp mat4 inv_projection_matrix;
  765. highp mat4 inv_view_matrix;
  766. highp mat4 view_matrix;
  767. // Used for billboards to cast correct shadows.
  768. highp mat4 main_cam_inv_view_matrix;
  769. vec2 viewport_size;
  770. vec2 screen_pixel_size;
  771. mediump vec4 ambient_light_color_energy;
  772. mediump float ambient_color_sky_mix;
  773. float pad2;
  774. float emissive_exposure_normalization;
  775. bool use_ambient_light;
  776. bool use_ambient_cubemap;
  777. bool use_reflection_cubemap;
  778. float fog_aerial_perspective;
  779. float time;
  780. mat3 radiance_inverse_xform;
  781. uint directional_light_count;
  782. float z_far;
  783. float z_near;
  784. float IBL_exposure_normalization;
  785. bool fog_enabled;
  786. uint fog_mode;
  787. float fog_density;
  788. float fog_height;
  789. float fog_height_density;
  790. float fog_depth_curve;
  791. float fog_sun_scatter;
  792. float fog_depth_begin;
  793. vec3 fog_light_color;
  794. float fog_depth_end;
  795. float shadow_bias;
  796. float luminance_multiplier;
  797. uint camera_visible_layers;
  798. bool pancake_shadows;
  799. }
  800. scene_data;
  801. #ifdef USE_MULTIVIEW
  802. layout(std140) uniform MultiviewData { // ubo:8
  803. highp mat4 projection_matrix_view[MAX_VIEWS];
  804. highp mat4 inv_projection_matrix_view[MAX_VIEWS];
  805. highp vec4 eye_offset[MAX_VIEWS];
  806. }
  807. multiview_data;
  808. #endif
  809. uniform highp mat4 world_transform;
  810. uniform highp uint instance_offset;
  811. uniform highp uint model_flags;
  812. /* clang-format off */
  813. #GLOBALS
  814. /* clang-format on */
  815. #define LIGHT_BAKE_DISABLED 0u
  816. #define LIGHT_BAKE_STATIC 1u
  817. #define LIGHT_BAKE_DYNAMIC 2u
  818. #ifndef MODE_RENDER_DEPTH
  819. #ifdef USE_VERTEX_LIGHTING
  820. in vec3 diffuse_light_interp;
  821. in vec3 specular_light_interp;
  822. #ifdef USE_ADDITIVE_LIGHTING
  823. in vec3 additive_diffuse_light_interp;
  824. in vec3 additive_specular_light_interp;
  825. #endif // USE_ADDITIVE_LIGHTING
  826. #endif // USE_VERTEX_LIGHTING
  827. // Directional light data.
  828. #if !defined(DISABLE_LIGHT_DIRECTIONAL) || (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT))
  829. struct DirectionalLightData {
  830. mediump vec3 direction;
  831. mediump float energy;
  832. mediump vec3 color;
  833. mediump float size;
  834. lowp uint unused;
  835. lowp uint bake_mode;
  836. mediump float shadow_opacity;
  837. mediump float specular;
  838. };
  839. layout(std140) uniform DirectionalLights { // ubo:7
  840. DirectionalLightData directional_lights[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
  841. };
  842. #if defined(USE_ADDITIVE_LIGHTING) && (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT))
  843. // Directional shadows can be in the base pass or in the additive passes
  844. uniform highp sampler2DShadow directional_shadow_atlas; // texunit:-3
  845. #endif // defined(USE_ADDITIVE_LIGHTING) && (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT))
  846. #endif // !DISABLE_LIGHT_DIRECTIONAL
  847. // Omni and spot light data.
  848. #if !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
  849. struct LightData { // This structure needs to be as packed as possible.
  850. highp vec3 position;
  851. highp float inv_radius;
  852. mediump vec3 direction;
  853. highp float size;
  854. mediump vec3 color;
  855. mediump float attenuation;
  856. mediump float cone_attenuation;
  857. mediump float cone_angle;
  858. mediump float specular_amount;
  859. mediump float shadow_opacity;
  860. lowp vec3 pad;
  861. lowp uint bake_mode;
  862. };
  863. #if !defined(DISABLE_LIGHT_OMNI) || defined(ADDITIVE_OMNI)
  864. layout(std140) uniform OmniLightData { // ubo:5
  865. LightData omni_lights[MAX_LIGHT_DATA_STRUCTS];
  866. };
  867. #if defined(BASE_PASS) && !defined(USE_VERTEX_LIGHTING)
  868. uniform uint omni_light_indices[MAX_FORWARD_LIGHTS];
  869. uniform uint omni_light_count;
  870. #endif // defined(BASE_PASS) && !defined(USE_VERTEX_LIGHTING)
  871. #endif // !defined(DISABLE_LIGHT_OMNI) || defined(ADDITIVE_OMNI)
  872. #if !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_SPOT)
  873. layout(std140) uniform SpotLightData { // ubo:6
  874. LightData spot_lights[MAX_LIGHT_DATA_STRUCTS];
  875. };
  876. #if defined(BASE_PASS) && !defined(USE_VERTEX_LIGHTING)
  877. uniform uint spot_light_indices[MAX_FORWARD_LIGHTS];
  878. uniform uint spot_light_count;
  879. #endif // defined(BASE_PASS) && !defined(USE_VERTEX_LIGHTING)
  880. #endif // !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_SPOT)
  881. #endif // !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
  882. #ifdef USE_ADDITIVE_LIGHTING
  883. #ifdef ADDITIVE_OMNI
  884. uniform highp samplerCubeShadow omni_shadow_texture; // texunit:-3
  885. uniform lowp uint omni_light_index;
  886. #endif
  887. #ifdef ADDITIVE_SPOT
  888. uniform highp sampler2DShadow spot_shadow_texture; // texunit:-3
  889. uniform lowp uint spot_light_index;
  890. #endif
  891. #if defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
  892. struct PositionalShadowData {
  893. highp mat4 shadow_matrix;
  894. highp vec3 light_position;
  895. highp float shadow_normal_bias;
  896. vec3 pad;
  897. highp float shadow_atlas_pixel_size;
  898. };
  899. layout(std140) uniform PositionalShadows { // ubo:9
  900. PositionalShadowData positional_shadows[MAX_LIGHT_DATA_STRUCTS];
  901. };
  902. uniform lowp uint positional_shadow_index;
  903. #else // ADDITIVE_DIRECTIONAL
  904. struct DirectionalShadowData {
  905. highp vec3 direction;
  906. highp float shadow_atlas_pixel_size;
  907. highp vec4 shadow_normal_bias;
  908. highp vec4 shadow_split_offsets;
  909. highp mat4 shadow_matrix1;
  910. highp mat4 shadow_matrix2;
  911. highp mat4 shadow_matrix3;
  912. highp mat4 shadow_matrix4;
  913. mediump float fade_from;
  914. mediump float fade_to;
  915. mediump vec2 pad;
  916. };
  917. layout(std140) uniform DirectionalShadows { // ubo:10
  918. DirectionalShadowData directional_shadows[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
  919. };
  920. uniform lowp uint directional_shadow_index;
  921. #endif // !(defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT))
  922. #if !defined(ADDITIVE_OMNI)
  923. float sample_shadow(highp sampler2DShadow shadow, float shadow_pixel_size, vec4 pos) {
  924. // Use textureProjLod with LOD set to 0.0 over textureProj, as textureProj not working correctly on ANGLE with Metal backend.
  925. // https://github.com/godotengine/godot/issues/93537
  926. float avg = textureProjLod(shadow, pos, 0.0);
  927. #ifdef SHADOW_MODE_PCF_13
  928. pos /= pos.w;
  929. avg += textureProjLod(shadow, vec4(pos.xy + vec2(shadow_pixel_size * 2.0, 0.0), pos.zw), 0.0);
  930. avg += textureProjLod(shadow, vec4(pos.xy + vec2(-shadow_pixel_size * 2.0, 0.0), pos.zw), 0.0);
  931. avg += textureProjLod(shadow, vec4(pos.xy + vec2(0.0, shadow_pixel_size * 2.0), pos.zw), 0.0);
  932. avg += textureProjLod(shadow, vec4(pos.xy + vec2(0.0, -shadow_pixel_size * 2.0), pos.zw), 0.0);
  933. // Early bail if distant samples are fully shaded (or none are shaded) to improve performance.
  934. if (avg <= 0.000001) {
  935. // None shaded at all.
  936. return 0.0;
  937. } else if (avg >= 4.999999) {
  938. // All fully shaded.
  939. return 1.0;
  940. }
  941. avg += textureProjLod(shadow, vec4(pos.xy + vec2(shadow_pixel_size, 0.0), pos.zw), 0.0);
  942. avg += textureProjLod(shadow, vec4(pos.xy + vec2(-shadow_pixel_size, 0.0), pos.zw), 0.0);
  943. avg += textureProjLod(shadow, vec4(pos.xy + vec2(0.0, shadow_pixel_size), pos.zw), 0.0);
  944. avg += textureProjLod(shadow, vec4(pos.xy + vec2(0.0, -shadow_pixel_size), pos.zw), 0.0);
  945. avg += textureProjLod(shadow, vec4(pos.xy + vec2(shadow_pixel_size, shadow_pixel_size), pos.zw), 0.0);
  946. avg += textureProjLod(shadow, vec4(pos.xy + vec2(-shadow_pixel_size, shadow_pixel_size), pos.zw), 0.0);
  947. avg += textureProjLod(shadow, vec4(pos.xy + vec2(shadow_pixel_size, -shadow_pixel_size), pos.zw), 0.0);
  948. avg += textureProjLod(shadow, vec4(pos.xy + vec2(-shadow_pixel_size, -shadow_pixel_size), pos.zw), 0.0);
  949. return avg * (1.0 / 13.0);
  950. #endif
  951. #ifdef SHADOW_MODE_PCF_5
  952. pos /= pos.w;
  953. avg += textureProjLod(shadow, vec4(pos.xy + vec2(shadow_pixel_size, 0.0), pos.zw), 0.0);
  954. avg += textureProjLod(shadow, vec4(pos.xy + vec2(-shadow_pixel_size, 0.0), pos.zw), 0.0);
  955. avg += textureProjLod(shadow, vec4(pos.xy + vec2(0.0, shadow_pixel_size), pos.zw), 0.0);
  956. avg += textureProjLod(shadow, vec4(pos.xy + vec2(0.0, -shadow_pixel_size), pos.zw), 0.0);
  957. return avg * (1.0 / 5.0);
  958. #endif
  959. return avg;
  960. }
  961. #endif //!defined(ADDITIVE_OMNI)
  962. #endif // USE_ADDITIVE_LIGHTING
  963. #endif // !MODE_RENDER_DEPTH
  964. #ifndef DISABLE_LIGHTMAP
  965. #ifdef USE_LIGHTMAP
  966. uniform mediump sampler2DArray lightmap_textures; //texunit:-4
  967. uniform lowp sampler2DArray shadowmask_textures; //texunit:-5
  968. uniform lowp uint lightmap_slice;
  969. uniform highp vec4 lightmap_uv_scale;
  970. uniform float lightmap_exposure_normalization;
  971. uniform uint lightmap_shadowmask_mode;
  972. #define SHADOWMASK_MODE_NONE uint(0)
  973. #define SHADOWMASK_MODE_REPLACE uint(1)
  974. #define SHADOWMASK_MODE_OVERLAY uint(2)
  975. #define SHADOWMASK_MODE_ONLY uint(3)
  976. #ifdef LIGHTMAP_BICUBIC_FILTER
  977. uniform highp vec2 lightmap_texture_size;
  978. #endif
  979. #ifdef USE_SH_LIGHTMAP
  980. uniform mediump mat3 lightmap_normal_xform;
  981. #endif // USE_SH_LIGHTMAP
  982. #endif // USE_LIGHTMAP
  983. #ifdef USE_LIGHTMAP_CAPTURE
  984. uniform mediump vec4[9] lightmap_captures;
  985. #endif // USE_LIGHTMAP_CAPTURE
  986. #endif // !DISABLE_LIGHTMAP
  987. #ifdef USE_MULTIVIEW
  988. uniform highp sampler2DArray depth_buffer; // texunit:-7
  989. uniform highp sampler2DArray color_buffer; // texunit:-6
  990. vec3 multiview_uv(vec2 uv) {
  991. return vec3(uv, ViewIndex);
  992. }
  993. ivec3 multiview_uv(ivec2 uv) {
  994. return ivec3(uv, int(ViewIndex));
  995. }
  996. #else
  997. uniform highp sampler2D depth_buffer; // texunit:-7
  998. uniform highp sampler2D color_buffer; // texunit:-6
  999. vec2 multiview_uv(vec2 uv) {
  1000. return uv;
  1001. }
  1002. ivec2 multiview_uv(ivec2 uv) {
  1003. return uv;
  1004. }
  1005. #endif
  1006. uniform mediump float opaque_prepass_threshold;
  1007. #if defined(RENDER_MATERIAL)
  1008. layout(location = 0) out vec4 albedo_output_buffer;
  1009. layout(location = 1) out vec4 normal_output_buffer;
  1010. layout(location = 2) out vec4 orm_output_buffer;
  1011. layout(location = 3) out vec4 emission_output_buffer;
  1012. #else // !RENDER_MATERIAL
  1013. // Normal color rendering.
  1014. layout(location = 0) out vec4 frag_color;
  1015. #endif // !RENDER_MATERIAL
  1016. vec3 F0(float metallic, float specular, vec3 albedo) {
  1017. float dielectric = 0.16 * specular * specular;
  1018. // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials;
  1019. // see https://google.github.io/filament/Filament.md.html
  1020. return mix(vec3(dielectric), albedo, vec3(metallic));
  1021. }
  1022. #ifndef MODE_RENDER_DEPTH
  1023. #ifndef USE_VERTEX_LIGHTING
  1024. #if !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) || defined(USE_ADDITIVE_LIGHTING)
  1025. float D_GGX(float cos_theta_m, float alpha) {
  1026. float a = cos_theta_m * alpha;
  1027. float k = alpha / (1.0 - cos_theta_m * cos_theta_m + a * a);
  1028. return k * k * (1.0 / M_PI);
  1029. }
  1030. // From Earl Hammon, Jr. "PBR Diffuse Lighting for GGX+Smith Microsurfaces" https://www.gdcvault.com/play/1024478/PBR-Diffuse-Lighting-for-GGX
  1031. float V_GGX(float NdotL, float NdotV, float alpha) {
  1032. return 0.5 / mix(2.0 * NdotL * NdotV, NdotL + NdotV, alpha);
  1033. }
  1034. float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) {
  1035. float alpha2 = alpha_x * alpha_y;
  1036. highp vec3 v = vec3(alpha_y * cos_phi, alpha_x * sin_phi, alpha2 * cos_theta_m);
  1037. highp float v2 = dot(v, v);
  1038. float w2 = alpha2 / v2;
  1039. float D = alpha2 * w2 * w2 * (1.0 / M_PI);
  1040. return D;
  1041. }
  1042. float V_GGX_anisotropic(float alpha_x, float alpha_y, float TdotV, float TdotL, float BdotV, float BdotL, float NdotV, float NdotL) {
  1043. float Lambda_V = NdotL * length(vec3(alpha_x * TdotV, alpha_y * BdotV, NdotV));
  1044. float Lambda_L = NdotV * length(vec3(alpha_x * TdotL, alpha_y * BdotL, NdotL));
  1045. return 0.5 / (Lambda_V + Lambda_L);
  1046. }
  1047. float SchlickFresnel(float u) {
  1048. float m = 1.0 - u;
  1049. float m2 = m * m;
  1050. return m2 * m2 * m; // pow(m,5)
  1051. }
  1052. void light_compute(vec3 N, vec3 L, vec3 V, float A, vec3 light_color, bool is_directional, float attenuation, vec3 f0, float roughness, float metallic, float specular_amount, vec3 albedo, inout float alpha, vec2 screen_uv,
  1053. #ifdef LIGHT_BACKLIGHT_USED
  1054. vec3 backlight,
  1055. #endif
  1056. #ifdef LIGHT_RIM_USED
  1057. float rim, float rim_tint,
  1058. #endif
  1059. #ifdef LIGHT_CLEARCOAT_USED
  1060. float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
  1061. #endif
  1062. #ifdef LIGHT_ANISOTROPY_USED
  1063. vec3 B, vec3 T, float anisotropy,
  1064. #endif
  1065. inout vec3 diffuse_light, inout vec3 specular_light) {
  1066. #if defined(LIGHT_CODE_USED)
  1067. // light is written by the light shader
  1068. highp mat4 model_matrix = world_transform;
  1069. mat4 projection_matrix = scene_data.projection_matrix;
  1070. mat4 inv_projection_matrix = scene_data.inv_projection_matrix;
  1071. vec3 normal = N;
  1072. vec3 light = L;
  1073. vec3 view = V;
  1074. /* clang-format off */
  1075. #CODE : LIGHT
  1076. /* clang-format on */
  1077. #else
  1078. float NdotL = min(A + dot(N, L), 1.0);
  1079. float cNdotL = max(NdotL, 0.0); // clamped NdotL
  1080. float NdotV = dot(N, V);
  1081. float cNdotV = max(NdotV, 1e-4);
  1082. #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED)
  1083. vec3 H = normalize(V + L);
  1084. #endif
  1085. #if defined(SPECULAR_SCHLICK_GGX)
  1086. float cNdotH = clamp(A + dot(N, H), 0.0, 1.0);
  1087. #endif
  1088. #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED)
  1089. float cLdotH = clamp(A + dot(L, H), 0.0, 1.0);
  1090. #endif
  1091. if (metallic < 1.0) {
  1092. float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance
  1093. #if defined(DIFFUSE_LAMBERT_WRAP)
  1094. // Energy conserving lambert wrap shader.
  1095. // https://web.archive.org/web/20210228210901/http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/
  1096. diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))) * (1.0 / M_PI);
  1097. #elif defined(DIFFUSE_TOON)
  1098. diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL) * (1.0 / M_PI);
  1099. #elif defined(DIFFUSE_BURLEY)
  1100. {
  1101. float FD90_minus_1 = 2.0 * cLdotH * cLdotH * roughness - 0.5;
  1102. float FdV = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotV);
  1103. float FdL = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotL);
  1104. diffuse_brdf_NL = (1.0 / M_PI) * FdV * FdL * cNdotL;
  1105. }
  1106. #else
  1107. // Lambert
  1108. diffuse_brdf_NL = cNdotL * (1.0 / M_PI);
  1109. #endif
  1110. diffuse_light += light_color * diffuse_brdf_NL * attenuation;
  1111. #if defined(LIGHT_BACKLIGHT_USED)
  1112. diffuse_light += light_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * backlight * attenuation;
  1113. #endif
  1114. #if defined(LIGHT_RIM_USED)
  1115. // Epsilon min to prevent pow(0, 0) singularity which results in undefined behavior.
  1116. float rim_light = pow(max(1e-4, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0));
  1117. diffuse_light += rim_light * rim * mix(vec3(1.0), albedo, rim_tint) * light_color;
  1118. #endif
  1119. }
  1120. if (roughness > 0.0) { // FIXME: roughness == 0 should not disable specular light entirely
  1121. // D
  1122. #if defined(SPECULAR_TOON)
  1123. vec3 R = normalize(-reflect(L, N));
  1124. float RdotV = dot(R, V);
  1125. float mid = 1.0 - roughness;
  1126. mid *= mid;
  1127. float intensity = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid;
  1128. diffuse_light += light_color * intensity * attenuation * specular_amount; // write to diffuse_light, as in toon shading you generally want no reflection
  1129. #elif defined(SPECULAR_DISABLED)
  1130. // none..
  1131. #elif defined(SPECULAR_SCHLICK_GGX)
  1132. // shlick+ggx as default
  1133. float alpha_ggx = roughness * roughness;
  1134. #if defined(LIGHT_ANISOTROPY_USED)
  1135. float aspect = sqrt(1.0 - anisotropy * 0.9);
  1136. float ax = alpha_ggx / aspect;
  1137. float ay = alpha_ggx * aspect;
  1138. float XdotH = dot(T, H);
  1139. float YdotH = dot(B, H);
  1140. float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH);
  1141. float G = V_GGX_anisotropic(ax, ay, dot(T, V), dot(T, L), dot(B, V), dot(B, L), cNdotV, cNdotL);
  1142. #else
  1143. float D = D_GGX(cNdotH, alpha_ggx);
  1144. float G = V_GGX(cNdotL, cNdotV, alpha_ggx);
  1145. #endif // LIGHT_ANISOTROPY_USED
  1146. // F
  1147. float cLdotH5 = SchlickFresnel(cLdotH);
  1148. // Calculate Fresnel using cheap approximate specular occlusion term from Filament:
  1149. // https://google.github.io/filament/Filament.html#lighting/occlusion/specularocclusion
  1150. float f90 = clamp(50.0 * f0.g, 0.0, 1.0);
  1151. vec3 F = f0 + (f90 - f0) * cLdotH5;
  1152. vec3 specular_brdf_NL = cNdotL * D * F * G;
  1153. specular_light += specular_brdf_NL * light_color * attenuation * specular_amount;
  1154. #endif
  1155. #if defined(LIGHT_CLEARCOAT_USED)
  1156. // Clearcoat ignores normal_map, use vertex normal instead
  1157. float ccNdotL = max(min(A + dot(vertex_normal, L), 1.0), 0.0);
  1158. float ccNdotH = clamp(A + dot(vertex_normal, H), 0.0, 1.0);
  1159. float ccNdotV = max(dot(vertex_normal, V), 1e-4);
  1160. #if !defined(SPECULAR_SCHLICK_GGX)
  1161. float cLdotH5 = SchlickFresnel(cLdotH);
  1162. #endif
  1163. float Dr = D_GGX(ccNdotH, mix(0.001, 0.1, clearcoat_roughness));
  1164. float Gr = 0.25 / (cLdotH * cLdotH + 1e-4);
  1165. float Fr = mix(.04, 1.0, cLdotH5);
  1166. float clearcoat_specular_brdf_NL = clearcoat * Gr * Fr * Dr * cNdotL;
  1167. specular_light += clearcoat_specular_brdf_NL * light_color * attenuation * specular_amount;
  1168. // 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)
  1169. // but to do so we need to rearrange this entire function
  1170. #endif // LIGHT_CLEARCOAT_USED
  1171. }
  1172. #ifdef USE_SHADOW_TO_OPACITY
  1173. alpha = min(alpha, clamp(1.0 - attenuation, 0.0, 1.0));
  1174. #endif
  1175. #endif // LIGHT_CODE_USED
  1176. }
  1177. float get_omni_spot_attenuation(float distance, float inv_range, float decay) {
  1178. float nd = distance * inv_range;
  1179. nd *= nd;
  1180. nd *= nd; // nd^4
  1181. nd = max(1.0 - nd, 0.0);
  1182. nd *= nd; // nd^2
  1183. return nd * pow(max(distance, 0.0001), -decay);
  1184. }
  1185. #if !defined(DISABLE_LIGHT_OMNI) || defined(ADDITIVE_OMNI)
  1186. 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, vec2 screen_uv,
  1187. #ifdef LIGHT_BACKLIGHT_USED
  1188. vec3 backlight,
  1189. #endif
  1190. #ifdef LIGHT_RIM_USED
  1191. float rim, float rim_tint,
  1192. #endif
  1193. #ifdef LIGHT_CLEARCOAT_USED
  1194. float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
  1195. #endif
  1196. #ifdef LIGHT_ANISOTROPY_USED
  1197. vec3 binormal, vec3 tangent, float anisotropy,
  1198. #endif
  1199. inout vec3 diffuse_light, inout vec3 specular_light) {
  1200. vec3 light_rel_vec = omni_lights[idx].position - vertex;
  1201. float light_length = length(light_rel_vec);
  1202. float omni_attenuation = get_omni_spot_attenuation(light_length, omni_lights[idx].inv_radius, omni_lights[idx].attenuation);
  1203. vec3 color = omni_lights[idx].color;
  1204. float size_A = 0.0;
  1205. if (omni_lights[idx].size > 0.0) {
  1206. float t = omni_lights[idx].size / max(0.001, light_length);
  1207. size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t));
  1208. }
  1209. omni_attenuation *= shadow;
  1210. light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, omni_attenuation, f0, roughness, metallic, omni_lights[idx].specular_amount, albedo, alpha, screen_uv,
  1211. #ifdef LIGHT_BACKLIGHT_USED
  1212. backlight,
  1213. #endif
  1214. #ifdef LIGHT_RIM_USED
  1215. rim * omni_attenuation, rim_tint,
  1216. #endif
  1217. #ifdef LIGHT_CLEARCOAT_USED
  1218. clearcoat, clearcoat_roughness, vertex_normal,
  1219. #endif
  1220. #ifdef LIGHT_ANISOTROPY_USED
  1221. binormal, tangent, anisotropy,
  1222. #endif
  1223. diffuse_light,
  1224. specular_light);
  1225. }
  1226. #endif // !DISABLE_LIGHT_OMNI
  1227. #if !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_SPOT)
  1228. 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, vec2 screen_uv,
  1229. #ifdef LIGHT_BACKLIGHT_USED
  1230. vec3 backlight,
  1231. #endif
  1232. #ifdef LIGHT_RIM_USED
  1233. float rim, float rim_tint,
  1234. #endif
  1235. #ifdef LIGHT_CLEARCOAT_USED
  1236. float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
  1237. #endif
  1238. #ifdef LIGHT_ANISOTROPY_USED
  1239. vec3 binormal, vec3 tangent, float anisotropy,
  1240. #endif
  1241. inout vec3 diffuse_light,
  1242. inout vec3 specular_light) {
  1243. vec3 light_rel_vec = spot_lights[idx].position - vertex;
  1244. float light_length = length(light_rel_vec);
  1245. float spot_attenuation = get_omni_spot_attenuation(light_length, spot_lights[idx].inv_radius, spot_lights[idx].attenuation);
  1246. vec3 spot_dir = spot_lights[idx].direction;
  1247. float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights[idx].cone_angle);
  1248. float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights[idx].cone_angle));
  1249. mediump float cone_attenuation = spot_lights[idx].cone_attenuation;
  1250. spot_attenuation *= 1.0 - pow(spot_rim, cone_attenuation);
  1251. vec3 color = spot_lights[idx].color;
  1252. float size_A = 0.0;
  1253. if (spot_lights[idx].size > 0.0) {
  1254. float t = spot_lights[idx].size / max(0.001, light_length);
  1255. size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t));
  1256. }
  1257. spot_attenuation *= shadow;
  1258. light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, spot_attenuation, f0, roughness, metallic, spot_lights[idx].specular_amount, albedo, alpha, screen_uv,
  1259. #ifdef LIGHT_BACKLIGHT_USED
  1260. backlight,
  1261. #endif
  1262. #ifdef LIGHT_RIM_USED
  1263. rim * spot_attenuation, rim_tint,
  1264. #endif
  1265. #ifdef LIGHT_CLEARCOAT_USED
  1266. clearcoat, clearcoat_roughness, vertex_normal,
  1267. #endif
  1268. #ifdef LIGHT_ANISOTROPY_USED
  1269. binormal, tangent, anisotropy,
  1270. #endif
  1271. diffuse_light, specular_light);
  1272. }
  1273. #endif // !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_SPOT)
  1274. #endif // !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT)
  1275. #endif // !USE_VERTEX_LIGHTING
  1276. vec4 fog_process(vec3 vertex) {
  1277. vec3 fog_color = scene_data.fog_light_color;
  1278. #ifdef USE_RADIANCE_MAP
  1279. /*
  1280. if (scene_data.fog_aerial_perspective > 0.0) {
  1281. vec3 sky_fog_color = vec3(0.0);
  1282. vec3 cube_view = scene_data.radiance_inverse_xform * vertex;
  1283. // mip_level always reads from the second mipmap and higher so the fog is always slightly blurred
  1284. 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));
  1285. sky_fog_color = textureLod(radiance_map, cube_view, mip_level * RADIANCE_MAX_LOD).rgb;
  1286. fog_color = mix(fog_color, sky_fog_color, scene_data.fog_aerial_perspective);
  1287. }
  1288. */
  1289. #endif
  1290. #ifndef DISABLE_LIGHT_DIRECTIONAL
  1291. if (scene_data.fog_sun_scatter > 0.001) {
  1292. vec4 sun_scatter = vec4(0.0);
  1293. float sun_total = 0.0;
  1294. vec3 view = normalize(vertex);
  1295. for (uint i = uint(0); i < scene_data.directional_light_count; i++) {
  1296. vec3 light_color = directional_lights[i].color * directional_lights[i].energy;
  1297. float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0);
  1298. fog_color += light_color * light_amount * scene_data.fog_sun_scatter;
  1299. }
  1300. }
  1301. #endif // !DISABLE_LIGHT_DIRECTIONAL
  1302. float fog_amount = 0.0;
  1303. #ifdef USE_DEPTH_FOG
  1304. float fog_z = smoothstep(scene_data.fog_depth_begin, scene_data.fog_depth_end, length(vertex));
  1305. fog_amount = pow(fog_z, scene_data.fog_depth_curve) * scene_data.fog_density;
  1306. #else
  1307. fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density));
  1308. #endif // USE_DEPTH_FOG
  1309. if (abs(scene_data.fog_height_density) >= 0.0001) {
  1310. float y = (scene_data.inv_view_matrix * vec4(vertex, 1.0)).y;
  1311. float y_dist = y - scene_data.fog_height;
  1312. float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data.fog_height_density));
  1313. fog_amount = max(vfog_amount, fog_amount);
  1314. }
  1315. return vec4(fog_color, fog_amount);
  1316. }
  1317. #ifndef DISABLE_REFLECTION_PROBE
  1318. #define REFLECTION_AMBIENT_DISABLED 0
  1319. #define REFLECTION_AMBIENT_ENVIRONMENT 1
  1320. #define REFLECTION_AMBIENT_COLOR 2
  1321. void reflection_process(samplerCube reflection_map,
  1322. vec3 normal, vec3 vertex,
  1323. mat4 local_matrix,
  1324. bool use_box_project, vec3 box_extents, vec3 box_offset,
  1325. bool exterior, float intensity, float blend_distance, int ref_ambient_mode, vec4 ref_ambient_color,
  1326. float roughness, vec3 ambient, vec3 skybox,
  1327. inout highp vec4 reflection_accum, inout highp vec4 ambient_accum) {
  1328. vec4 reflection;
  1329. vec3 local_pos = (local_matrix * vec4(vertex, 1.0)).xyz;
  1330. if (any(greaterThan(abs(local_pos), box_extents))) { //out of the reflection box
  1331. return;
  1332. }
  1333. float blend = 1.0;
  1334. if (blend_distance != 0.0) {
  1335. vec3 axis_blend_distance = min(vec3(blend_distance), box_extents);
  1336. vec3 blend_axes = abs(local_pos) - box_extents + axis_blend_distance;
  1337. blend_axes /= axis_blend_distance;
  1338. blend_axes = clamp(1.0 - blend_axes, vec3(0.0), vec3(1.0));
  1339. blend = pow(blend_axes.x * blend_axes.y * blend_axes.z, 2.0);
  1340. }
  1341. //reflect and make local
  1342. vec3 ref_normal = normalize(reflect(vertex, normal));
  1343. ref_normal = (local_matrix * vec4(ref_normal, 0.0)).xyz;
  1344. if (use_box_project) { //box project
  1345. vec3 nrdir = normalize(ref_normal);
  1346. vec3 rbmax = (box_extents - local_pos) / nrdir;
  1347. vec3 rbmin = (-box_extents - local_pos) / nrdir;
  1348. vec3 rbminmax = mix(rbmin, rbmax, vec3(greaterThan(nrdir, vec3(0.0, 0.0, 0.0))));
  1349. float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z);
  1350. vec3 posonbox = local_pos + nrdir * fa;
  1351. ref_normal = posonbox - box_offset.xyz;
  1352. }
  1353. reflection.rgb = srgb_to_linear(textureLod(reflection_map, ref_normal, roughness * MAX_ROUGHNESS_LOD).rgb);
  1354. if (exterior) {
  1355. reflection.rgb = mix(skybox, reflection.rgb, blend);
  1356. }
  1357. reflection.rgb *= intensity;
  1358. reflection.a = blend;
  1359. reflection.rgb *= blend;
  1360. reflection_accum += reflection;
  1361. #ifndef USE_LIGHTMAP
  1362. if (ref_ambient_mode == REFLECTION_AMBIENT_ENVIRONMENT) {
  1363. vec4 ambient_out;
  1364. vec3 amb_normal = (local_matrix * vec4(normal, 0.0)).xyz;
  1365. ambient_out.rgb = srgb_to_linear(textureLod(reflection_map, amb_normal, MAX_ROUGHNESS_LOD).rgb);
  1366. if (exterior) {
  1367. ambient_out.rgb = mix(ambient, ambient_out.rgb, blend);
  1368. }
  1369. ambient_out.a = blend;
  1370. ambient_out.rgb *= blend;
  1371. ambient_accum += ambient_out;
  1372. } else if (ref_ambient_mode == REFLECTION_AMBIENT_COLOR) {
  1373. vec4 ambient_out;
  1374. ambient_out.rgb = ref_ambient_color.rgb;
  1375. if (exterior) {
  1376. ambient_out.rgb = mix(ambient, ambient_out.rgb, blend);
  1377. }
  1378. ambient_out.a = blend;
  1379. ambient_out.rgb *= blend;
  1380. ambient_accum += ambient_out;
  1381. }
  1382. #endif // USE_LIGHTMAP
  1383. }
  1384. #endif // DISABLE_REFLECTION_PROBE
  1385. #endif // !MODE_RENDER_DEPTH
  1386. #ifdef LIGHTMAP_BICUBIC_FILTER
  1387. // w0, w1, w2, and w3 are the four cubic B-spline basis functions
  1388. float w0(float a) {
  1389. return (1.0 / 6.0) * (a * (a * (-a + 3.0) - 3.0) + 1.0);
  1390. }
  1391. float w1(float a) {
  1392. return (1.0 / 6.0) * (a * a * (3.0 * a - 6.0) + 4.0);
  1393. }
  1394. float w2(float a) {
  1395. return (1.0 / 6.0) * (a * (a * (-3.0 * a + 3.0) + 3.0) + 1.0);
  1396. }
  1397. float w3(float a) {
  1398. return (1.0 / 6.0) * (a * a * a);
  1399. }
  1400. // g0 and g1 are the two amplitude functions
  1401. float g0(float a) {
  1402. return w0(a) + w1(a);
  1403. }
  1404. float g1(float a) {
  1405. return w2(a) + w3(a);
  1406. }
  1407. // h0 and h1 are the two offset functions
  1408. float h0(float a) {
  1409. return -1.0 + w1(a) / (w0(a) + w1(a));
  1410. }
  1411. float h1(float a) {
  1412. return 1.0 + w3(a) / (w2(a) + w3(a));
  1413. }
  1414. vec4 textureArray_bicubic(sampler2DArray tex, vec3 uv, vec2 texture_size) {
  1415. vec2 texel_size = vec2(1.0) / texture_size;
  1416. uv.xy = uv.xy * texture_size + vec2(0.5);
  1417. vec2 iuv = floor(uv.xy);
  1418. vec2 fuv = fract(uv.xy);
  1419. float g0x = g0(fuv.x);
  1420. float g1x = g1(fuv.x);
  1421. float h0x = h0(fuv.x);
  1422. float h1x = h1(fuv.x);
  1423. float h0y = h0(fuv.y);
  1424. float h1y = h1(fuv.y);
  1425. vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5)) * texel_size;
  1426. vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5)) * texel_size;
  1427. vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5)) * texel_size;
  1428. vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5)) * texel_size;
  1429. return (g0(fuv.y) * (g0x * texture(tex, vec3(p0, uv.z)) + g1x * texture(tex, vec3(p1, uv.z)))) +
  1430. (g1(fuv.y) * (g0x * texture(tex, vec3(p2, uv.z)) + g1x * texture(tex, vec3(p3, uv.z))));
  1431. }
  1432. #endif //LIGHTMAP_BICUBIC_FILTER
  1433. void main() {
  1434. //lay out everything, whatever is unused is optimized away anyway
  1435. vec3 vertex = vertex_interp;
  1436. #ifdef USE_MULTIVIEW
  1437. vec3 eye_offset = multiview_data.eye_offset[ViewIndex].xyz;
  1438. vec3 view = -normalize(vertex_interp - eye_offset);
  1439. mat4 projection_matrix = multiview_data.projection_matrix_view[ViewIndex];
  1440. mat4 inv_projection_matrix = multiview_data.inv_projection_matrix_view[ViewIndex];
  1441. #else
  1442. vec3 eye_offset = vec3(0.0, 0.0, 0.0);
  1443. vec3 view = -normalize(vertex_interp);
  1444. mat4 projection_matrix = scene_data.projection_matrix;
  1445. mat4 inv_projection_matrix = scene_data.inv_projection_matrix;
  1446. #endif
  1447. highp mat4 model_matrix = world_transform;
  1448. vec3 albedo = vec3(1.0);
  1449. vec3 backlight = vec3(0.0);
  1450. vec4 transmittance_color = vec4(0.0, 0.0, 0.0, 1.0);
  1451. float transmittance_depth = 0.0;
  1452. float transmittance_boost = 0.0;
  1453. float metallic = 0.0;
  1454. float specular = 0.5;
  1455. vec3 emission = vec3(0.0);
  1456. float roughness = 1.0;
  1457. float rim = 0.0;
  1458. float rim_tint = 0.0;
  1459. float clearcoat = 0.0;
  1460. float clearcoat_roughness = 0.0;
  1461. float anisotropy = 0.0;
  1462. vec2 anisotropy_flow = vec2(1.0, 0.0);
  1463. #ifdef PREMUL_ALPHA_USED
  1464. float premul_alpha = 1.0;
  1465. #endif // PREMUL_ALPHA_USED
  1466. #ifndef FOG_DISABLED
  1467. vec4 fog = vec4(0.0);
  1468. #endif // !FOG_DISABLED
  1469. #if defined(CUSTOM_RADIANCE_USED)
  1470. vec4 custom_radiance = vec4(0.0);
  1471. #endif
  1472. #if defined(CUSTOM_IRRADIANCE_USED)
  1473. vec4 custom_irradiance = vec4(0.0);
  1474. #endif
  1475. float ao = 1.0;
  1476. float ao_light_affect = 0.0;
  1477. float alpha = 1.0;
  1478. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  1479. vec3 binormal = binormal_interp;
  1480. vec3 tangent = tangent_interp;
  1481. #else
  1482. vec3 binormal = vec3(0.0);
  1483. vec3 tangent = vec3(0.0);
  1484. #endif
  1485. #ifdef NORMAL_USED
  1486. vec3 normal = normal_interp;
  1487. #if defined(DO_SIDE_CHECK)
  1488. if (!gl_FrontFacing) {
  1489. normal = -normal;
  1490. }
  1491. #endif // DO_SIDE_CHECK
  1492. #endif // NORMAL_USED
  1493. #ifdef UV_USED
  1494. vec2 uv = uv_interp;
  1495. #endif
  1496. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  1497. vec2 uv2 = uv2_interp;
  1498. #endif
  1499. #if defined(COLOR_USED)
  1500. vec4 color = color_interp;
  1501. #endif
  1502. #if defined(NORMAL_MAP_USED)
  1503. vec3 normal_map = vec3(0.5);
  1504. #endif
  1505. #if defined(BENT_NORMAL_MAP_USED)
  1506. vec3 bent_normal_map = vec3(0.5);
  1507. #endif
  1508. float normal_map_depth = 1.0;
  1509. vec2 screen_uv = gl_FragCoord.xy * scene_data.screen_pixel_size;
  1510. float sss_strength = 0.0;
  1511. #ifdef ALPHA_SCISSOR_USED
  1512. float alpha_scissor_threshold = 1.0;
  1513. #endif // ALPHA_SCISSOR_USED
  1514. #ifdef ALPHA_HASH_USED
  1515. float alpha_hash_scale = 1.0;
  1516. #endif // ALPHA_HASH_USED
  1517. #ifdef ALPHA_ANTIALIASING_EDGE_USED
  1518. float alpha_antialiasing_edge = 0.0;
  1519. vec2 alpha_texture_coordinate = vec2(0.0, 0.0);
  1520. #endif // ALPHA_ANTIALIASING_EDGE_USED
  1521. #ifdef LIGHT_VERTEX_USED
  1522. vec3 light_vertex = vertex;
  1523. #endif //LIGHT_VERTEX_USED
  1524. highp mat3 model_normal_matrix;
  1525. if (bool(model_flags & uint(FLAGS_NON_UNIFORM_SCALE))) {
  1526. model_normal_matrix = transpose(inverse(mat3(model_matrix)));
  1527. } else {
  1528. model_normal_matrix = mat3(model_matrix);
  1529. }
  1530. {
  1531. #CODE : FRAGMENT
  1532. }
  1533. // Keep albedo values in positive number range as negative values "wraparound" into positive numbers resulting in wrong colors
  1534. albedo = max(albedo, vec3(0.0));
  1535. #ifdef LIGHT_VERTEX_USED
  1536. vertex = light_vertex;
  1537. #ifdef USE_MULTIVIEW
  1538. view = -normalize(vertex - eye_offset);
  1539. #else
  1540. view = -normalize(vertex);
  1541. #endif //USE_MULTIVIEW
  1542. #endif //LIGHT_VERTEX_USED
  1543. #ifdef NORMAL_USED
  1544. vec3 geo_normal = normalize(normal);
  1545. #endif // NORMAL_USED
  1546. #ifndef USE_SHADOW_TO_OPACITY
  1547. #if defined(ALPHA_SCISSOR_USED)
  1548. #ifdef RENDER_MATERIAL
  1549. if (alpha < alpha_scissor_threshold) {
  1550. alpha = 0.0;
  1551. } else {
  1552. alpha = 1.0;
  1553. }
  1554. #else
  1555. if (alpha < alpha_scissor_threshold) {
  1556. discard;
  1557. }
  1558. alpha = 1.0;
  1559. #endif // RENDER_MATERIAL
  1560. #else
  1561. #ifdef MODE_RENDER_DEPTH
  1562. #ifdef USE_OPAQUE_PREPASS
  1563. if (alpha < opaque_prepass_threshold) {
  1564. discard;
  1565. }
  1566. #endif // USE_OPAQUE_PREPASS
  1567. #endif // MODE_RENDER_DEPTH
  1568. #endif // !ALPHA_SCISSOR_USED
  1569. #endif // !USE_SHADOW_TO_OPACITY
  1570. #if defined(NORMAL_MAP_USED)
  1571. normal_map.xy = normal_map.xy * 2.0 - 1.0;
  1572. 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.
  1573. // Tangent-space transformation is performed using unnormalized TBN vectors, per MikkTSpace.
  1574. // See: http://www.mikktspace.com/
  1575. normal = normalize(mix(normal, tangent * normal_map.x + binormal * normal_map.y + normal * normal_map.z, normal_map_depth));
  1576. #elif defined(NORMAL_USED)
  1577. normal = geo_normal;
  1578. #endif // NORMAL_MAP_USED
  1579. #ifdef LIGHT_ANISOTROPY_USED
  1580. if (anisotropy > 0.01) {
  1581. mat3 rot = mat3(normalize(tangent), normalize(binormal), normal);
  1582. // Make local to space.
  1583. tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0));
  1584. binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0));
  1585. }
  1586. #endif
  1587. #ifndef MODE_RENDER_DEPTH
  1588. #ifndef FOG_DISABLED
  1589. #ifndef CUSTOM_FOG_USED
  1590. #ifndef DISABLE_FOG
  1591. // fog must be processed as early as possible and then packed.
  1592. // to maximize VGPR usage
  1593. if (scene_data.fog_enabled) {
  1594. fog = fog_process(vertex);
  1595. }
  1596. #endif // !DISABLE_FOG
  1597. #endif // !CUSTOM_FOG_USED
  1598. uint fog_rg = packHalf2x16(fog.rg);
  1599. uint fog_ba = packHalf2x16(fog.ba);
  1600. #endif // !FOG_DISABLED
  1601. // Convert colors to linear
  1602. albedo = srgb_to_linear(albedo);
  1603. emission = srgb_to_linear(emission);
  1604. // TODO Backlight and transmittance when used
  1605. #ifndef MODE_UNSHADED
  1606. vec3 f0 = F0(metallic, specular, albedo);
  1607. vec3 specular_light = vec3(0.0, 0.0, 0.0);
  1608. vec3 diffuse_light = vec3(0.0, 0.0, 0.0);
  1609. vec3 ambient_light = vec3(0.0, 0.0, 0.0);
  1610. #ifdef BASE_PASS
  1611. /////////////////////// LIGHTING //////////////////////////////
  1612. #ifndef AMBIENT_LIGHT_DISABLED
  1613. // IBL precalculations
  1614. float ndotv = clamp(dot(normal, view), 0.0, 1.0);
  1615. vec3 F = f0 + (max(vec3(1.0 - roughness), f0) - f0) * pow(1.0 - ndotv, 5.0);
  1616. #ifdef USE_RADIANCE_MAP
  1617. if (scene_data.use_reflection_cubemap) {
  1618. #ifdef LIGHT_ANISOTROPY_USED
  1619. // https://google.github.io/filament/Filament.html#lighting/imagebasedlights/anisotropy
  1620. vec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent;
  1621. vec3 anisotropic_tangent = cross(anisotropic_direction, view);
  1622. vec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction);
  1623. vec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0)));
  1624. vec3 ref_vec = reflect(-view, bent_normal);
  1625. #else
  1626. vec3 ref_vec = reflect(-view, normal);
  1627. #endif
  1628. ref_vec = mix(ref_vec, normal, roughness * roughness);
  1629. float horizon = min(1.0 + dot(ref_vec, normal), 1.0);
  1630. ref_vec = scene_data.radiance_inverse_xform * ref_vec;
  1631. specular_light = textureLod(radiance_map, ref_vec, sqrt(roughness) * RADIANCE_MAX_LOD).rgb;
  1632. specular_light = srgb_to_linear(specular_light);
  1633. specular_light *= horizon * horizon;
  1634. specular_light *= scene_data.ambient_light_color_energy.a;
  1635. }
  1636. #endif // USE_RADIANCE_MAP
  1637. // Calculate Reflection probes
  1638. #ifndef DISABLE_REFLECTION_PROBE
  1639. vec4 ambient_accum = vec4(0.0);
  1640. {
  1641. vec4 reflection_accum = vec4(0.0);
  1642. reflection_process(refprobe1_texture, normal, vertex_interp, refprobe1_local_matrix,
  1643. refprobe1_use_box_project, refprobe1_box_extents, refprobe1_box_offset,
  1644. refprobe1_exterior, refprobe1_intensity, refprobe1_blend_distance, refprobe1_ambient_mode, refprobe1_ambient_color,
  1645. roughness, ambient_light, specular_light, reflection_accum, ambient_accum);
  1646. #ifdef SECOND_REFLECTION_PROBE
  1647. reflection_process(refprobe2_texture, normal, vertex_interp, refprobe2_local_matrix,
  1648. refprobe2_use_box_project, refprobe2_box_extents, refprobe2_box_offset,
  1649. refprobe2_exterior, refprobe2_intensity, refprobe2_blend_distance, refprobe2_ambient_mode, refprobe2_ambient_color,
  1650. roughness, ambient_light, specular_light, reflection_accum, ambient_accum);
  1651. #endif // SECOND_REFLECTION_PROBE
  1652. if (reflection_accum.a > 0.0) {
  1653. specular_light = reflection_accum.rgb / reflection_accum.a;
  1654. }
  1655. }
  1656. #endif // DISABLE_REFLECTION_PROBE
  1657. #if defined(CUSTOM_RADIANCE_USED)
  1658. specular_light = mix(specular_light, custom_radiance.rgb, custom_radiance.a);
  1659. #endif // CUSTOM_RADIANCE_USED
  1660. #if !defined(USE_LIGHTMAP) && !defined(USE_LIGHTMAP_CAPTURE)
  1661. //lightmap overrides everything
  1662. if (scene_data.use_ambient_light) {
  1663. ambient_light = scene_data.ambient_light_color_energy.rgb;
  1664. #ifdef USE_RADIANCE_MAP
  1665. if (scene_data.use_ambient_cubemap) {
  1666. vec3 ambient_dir = scene_data.radiance_inverse_xform * normal;
  1667. vec3 cubemap_ambient = textureLod(radiance_map, ambient_dir, RADIANCE_MAX_LOD).rgb;
  1668. cubemap_ambient = srgb_to_linear(cubemap_ambient);
  1669. ambient_light = mix(ambient_light, cubemap_ambient * scene_data.ambient_light_color_energy.a, scene_data.ambient_color_sky_mix);
  1670. }
  1671. #endif // USE_RADIANCE_MAP
  1672. #ifndef DISABLE_REFLECTION_PROBE
  1673. if (ambient_accum.a > 0.0) {
  1674. ambient_light = mix(ambient_light, (ambient_accum.rgb / ambient_accum.a) * scene_data.ambient_light_color_energy.a, scene_data.ambient_color_sky_mix);
  1675. }
  1676. #endif // DISABLE_REFLECTION_PROBE
  1677. }
  1678. #endif // !USE_LIGHTMAP && !USE_LIGHTMAP_CAPTURE
  1679. #if defined(CUSTOM_IRRADIANCE_USED)
  1680. ambient_light = mix(ambient_light, custom_irradiance.rgb, custom_irradiance.a);
  1681. #endif // CUSTOM_IRRADIANCE_USED
  1682. #ifndef DISABLE_LIGHTMAP
  1683. #ifdef USE_LIGHTMAP_CAPTURE
  1684. {
  1685. // The world normal.
  1686. vec3 wnormal = mat3(scene_data.inv_view_matrix) * normal;
  1687. // The SH coefficients used for evaluating diffuse data from SH probes.
  1688. const float c0 = 0.886227; // l0 sqrt(1.0/(4.0*PI)) * PI
  1689. const float c1 = 1.023327; // l1 sqrt(3.0/(4.0*PI)) * PI*2.0/3.0
  1690. const float c2 = 0.858086; // l2n2, l2n1, l2p1 sqrt(15.0/(4.0*PI)) * PI*1.0/4.0
  1691. const float c3 = 0.247708; // l20 sqrt(5.0/(16.0*PI)) * PI*1.0/4.0
  1692. const float c4 = 0.429043; // l2p2 sqrt(15.0/(16.0*PI))* PI*1.0/4.0
  1693. ambient_light += (c0 * lightmap_captures[0].rgb +
  1694. c1 * lightmap_captures[1].rgb * wnormal.y +
  1695. c1 * lightmap_captures[2].rgb * wnormal.z +
  1696. c1 * lightmap_captures[3].rgb * wnormal.x +
  1697. c2 * lightmap_captures[4].rgb * wnormal.x * wnormal.y +
  1698. c2 * lightmap_captures[5].rgb * wnormal.y * wnormal.z +
  1699. c3 * lightmap_captures[6].rgb * (3.0 * wnormal.z * wnormal.z - 1.0) +
  1700. c2 * lightmap_captures[7].rgb * wnormal.x * wnormal.z +
  1701. c4 * lightmap_captures[8].rgb * (wnormal.x * wnormal.x - wnormal.y * wnormal.y)) *
  1702. scene_data.IBL_exposure_normalization;
  1703. }
  1704. #else
  1705. #ifdef USE_LIGHTMAP
  1706. {
  1707. vec3 uvw;
  1708. uvw.xy = uv2 * lightmap_uv_scale.zw + lightmap_uv_scale.xy;
  1709. uvw.z = float(lightmap_slice);
  1710. #ifdef USE_SH_LIGHTMAP
  1711. uvw.z *= 4.0; // SH textures use 4 times more data.
  1712. #ifdef LIGHTMAP_BICUBIC_FILTER
  1713. vec3 lm_light_l0 = textureArray_bicubic(lightmap_textures, uvw + vec3(0.0, 0.0, 0.0), lightmap_texture_size).rgb;
  1714. vec3 lm_light_l1n1 = (textureArray_bicubic(lightmap_textures, uvw + vec3(0.0, 0.0, 1.0), lightmap_texture_size).rgb - vec3(0.5)) * 2.0;
  1715. vec3 lm_light_l1_0 = (textureArray_bicubic(lightmap_textures, uvw + vec3(0.0, 0.0, 2.0), lightmap_texture_size).rgb - vec3(0.5)) * 2.0;
  1716. vec3 lm_light_l1p1 = (textureArray_bicubic(lightmap_textures, uvw + vec3(0.0, 0.0, 3.0), lightmap_texture_size).rgb - vec3(0.5)) * 2.0;
  1717. #else
  1718. vec3 lm_light_l0 = textureLod(lightmap_textures, uvw + vec3(0.0, 0.0, 0.0), 0.0).rgb;
  1719. vec3 lm_light_l1n1 = (textureLod(lightmap_textures, uvw + vec3(0.0, 0.0, 1.0), 0.0).rgb - vec3(0.5)) * 2.0;
  1720. vec3 lm_light_l1_0 = (textureLod(lightmap_textures, uvw + vec3(0.0, 0.0, 2.0), 0.0).rgb - vec3(0.5)) * 2.0;
  1721. vec3 lm_light_l1p1 = (textureLod(lightmap_textures, uvw + vec3(0.0, 0.0, 3.0), 0.0).rgb - vec3(0.5)) * 2.0;
  1722. #endif
  1723. vec3 n = normalize(lightmap_normal_xform * normal);
  1724. ambient_light += lm_light_l0 * lightmap_exposure_normalization;
  1725. ambient_light += lm_light_l1n1 * n.y * (lm_light_l0 * lightmap_exposure_normalization * 4.0);
  1726. ambient_light += lm_light_l1_0 * n.z * (lm_light_l0 * lightmap_exposure_normalization * 4.0);
  1727. ambient_light += lm_light_l1p1 * n.x * (lm_light_l0 * lightmap_exposure_normalization * 4.0);
  1728. #else
  1729. #ifdef LIGHTMAP_BICUBIC_FILTER
  1730. ambient_light += textureArray_bicubic(lightmap_textures, uvw, lightmap_texture_size).rgb * lightmap_exposure_normalization;
  1731. #else
  1732. ambient_light += textureLod(lightmap_textures, uvw, 0.0).rgb * lightmap_exposure_normalization;
  1733. #endif
  1734. #endif
  1735. }
  1736. #endif // USE_LIGHTMAP
  1737. #endif // USE_LIGHTMAP_CAPTURE
  1738. #endif // !DISABLE_LIGHTMAP
  1739. ambient_light *= ao;
  1740. #ifndef SPECULAR_OCCLUSION_DISABLED
  1741. float specular_occlusion = (ambient_light.r * 0.3 + ambient_light.g * 0.59 + ambient_light.b * 0.11) * 2.0; // Luminance of ambient light.
  1742. specular_occlusion = min(specular_occlusion * 4.0, 1.0); // This multiplication preserves speculars on bright areas.
  1743. float reflective_f = (1.0 - roughness) * metallic;
  1744. // 10.0 is a magic number, it gives the intended effect in most scenarios.
  1745. // Low enough for occlusion, high enough for reaction to lights and shadows.
  1746. specular_occlusion = max(min(reflective_f * specular_occlusion * 10.0, 1.0), specular_occlusion);
  1747. specular_light *= specular_occlusion;
  1748. #endif // !SPECULAR_OCCLUSION_DISABLED
  1749. ambient_light *= albedo.rgb;
  1750. #endif // !AMBIENT_LIGHT_DISABLED
  1751. // convert ao to direct light ao
  1752. ao = mix(1.0, ao, ao_light_affect);
  1753. #ifndef AMBIENT_LIGHT_DISABLED
  1754. {
  1755. #if defined(DIFFUSE_TOON)
  1756. //simplify for toon, as
  1757. specular_light *= specular * metallic * albedo * 2.0;
  1758. #else
  1759. // scales the specular reflections, needs to be be computed before lighting happens,
  1760. // but after environment, GI, and reflection probes are added
  1761. // Environment brdf approximation (Lazarov 2013)
  1762. // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
  1763. const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
  1764. const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);
  1765. vec4 r = roughness * c0 + c1;
  1766. float ndotv = clamp(dot(normal, view), 0.0, 1.0);
  1767. float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y;
  1768. vec2 env = vec2(-1.04, 1.04) * a004 + r.zw;
  1769. specular_light *= env.x * f0 + env.y * clamp(50.0 * f0.g, metallic, 1.0);
  1770. #endif
  1771. }
  1772. #endif // !AMBIENT_LIGHT_DISABLED
  1773. #ifdef USE_VERTEX_LIGHTING
  1774. specular_light += specular_light_interp * f0;
  1775. diffuse_light += diffuse_light_interp;
  1776. #else
  1777. #ifndef DISABLE_LIGHT_DIRECTIONAL
  1778. for (uint i = uint(0); i < scene_data.directional_light_count; i++) {
  1779. #if defined(USE_LIGHTMAP) && !defined(DISABLE_LIGHTMAP)
  1780. if (directional_lights[i].bake_mode == LIGHT_BAKE_STATIC) {
  1781. continue;
  1782. }
  1783. #endif
  1784. light_compute(normal, normalize(directional_lights[i].direction), normalize(view), directional_lights[i].size, directional_lights[i].color * directional_lights[i].energy, true, 1.0, f0, roughness, metallic, directional_lights[i].specular, albedo, alpha, screen_uv,
  1785. #ifdef LIGHT_BACKLIGHT_USED
  1786. backlight,
  1787. #endif
  1788. #ifdef LIGHT_RIM_USED
  1789. rim, rim_tint,
  1790. #endif
  1791. #ifdef LIGHT_CLEARCOAT_USED
  1792. clearcoat, clearcoat_roughness, geo_normal,
  1793. #endif // LIGHT_CLEARCOAT_USED
  1794. #ifdef LIGHT_ANISOTROPY_USED
  1795. binormal,
  1796. tangent, anisotropy,
  1797. #endif
  1798. diffuse_light,
  1799. specular_light);
  1800. }
  1801. #endif // !DISABLE_LIGHT_DIRECTIONAL
  1802. #ifndef DISABLE_LIGHT_OMNI
  1803. for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) {
  1804. if (i >= omni_light_count) {
  1805. break;
  1806. }
  1807. light_process_omni(omni_light_indices[i], vertex, view, normal, f0, roughness, metallic, 1.0, albedo, alpha, screen_uv,
  1808. #ifdef LIGHT_BACKLIGHT_USED
  1809. backlight,
  1810. #endif
  1811. #ifdef LIGHT_RIM_USED
  1812. rim,
  1813. rim_tint,
  1814. #endif
  1815. #ifdef LIGHT_CLEARCOAT_USED
  1816. clearcoat, clearcoat_roughness, geo_normal,
  1817. #endif // LIGHT_CLEARCOAT_USED
  1818. #ifdef LIGHT_ANISOTROPY_USED
  1819. binormal, tangent, anisotropy,
  1820. #endif
  1821. diffuse_light, specular_light);
  1822. }
  1823. #endif // !DISABLE_LIGHT_OMNI
  1824. #ifndef DISABLE_LIGHT_SPOT
  1825. for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) {
  1826. if (i >= spot_light_count) {
  1827. break;
  1828. }
  1829. light_process_spot(spot_light_indices[i], vertex, view, normal, f0, roughness, metallic, 1.0, albedo, alpha, screen_uv,
  1830. #ifdef LIGHT_BACKLIGHT_USED
  1831. backlight,
  1832. #endif
  1833. #ifdef LIGHT_RIM_USED
  1834. rim,
  1835. rim_tint,
  1836. #endif
  1837. #ifdef LIGHT_CLEARCOAT_USED
  1838. clearcoat, clearcoat_roughness, geo_normal,
  1839. #endif // LIGHT_CLEARCOAT_USED
  1840. #ifdef LIGHT_ANISOTROPY_USED
  1841. tangent,
  1842. binormal, anisotropy,
  1843. #endif
  1844. diffuse_light, specular_light);
  1845. }
  1846. #endif // !DISABLE_LIGHT_SPOT
  1847. #endif // !USE_VERTEX_LIGHTING
  1848. #endif // BASE_PASS
  1849. #endif // !MODE_UNSHADED
  1850. #endif // !MODE_RENDER_DEPTH
  1851. #if defined(USE_SHADOW_TO_OPACITY)
  1852. #ifndef MODE_RENDER_DEPTH
  1853. #ifndef MODE_UNSHADED
  1854. alpha = min(alpha, clamp(length(ambient_light), 0.0, 1.0));
  1855. #else
  1856. alpha = 0.0;
  1857. #endif
  1858. #if defined(ALPHA_SCISSOR_USED)
  1859. #ifdef RENDER_MATERIAL
  1860. if (alpha < alpha_scissor_threshold) {
  1861. alpha = 0.0;
  1862. } else {
  1863. alpha = 1.0;
  1864. }
  1865. #else
  1866. if (alpha < alpha_scissor_threshold) {
  1867. discard;
  1868. }
  1869. #endif // RENDER_MATERIAL
  1870. #endif // !ALPHA_SCISSOR_USED
  1871. #endif // !MODE_RENDER_DEPTH
  1872. #endif // USE_SHADOW_TO_OPACITY
  1873. #ifdef MODE_RENDER_DEPTH
  1874. #ifdef RENDER_SHADOWS_LINEAR
  1875. // Linearize the depth buffer if rendering cubemap shadows.
  1876. gl_FragDepth = (scene_data.z_far - (length(vertex) + scene_data.shadow_bias)) / scene_data.z_far;
  1877. #endif
  1878. // Nothing happens, so a tree-ssa optimizer will result in no fragment shader :)
  1879. #else // !MODE_RENDER_DEPTH
  1880. #ifdef RENDER_MATERIAL
  1881. albedo_output_buffer.rgb = albedo;
  1882. albedo_output_buffer.a = alpha;
  1883. normal_output_buffer.rgb = normal * 0.5 + 0.5;
  1884. normal_output_buffer.a = 0.0;
  1885. orm_output_buffer.r = ao;
  1886. orm_output_buffer.g = roughness;
  1887. orm_output_buffer.b = metallic;
  1888. orm_output_buffer.a = 1.0;
  1889. emission_output_buffer.rgb = emission;
  1890. emission_output_buffer.a = 0.0;
  1891. #else // !RENDER_MATERIAL
  1892. #ifdef BASE_PASS
  1893. #ifdef MODE_UNSHADED
  1894. frag_color = vec4(albedo, alpha);
  1895. #else
  1896. diffuse_light *= albedo;
  1897. diffuse_light *= 1.0 - metallic;
  1898. ambient_light *= 1.0 - metallic;
  1899. frag_color = vec4(diffuse_light + specular_light, alpha);
  1900. frag_color.rgb += emission + ambient_light;
  1901. #endif //!MODE_UNSHADED
  1902. #ifndef FOG_DISABLED
  1903. fog.xy = unpackHalf2x16(fog_rg);
  1904. fog.zw = unpackHalf2x16(fog_ba);
  1905. frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a);
  1906. #endif // !FOG_DISABLED
  1907. // Tonemap before writing as we are writing to an sRGB framebuffer
  1908. frag_color.rgb *= exposure;
  1909. #ifdef APPLY_TONEMAPPING
  1910. frag_color.rgb = apply_tonemapping(frag_color.rgb, white);
  1911. #endif
  1912. frag_color.rgb = linear_to_srgb(frag_color.rgb);
  1913. #else // !BASE_PASS
  1914. frag_color = vec4(0.0, 0.0, 0.0, alpha);
  1915. #endif // !BASE_PASS
  1916. /* ADDITIVE LIGHTING PASS */
  1917. #ifdef USE_ADDITIVE_LIGHTING
  1918. diffuse_light = vec3(0.0);
  1919. specular_light = vec3(0.0);
  1920. #ifdef USE_VERTEX_LIGHTING
  1921. diffuse_light = additive_diffuse_light_interp;
  1922. specular_light = additive_specular_light_interp * f0;
  1923. #endif // USE_VERTEX_LIGHTING
  1924. #if !defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)
  1925. #ifndef SHADOWS_DISABLED
  1926. // Baked shadowmasks
  1927. #ifdef USE_LIGHTMAP
  1928. float shadowmask = 1.0f;
  1929. if (lightmap_shadowmask_mode != SHADOWMASK_MODE_NONE) {
  1930. vec3 uvw;
  1931. uvw.xy = uv2 * lightmap_uv_scale.zw + lightmap_uv_scale.xy;
  1932. uvw.z = float(lightmap_slice);
  1933. #ifdef LIGHTMAP_BICUBIC_FILTER
  1934. shadowmask = textureArray_bicubic(shadowmask_textures, uvw, lightmap_texture_size).x;
  1935. #else
  1936. shadowmask = textureLod(shadowmask_textures, uvw, 0.0).x;
  1937. #endif
  1938. }
  1939. #endif //USE_LIGHTMAP
  1940. float directional_shadow = 1.0;
  1941. #ifdef USE_LIGHTMAP
  1942. if (lightmap_shadowmask_mode != SHADOWMASK_MODE_ONLY) {
  1943. #endif
  1944. // Orthogonal shadows
  1945. #if !defined(LIGHT_USE_PSSM2) && !defined(LIGHT_USE_PSSM4)
  1946. directional_shadow = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord);
  1947. #endif // !defined(LIGHT_USE_PSSM2) && !defined(LIGHT_USE_PSSM4)
  1948. // PSSM2 shadows
  1949. #ifdef LIGHT_USE_PSSM2
  1950. float depth_z = -vertex.z;
  1951. vec4 light_split_offsets = directional_shadows[directional_shadow_index].shadow_split_offsets;
  1952. //take advantage of prefetch
  1953. float shadow1 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord);
  1954. float shadow2 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord2);
  1955. if (depth_z < light_split_offsets.y) {
  1956. #ifdef LIGHT_USE_PSSM_BLEND
  1957. float directional_shadow2 = 1.0;
  1958. float pssm_blend = 0.0;
  1959. bool use_blend = true;
  1960. #endif
  1961. if (depth_z < light_split_offsets.x) {
  1962. directional_shadow = shadow1;
  1963. #ifdef LIGHT_USE_PSSM_BLEND
  1964. directional_shadow2 = shadow2;
  1965. pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z);
  1966. #endif
  1967. } else {
  1968. directional_shadow = shadow2;
  1969. #ifdef LIGHT_USE_PSSM_BLEND
  1970. use_blend = false;
  1971. #endif
  1972. }
  1973. #ifdef LIGHT_USE_PSSM_BLEND
  1974. if (use_blend) {
  1975. directional_shadow = mix(directional_shadow, directional_shadow2, pssm_blend);
  1976. }
  1977. #endif
  1978. }
  1979. #endif //LIGHT_USE_PSSM2
  1980. // PSSM4 shadows
  1981. #ifdef LIGHT_USE_PSSM4
  1982. float depth_z = -vertex.z;
  1983. vec4 light_split_offsets = directional_shadows[directional_shadow_index].shadow_split_offsets;
  1984. float shadow1 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord);
  1985. float shadow2 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord2);
  1986. float shadow3 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord3);
  1987. float shadow4 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord4);
  1988. if (depth_z < light_split_offsets.w) {
  1989. #ifdef LIGHT_USE_PSSM_BLEND
  1990. float directional_shadow2 = 1.0;
  1991. float pssm_blend = 0.0;
  1992. bool use_blend = true;
  1993. #endif
  1994. if (depth_z < light_split_offsets.y) {
  1995. if (depth_z < light_split_offsets.x) {
  1996. directional_shadow = shadow1;
  1997. #ifdef LIGHT_USE_PSSM_BLEND
  1998. directional_shadow2 = shadow2;
  1999. pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z);
  2000. #endif
  2001. } else {
  2002. directional_shadow = shadow2;
  2003. #ifdef LIGHT_USE_PSSM_BLEND
  2004. directional_shadow2 = shadow3;
  2005. pssm_blend = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z);
  2006. #endif
  2007. }
  2008. } else {
  2009. if (depth_z < light_split_offsets.z) {
  2010. directional_shadow = shadow3;
  2011. #if defined(LIGHT_USE_PSSM_BLEND)
  2012. directional_shadow2 = shadow4;
  2013. pssm_blend = smoothstep(light_split_offsets.y, light_split_offsets.z, depth_z);
  2014. #endif
  2015. } else {
  2016. directional_shadow = shadow4;
  2017. #if defined(LIGHT_USE_PSSM_BLEND)
  2018. use_blend = false;
  2019. #endif
  2020. }
  2021. }
  2022. #if defined(LIGHT_USE_PSSM_BLEND)
  2023. if (use_blend) {
  2024. directional_shadow = mix(directional_shadow, directional_shadow2, pssm_blend);
  2025. }
  2026. #endif
  2027. }
  2028. #endif //LIGHT_USE_PSSM4
  2029. #ifdef USE_LIGHTMAP
  2030. if (lightmap_shadowmask_mode == SHADOWMASK_MODE_REPLACE) {
  2031. directional_shadow = mix(directional_shadow, shadowmask, smoothstep(directional_shadows[directional_shadow_index].fade_from, directional_shadows[directional_shadow_index].fade_to, vertex.z));
  2032. } else if (lightmap_shadowmask_mode == SHADOWMASK_MODE_OVERLAY) {
  2033. directional_shadow = shadowmask * mix(directional_shadow, 1.0, smoothstep(directional_shadows[directional_shadow_index].fade_from, directional_shadows[directional_shadow_index].fade_to, vertex.z));
  2034. } else {
  2035. #endif
  2036. directional_shadow = mix(directional_shadow, 1.0, smoothstep(directional_shadows[directional_shadow_index].fade_from, directional_shadows[directional_shadow_index].fade_to, vertex.z));
  2037. #ifdef USE_LIGHTMAP
  2038. }
  2039. } else { // lightmap_shadowmask_mode == SHADOWMASK_MODE_ONLY
  2040. directional_shadow = shadowmask;
  2041. }
  2042. #endif
  2043. directional_shadow = mix(1.0, directional_shadow, directional_lights[directional_shadow_index].shadow_opacity);
  2044. #else
  2045. float directional_shadow = 1.0f;
  2046. #endif // SHADOWS_DISABLED
  2047. #ifndef USE_VERTEX_LIGHTING
  2048. light_compute(normal, normalize(directional_lights[directional_shadow_index].direction), normalize(view), directional_lights[directional_shadow_index].size, directional_lights[directional_shadow_index].color * directional_lights[directional_shadow_index].energy, true, directional_shadow, f0, roughness, metallic, directional_lights[directional_shadow_index].specular, albedo, alpha, screen_uv,
  2049. #ifdef LIGHT_BACKLIGHT_USED
  2050. backlight,
  2051. #endif
  2052. #ifdef LIGHT_RIM_USED
  2053. rim, rim_tint,
  2054. #endif
  2055. #ifdef LIGHT_CLEARCOAT_USED
  2056. clearcoat, clearcoat_roughness, geo_normal,
  2057. #endif // LIGHT_CLEARCOAT_USED
  2058. #ifdef LIGHT_ANISOTROPY_USED
  2059. binormal,
  2060. tangent, anisotropy,
  2061. #endif
  2062. diffuse_light,
  2063. specular_light);
  2064. #else
  2065. // Just apply shadows to vertex lighting.
  2066. diffuse_light *= directional_shadow;
  2067. specular_light *= directional_shadow;
  2068. #endif // !USE_VERTEX_LIGHTING
  2069. #endif // !defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)
  2070. #ifdef ADDITIVE_OMNI
  2071. float omni_shadow = 1.0f;
  2072. #ifndef SHADOWS_DISABLED
  2073. vec3 light_ray = ((positional_shadows[positional_shadow_index].shadow_matrix * vec4(shadow_coord.xyz, 1.0))).xyz;
  2074. omni_shadow = texture(omni_shadow_texture, vec4(light_ray, 1.0 - length(light_ray) * omni_lights[omni_light_index].inv_radius));
  2075. omni_shadow = mix(1.0, omni_shadow, omni_lights[omni_light_index].shadow_opacity);
  2076. #endif // SHADOWS_DISABLED
  2077. #ifndef USE_VERTEX_LIGHTING
  2078. light_process_omni(omni_light_index, vertex, view, normal, f0, roughness, metallic, omni_shadow, albedo, alpha, screen_uv,
  2079. #ifdef LIGHT_BACKLIGHT_USED
  2080. backlight,
  2081. #endif
  2082. #ifdef LIGHT_RIM_USED
  2083. rim,
  2084. rim_tint,
  2085. #endif
  2086. #ifdef LIGHT_CLEARCOAT_USED
  2087. clearcoat, clearcoat_roughness, geo_normal,
  2088. #endif // LIGHT_CLEARCOAT_USED
  2089. #ifdef LIGHT_ANISOTROPY_USED
  2090. binormal, tangent, anisotropy,
  2091. #endif
  2092. diffuse_light, specular_light);
  2093. #else
  2094. // Just apply shadows to vertex lighting.
  2095. diffuse_light *= omni_shadow;
  2096. specular_light *= omni_shadow;
  2097. #endif // !USE_VERTEX_LIGHTING
  2098. #endif // ADDITIVE_OMNI
  2099. #ifdef ADDITIVE_SPOT
  2100. float spot_shadow = 1.0f;
  2101. #ifndef SHADOWS_DISABLED
  2102. spot_shadow = sample_shadow(spot_shadow_texture, positional_shadows[positional_shadow_index].shadow_atlas_pixel_size, shadow_coord);
  2103. spot_shadow = mix(1.0, spot_shadow, spot_lights[spot_light_index].shadow_opacity);
  2104. #endif // SHADOWS_DISABLED
  2105. #ifndef USE_VERTEX_LIGHTING
  2106. light_process_spot(spot_light_index, vertex, view, normal, f0, roughness, metallic, spot_shadow, albedo, alpha, screen_uv,
  2107. #ifdef LIGHT_BACKLIGHT_USED
  2108. backlight,
  2109. #endif
  2110. #ifdef LIGHT_RIM_USED
  2111. rim,
  2112. rim_tint,
  2113. #endif
  2114. #ifdef LIGHT_CLEARCOAT_USED
  2115. clearcoat, clearcoat_roughness, geo_normal,
  2116. #endif // LIGHT_RIM_USED
  2117. #ifdef LIGHT_ANISOTROPY_USED
  2118. tangent,
  2119. binormal, anisotropy,
  2120. #endif
  2121. diffuse_light, specular_light);
  2122. #else
  2123. // Just apply shadows to vertex lighting.
  2124. diffuse_light *= spot_shadow;
  2125. specular_light *= spot_shadow;
  2126. #endif // !USE_VERTEX_LIGHTING
  2127. #endif // ADDITIVE_SPOT
  2128. diffuse_light *= albedo;
  2129. diffuse_light *= 1.0 - metallic;
  2130. vec3 additive_light_color = diffuse_light + specular_light;
  2131. #ifndef FOG_DISABLED
  2132. fog.xy = unpackHalf2x16(fog_rg);
  2133. fog.zw = unpackHalf2x16(fog_ba);
  2134. additive_light_color *= (1.0 - fog.a);
  2135. #endif // !FOG_DISABLED
  2136. // Tonemap before writing as we are writing to an sRGB framebuffer
  2137. additive_light_color *= exposure;
  2138. #ifdef APPLY_TONEMAPPING
  2139. additive_light_color = apply_tonemapping(additive_light_color, white);
  2140. #endif
  2141. additive_light_color = linear_to_srgb(additive_light_color);
  2142. frag_color.rgb += additive_light_color;
  2143. #endif // USE_ADDITIVE_LIGHTING
  2144. frag_color.rgb *= scene_data.luminance_multiplier;
  2145. #endif // !RENDER_MATERIAL
  2146. #endif // !MODE_RENDER_DEPTH
  2147. #ifdef PREMUL_ALPHA_USED
  2148. frag_color.rgb *= premul_alpha;
  2149. #endif // PREMUL_ALPHA_USED
  2150. }