scene.glsl 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812
  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_FOG = false
  13. USE_RADIANCE_MAP = true
  14. USE_MULTIVIEW = false
  15. RENDER_SHADOWS = false
  16. RENDER_SHADOWS_LINEAR = false
  17. SHADOW_MODE_PCF_5 = false
  18. SHADOW_MODE_PCF_13 = false
  19. LIGHT_USE_PSSM2 = false
  20. LIGHT_USE_PSSM4 = false
  21. LIGHT_USE_PSSM_BLEND = false
  22. BASE_PASS = true
  23. USE_ADDITIVE_LIGHTING = false
  24. // We can only use one type of light per additive pass. This means that if USE_ADDITIVE_LIGHTING is defined, and
  25. // these are false, we are doing a directional light pass.
  26. ADDITIVE_OMNI = false
  27. ADDITIVE_SPOT = false
  28. #[vertex]
  29. #define M_PI 3.14159265359
  30. #define SHADER_IS_SRGB true
  31. #include "stdlib_inc.glsl"
  32. #if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED)
  33. #ifndef NORMAL_USED
  34. #define NORMAL_USED
  35. #endif
  36. #endif
  37. #ifdef MODE_UNSHADED
  38. #ifdef USE_ADDITIVE_LIGHTING
  39. #undef USE_ADDITIVE_LIGHTING
  40. #endif
  41. #endif // MODE_UNSHADED
  42. /*
  43. from RenderingServer:
  44. ARRAY_VERTEX = 0, // RGB32F or RGBA16
  45. ARRAY_NORMAL = 1, // RG16 octahedral compression or RGBA16 normal + angle
  46. ARRAY_TANGENT = 2, // RG16 octahedral compression, sign stored in sign of G
  47. ARRAY_COLOR = 3, // RGBA8
  48. ARRAY_TEX_UV = 4, // RG32F
  49. ARRAY_TEX_UV2 = 5, // RG32F
  50. ARRAY_CUSTOM0 = 6, // Depends on ArrayCustomFormat.
  51. ARRAY_CUSTOM1 = 7,
  52. ARRAY_CUSTOM2 = 8,
  53. ARRAY_CUSTOM3 = 9,
  54. ARRAY_BONES = 10, // RGBA16UI (x2 if 8 weights)
  55. ARRAY_WEIGHTS = 11, // RGBA16UNORM (x2 if 8 weights)
  56. */
  57. /* INPUT ATTRIBS */
  58. // Always contains vertex position in XYZ, can contain tangent angle in W.
  59. layout(location = 0) in highp vec4 vertex_angle_attrib;
  60. /* clang-format on */
  61. #ifdef NORMAL_USED
  62. // Contains Normal/Axis in RG, can contain tangent in BA.
  63. layout(location = 1) in vec4 axis_tangent_attrib;
  64. #endif
  65. // location 2 is unused.
  66. #if defined(COLOR_USED)
  67. layout(location = 3) in vec4 color_attrib;
  68. #endif
  69. #ifdef UV_USED
  70. layout(location = 4) in vec2 uv_attrib;
  71. #endif
  72. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  73. layout(location = 5) in vec2 uv2_attrib;
  74. #endif
  75. #if defined(CUSTOM0_USED)
  76. layout(location = 6) in vec4 custom0_attrib;
  77. #endif
  78. #if defined(CUSTOM1_USED)
  79. layout(location = 7) in vec4 custom1_attrib;
  80. #endif
  81. #if defined(CUSTOM2_USED)
  82. layout(location = 8) in vec4 custom2_attrib;
  83. #endif
  84. #if defined(CUSTOM3_USED)
  85. layout(location = 9) in vec4 custom3_attrib;
  86. #endif
  87. #if defined(BONES_USED)
  88. layout(location = 10) in uvec4 bone_attrib;
  89. #endif
  90. #if defined(WEIGHTS_USED)
  91. layout(location = 11) in vec4 weight_attrib;
  92. #endif
  93. vec3 oct_to_vec3(vec2 e) {
  94. vec3 v = vec3(e.xy, 1.0 - abs(e.x) - abs(e.y));
  95. float t = max(-v.z, 0.0);
  96. v.xy += t * -sign(v.xy);
  97. return normalize(v);
  98. }
  99. void axis_angle_to_tbn(vec3 axis, float angle, out vec3 tangent, out vec3 binormal, out vec3 normal) {
  100. float c = cos(angle);
  101. float s = sin(angle);
  102. vec3 omc_axis = (1.0 - c) * axis;
  103. vec3 s_axis = s * axis;
  104. tangent = omc_axis.xxx * axis + vec3(c, -s_axis.z, s_axis.y);
  105. binormal = omc_axis.yyy * axis + vec3(s_axis.z, c, -s_axis.x);
  106. normal = omc_axis.zzz * axis + vec3(-s_axis.y, s_axis.x, c);
  107. }
  108. #ifdef USE_INSTANCING
  109. layout(location = 12) in highp vec4 instance_xform0;
  110. layout(location = 13) in highp vec4 instance_xform1;
  111. layout(location = 14) in highp vec4 instance_xform2;
  112. layout(location = 15) in highp uvec4 instance_color_custom_data; // Color packed into xy, Custom data into zw.
  113. #endif
  114. layout(std140) uniform GlobalShaderUniformData { //ubo:1
  115. vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS];
  116. };
  117. layout(std140) uniform SceneData { // ubo:2
  118. highp mat4 projection_matrix;
  119. highp mat4 inv_projection_matrix;
  120. highp mat4 inv_view_matrix;
  121. highp mat4 view_matrix;
  122. vec2 viewport_size;
  123. vec2 screen_pixel_size;
  124. mediump vec4 ambient_light_color_energy;
  125. mediump float ambient_color_sky_mix;
  126. bool material_uv2_mode;
  127. float emissive_exposure_normalization;
  128. bool use_ambient_light;
  129. bool use_ambient_cubemap;
  130. bool use_reflection_cubemap;
  131. float fog_aerial_perspective;
  132. float time;
  133. mat3 radiance_inverse_xform;
  134. uint directional_light_count;
  135. float z_far;
  136. float z_near;
  137. float IBL_exposure_normalization;
  138. bool fog_enabled;
  139. float fog_density;
  140. float fog_height;
  141. float fog_height_density;
  142. vec3 fog_light_color;
  143. float fog_sun_scatter;
  144. float shadow_bias;
  145. float pad;
  146. uint camera_visible_layers;
  147. bool pancake_shadows;
  148. }
  149. scene_data;
  150. #ifdef USE_ADDITIVE_LIGHTING
  151. #if defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
  152. struct PositionalShadowData {
  153. highp mat4 shadow_matrix;
  154. highp vec3 light_position;
  155. highp float shadow_normal_bias;
  156. vec3 pad;
  157. highp float shadow_atlas_pixel_size;
  158. };
  159. layout(std140) uniform PositionalShadows { // ubo:9
  160. PositionalShadowData positional_shadows[MAX_LIGHT_DATA_STRUCTS];
  161. };
  162. uniform lowp uint positional_shadow_index;
  163. #else // ADDITIVE_DIRECTIONAL
  164. struct DirectionalShadowData {
  165. highp vec3 direction;
  166. highp float shadow_atlas_pixel_size;
  167. highp vec4 shadow_normal_bias;
  168. highp vec4 shadow_split_offsets;
  169. highp mat4 shadow_matrix1;
  170. highp mat4 shadow_matrix2;
  171. highp mat4 shadow_matrix3;
  172. highp mat4 shadow_matrix4;
  173. mediump float fade_from;
  174. mediump float fade_to;
  175. mediump vec2 pad;
  176. };
  177. layout(std140) uniform DirectionalShadows { // ubo:10
  178. DirectionalShadowData directional_shadows[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
  179. };
  180. uniform lowp uint directional_shadow_index;
  181. #endif // !(defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT))
  182. #endif // USE_ADDITIVE_LIGHTING
  183. #ifdef USE_MULTIVIEW
  184. layout(std140) uniform MultiviewData { // ubo:8
  185. highp mat4 projection_matrix_view[MAX_VIEWS];
  186. highp mat4 inv_projection_matrix_view[MAX_VIEWS];
  187. highp vec4 eye_offset[MAX_VIEWS];
  188. }
  189. multiview_data;
  190. #endif
  191. uniform highp mat4 world_transform;
  192. uniform highp vec3 compressed_aabb_position;
  193. uniform highp vec3 compressed_aabb_size;
  194. uniform highp vec4 uv_scale;
  195. /* Varyings */
  196. out highp vec3 vertex_interp;
  197. #ifdef NORMAL_USED
  198. out vec3 normal_interp;
  199. #endif
  200. #if defined(COLOR_USED)
  201. out vec4 color_interp;
  202. #endif
  203. #if defined(UV_USED)
  204. out vec2 uv_interp;
  205. #endif
  206. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  207. out vec2 uv2_interp;
  208. #endif
  209. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  210. out vec3 tangent_interp;
  211. out vec3 binormal_interp;
  212. #endif
  213. #ifdef USE_ADDITIVE_LIGHTING
  214. out highp vec4 shadow_coord;
  215. #if defined(LIGHT_USE_PSSM2) || defined(LIGHT_USE_PSSM4)
  216. out highp vec4 shadow_coord2;
  217. #endif
  218. #ifdef LIGHT_USE_PSSM4
  219. out highp vec4 shadow_coord3;
  220. out highp vec4 shadow_coord4;
  221. #endif //LIGHT_USE_PSSM4
  222. #endif
  223. #ifdef MATERIAL_UNIFORMS_USED
  224. /* clang-format off */
  225. layout(std140) uniform MaterialUniforms { // ubo:3
  226. #MATERIAL_UNIFORMS
  227. };
  228. /* clang-format on */
  229. #endif
  230. /* clang-format off */
  231. #GLOBALS
  232. /* clang-format on */
  233. invariant gl_Position;
  234. void main() {
  235. highp vec3 vertex = vertex_angle_attrib.xyz * compressed_aabb_size + compressed_aabb_position;
  236. highp mat4 model_matrix = world_transform;
  237. #ifdef USE_INSTANCING
  238. highp mat4 m = mat4(instance_xform0, instance_xform1, instance_xform2, vec4(0.0, 0.0, 0.0, 1.0));
  239. model_matrix = model_matrix * transpose(m);
  240. #endif
  241. #ifdef NORMAL_USED
  242. vec3 normal = oct_to_vec3(axis_tangent_attrib.xy * 2.0 - 1.0);
  243. #endif
  244. highp mat3 model_normal_matrix = mat3(model_matrix);
  245. #if defined(NORMAL_USED) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  246. vec3 binormal;
  247. float binormal_sign;
  248. vec3 tangent;
  249. if (axis_tangent_attrib.z > 0.0 || axis_tangent_attrib.w < 1.0) {
  250. // Uncompressed format.
  251. vec2 signed_tangent_attrib = axis_tangent_attrib.zw * 2.0 - 1.0;
  252. tangent = oct_to_vec3(vec2(signed_tangent_attrib.x, abs(signed_tangent_attrib.y) * 2.0 - 1.0));
  253. binormal_sign = sign(signed_tangent_attrib.y);
  254. binormal = normalize(cross(normal, tangent) * binormal_sign);
  255. } else {
  256. // Compressed format.
  257. float angle = vertex_angle_attrib.w;
  258. binormal_sign = angle > 0.5 ? 1.0 : -1.0; // 0.5 does not exist in UNORM16, so values are either greater or smaller.
  259. angle = abs(angle * 2.0 - 1.0) * M_PI; // 0.5 is basically zero, allowing to encode both signs reliably.
  260. vec3 axis = normal;
  261. axis_angle_to_tbn(axis, angle, tangent, binormal, normal);
  262. binormal *= binormal_sign;
  263. }
  264. #endif
  265. #if defined(COLOR_USED)
  266. color_interp = color_attrib;
  267. #ifdef USE_INSTANCING
  268. vec4 instance_color = vec4(unpackHalf2x16(instance_color_custom_data.x), unpackHalf2x16(instance_color_custom_data.y));
  269. color_interp *= instance_color;
  270. #endif
  271. #endif
  272. #if defined(UV_USED)
  273. uv_interp = uv_attrib;
  274. #endif
  275. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  276. uv2_interp = uv2_attrib;
  277. #endif
  278. if (uv_scale != vec4(0.0)) { // Compression enabled
  279. #ifdef UV_USED
  280. uv_interp = (uv_interp - 0.5) * uv_scale.xy;
  281. #endif
  282. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  283. uv2_interp = (uv2_interp - 0.5) * uv_scale.zw;
  284. #endif
  285. }
  286. #if defined(OVERRIDE_POSITION)
  287. highp vec4 position;
  288. #endif
  289. #ifdef USE_MULTIVIEW
  290. mat4 projection_matrix = multiview_data.projection_matrix_view[ViewIndex];
  291. mat4 inv_projection_matrix = multiview_data.inv_projection_matrix_view[ViewIndex];
  292. vec3 eye_offset = multiview_data.eye_offset[ViewIndex].xyz;
  293. #else
  294. mat4 projection_matrix = scene_data.projection_matrix;
  295. mat4 inv_projection_matrix = scene_data.inv_projection_matrix;
  296. vec3 eye_offset = vec3(0.0, 0.0, 0.0);
  297. #endif //USE_MULTIVIEW
  298. #ifdef USE_INSTANCING
  299. vec4 instance_custom = vec4(unpackHalf2x16(instance_color_custom_data.z), unpackHalf2x16(instance_color_custom_data.w));
  300. #else
  301. vec4 instance_custom = vec4(0.0);
  302. #endif
  303. // Using world coordinates
  304. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  305. vertex = (model_matrix * vec4(vertex, 1.0)).xyz;
  306. #ifdef NORMAL_USED
  307. normal = model_normal_matrix * normal;
  308. #endif
  309. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  310. tangent = model_normal_matrix * tangent;
  311. binormal = model_normal_matrix * binormal;
  312. #endif
  313. #endif
  314. float roughness = 1.0;
  315. highp mat4 modelview = scene_data.view_matrix * model_matrix;
  316. highp mat3 modelview_normal = mat3(scene_data.view_matrix) * model_normal_matrix;
  317. float point_size = 1.0;
  318. {
  319. #CODE : VERTEX
  320. }
  321. gl_PointSize = point_size;
  322. // Using local coordinates (default)
  323. #if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
  324. vertex = (modelview * vec4(vertex, 1.0)).xyz;
  325. #ifdef NORMAL_USED
  326. normal = modelview_normal * normal;
  327. #endif
  328. #endif
  329. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  330. binormal = modelview_normal * binormal;
  331. tangent = modelview_normal * tangent;
  332. #endif
  333. // Using world coordinates
  334. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  335. vertex = (scene_data.view_matrix * vec4(vertex, 1.0)).xyz;
  336. #ifdef NORMAL_USED
  337. normal = (scene_data.view_matrix * vec4(normal, 0.0)).xyz;
  338. #endif
  339. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  340. binormal = (scene_data.view_matrix * vec4(binormal, 0.0)).xyz;
  341. tangent = (scene_data.view_matrix * vec4(tangent, 0.0)).xyz;
  342. #endif
  343. #endif
  344. vertex_interp = vertex;
  345. #ifdef NORMAL_USED
  346. normal_interp = normal;
  347. #endif
  348. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  349. tangent_interp = tangent;
  350. binormal_interp = binormal;
  351. #endif
  352. // Calculate shadows.
  353. #ifdef USE_ADDITIVE_LIGHTING
  354. #if defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
  355. // Apply normal bias at draw time to avoid issues with scaling non-fused geometry.
  356. vec3 light_rel_vec = positional_shadows[positional_shadow_index].light_position - vertex_interp;
  357. float light_length = length(light_rel_vec);
  358. float aNdotL = abs(dot(normalize(normal_interp), normalize(light_rel_vec)));
  359. vec3 normal_offset = (1.0 - aNdotL) * positional_shadows[positional_shadow_index].shadow_normal_bias * light_length * normal_interp;
  360. #ifdef ADDITIVE_SPOT
  361. // Calculate coord here so we can take advantage of prefetch.
  362. shadow_coord = positional_shadows[positional_shadow_index].shadow_matrix * vec4(vertex_interp + normal_offset, 1.0);
  363. #endif
  364. #ifdef ADDITIVE_OMNI
  365. // Can't interpolate unit direction nicely, so forget about prefetch.
  366. shadow_coord = vec4(vertex_interp + normal_offset, 1.0);
  367. #endif
  368. #else // ADDITIVE_DIRECTIONAL
  369. vec3 base_normal_bias = normalize(normal_interp) * (1.0 - max(0.0, dot(directional_shadows[directional_shadow_index].direction, -normalize(normal_interp))));
  370. vec3 normal_offset = base_normal_bias * directional_shadows[directional_shadow_index].shadow_normal_bias.x;
  371. shadow_coord = directional_shadows[directional_shadow_index].shadow_matrix1 * vec4(vertex_interp + normal_offset, 1.0);
  372. #if defined(LIGHT_USE_PSSM2) || defined(LIGHT_USE_PSSM4)
  373. normal_offset = base_normal_bias * directional_shadows[directional_shadow_index].shadow_normal_bias.y;
  374. shadow_coord2 = directional_shadows[directional_shadow_index].shadow_matrix2 * vec4(vertex_interp + normal_offset, 1.0);
  375. #endif
  376. #ifdef LIGHT_USE_PSSM4
  377. normal_offset = base_normal_bias * directional_shadows[directional_shadow_index].shadow_normal_bias.z;
  378. shadow_coord3 = directional_shadows[directional_shadow_index].shadow_matrix3 * vec4(vertex_interp + normal_offset, 1.0);
  379. normal_offset = base_normal_bias * directional_shadows[directional_shadow_index].shadow_normal_bias.w;
  380. shadow_coord4 = directional_shadows[directional_shadow_index].shadow_matrix4 * vec4(vertex_interp + normal_offset, 1.0);
  381. #endif //LIGHT_USE_PSSM4
  382. #endif // !(defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT))
  383. #endif // USE_ADDITIVE_LIGHTING
  384. #if defined(RENDER_SHADOWS) && !defined(RENDER_SHADOWS_LINEAR)
  385. // This is an optimized version of normalize(vertex_interp) * scene_data.shadow_bias / length(vertex_interp).
  386. float light_length_sq = dot(vertex_interp, vertex_interp);
  387. vertex_interp += vertex_interp * scene_data.shadow_bias / light_length_sq;
  388. #endif
  389. #if defined(OVERRIDE_POSITION)
  390. gl_Position = position;
  391. #else
  392. gl_Position = projection_matrix * vec4(vertex_interp, 1.0);
  393. #endif
  394. }
  395. /* clang-format off */
  396. #[fragment]
  397. // Default to SPECULAR_SCHLICK_GGX.
  398. #if !defined(SPECULAR_DISABLED) && !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_TOON)
  399. #define SPECULAR_SCHLICK_GGX
  400. #endif
  401. #if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED)
  402. #ifndef NORMAL_USED
  403. #define NORMAL_USED
  404. #endif
  405. #endif
  406. #ifdef MODE_UNSHADED
  407. #ifdef USE_ADDITIVE_LIGHTING
  408. #undef USE_ADDITIVE_LIGHTING
  409. #endif
  410. #endif // MODE_UNSHADED
  411. #ifndef MODE_RENDER_DEPTH
  412. #include "tonemap_inc.glsl"
  413. #endif
  414. #include "stdlib_inc.glsl"
  415. /* texture unit usage, N is max_texture_unit-N
  416. 1-color correction // In tonemap_inc.glsl
  417. 2-radiance
  418. 3-shadow
  419. 5-screen
  420. 6-depth
  421. */
  422. #define M_PI 3.14159265359
  423. /* clang-format on */
  424. #define SHADER_IS_SRGB true
  425. /* Varyings */
  426. #if defined(COLOR_USED)
  427. in vec4 color_interp;
  428. #endif
  429. #if defined(UV_USED)
  430. in vec2 uv_interp;
  431. #endif
  432. #if defined(UV2_USED)
  433. in vec2 uv2_interp;
  434. #else
  435. #ifdef USE_LIGHTMAP
  436. in vec2 uv2_interp;
  437. #endif
  438. #endif
  439. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  440. in vec3 tangent_interp;
  441. in vec3 binormal_interp;
  442. #endif
  443. #ifdef NORMAL_USED
  444. in vec3 normal_interp;
  445. #endif
  446. in highp vec3 vertex_interp;
  447. #ifdef USE_ADDITIVE_LIGHTING
  448. in highp vec4 shadow_coord;
  449. #if defined(LIGHT_USE_PSSM2) || defined(LIGHT_USE_PSSM4)
  450. in highp vec4 shadow_coord2;
  451. #endif
  452. #ifdef LIGHT_USE_PSSM4
  453. in highp vec4 shadow_coord3;
  454. in highp vec4 shadow_coord4;
  455. #endif //LIGHT_USE_PSSM4
  456. #endif
  457. #ifdef USE_RADIANCE_MAP
  458. #define RADIANCE_MAX_LOD 5.0
  459. uniform samplerCube radiance_map; // texunit:-2
  460. #endif
  461. layout(std140) uniform GlobalShaderUniformData { //ubo:1
  462. vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS];
  463. };
  464. /* Material Uniforms */
  465. #ifdef MATERIAL_UNIFORMS_USED
  466. /* clang-format off */
  467. layout(std140) uniform MaterialUniforms { // ubo:3
  468. #MATERIAL_UNIFORMS
  469. };
  470. /* clang-format on */
  471. #endif
  472. layout(std140) uniform SceneData { // ubo:2
  473. highp mat4 projection_matrix;
  474. highp mat4 inv_projection_matrix;
  475. highp mat4 inv_view_matrix;
  476. highp mat4 view_matrix;
  477. vec2 viewport_size;
  478. vec2 screen_pixel_size;
  479. mediump vec4 ambient_light_color_energy;
  480. mediump float ambient_color_sky_mix;
  481. bool material_uv2_mode;
  482. float emissive_exposure_normalization;
  483. bool use_ambient_light;
  484. bool use_ambient_cubemap;
  485. bool use_reflection_cubemap;
  486. float fog_aerial_perspective;
  487. float time;
  488. mat3 radiance_inverse_xform;
  489. uint directional_light_count;
  490. float z_far;
  491. float z_near;
  492. float IBL_exposure_normalization;
  493. bool fog_enabled;
  494. float fog_density;
  495. float fog_height;
  496. float fog_height_density;
  497. vec3 fog_light_color;
  498. float fog_sun_scatter;
  499. float shadow_bias;
  500. float pad;
  501. uint camera_visible_layers;
  502. bool pancake_shadows;
  503. }
  504. scene_data;
  505. #ifdef USE_MULTIVIEW
  506. layout(std140) uniform MultiviewData { // ubo:8
  507. highp mat4 projection_matrix_view[MAX_VIEWS];
  508. highp mat4 inv_projection_matrix_view[MAX_VIEWS];
  509. highp vec4 eye_offset[MAX_VIEWS];
  510. }
  511. multiview_data;
  512. #endif
  513. /* clang-format off */
  514. #GLOBALS
  515. /* clang-format on */
  516. #ifndef MODE_RENDER_DEPTH
  517. // Directional light data.
  518. #if !defined(DISABLE_LIGHT_DIRECTIONAL) || (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT))
  519. struct DirectionalLightData {
  520. mediump vec3 direction;
  521. mediump float energy;
  522. mediump vec3 color;
  523. mediump float size;
  524. mediump vec2 pad;
  525. mediump float shadow_opacity;
  526. mediump float specular;
  527. };
  528. layout(std140) uniform DirectionalLights { // ubo:7
  529. DirectionalLightData directional_lights[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
  530. };
  531. #if defined(USE_ADDITIVE_LIGHTING) && (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT))
  532. // Directional shadows can be in the base pass or in the additive passes
  533. uniform highp sampler2DShadow directional_shadow_atlas; // texunit:-3
  534. #endif // defined(USE_ADDITIVE_LIGHTING) && (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT))
  535. #endif // !DISABLE_LIGHT_DIRECTIONAL
  536. // Omni and spot light data.
  537. #if !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
  538. struct LightData { // This structure needs to be as packed as possible.
  539. highp vec3 position;
  540. highp float inv_radius;
  541. mediump vec3 direction;
  542. highp float size;
  543. mediump vec3 color;
  544. mediump float attenuation;
  545. mediump float cone_attenuation;
  546. mediump float cone_angle;
  547. mediump float specular_amount;
  548. mediump float shadow_opacity;
  549. };
  550. #if !defined(DISABLE_LIGHT_OMNI) || defined(ADDITIVE_OMNI)
  551. layout(std140) uniform OmniLightData { // ubo:5
  552. LightData omni_lights[MAX_LIGHT_DATA_STRUCTS];
  553. };
  554. #ifdef BASE_PASS
  555. uniform uint omni_light_indices[MAX_FORWARD_LIGHTS];
  556. uniform uint omni_light_count;
  557. #endif // BASE_PASS
  558. #endif // DISABLE_LIGHT_OMNI
  559. #if !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_SPOT)
  560. layout(std140) uniform SpotLightData { // ubo:6
  561. LightData spot_lights[MAX_LIGHT_DATA_STRUCTS];
  562. };
  563. #ifdef BASE_PASS
  564. uniform uint spot_light_indices[MAX_FORWARD_LIGHTS];
  565. uniform uint spot_light_count;
  566. #endif // BASE_PASS
  567. #endif // DISABLE_LIGHT_SPOT
  568. #endif // !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT)
  569. #ifdef USE_ADDITIVE_LIGHTING
  570. #ifdef ADDITIVE_OMNI
  571. uniform highp samplerCubeShadow omni_shadow_texture; // texunit:-3
  572. uniform lowp uint omni_light_index;
  573. #endif
  574. #ifdef ADDITIVE_SPOT
  575. uniform highp sampler2DShadow spot_shadow_texture; // texunit:-3
  576. uniform lowp uint spot_light_index;
  577. #endif
  578. #if defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
  579. struct PositionalShadowData {
  580. highp mat4 shadow_matrix;
  581. highp vec3 light_position;
  582. highp float shadow_normal_bias;
  583. vec3 pad;
  584. highp float shadow_atlas_pixel_size;
  585. };
  586. layout(std140) uniform PositionalShadows { // ubo:9
  587. PositionalShadowData positional_shadows[MAX_LIGHT_DATA_STRUCTS];
  588. };
  589. uniform lowp uint positional_shadow_index;
  590. #else // ADDITIVE_DIRECTIONAL
  591. struct DirectionalShadowData {
  592. highp vec3 direction;
  593. highp float shadow_atlas_pixel_size;
  594. highp vec4 shadow_normal_bias;
  595. highp vec4 shadow_split_offsets;
  596. highp mat4 shadow_matrix1;
  597. highp mat4 shadow_matrix2;
  598. highp mat4 shadow_matrix3;
  599. highp mat4 shadow_matrix4;
  600. mediump float fade_from;
  601. mediump float fade_to;
  602. mediump vec2 pad;
  603. };
  604. layout(std140) uniform DirectionalShadows { // ubo:10
  605. DirectionalShadowData directional_shadows[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
  606. };
  607. uniform lowp uint directional_shadow_index;
  608. #endif // !(defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT))
  609. #if !defined(ADDITIVE_OMNI)
  610. float sample_shadow(highp sampler2DShadow shadow, float shadow_pixel_size, vec4 pos) {
  611. float avg = textureProj(shadow, pos);
  612. #ifdef SHADOW_MODE_PCF_13
  613. pos /= pos.w;
  614. avg += textureProj(shadow, vec4(pos.xy + vec2(shadow_pixel_size * 2.0, 0.0), pos.zw));
  615. avg += textureProj(shadow, vec4(pos.xy + vec2(-shadow_pixel_size * 2.0, 0.0), pos.zw));
  616. avg += textureProj(shadow, vec4(pos.xy + vec2(0.0, shadow_pixel_size * 2.0), pos.zw));
  617. avg += textureProj(shadow, vec4(pos.xy + vec2(0.0, -shadow_pixel_size * 2.0), pos.zw));
  618. // Early bail if distant samples are fully shaded (or none are shaded) to improve performance.
  619. if (avg <= 0.000001) {
  620. // None shaded at all.
  621. return 0.0;
  622. } else if (avg >= 4.999999) {
  623. // All fully shaded.
  624. return 1.0;
  625. }
  626. avg += textureProj(shadow, vec4(pos.xy + vec2(shadow_pixel_size, 0.0), pos.zw));
  627. avg += textureProj(shadow, vec4(pos.xy + vec2(-shadow_pixel_size, 0.0), pos.zw));
  628. avg += textureProj(shadow, vec4(pos.xy + vec2(0.0, shadow_pixel_size), pos.zw));
  629. avg += textureProj(shadow, vec4(pos.xy + vec2(0.0, -shadow_pixel_size), pos.zw));
  630. avg += textureProj(shadow, vec4(pos.xy + vec2(shadow_pixel_size, shadow_pixel_size), pos.zw));
  631. avg += textureProj(shadow, vec4(pos.xy + vec2(-shadow_pixel_size, shadow_pixel_size), pos.zw));
  632. avg += textureProj(shadow, vec4(pos.xy + vec2(shadow_pixel_size, -shadow_pixel_size), pos.zw));
  633. avg += textureProj(shadow, vec4(pos.xy + vec2(-shadow_pixel_size, -shadow_pixel_size), pos.zw));
  634. return avg * (1.0 / 13.0);
  635. #endif
  636. #ifdef SHADOW_MODE_PCF_5
  637. pos /= pos.w;
  638. avg += textureProj(shadow, vec4(pos.xy + vec2(shadow_pixel_size, 0.0), pos.zw));
  639. avg += textureProj(shadow, vec4(pos.xy + vec2(-shadow_pixel_size, 0.0), pos.zw));
  640. avg += textureProj(shadow, vec4(pos.xy + vec2(0.0, shadow_pixel_size), pos.zw));
  641. avg += textureProj(shadow, vec4(pos.xy + vec2(0.0, -shadow_pixel_size), pos.zw));
  642. return avg * (1.0 / 5.0);
  643. #endif
  644. return avg;
  645. }
  646. #endif //!defined(ADDITIVE_OMNI)
  647. #endif // USE_ADDITIVE_LIGHTING
  648. #endif // !MODE_RENDER_DEPTH
  649. #ifdef USE_MULTIVIEW
  650. uniform highp sampler2DArray depth_buffer; // texunit:-6
  651. uniform highp sampler2DArray color_buffer; // texunit:-5
  652. vec3 multiview_uv(vec2 uv) {
  653. return vec3(uv, ViewIndex);
  654. }
  655. #else
  656. uniform highp sampler2D depth_buffer; // texunit:-6
  657. uniform highp sampler2D color_buffer; // texunit:-5
  658. vec2 multiview_uv(vec2 uv) {
  659. return uv;
  660. }
  661. #endif
  662. uniform highp mat4 world_transform;
  663. uniform mediump float opaque_prepass_threshold;
  664. layout(location = 0) out vec4 frag_color;
  665. vec3 F0(float metallic, float specular, vec3 albedo) {
  666. float dielectric = 0.16 * specular * specular;
  667. // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials;
  668. // see https://google.github.io/filament/Filament.md.html
  669. return mix(vec3(dielectric), albedo, vec3(metallic));
  670. }
  671. #ifndef MODE_RENDER_DEPTH
  672. #if !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) || defined(USE_ADDITIVE_LIGHTING)
  673. float D_GGX(float cos_theta_m, float alpha) {
  674. float a = cos_theta_m * alpha;
  675. float k = alpha / (1.0 - cos_theta_m * cos_theta_m + a * a);
  676. return k * k * (1.0 / M_PI);
  677. }
  678. // From Earl Hammon, Jr. "PBR Diffuse Lighting for GGX+Smith Microsurfaces" https://www.gdcvault.com/play/1024478/PBR-Diffuse-Lighting-for-GGX
  679. float V_GGX(float NdotL, float NdotV, float alpha) {
  680. return 0.5 / mix(2.0 * NdotL * NdotV, NdotL + NdotV, alpha);
  681. }
  682. float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) {
  683. float alpha2 = alpha_x * alpha_y;
  684. highp vec3 v = vec3(alpha_y * cos_phi, alpha_x * sin_phi, alpha2 * cos_theta_m);
  685. highp float v2 = dot(v, v);
  686. float w2 = alpha2 / v2;
  687. float D = alpha2 * w2 * w2 * (1.0 / M_PI);
  688. return D;
  689. }
  690. float V_GGX_anisotropic(float alpha_x, float alpha_y, float TdotV, float TdotL, float BdotV, float BdotL, float NdotV, float NdotL) {
  691. float Lambda_V = NdotL * length(vec3(alpha_x * TdotV, alpha_y * BdotV, NdotV));
  692. float Lambda_L = NdotV * length(vec3(alpha_x * TdotL, alpha_y * BdotL, NdotL));
  693. return 0.5 / (Lambda_V + Lambda_L);
  694. }
  695. float SchlickFresnel(float u) {
  696. float m = 1.0 - u;
  697. float m2 = m * m;
  698. return m2 * m2 * m; // pow(m,5)
  699. }
  700. 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,
  701. #ifdef LIGHT_BACKLIGHT_USED
  702. vec3 backlight,
  703. #endif
  704. #ifdef LIGHT_RIM_USED
  705. float rim, float rim_tint,
  706. #endif
  707. #ifdef LIGHT_CLEARCOAT_USED
  708. float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
  709. #endif
  710. #ifdef LIGHT_ANISOTROPY_USED
  711. vec3 B, vec3 T, float anisotropy,
  712. #endif
  713. inout vec3 diffuse_light, inout vec3 specular_light) {
  714. #if defined(USE_LIGHT_SHADER_CODE)
  715. // light is written by the light shader
  716. highp mat4 model_matrix = world_transform;
  717. mat4 projection_matrix = scene_data.projection_matrix;
  718. mat4 inv_projection_matrix = scene_data.inv_projection_matrix;
  719. vec3 normal = N;
  720. vec3 light = L;
  721. vec3 view = V;
  722. /* clang-format off */
  723. #CODE : LIGHT
  724. /* clang-format on */
  725. #else
  726. float NdotL = min(A + dot(N, L), 1.0);
  727. float cNdotL = max(NdotL, 0.0); // clamped NdotL
  728. float NdotV = dot(N, V);
  729. float cNdotV = max(NdotV, 1e-4);
  730. #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED)
  731. vec3 H = normalize(V + L);
  732. #endif
  733. #if defined(SPECULAR_SCHLICK_GGX)
  734. float cNdotH = clamp(A + dot(N, H), 0.0, 1.0);
  735. #endif
  736. #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED)
  737. float cLdotH = clamp(A + dot(L, H), 0.0, 1.0);
  738. #endif
  739. if (metallic < 1.0) {
  740. float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance
  741. #if defined(DIFFUSE_LAMBERT_WRAP)
  742. // Energy conserving lambert wrap shader.
  743. // https://web.archive.org/web/20210228210901/http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/
  744. diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))) * (1.0 / M_PI);
  745. #elif defined(DIFFUSE_TOON)
  746. diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL) * (1.0 / M_PI);
  747. #elif defined(DIFFUSE_BURLEY)
  748. {
  749. float FD90_minus_1 = 2.0 * cLdotH * cLdotH * roughness - 0.5;
  750. float FdV = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotV);
  751. float FdL = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotL);
  752. diffuse_brdf_NL = (1.0 / M_PI) * FdV * FdL * cNdotL;
  753. }
  754. #else
  755. // Lambert
  756. diffuse_brdf_NL = cNdotL * (1.0 / M_PI);
  757. #endif
  758. diffuse_light += light_color * diffuse_brdf_NL * attenuation;
  759. #if defined(LIGHT_BACKLIGHT_USED)
  760. diffuse_light += light_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * backlight * attenuation;
  761. #endif
  762. #if defined(LIGHT_RIM_USED)
  763. // Epsilon min to prevent pow(0, 0) singularity which results in undefined behavior.
  764. float rim_light = pow(max(1e-4, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0));
  765. diffuse_light += rim_light * rim * mix(vec3(1.0), albedo, rim_tint) * light_color;
  766. #endif
  767. }
  768. if (roughness > 0.0) { // FIXME: roughness == 0 should not disable specular light entirely
  769. // D
  770. #if defined(SPECULAR_TOON)
  771. vec3 R = normalize(-reflect(L, N));
  772. float RdotV = dot(R, V);
  773. float mid = 1.0 - roughness;
  774. mid *= mid;
  775. float intensity = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid;
  776. diffuse_light += light_color * intensity * attenuation * specular_amount; // write to diffuse_light, as in toon shading you generally want no reflection
  777. #elif defined(SPECULAR_DISABLED)
  778. // none..
  779. #elif defined(SPECULAR_SCHLICK_GGX)
  780. // shlick+ggx as default
  781. float alpha_ggx = roughness * roughness;
  782. #if defined(LIGHT_ANISOTROPY_USED)
  783. float aspect = sqrt(1.0 - anisotropy * 0.9);
  784. float ax = alpha_ggx / aspect;
  785. float ay = alpha_ggx * aspect;
  786. float XdotH = dot(T, H);
  787. float YdotH = dot(B, H);
  788. float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH);
  789. float G = V_GGX_anisotropic(ax, ay, dot(T, V), dot(T, L), dot(B, V), dot(B, L), cNdotV, cNdotL);
  790. #else
  791. float D = D_GGX(cNdotH, alpha_ggx);
  792. float G = V_GGX(cNdotL, cNdotV, alpha_ggx);
  793. #endif // LIGHT_ANISOTROPY_USED
  794. // F
  795. float cLdotH5 = SchlickFresnel(cLdotH);
  796. // Calculate Fresnel using cheap approximate specular occlusion term from Filament:
  797. // https://google.github.io/filament/Filament.html#lighting/occlusion/specularocclusion
  798. float f90 = clamp(50.0 * f0.g, 0.0, 1.0);
  799. vec3 F = f0 + (f90 - f0) * cLdotH5;
  800. vec3 specular_brdf_NL = cNdotL * D * F * G;
  801. specular_light += specular_brdf_NL * light_color * attenuation * specular_amount;
  802. #endif
  803. #if defined(LIGHT_CLEARCOAT_USED)
  804. // Clearcoat ignores normal_map, use vertex normal instead
  805. float ccNdotL = max(min(A + dot(vertex_normal, L), 1.0), 0.0);
  806. float ccNdotH = clamp(A + dot(vertex_normal, H), 0.0, 1.0);
  807. float ccNdotV = max(dot(vertex_normal, V), 1e-4);
  808. #if !defined(SPECULAR_SCHLICK_GGX)
  809. float cLdotH5 = SchlickFresnel(cLdotH);
  810. #endif
  811. float Dr = D_GGX(ccNdotH, mix(0.001, 0.1, clearcoat_roughness));
  812. float Gr = 0.25 / (cLdotH * cLdotH);
  813. float Fr = mix(.04, 1.0, cLdotH5);
  814. float clearcoat_specular_brdf_NL = clearcoat * Gr * Fr * Dr * cNdotL;
  815. specular_light += clearcoat_specular_brdf_NL * light_color * attenuation * specular_amount;
  816. // 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)
  817. // but to do so we need to rearrange this entire function
  818. #endif // LIGHT_CLEARCOAT_USED
  819. }
  820. #ifdef USE_SHADOW_TO_OPACITY
  821. alpha = min(alpha, clamp(1.0 - attenuation, 0.0, 1.0));
  822. #endif
  823. #endif // USE_LIGHT_SHADER_CODE
  824. }
  825. float get_omni_spot_attenuation(float distance, float inv_range, float decay) {
  826. float nd = distance * inv_range;
  827. nd *= nd;
  828. nd *= nd; // nd^4
  829. nd = max(1.0 - nd, 0.0);
  830. nd *= nd; // nd^2
  831. return nd * pow(max(distance, 0.0001), -decay);
  832. }
  833. #if !defined(DISABLE_LIGHT_OMNI) || defined(ADDITIVE_OMNI)
  834. 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,
  835. #ifdef LIGHT_BACKLIGHT_USED
  836. vec3 backlight,
  837. #endif
  838. #ifdef LIGHT_RIM_USED
  839. float rim, float rim_tint,
  840. #endif
  841. #ifdef LIGHT_CLEARCOAT_USED
  842. float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
  843. #endif
  844. #ifdef LIGHT_ANISOTROPY_USED
  845. vec3 binormal, vec3 tangent, float anisotropy,
  846. #endif
  847. inout vec3 diffuse_light, inout vec3 specular_light) {
  848. vec3 light_rel_vec = omni_lights[idx].position - vertex;
  849. float light_length = length(light_rel_vec);
  850. float omni_attenuation = get_omni_spot_attenuation(light_length, omni_lights[idx].inv_radius, omni_lights[idx].attenuation);
  851. vec3 color = omni_lights[idx].color;
  852. float size_A = 0.0;
  853. if (omni_lights[idx].size > 0.0) {
  854. float t = omni_lights[idx].size / max(0.001, light_length);
  855. size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t));
  856. }
  857. omni_attenuation *= shadow;
  858. 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,
  859. #ifdef LIGHT_BACKLIGHT_USED
  860. backlight,
  861. #endif
  862. #ifdef LIGHT_RIM_USED
  863. rim * omni_attenuation, rim_tint,
  864. #endif
  865. #ifdef LIGHT_CLEARCOAT_USED
  866. clearcoat, clearcoat_roughness, vertex_normal,
  867. #endif
  868. #ifdef LIGHT_ANISOTROPY_USED
  869. binormal, tangent, anisotropy,
  870. #endif
  871. diffuse_light,
  872. specular_light);
  873. }
  874. #endif // !DISABLE_LIGHT_OMNI
  875. #if !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_SPOT)
  876. 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,
  877. #ifdef LIGHT_BACKLIGHT_USED
  878. vec3 backlight,
  879. #endif
  880. #ifdef LIGHT_RIM_USED
  881. float rim, float rim_tint,
  882. #endif
  883. #ifdef LIGHT_CLEARCOAT_USED
  884. float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
  885. #endif
  886. #ifdef LIGHT_ANISOTROPY_USED
  887. vec3 binormal, vec3 tangent, float anisotropy,
  888. #endif
  889. inout vec3 diffuse_light,
  890. inout vec3 specular_light) {
  891. vec3 light_rel_vec = spot_lights[idx].position - vertex;
  892. float light_length = length(light_rel_vec);
  893. float spot_attenuation = get_omni_spot_attenuation(light_length, spot_lights[idx].inv_radius, spot_lights[idx].attenuation);
  894. vec3 spot_dir = spot_lights[idx].direction;
  895. float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights[idx].cone_angle);
  896. float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights[idx].cone_angle));
  897. spot_attenuation *= 1.0 - pow(spot_rim, spot_lights[idx].cone_attenuation);
  898. vec3 color = spot_lights[idx].color;
  899. float size_A = 0.0;
  900. if (spot_lights[idx].size > 0.0) {
  901. float t = spot_lights[idx].size / max(0.001, light_length);
  902. size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t));
  903. }
  904. spot_attenuation *= shadow;
  905. 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,
  906. #ifdef LIGHT_BACKLIGHT_USED
  907. backlight,
  908. #endif
  909. #ifdef LIGHT_RIM_USED
  910. rim * spot_attenuation, rim_tint,
  911. #endif
  912. #ifdef LIGHT_CLEARCOAT_USED
  913. clearcoat, clearcoat_roughness, vertex_normal,
  914. #endif
  915. #ifdef LIGHT_ANISOTROPY_USED
  916. binormal, tangent, anisotropy,
  917. #endif
  918. diffuse_light, specular_light);
  919. }
  920. #endif // !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_SPOT)
  921. #endif // !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT)
  922. vec4 fog_process(vec3 vertex) {
  923. vec3 fog_color = scene_data.fog_light_color;
  924. #ifdef USE_RADIANCE_MAP
  925. /*
  926. if (scene_data.fog_aerial_perspective > 0.0) {
  927. vec3 sky_fog_color = vec3(0.0);
  928. vec3 cube_view = scene_data.radiance_inverse_xform * vertex;
  929. // mip_level always reads from the second mipmap and higher so the fog is always slightly blurred
  930. 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));
  931. sky_fog_color = textureLod(radiance_map, cube_view, mip_level * RADIANCE_MAX_LOD).rgb;
  932. fog_color = mix(fog_color, sky_fog_color, scene_data.fog_aerial_perspective);
  933. }
  934. */
  935. #endif
  936. #ifndef DISABLE_LIGHT_DIRECTIONAL
  937. if (scene_data.fog_sun_scatter > 0.001) {
  938. vec4 sun_scatter = vec4(0.0);
  939. float sun_total = 0.0;
  940. vec3 view = normalize(vertex);
  941. for (uint i = uint(0); i < scene_data.directional_light_count; i++) {
  942. vec3 light_color = directional_lights[i].color * directional_lights[i].energy;
  943. float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0);
  944. fog_color += light_color * light_amount * scene_data.fog_sun_scatter;
  945. }
  946. }
  947. #endif // !DISABLE_LIGHT_DIRECTIONAL
  948. float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density));
  949. if (abs(scene_data.fog_height_density) >= 0.0001) {
  950. float y = (scene_data.inv_view_matrix * vec4(vertex, 1.0)).y;
  951. float y_dist = y - scene_data.fog_height;
  952. float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data.fog_height_density));
  953. fog_amount = max(vfog_amount, fog_amount);
  954. }
  955. return vec4(fog_color, fog_amount);
  956. }
  957. #endif // !MODE_RENDER_DEPTH
  958. void main() {
  959. //lay out everything, whatever is unused is optimized away anyway
  960. vec3 vertex = vertex_interp;
  961. #ifdef USE_MULTIVIEW
  962. vec3 eye_offset = multiview_data.eye_offset[ViewIndex].xyz;
  963. vec3 view = -normalize(vertex_interp - eye_offset);
  964. mat4 projection_matrix = multiview_data.projection_matrix_view[ViewIndex];
  965. mat4 inv_projection_matrix = multiview_data.inv_projection_matrix_view[ViewIndex];
  966. #else
  967. vec3 eye_offset = vec3(0.0, 0.0, 0.0);
  968. vec3 view = -normalize(vertex_interp);
  969. mat4 projection_matrix = scene_data.projection_matrix;
  970. mat4 inv_projection_matrix = scene_data.inv_projection_matrix;
  971. #endif
  972. highp mat4 model_matrix = world_transform;
  973. vec3 albedo = vec3(1.0);
  974. vec3 backlight = vec3(0.0);
  975. vec4 transmittance_color = vec4(0.0, 0.0, 0.0, 1.0);
  976. float transmittance_depth = 0.0;
  977. float transmittance_boost = 0.0;
  978. float metallic = 0.0;
  979. float specular = 0.5;
  980. vec3 emission = vec3(0.0);
  981. float roughness = 1.0;
  982. float rim = 0.0;
  983. float rim_tint = 0.0;
  984. float clearcoat = 0.0;
  985. float clearcoat_roughness = 0.0;
  986. float anisotropy = 0.0;
  987. vec2 anisotropy_flow = vec2(1.0, 0.0);
  988. #ifndef FOG_DISABLED
  989. vec4 fog = vec4(0.0);
  990. #endif // !FOG_DISABLED
  991. #if defined(CUSTOM_RADIANCE_USED)
  992. vec4 custom_radiance = vec4(0.0);
  993. #endif
  994. #if defined(CUSTOM_IRRADIANCE_USED)
  995. vec4 custom_irradiance = vec4(0.0);
  996. #endif
  997. float ao = 1.0;
  998. float ao_light_affect = 0.0;
  999. float alpha = 1.0;
  1000. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  1001. vec3 binormal = normalize(binormal_interp);
  1002. vec3 tangent = normalize(tangent_interp);
  1003. #else
  1004. vec3 binormal = vec3(0.0);
  1005. vec3 tangent = vec3(0.0);
  1006. #endif
  1007. #ifdef NORMAL_USED
  1008. vec3 normal = normalize(normal_interp);
  1009. #if defined(DO_SIDE_CHECK)
  1010. if (!gl_FrontFacing) {
  1011. normal = -normal;
  1012. }
  1013. #endif
  1014. #endif //NORMAL_USED
  1015. #ifdef UV_USED
  1016. vec2 uv = uv_interp;
  1017. #endif
  1018. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  1019. vec2 uv2 = uv2_interp;
  1020. #endif
  1021. #if defined(COLOR_USED)
  1022. vec4 color = color_interp;
  1023. #endif
  1024. #if defined(NORMAL_MAP_USED)
  1025. vec3 normal_map = vec3(0.5);
  1026. #endif
  1027. float normal_map_depth = 1.0;
  1028. vec2 screen_uv = gl_FragCoord.xy * scene_data.screen_pixel_size;
  1029. float sss_strength = 0.0;
  1030. #ifdef ALPHA_SCISSOR_USED
  1031. float alpha_scissor_threshold = 1.0;
  1032. #endif // ALPHA_SCISSOR_USED
  1033. #ifdef ALPHA_HASH_USED
  1034. float alpha_hash_scale = 1.0;
  1035. #endif // ALPHA_HASH_USED
  1036. #ifdef ALPHA_ANTIALIASING_EDGE_USED
  1037. float alpha_antialiasing_edge = 0.0;
  1038. vec2 alpha_texture_coordinate = vec2(0.0, 0.0);
  1039. #endif // ALPHA_ANTIALIASING_EDGE_USED
  1040. {
  1041. #CODE : FRAGMENT
  1042. }
  1043. #ifndef USE_SHADOW_TO_OPACITY
  1044. #if defined(ALPHA_SCISSOR_USED)
  1045. if (alpha < alpha_scissor_threshold) {
  1046. discard;
  1047. }
  1048. #else
  1049. #ifdef MODE_RENDER_DEPTH
  1050. #ifdef USE_OPAQUE_PREPASS
  1051. if (alpha < opaque_prepass_threshold) {
  1052. discard;
  1053. }
  1054. #endif // USE_OPAQUE_PREPASS
  1055. #endif // MODE_RENDER_DEPTH
  1056. #endif // !ALPHA_SCISSOR_USED
  1057. #endif // !USE_SHADOW_TO_OPACITY
  1058. #ifdef NORMAL_MAP_USED
  1059. normal_map.xy = normal_map.xy * 2.0 - 1.0;
  1060. 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.
  1061. normal = normalize(mix(normal, tangent * normal_map.x + binormal * normal_map.y + normal * normal_map.z, normal_map_depth));
  1062. #endif
  1063. #ifdef LIGHT_ANISOTROPY_USED
  1064. if (anisotropy > 0.01) {
  1065. //rotation matrix
  1066. mat3 rot = mat3(tangent, binormal, normal);
  1067. //make local to space
  1068. tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0));
  1069. binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0));
  1070. }
  1071. #endif
  1072. #ifndef MODE_RENDER_DEPTH
  1073. #ifndef FOG_DISABLED
  1074. #ifndef CUSTOM_FOG_USED
  1075. #ifndef DISABLE_FOG
  1076. // fog must be processed as early as possible and then packed.
  1077. // to maximize VGPR usage
  1078. if (scene_data.fog_enabled) {
  1079. fog = fog_process(vertex);
  1080. }
  1081. #endif // !DISABLE_FOG
  1082. #endif // !CUSTOM_FOG_USED
  1083. uint fog_rg = packHalf2x16(fog.rg);
  1084. uint fog_ba = packHalf2x16(fog.ba);
  1085. #endif // !FOG_DISABLED
  1086. // Convert colors to linear
  1087. albedo = srgb_to_linear(albedo);
  1088. emission = srgb_to_linear(emission);
  1089. // TODO Backlight and transmittance when used
  1090. #ifndef MODE_UNSHADED
  1091. vec3 f0 = F0(metallic, specular, albedo);
  1092. vec3 specular_light = vec3(0.0, 0.0, 0.0);
  1093. vec3 diffuse_light = vec3(0.0, 0.0, 0.0);
  1094. vec3 ambient_light = vec3(0.0, 0.0, 0.0);
  1095. #ifdef BASE_PASS
  1096. /////////////////////// LIGHTING //////////////////////////////
  1097. // IBL precalculations
  1098. float ndotv = clamp(dot(normal, view), 0.0, 1.0);
  1099. vec3 F = f0 + (max(vec3(1.0 - roughness), f0) - f0) * pow(1.0 - ndotv, 5.0);
  1100. #ifdef USE_RADIANCE_MAP
  1101. if (scene_data.use_reflection_cubemap) {
  1102. #ifdef LIGHT_ANISOTROPY_USED
  1103. // https://google.github.io/filament/Filament.html#lighting/imagebasedlights/anisotropy
  1104. vec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent;
  1105. vec3 anisotropic_tangent = cross(anisotropic_direction, view);
  1106. vec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction);
  1107. vec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0)));
  1108. vec3 ref_vec = reflect(-view, bent_normal);
  1109. #else
  1110. vec3 ref_vec = reflect(-view, normal);
  1111. #endif
  1112. ref_vec = mix(ref_vec, normal, roughness * roughness);
  1113. float horizon = min(1.0 + dot(ref_vec, normal), 1.0);
  1114. ref_vec = scene_data.radiance_inverse_xform * ref_vec;
  1115. specular_light = textureLod(radiance_map, ref_vec, sqrt(roughness) * RADIANCE_MAX_LOD).rgb;
  1116. specular_light = srgb_to_linear(specular_light);
  1117. specular_light *= horizon * horizon;
  1118. specular_light *= scene_data.ambient_light_color_energy.a;
  1119. }
  1120. #endif
  1121. // Calculate Reflection probes
  1122. // Calculate Lightmaps
  1123. #if defined(CUSTOM_RADIANCE_USED)
  1124. specular_light = mix(specular_light, custom_radiance.rgb, custom_radiance.a);
  1125. #endif // CUSTOM_RADIANCE_USED
  1126. #ifndef USE_LIGHTMAP
  1127. //lightmap overrides everything
  1128. if (scene_data.use_ambient_light) {
  1129. ambient_light = scene_data.ambient_light_color_energy.rgb;
  1130. #ifdef USE_RADIANCE_MAP
  1131. if (scene_data.use_ambient_cubemap) {
  1132. vec3 ambient_dir = scene_data.radiance_inverse_xform * normal;
  1133. vec3 cubemap_ambient = textureLod(radiance_map, ambient_dir, RADIANCE_MAX_LOD).rgb;
  1134. cubemap_ambient = srgb_to_linear(cubemap_ambient);
  1135. ambient_light = mix(ambient_light, cubemap_ambient * scene_data.ambient_light_color_energy.a, scene_data.ambient_color_sky_mix);
  1136. }
  1137. #endif
  1138. }
  1139. #endif // USE_LIGHTMAP
  1140. #if defined(CUSTOM_IRRADIANCE_USED)
  1141. ambient_light = mix(ambient_light, custom_irradiance.rgb, custom_irradiance.a);
  1142. #endif // CUSTOM_IRRADIANCE_USED
  1143. {
  1144. #if defined(AMBIENT_LIGHT_DISABLED)
  1145. ambient_light = vec3(0.0, 0.0, 0.0);
  1146. #else
  1147. ambient_light *= albedo.rgb;
  1148. ambient_light *= ao;
  1149. #endif // AMBIENT_LIGHT_DISABLED
  1150. }
  1151. // convert ao to direct light ao
  1152. ao = mix(1.0, ao, ao_light_affect);
  1153. {
  1154. #if defined(DIFFUSE_TOON)
  1155. //simplify for toon, as
  1156. specular_light *= specular * metallic * albedo * 2.0;
  1157. #else
  1158. // scales the specular reflections, needs to be be computed before lighting happens,
  1159. // but after environment, GI, and reflection probes are added
  1160. // Environment brdf approximation (Lazarov 2013)
  1161. // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
  1162. const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
  1163. const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);
  1164. vec4 r = roughness * c0 + c1;
  1165. float ndotv = clamp(dot(normal, view), 0.0, 1.0);
  1166. float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y;
  1167. vec2 env = vec2(-1.04, 1.04) * a004 + r.zw;
  1168. specular_light *= env.x * f0 + env.y * clamp(50.0 * f0.g, metallic, 1.0);
  1169. #endif
  1170. }
  1171. #ifndef DISABLE_LIGHT_DIRECTIONAL
  1172. for (uint i = uint(0); i < scene_data.directional_light_count; i++) {
  1173. 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, 1.0, albedo, alpha,
  1174. #ifdef LIGHT_BACKLIGHT_USED
  1175. backlight,
  1176. #endif
  1177. #ifdef LIGHT_RIM_USED
  1178. rim, rim_tint,
  1179. #endif
  1180. #ifdef LIGHT_CLEARCOAT_USED
  1181. clearcoat, clearcoat_roughness, normalize(normal_interp),
  1182. #endif
  1183. #ifdef LIGHT_ANISOTROPY_USED
  1184. binormal,
  1185. tangent, anisotropy,
  1186. #endif
  1187. diffuse_light,
  1188. specular_light);
  1189. }
  1190. #endif // !DISABLE_LIGHT_DIRECTIONAL
  1191. #ifndef DISABLE_LIGHT_OMNI
  1192. for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) {
  1193. if (i >= omni_light_count) {
  1194. break;
  1195. }
  1196. light_process_omni(omni_light_indices[i], vertex, view, normal, f0, roughness, metallic, 1.0, albedo, alpha,
  1197. #ifdef LIGHT_BACKLIGHT_USED
  1198. backlight,
  1199. #endif
  1200. #ifdef LIGHT_RIM_USED
  1201. rim,
  1202. rim_tint,
  1203. #endif
  1204. #ifdef LIGHT_CLEARCOAT_USED
  1205. clearcoat, clearcoat_roughness, normalize(normal_interp),
  1206. #endif
  1207. #ifdef LIGHT_ANISOTROPY_USED
  1208. binormal, tangent, anisotropy,
  1209. #endif
  1210. diffuse_light, specular_light);
  1211. }
  1212. #endif // !DISABLE_LIGHT_OMNI
  1213. #ifndef DISABLE_LIGHT_SPOT
  1214. for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) {
  1215. if (i >= spot_light_count) {
  1216. break;
  1217. }
  1218. light_process_spot(spot_light_indices[i], vertex, view, normal, f0, roughness, metallic, 1.0, albedo, alpha,
  1219. #ifdef LIGHT_BACKLIGHT_USED
  1220. backlight,
  1221. #endif
  1222. #ifdef LIGHT_RIM_USED
  1223. rim,
  1224. rim_tint,
  1225. #endif
  1226. #ifdef LIGHT_CLEARCOAT_USED
  1227. clearcoat, clearcoat_roughness, normalize(normal_interp),
  1228. #endif
  1229. #ifdef LIGHT_ANISOTROPY_USED
  1230. tangent,
  1231. binormal, anisotropy,
  1232. #endif
  1233. diffuse_light, specular_light);
  1234. }
  1235. #endif // !DISABLE_LIGHT_SPOT
  1236. #endif // BASE_PASS
  1237. #endif // !MODE_UNSHADED
  1238. #endif // !MODE_RENDER_DEPTH
  1239. #if defined(USE_SHADOW_TO_OPACITY)
  1240. alpha = min(alpha, clamp(length(ambient_light), 0.0, 1.0));
  1241. #if defined(ALPHA_SCISSOR_USED)
  1242. if (alpha < alpha_scissor) {
  1243. discard;
  1244. }
  1245. #else
  1246. #ifdef MODE_RENDER_DEPTH
  1247. #ifdef USE_OPAQUE_PREPASS
  1248. if (alpha < opaque_prepass_threshold) {
  1249. discard;
  1250. }
  1251. #endif // USE_OPAQUE_PREPASS
  1252. #endif // MODE_RENDER_DEPTH
  1253. #endif // !ALPHA_SCISSOR_USED
  1254. #endif // USE_SHADOW_TO_OPACITY
  1255. #ifdef MODE_RENDER_DEPTH
  1256. #ifdef RENDER_SHADOWS_LINEAR
  1257. // Linearize the depth buffer if rendering cubemap shadows.
  1258. gl_FragDepth = (length(vertex) + scene_data.shadow_bias) / scene_data.z_far;
  1259. #endif
  1260. // Nothing happens, so a tree-ssa optimizer will result in no fragment shader :)
  1261. #else // !MODE_RENDER_DEPTH
  1262. #ifdef BASE_PASS
  1263. #ifdef MODE_UNSHADED
  1264. frag_color = vec4(albedo, alpha);
  1265. #else
  1266. diffuse_light *= albedo;
  1267. diffuse_light *= 1.0 - metallic;
  1268. ambient_light *= 1.0 - metallic;
  1269. frag_color = vec4(diffuse_light + specular_light, alpha);
  1270. frag_color.rgb += emission + ambient_light;
  1271. #endif //!MODE_UNSHADED
  1272. #ifndef FOG_DISABLED
  1273. fog = vec4(unpackHalf2x16(fog_rg), unpackHalf2x16(fog_ba));
  1274. #ifndef DISABLE_FOG
  1275. if (scene_data.fog_enabled) {
  1276. frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a);
  1277. }
  1278. #endif // !DISABLE_FOG
  1279. #endif // !FOG_DISABLED
  1280. // Tonemap before writing as we are writing to an sRGB framebuffer
  1281. frag_color.rgb *= exposure;
  1282. frag_color.rgb = apply_tonemapping(frag_color.rgb, white);
  1283. frag_color.rgb = linear_to_srgb(frag_color.rgb);
  1284. #ifdef USE_BCS
  1285. frag_color.rgb = apply_bcs(frag_color.rgb, bcs);
  1286. #endif
  1287. #ifdef USE_COLOR_CORRECTION
  1288. frag_color.rgb = apply_color_correction(frag_color.rgb, color_correction);
  1289. #endif
  1290. #else // !BASE_PASS
  1291. frag_color = vec4(0.0, 0.0, 0.0, alpha);
  1292. #endif // !BASE_PASS
  1293. /* ADDITIVE LIGHTING PASS */
  1294. #ifdef USE_ADDITIVE_LIGHTING
  1295. diffuse_light = vec3(0.0);
  1296. specular_light = vec3(0.0);
  1297. #if !defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)
  1298. // Orthogonal shadows
  1299. #if !defined(LIGHT_USE_PSSM2) && !defined(LIGHT_USE_PSSM4)
  1300. float directional_shadow = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord);
  1301. #endif // !defined(LIGHT_USE_PSSM2) && !defined(LIGHT_USE_PSSM4)
  1302. // PSSM2 shadows
  1303. #ifdef LIGHT_USE_PSSM2
  1304. float depth_z = -vertex.z;
  1305. vec4 light_split_offsets = directional_shadows[directional_shadow_index].shadow_split_offsets;
  1306. //take advantage of prefetch
  1307. float shadow1 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord);
  1308. float shadow2 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord2);
  1309. float directional_shadow = 1.0;
  1310. if (depth_z < light_split_offsets.y) {
  1311. float pssm_fade = 0.0;
  1312. #ifdef LIGHT_USE_PSSM_BLEND
  1313. float directional_shadow2 = 1.0;
  1314. float pssm_blend = 0.0;
  1315. bool use_blend = true;
  1316. #endif
  1317. if (depth_z < light_split_offsets.x) {
  1318. float pssm_fade = 0.0;
  1319. directional_shadow = shadow1;
  1320. #ifdef LIGHT_USE_PSSM_BLEND
  1321. directional_shadow2 = shadow2;
  1322. pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z);
  1323. #endif
  1324. } else {
  1325. directional_shadow = shadow2;
  1326. pssm_fade = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z);
  1327. #ifdef LIGHT_USE_PSSM_BLEND
  1328. use_blend = false;
  1329. #endif
  1330. }
  1331. #ifdef LIGHT_USE_PSSM_BLEND
  1332. if (use_blend) {
  1333. directional_shadow = mix(directional_shadow, directional_shadow2, pssm_blend);
  1334. }
  1335. #endif
  1336. directional_shadow = mix(directional_shadow, 1.0, pssm_fade);
  1337. }
  1338. #endif //LIGHT_USE_PSSM2
  1339. // PSSM4 shadows
  1340. #ifdef LIGHT_USE_PSSM4
  1341. float depth_z = -vertex.z;
  1342. vec4 light_split_offsets = directional_shadows[directional_shadow_index].shadow_split_offsets;
  1343. float shadow1 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord);
  1344. float shadow2 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord2);
  1345. float shadow3 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord3);
  1346. float shadow4 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord4);
  1347. float directional_shadow = 1.0;
  1348. if (depth_z < light_split_offsets.w) {
  1349. float pssm_fade = 0.0;
  1350. #ifdef LIGHT_USE_PSSM_BLEND
  1351. float directional_shadow2 = 1.0;
  1352. float pssm_blend = 0.0;
  1353. bool use_blend = true;
  1354. #endif
  1355. if (depth_z < light_split_offsets.y) {
  1356. if (depth_z < light_split_offsets.x) {
  1357. directional_shadow = shadow1;
  1358. #ifdef LIGHT_USE_PSSM_BLEND
  1359. directional_shadow2 = shadow2;
  1360. pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z);
  1361. #endif
  1362. } else {
  1363. directional_shadow = shadow2;
  1364. #ifdef LIGHT_USE_PSSM_BLEND
  1365. directional_shadow2 = shadow3;
  1366. pssm_blend = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z);
  1367. #endif
  1368. }
  1369. } else {
  1370. if (depth_z < light_split_offsets.z) {
  1371. directional_shadow = shadow3;
  1372. #if defined(LIGHT_USE_PSSM_BLEND)
  1373. directional_shadow2 = shadow4;
  1374. pssm_blend = smoothstep(light_split_offsets.y, light_split_offsets.z, depth_z);
  1375. #endif
  1376. } else {
  1377. directional_shadow = shadow4;
  1378. pssm_fade = smoothstep(light_split_offsets.z, light_split_offsets.w, depth_z);
  1379. #if defined(LIGHT_USE_PSSM_BLEND)
  1380. use_blend = false;
  1381. #endif
  1382. }
  1383. }
  1384. #if defined(LIGHT_USE_PSSM_BLEND)
  1385. if (use_blend) {
  1386. directional_shadow = mix(directional_shadow, directional_shadow2, pssm_blend);
  1387. }
  1388. #endif
  1389. directional_shadow = mix(directional_shadow, 1.0, pssm_fade);
  1390. }
  1391. #endif //LIGHT_USE_PSSM4
  1392. directional_shadow = mix(directional_shadow, 1.0, smoothstep(directional_shadows[directional_shadow_index].fade_from, directional_shadows[directional_shadow_index].fade_to, vertex.z));
  1393. directional_shadow = mix(1.0, directional_shadow, directional_lights[directional_shadow_index].shadow_opacity);
  1394. 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, 1.0, albedo, alpha,
  1395. #ifdef LIGHT_BACKLIGHT_USED
  1396. backlight,
  1397. #endif
  1398. #ifdef LIGHT_RIM_USED
  1399. rim, rim_tint,
  1400. #endif
  1401. #ifdef LIGHT_CLEARCOAT_USED
  1402. clearcoat, clearcoat_roughness, normalize(normal_interp),
  1403. #endif
  1404. #ifdef LIGHT_ANISOTROPY_USED
  1405. binormal,
  1406. tangent, anisotropy,
  1407. #endif
  1408. diffuse_light,
  1409. specular_light);
  1410. #endif // !defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)
  1411. #ifdef ADDITIVE_OMNI
  1412. vec3 light_ray = ((positional_shadows[positional_shadow_index].shadow_matrix * vec4(shadow_coord.xyz, 1.0))).xyz;
  1413. float omni_shadow = texture(omni_shadow_texture, vec4(light_ray, length(light_ray) * omni_lights[omni_light_index].inv_radius));
  1414. omni_shadow = mix(1.0, omni_shadow, omni_lights[omni_light_index].shadow_opacity);
  1415. light_process_omni(omni_light_index, vertex, view, normal, f0, roughness, metallic, omni_shadow, albedo, alpha,
  1416. #ifdef LIGHT_BACKLIGHT_USED
  1417. backlight,
  1418. #endif
  1419. #ifdef LIGHT_RIM_USED
  1420. rim,
  1421. rim_tint,
  1422. #endif
  1423. #ifdef LIGHT_CLEARCOAT_USED
  1424. clearcoat, clearcoat_roughness, normalize(normal_interp),
  1425. #endif
  1426. #ifdef LIGHT_ANISOTROPY_USED
  1427. binormal, tangent, anisotropy,
  1428. #endif
  1429. diffuse_light, specular_light);
  1430. #endif // ADDITIVE_OMNI
  1431. #ifdef ADDITIVE_SPOT
  1432. float spot_shadow = sample_shadow(spot_shadow_texture, positional_shadows[positional_shadow_index].shadow_atlas_pixel_size, shadow_coord);
  1433. spot_shadow = mix(1.0, spot_shadow, spot_lights[spot_light_index].shadow_opacity);
  1434. light_process_spot(spot_light_index, vertex, view, normal, f0, roughness, metallic, spot_shadow, albedo, alpha,
  1435. #ifdef LIGHT_BACKLIGHT_USED
  1436. backlight,
  1437. #endif
  1438. #ifdef LIGHT_RIM_USED
  1439. rim,
  1440. rim_tint,
  1441. #endif
  1442. #ifdef LIGHT_CLEARCOAT_USED
  1443. clearcoat, clearcoat_roughness, normalize(normal_interp),
  1444. #endif
  1445. #ifdef LIGHT_ANISOTROPY_USED
  1446. tangent,
  1447. binormal, anisotropy,
  1448. #endif
  1449. diffuse_light, specular_light);
  1450. #endif // ADDITIVE_SPOT
  1451. diffuse_light *= albedo;
  1452. diffuse_light *= 1.0 - metallic;
  1453. vec3 additive_light_color = diffuse_light + specular_light;
  1454. #ifndef FOG_DISABLED
  1455. fog = vec4(unpackHalf2x16(fog_rg), unpackHalf2x16(fog_ba));
  1456. #ifndef DISABLE_FOG
  1457. if (scene_data.fog_enabled) {
  1458. additive_light_color *= (1.0 - fog.a);
  1459. }
  1460. #endif // !DISABLE_FOG
  1461. #endif // !FOG_DISABLED
  1462. // Tonemap before writing as we are writing to an sRGB framebuffer
  1463. additive_light_color *= exposure;
  1464. additive_light_color = apply_tonemapping(additive_light_color, white);
  1465. additive_light_color = linear_to_srgb(additive_light_color);
  1466. #ifdef USE_BCS
  1467. additive_light_color = apply_bcs(additive_light_color, bcs);
  1468. #endif
  1469. #ifdef USE_COLOR_CORRECTION
  1470. additive_light_color = apply_color_correction(additive_light_color, color_correction);
  1471. #endif
  1472. frag_color.rgb += additive_light_color;
  1473. #endif // USE_ADDITIVE_LIGHTING
  1474. #endif //!MODE_RENDER_DEPTH
  1475. }