scene_forward_mobile.glsl 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. #[vertex]
  2. #version 450
  3. #VERSION_DEFINES
  4. /* Include our forward mobile UBOs definitions etc. */
  5. #include "scene_forward_mobile_inc.glsl"
  6. #define SHADER_IS_SRGB false
  7. /* INPUT ATTRIBS */
  8. layout(location = 0) in vec3 vertex_attrib;
  9. //only for pure render depth when normal is not used
  10. #ifdef NORMAL_USED
  11. layout(location = 1) in vec3 normal_attrib;
  12. #endif
  13. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  14. layout(location = 2) in vec4 tangent_attrib;
  15. #endif
  16. #if defined(COLOR_USED)
  17. layout(location = 3) in vec4 color_attrib;
  18. #endif
  19. #ifdef UV_USED
  20. layout(location = 4) in vec2 uv_attrib;
  21. #endif
  22. #if defined(UV2_USED) || defined(USE_LIGHTMAP) || defined(MODE_RENDER_MATERIAL)
  23. layout(location = 5) in vec2 uv2_attrib;
  24. #endif // MODE_RENDER_MATERIAL
  25. #if defined(CUSTOM0_USED)
  26. layout(location = 6) in vec4 custom0_attrib;
  27. #endif
  28. #if defined(CUSTOM1_USED)
  29. layout(location = 7) in vec4 custom1_attrib;
  30. #endif
  31. #if defined(CUSTOM2_USED)
  32. layout(location = 8) in vec4 custom2_attrib;
  33. #endif
  34. #if defined(CUSTOM3_USED)
  35. layout(location = 9) in vec4 custom3_attrib;
  36. #endif
  37. #if defined(BONES_USED) || defined(USE_PARTICLE_TRAILS)
  38. layout(location = 10) in uvec4 bone_attrib;
  39. #endif
  40. #if defined(WEIGHTS_USED) || defined(USE_PARTICLE_TRAILS)
  41. layout(location = 11) in vec4 weight_attrib;
  42. #endif
  43. /* Varyings */
  44. layout(location = 0) highp out vec3 vertex_interp;
  45. #ifdef NORMAL_USED
  46. layout(location = 1) mediump out vec3 normal_interp;
  47. #endif
  48. #if defined(COLOR_USED)
  49. layout(location = 2) mediump out vec4 color_interp;
  50. #endif
  51. #ifdef UV_USED
  52. layout(location = 3) mediump out vec2 uv_interp;
  53. #endif
  54. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  55. layout(location = 4) mediump out vec2 uv2_interp;
  56. #endif
  57. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  58. layout(location = 5) mediump out vec3 tangent_interp;
  59. layout(location = 6) mediump out vec3 binormal_interp;
  60. #endif
  61. #ifdef MATERIAL_UNIFORMS_USED
  62. layout(set = MATERIAL_UNIFORM_SET, binding = 0, std140) uniform MaterialUniforms{
  63. #MATERIAL_UNIFORMS
  64. } material;
  65. #endif
  66. #ifdef MODE_DUAL_PARABOLOID
  67. layout(location = 8) out highp float dp_clip;
  68. #endif
  69. #ifdef USE_MULTIVIEW
  70. #ifdef has_VK_KHR_multiview
  71. #define ViewIndex gl_ViewIndex
  72. #else
  73. // !BAS! This needs to become an input once we implement our fallback!
  74. #define ViewIndex 0
  75. #endif
  76. #else
  77. // Set to zero, not supported in non stereo
  78. #define ViewIndex 0
  79. #endif //USE_MULTIVIEW
  80. invariant gl_Position;
  81. #GLOBALS
  82. void main() {
  83. vec4 instance_custom = vec4(0.0);
  84. #if defined(COLOR_USED)
  85. color_interp = color_attrib;
  86. #endif
  87. bool is_multimesh = bool(draw_call.flags & INSTANCE_FLAGS_MULTIMESH);
  88. mat4 model_matrix = draw_call.transform;
  89. mat3 model_normal_matrix;
  90. if (bool(draw_call.flags & INSTANCE_FLAGS_NON_UNIFORM_SCALE)) {
  91. model_normal_matrix = transpose(inverse(mat3(model_matrix)));
  92. } else {
  93. model_normal_matrix = mat3(model_matrix);
  94. }
  95. if (is_multimesh) {
  96. //multimesh, instances are for it
  97. mat4 matrix;
  98. #ifdef USE_PARTICLE_TRAILS
  99. uint trail_size = (draw_call.flags >> INSTANCE_FLAGS_PARTICLE_TRAIL_SHIFT) & INSTANCE_FLAGS_PARTICLE_TRAIL_MASK;
  100. uint stride = 3 + 1 + 1; //particles always uses this format
  101. uint offset = trail_size * stride * gl_InstanceIndex;
  102. #ifdef COLOR_USED
  103. vec4 pcolor;
  104. #endif
  105. {
  106. uint boffset = offset + bone_attrib.x * stride;
  107. matrix = mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], transforms.data[boffset + 2], vec4(0.0, 0.0, 0.0, 1.0)) * weight_attrib.x;
  108. #ifdef COLOR_USED
  109. pcolor = transforms.data[boffset + 3] * weight_attrib.x;
  110. #endif
  111. }
  112. if (weight_attrib.y > 0.001) {
  113. uint boffset = offset + bone_attrib.y * stride;
  114. matrix += mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], transforms.data[boffset + 2], vec4(0.0, 0.0, 0.0, 1.0)) * weight_attrib.y;
  115. #ifdef COLOR_USED
  116. pcolor += transforms.data[boffset + 3] * weight_attrib.y;
  117. #endif
  118. }
  119. if (weight_attrib.z > 0.001) {
  120. uint boffset = offset + bone_attrib.z * stride;
  121. matrix += mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], transforms.data[boffset + 2], vec4(0.0, 0.0, 0.0, 1.0)) * weight_attrib.z;
  122. #ifdef COLOR_USED
  123. pcolor += transforms.data[boffset + 3] * weight_attrib.z;
  124. #endif
  125. }
  126. if (weight_attrib.w > 0.001) {
  127. uint boffset = offset + bone_attrib.w * stride;
  128. matrix += mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], transforms.data[boffset + 2], vec4(0.0, 0.0, 0.0, 1.0)) * weight_attrib.w;
  129. #ifdef COLOR_USED
  130. pcolor += transforms.data[boffset + 3] * weight_attrib.w;
  131. #endif
  132. }
  133. instance_custom = transforms.data[offset + 4];
  134. #ifdef COLOR_USED
  135. color_interp *= pcolor;
  136. #endif
  137. #else
  138. uint stride = 0;
  139. {
  140. //TODO implement a small lookup table for the stride
  141. if (bool(draw_call.flags & INSTANCE_FLAGS_MULTIMESH_FORMAT_2D)) {
  142. stride += 2;
  143. } else {
  144. stride += 3;
  145. }
  146. if (bool(draw_call.flags & INSTANCE_FLAGS_MULTIMESH_HAS_COLOR)) {
  147. stride += 1;
  148. }
  149. if (bool(draw_call.flags & INSTANCE_FLAGS_MULTIMESH_HAS_CUSTOM_DATA)) {
  150. stride += 1;
  151. }
  152. }
  153. uint offset = stride * gl_InstanceIndex;
  154. if (bool(draw_call.flags & INSTANCE_FLAGS_MULTIMESH_FORMAT_2D)) {
  155. matrix = mat4(transforms.data[offset + 0], transforms.data[offset + 1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));
  156. offset += 2;
  157. } else {
  158. matrix = mat4(transforms.data[offset + 0], transforms.data[offset + 1], transforms.data[offset + 2], vec4(0.0, 0.0, 0.0, 1.0));
  159. offset += 3;
  160. }
  161. if (bool(draw_call.flags & INSTANCE_FLAGS_MULTIMESH_HAS_COLOR)) {
  162. #ifdef COLOR_USED
  163. color_interp *= transforms.data[offset];
  164. #endif
  165. offset += 1;
  166. }
  167. if (bool(draw_call.flags & INSTANCE_FLAGS_MULTIMESH_HAS_CUSTOM_DATA)) {
  168. instance_custom = transforms.data[offset];
  169. }
  170. #endif
  171. //transpose
  172. matrix = transpose(matrix);
  173. model_matrix = model_matrix * matrix;
  174. model_normal_matrix = model_normal_matrix * mat3(matrix);
  175. }
  176. vec3 vertex = vertex_attrib;
  177. #ifdef NORMAL_USED
  178. vec3 normal = normal_attrib * 2.0 - 1.0;
  179. #endif
  180. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  181. vec3 tangent = tangent_attrib.xyz * 2.0 - 1.0;
  182. float binormalf = tangent_attrib.a * 2.0 - 1.0;
  183. vec3 binormal = normalize(cross(normal, tangent) * binormalf);
  184. #endif
  185. #ifdef UV_USED
  186. uv_interp = uv_attrib;
  187. #endif
  188. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  189. uv2_interp = uv2_attrib;
  190. #endif
  191. #ifdef OVERRIDE_POSITION
  192. vec4 position;
  193. #endif
  194. #ifdef USE_MULTIVIEW
  195. mat4 projection_matrix = scene_data.projection_matrix_view[ViewIndex];
  196. mat4 inv_projection_matrix = scene_data.inv_projection_matrix_view[ViewIndex];
  197. #else
  198. mat4 projection_matrix = scene_data.projection_matrix;
  199. mat4 inv_projection_matrix = scene_data.inv_projection_matrix;
  200. #endif //USE_MULTIVIEW
  201. //using world coordinates
  202. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  203. vertex = (model_matrix * vec4(vertex, 1.0)).xyz;
  204. #ifdef NORMAL_USED
  205. normal = model_normal_matrix * normal;
  206. #endif
  207. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  208. tangent = model_normal_matrix * tangent;
  209. binormal = model_normal_matrix * binormal;
  210. #endif
  211. #endif
  212. float roughness = 1.0;
  213. mat4 modelview = scene_data.view_matrix * model_matrix;
  214. mat3 modelview_normal = mat3(scene_data.view_matrix) * model_normal_matrix;
  215. {
  216. #CODE : VERTEX
  217. }
  218. /* output */
  219. // using local coordinates (default)
  220. #if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
  221. vertex = (modelview * vec4(vertex, 1.0)).xyz;
  222. #ifdef NORMAL_USED
  223. normal = modelview_normal * normal;
  224. #endif
  225. #endif
  226. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  227. binormal = modelview_normal * binormal;
  228. tangent = modelview_normal * tangent;
  229. #endif
  230. //using world coordinates
  231. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  232. vertex = (scene_data.view_matrix * vec4(vertex, 1.0)).xyz;
  233. #ifdef NORMAL_USED
  234. normal = (scene_data.view_matrix * vec4(normal, 0.0)).xyz;
  235. #endif
  236. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  237. binormal = (scene_data.view_matrix * vec4(binormal, 0.0)).xyz;
  238. tangent = (scene_data.view_matrix * vec4(tangent, 0.0)).xyz;
  239. #endif
  240. #endif
  241. vertex_interp = vertex;
  242. #ifdef NORMAL_USED
  243. normal_interp = normal;
  244. #endif
  245. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  246. tangent_interp = tangent;
  247. binormal_interp = binormal;
  248. #endif
  249. #ifdef MODE_RENDER_DEPTH
  250. #ifdef MODE_DUAL_PARABOLOID
  251. vertex_interp.z *= scene_data.dual_paraboloid_side;
  252. dp_clip = vertex_interp.z; //this attempts to avoid noise caused by objects sent to the other parabolloid side due to bias
  253. //for dual paraboloid shadow mapping, this is the fastest but least correct way, as it curves straight edges
  254. vec3 vtx = vertex_interp;
  255. float distance = length(vtx);
  256. vtx = normalize(vtx);
  257. vtx.xy /= 1.0 - vtx.z;
  258. vtx.z = (distance / scene_data.z_far);
  259. vtx.z = vtx.z * 2.0 - 1.0;
  260. vertex_interp = vtx;
  261. #endif
  262. #endif //MODE_RENDER_DEPTH
  263. #ifdef OVERRIDE_POSITION
  264. gl_Position = position;
  265. #else
  266. gl_Position = projection_matrix * vec4(vertex_interp, 1.0);
  267. #endif // OVERRIDE_POSITION
  268. #ifdef MODE_RENDER_DEPTH
  269. if (scene_data.pancake_shadows) {
  270. if (gl_Position.z <= 0.00001) {
  271. gl_Position.z = 0.00001;
  272. }
  273. }
  274. #endif // MODE_RENDER_DEPTH
  275. #ifdef MODE_RENDER_MATERIAL
  276. if (scene_data.material_uv2_mode) {
  277. vec2 uv_offset = draw_call.lightmap_uv_scale.xy; // we are abusing lightmap_uv_scale here, we shouldn't have a lightmap during a depth pass...
  278. gl_Position.xy = (uv2_attrib.xy + uv_offset) * 2.0 - 1.0;
  279. gl_Position.z = 0.00001;
  280. gl_Position.w = 1.0;
  281. }
  282. #endif // MODE_RENDER_MATERIAL
  283. }
  284. #[fragment]
  285. #version 450
  286. #VERSION_DEFINES
  287. #define SHADER_IS_SRGB false
  288. /* Specialization Constants */
  289. #if !defined(MODE_RENDER_DEPTH)
  290. #if !defined(MODE_UNSHADED)
  291. layout(constant_id = 0) const bool sc_use_light_projector = false;
  292. layout(constant_id = 1) const bool sc_use_light_soft_shadows = false;
  293. layout(constant_id = 2) const bool sc_use_directional_soft_shadows = false;
  294. layout(constant_id = 3) const uint sc_soft_shadow_samples = 4;
  295. layout(constant_id = 4) const uint sc_penumbra_shadow_samples = 4;
  296. layout(constant_id = 5) const uint sc_directional_soft_shadow_samples = 4;
  297. layout(constant_id = 6) const uint sc_directional_penumbra_shadow_samples = 4;
  298. layout(constant_id = 8) const bool sc_projector_use_mipmaps = true;
  299. layout(constant_id = 9) const bool sc_disable_omni_lights = false;
  300. layout(constant_id = 10) const bool sc_disable_spot_lights = false;
  301. layout(constant_id = 11) const bool sc_disable_reflection_probes = false;
  302. layout(constant_id = 12) const bool sc_disable_directional_lights = false;
  303. #endif //!MODE_UNSHADED
  304. layout(constant_id = 7) const bool sc_decal_use_mipmaps = true;
  305. layout(constant_id = 13) const bool sc_disable_decals = false;
  306. layout(constant_id = 14) const bool sc_disable_fog = false;
  307. #endif //!MODE_RENDER_DEPTH
  308. layout(constant_id = 15) const float sc_luminance_multiplier = 2.0;
  309. /* Include our forward mobile UBOs definitions etc. */
  310. #include "scene_forward_mobile_inc.glsl"
  311. /* Varyings */
  312. layout(location = 0) highp in vec3 vertex_interp;
  313. #ifdef NORMAL_USED
  314. layout(location = 1) mediump in vec3 normal_interp;
  315. #endif
  316. #if defined(COLOR_USED)
  317. layout(location = 2) mediump in vec4 color_interp;
  318. #endif
  319. #ifdef UV_USED
  320. layout(location = 3) mediump in vec2 uv_interp;
  321. #endif
  322. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  323. layout(location = 4) mediump in vec2 uv2_interp;
  324. #endif
  325. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  326. layout(location = 5) mediump in vec3 tangent_interp;
  327. layout(location = 6) mediump in vec3 binormal_interp;
  328. #endif
  329. #ifdef MODE_DUAL_PARABOLOID
  330. layout(location = 8) highp in float dp_clip;
  331. #endif
  332. #ifdef USE_MULTIVIEW
  333. #ifdef has_VK_KHR_multiview
  334. #define ViewIndex gl_ViewIndex
  335. #else
  336. // !BAS! This needs to become an input once we implement our fallback!
  337. #define ViewIndex 0
  338. #endif
  339. #else
  340. // Set to zero, not supported in non stereo
  341. #define ViewIndex 0
  342. #endif //USE_MULTIVIEW
  343. //defines to keep compatibility with vertex
  344. #define model_matrix draw_call.transform
  345. #ifdef USE_MULTIVIEW
  346. #define projection_matrix scene_data.projection_matrix_view[ViewIndex]
  347. #else
  348. #define projection_matrix scene_data.projection_matrix
  349. #endif
  350. #if defined(ENABLE_SSS) && defined(ENABLE_TRANSMITTANCE)
  351. //both required for transmittance to be enabled
  352. #define LIGHT_TRANSMITTANCE_USED
  353. #endif
  354. #ifdef MATERIAL_UNIFORMS_USED
  355. layout(set = MATERIAL_UNIFORM_SET, binding = 0, std140) uniform MaterialUniforms{
  356. #MATERIAL_UNIFORMS
  357. } material;
  358. #endif
  359. #GLOBALS
  360. /* clang-format on */
  361. #ifdef MODE_RENDER_DEPTH
  362. #ifdef MODE_RENDER_MATERIAL
  363. layout(location = 0) out vec4 albedo_output_buffer;
  364. layout(location = 1) out vec4 normal_output_buffer;
  365. layout(location = 2) out vec4 orm_output_buffer;
  366. layout(location = 3) out vec4 emission_output_buffer;
  367. layout(location = 4) out float depth_output_buffer;
  368. #endif // MODE_RENDER_MATERIAL
  369. #else // RENDER DEPTH
  370. #ifdef MODE_MULTIPLE_RENDER_TARGETS
  371. layout(location = 0) out vec4 diffuse_buffer; //diffuse (rgb) and roughness
  372. layout(location = 1) out vec4 specular_buffer; //specular and SSS (subsurface scatter)
  373. #else
  374. layout(location = 0) out mediump vec4 frag_color;
  375. #endif // MODE_MULTIPLE_RENDER_TARGETS
  376. #endif // RENDER DEPTH
  377. #include "scene_forward_aa_inc.glsl"
  378. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  379. // Default to SPECULAR_SCHLICK_GGX.
  380. #if !defined(SPECULAR_DISABLED) && !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_TOON)
  381. #define SPECULAR_SCHLICK_GGX
  382. #endif
  383. #include "scene_forward_lights_inc.glsl"
  384. #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  385. #ifndef MODE_RENDER_DEPTH
  386. /*
  387. Only supporting normal fog here.
  388. */
  389. vec4 fog_process(vec3 vertex) {
  390. vec3 fog_color = scene_data.fog_light_color;
  391. if (scene_data.fog_aerial_perspective > 0.0) {
  392. vec3 sky_fog_color = vec3(0.0);
  393. vec3 cube_view = scene_data.radiance_inverse_xform * vertex;
  394. // mip_level always reads from the second mipmap and higher so the fog is always slightly blurred
  395. 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));
  396. #ifdef USE_RADIANCE_CUBEMAP_ARRAY
  397. float lod, blend;
  398. blend = modf(mip_level * MAX_ROUGHNESS_LOD, lod);
  399. sky_fog_color = texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(cube_view, lod)).rgb;
  400. sky_fog_color = mix(sky_fog_color, texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(cube_view, lod + 1)).rgb, blend);
  401. #else
  402. sky_fog_color = textureLod(samplerCube(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), cube_view, mip_level * MAX_ROUGHNESS_LOD).rgb;
  403. #endif //USE_RADIANCE_CUBEMAP_ARRAY
  404. fog_color = mix(fog_color, sky_fog_color, scene_data.fog_aerial_perspective);
  405. }
  406. if (scene_data.fog_sun_scatter > 0.001) {
  407. vec4 sun_scatter = vec4(0.0);
  408. float sun_total = 0.0;
  409. vec3 view = normalize(vertex);
  410. for (uint i = 0; i < scene_data.directional_light_count; i++) {
  411. vec3 light_color = directional_lights.data[i].color * directional_lights.data[i].energy;
  412. float light_amount = pow(max(dot(view, directional_lights.data[i].direction), 0.0), 8.0);
  413. fog_color += light_color * light_amount * scene_data.fog_sun_scatter;
  414. }
  415. }
  416. float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density));
  417. if (abs(scene_data.fog_height_density) >= 0.0001) {
  418. float y = (scene_data.inv_view_matrix * vec4(vertex, 1.0)).y;
  419. float y_dist = y - scene_data.fog_height;
  420. float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data.fog_height_density));
  421. fog_amount = max(vfog_amount, fog_amount);
  422. }
  423. return vec4(fog_color, fog_amount);
  424. }
  425. #endif //!MODE_RENDER DEPTH
  426. void main() {
  427. #ifdef MODE_DUAL_PARABOLOID
  428. if (dp_clip > 0.0)
  429. discard;
  430. #endif
  431. //lay out everything, whatever is unused is optimized away anyway
  432. vec3 vertex = vertex_interp;
  433. vec3 view = -normalize(vertex_interp);
  434. vec3 albedo = vec3(1.0);
  435. vec3 backlight = vec3(0.0);
  436. vec4 transmittance_color = vec4(0.0);
  437. float transmittance_depth = 0.0;
  438. float transmittance_boost = 0.0;
  439. float metallic = 0.0;
  440. float specular = 0.5;
  441. vec3 emission = vec3(0.0);
  442. float roughness = 1.0;
  443. float rim = 0.0;
  444. float rim_tint = 0.0;
  445. float clearcoat = 0.0;
  446. float clearcoat_roughness = 0.0;
  447. float anisotropy = 0.0;
  448. vec2 anisotropy_flow = vec2(1.0, 0.0);
  449. vec4 fog = vec4(0.0);
  450. #if defined(CUSTOM_RADIANCE_USED)
  451. vec4 custom_radiance = vec4(0.0);
  452. #endif
  453. #if defined(CUSTOM_IRRADIANCE_USED)
  454. vec4 custom_irradiance = vec4(0.0);
  455. #endif
  456. float ao = 1.0;
  457. float ao_light_affect = 0.0;
  458. float alpha = 1.0;
  459. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  460. vec3 binormal = normalize(binormal_interp);
  461. vec3 tangent = normalize(tangent_interp);
  462. #else
  463. vec3 binormal = vec3(0.0);
  464. vec3 tangent = vec3(0.0);
  465. #endif
  466. #ifdef NORMAL_USED
  467. vec3 normal = normalize(normal_interp);
  468. #if defined(DO_SIDE_CHECK)
  469. if (!gl_FrontFacing) {
  470. normal = -normal;
  471. }
  472. #endif
  473. #endif //NORMAL_USED
  474. #ifdef UV_USED
  475. vec2 uv = uv_interp;
  476. #endif
  477. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  478. vec2 uv2 = uv2_interp;
  479. #endif
  480. #if defined(COLOR_USED)
  481. vec4 color = color_interp;
  482. #endif
  483. #if defined(NORMAL_MAP_USED)
  484. vec3 normal_map = vec3(0.5);
  485. #endif
  486. float normal_map_depth = 1.0;
  487. vec2 screen_uv = gl_FragCoord.xy * scene_data.screen_pixel_size + scene_data.screen_pixel_size * 0.5; //account for center
  488. float sss_strength = 0.0;
  489. #ifdef ALPHA_SCISSOR_USED
  490. float alpha_scissor_threshold = 1.0;
  491. #endif // ALPHA_SCISSOR_USED
  492. #ifdef ALPHA_HASH_USED
  493. float alpha_hash_scale = 1.0;
  494. #endif // ALPHA_HASH_USED
  495. #ifdef ALPHA_ANTIALIASING_EDGE_USED
  496. float alpha_antialiasing_edge = 0.0;
  497. vec2 alpha_texture_coordinate = vec2(0.0, 0.0);
  498. #endif // ALPHA_ANTIALIASING_EDGE_USED
  499. {
  500. #CODE : FRAGMENT
  501. }
  502. #ifdef LIGHT_TRANSMITTANCE_USED
  503. #ifdef SSS_MODE_SKIN
  504. transmittance_color.a = sss_strength;
  505. #else
  506. transmittance_color.a *= sss_strength;
  507. #endif
  508. #endif
  509. #ifndef USE_SHADOW_TO_OPACITY
  510. #ifdef ALPHA_SCISSOR_USED
  511. if (alpha < alpha_scissor_threshold) {
  512. discard;
  513. }
  514. #endif // ALPHA_SCISSOR_USED
  515. // alpha hash can be used in unison with alpha antialiasing
  516. #ifdef ALPHA_HASH_USED
  517. if (alpha < compute_alpha_hash_threshold(vertex, alpha_hash_scale)) {
  518. discard;
  519. }
  520. #endif // ALPHA_HASH_USED
  521. // If we are not edge antialiasing, we need to remove the output alpha channel from scissor and hash
  522. #if (defined(ALPHA_SCISSOR_USED) || defined(ALPHA_HASH_USED)) && !defined(ALPHA_ANTIALIASING_EDGE_USED)
  523. alpha = 1.0;
  524. #endif
  525. #ifdef ALPHA_ANTIALIASING_EDGE_USED
  526. // If alpha scissor is used, we must further the edge threshold, otherwise we won't get any edge feather
  527. #ifdef ALPHA_SCISSOR_USED
  528. alpha_antialiasing_edge = clamp(alpha_scissor_threshold + alpha_antialiasing_edge, 0.0, 1.0);
  529. #endif
  530. alpha = compute_alpha_antialiasing_edge(alpha, alpha_texture_coordinate, alpha_antialiasing_edge);
  531. #endif // ALPHA_ANTIALIASING_EDGE_USED
  532. #ifdef USE_OPAQUE_PREPASS
  533. if (alpha < scene_data.opaque_prepass_threshold) {
  534. discard;
  535. }
  536. #endif // USE_OPAQUE_PREPASS
  537. #endif // !USE_SHADOW_TO_OPACITY
  538. #ifdef NORMAL_MAP_USED
  539. normal_map.xy = normal_map.xy * 2.0 - 1.0;
  540. 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.
  541. normal = normalize(mix(normal, tangent * normal_map.x + binormal * normal_map.y + normal * normal_map.z, normal_map_depth));
  542. #endif
  543. #ifdef LIGHT_ANISOTROPY_USED
  544. if (anisotropy > 0.01) {
  545. //rotation matrix
  546. mat3 rot = mat3(tangent, binormal, normal);
  547. //make local to space
  548. tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0));
  549. binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0));
  550. }
  551. #endif
  552. #ifdef ENABLE_CLIP_ALPHA
  553. if (albedo.a < 0.99) {
  554. //used for doublepass and shadowmapping
  555. discard;
  556. }
  557. #endif
  558. /////////////////////// FOG //////////////////////
  559. #ifndef MODE_RENDER_DEPTH
  560. #ifndef CUSTOM_FOG_USED
  561. // fog must be processed as early as possible and then packed.
  562. // to maximize VGPR usage
  563. // Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky.
  564. if (!sc_disable_fog && scene_data.fog_enabled) {
  565. fog = fog_process(vertex);
  566. }
  567. #endif //!CUSTOM_FOG_USED
  568. uint fog_rg = packHalf2x16(fog.rg);
  569. uint fog_ba = packHalf2x16(fog.ba);
  570. #endif //!MODE_RENDER_DEPTH
  571. /////////////////////// DECALS ////////////////////////////////
  572. #ifndef MODE_RENDER_DEPTH
  573. vec3 vertex_ddx = dFdx(vertex);
  574. vec3 vertex_ddy = dFdy(vertex);
  575. if (!sc_disable_decals) { //Decals
  576. // must implement
  577. uint decal_indices = draw_call.decals.x;
  578. for (uint i = 0; i < 8; i++) {
  579. uint decal_index = decal_indices & 0xFF;
  580. if (i == 4) {
  581. decal_indices = draw_call.decals.y;
  582. } else {
  583. decal_indices = decal_indices >> 8;
  584. }
  585. if (decal_index == 0xFF) {
  586. break;
  587. }
  588. vec3 uv_local = (decals.data[decal_index].xform * vec4(vertex, 1.0)).xyz;
  589. if (any(lessThan(uv_local, vec3(0.0, -1.0, 0.0))) || any(greaterThan(uv_local, vec3(1.0)))) {
  590. continue; //out of decal
  591. }
  592. float fade = pow(1.0 - (uv_local.y > 0.0 ? uv_local.y : -uv_local.y), uv_local.y > 0.0 ? decals.data[decal_index].upper_fade : decals.data[decal_index].lower_fade);
  593. if (decals.data[decal_index].normal_fade > 0.0) {
  594. fade *= smoothstep(decals.data[decal_index].normal_fade, 1.0, dot(normal_interp, decals.data[decal_index].normal) * 0.5 + 0.5);
  595. }
  596. //we need ddx/ddy for mipmaps, so simulate them
  597. vec2 ddx = (decals.data[decal_index].xform * vec4(vertex_ddx, 0.0)).xz;
  598. vec2 ddy = (decals.data[decal_index].xform * vec4(vertex_ddy, 0.0)).xz;
  599. if (decals.data[decal_index].albedo_rect != vec4(0.0)) {
  600. //has albedo
  601. vec4 decal_albedo;
  602. if (sc_decal_use_mipmaps) {
  603. decal_albedo = textureGrad(sampler2D(decal_atlas_srgb, decal_sampler), uv_local.xz * decals.data[decal_index].albedo_rect.zw + decals.data[decal_index].albedo_rect.xy, ddx * decals.data[decal_index].albedo_rect.zw, ddy * decals.data[decal_index].albedo_rect.zw);
  604. } else {
  605. decal_albedo = textureLod(sampler2D(decal_atlas_srgb, decal_sampler), uv_local.xz * decals.data[decal_index].albedo_rect.zw + decals.data[decal_index].albedo_rect.xy, 0.0);
  606. }
  607. decal_albedo *= decals.data[decal_index].modulate;
  608. decal_albedo.a *= fade;
  609. albedo = mix(albedo, decal_albedo.rgb, decal_albedo.a * decals.data[decal_index].albedo_mix);
  610. if (decals.data[decal_index].normal_rect != vec4(0.0)) {
  611. vec3 decal_normal;
  612. if (sc_decal_use_mipmaps) {
  613. decal_normal = textureGrad(sampler2D(decal_atlas, decal_sampler), uv_local.xz * decals.data[decal_index].normal_rect.zw + decals.data[decal_index].normal_rect.xy, ddx * decals.data[decal_index].normal_rect.zw, ddy * decals.data[decal_index].normal_rect.zw).xyz;
  614. } else {
  615. decal_normal = textureLod(sampler2D(decal_atlas, decal_sampler), uv_local.xz * decals.data[decal_index].normal_rect.zw + decals.data[decal_index].normal_rect.xy, 0.0).xyz;
  616. }
  617. decal_normal.xy = decal_normal.xy * vec2(2.0, -2.0) - vec2(1.0, -1.0); //users prefer flipped y normal maps in most authoring software
  618. decal_normal.z = sqrt(max(0.0, 1.0 - dot(decal_normal.xy, decal_normal.xy)));
  619. //convert to view space, use xzy because y is up
  620. decal_normal = (decals.data[decal_index].normal_xform * decal_normal.xzy).xyz;
  621. normal = normalize(mix(normal, decal_normal, decal_albedo.a));
  622. }
  623. if (decals.data[decal_index].orm_rect != vec4(0.0)) {
  624. vec3 decal_orm;
  625. if (sc_decal_use_mipmaps) {
  626. decal_orm = textureGrad(sampler2D(decal_atlas, decal_sampler), uv_local.xz * decals.data[decal_index].orm_rect.zw + decals.data[decal_index].orm_rect.xy, ddx * decals.data[decal_index].orm_rect.zw, ddy * decals.data[decal_index].orm_rect.zw).xyz;
  627. } else {
  628. decal_orm = textureLod(sampler2D(decal_atlas, decal_sampler), uv_local.xz * decals.data[decal_index].orm_rect.zw + decals.data[decal_index].orm_rect.xy, 0.0).xyz;
  629. }
  630. ao = mix(ao, decal_orm.r, decal_albedo.a);
  631. roughness = mix(roughness, decal_orm.g, decal_albedo.a);
  632. metallic = mix(metallic, decal_orm.b, decal_albedo.a);
  633. }
  634. }
  635. if (decals.data[decal_index].emission_rect != vec4(0.0)) {
  636. //emission is additive, so its independent from albedo
  637. if (sc_decal_use_mipmaps) {
  638. emission += textureGrad(sampler2D(decal_atlas_srgb, decal_sampler), uv_local.xz * decals.data[decal_index].emission_rect.zw + decals.data[decal_index].emission_rect.xy, ddx * decals.data[decal_index].emission_rect.zw, ddy * decals.data[decal_index].emission_rect.zw).xyz * decals.data[decal_index].emission_energy * fade;
  639. } else {
  640. emission += textureLod(sampler2D(decal_atlas_srgb, decal_sampler), uv_local.xz * decals.data[decal_index].emission_rect.zw + decals.data[decal_index].emission_rect.xy, 0.0).xyz * decals.data[decal_index].emission_energy * fade;
  641. }
  642. }
  643. }
  644. } //Decals
  645. #endif //!MODE_RENDER_DEPTH
  646. /////////////////////// LIGHTING //////////////////////////////
  647. #ifdef NORMAL_USED
  648. if (scene_data.roughness_limiter_enabled) {
  649. //https://www.jp.square-enix.com/tech/library/pdf/ImprovedGeometricSpecularAA.pdf
  650. float roughness2 = roughness * roughness;
  651. vec3 dndu = dFdx(normal), dndv = dFdy(normal);
  652. float variance = scene_data.roughness_limiter_amount * (dot(dndu, dndu) + dot(dndv, dndv));
  653. float kernelRoughness2 = min(2.0 * variance, scene_data.roughness_limiter_limit); //limit effect
  654. float filteredRoughness2 = min(1.0, roughness2 + kernelRoughness2);
  655. roughness = sqrt(filteredRoughness2);
  656. }
  657. #endif // NORMAL_USED
  658. //apply energy conservation
  659. vec3 specular_light = vec3(0.0, 0.0, 0.0);
  660. vec3 diffuse_light = vec3(0.0, 0.0, 0.0);
  661. vec3 ambient_light = vec3(0.0, 0.0, 0.0);
  662. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  663. if (scene_data.use_reflection_cubemap) {
  664. #ifdef LIGHT_ANISOTROPY_USED
  665. // https://google.github.io/filament/Filament.html#lighting/imagebasedlights/anisotropy
  666. vec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent;
  667. vec3 anisotropic_tangent = cross(anisotropic_direction, view);
  668. vec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction);
  669. vec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0)));
  670. vec3 ref_vec = reflect(-view, bent_normal);
  671. #else
  672. vec3 ref_vec = reflect(-view, normal);
  673. #endif
  674. float horizon = min(1.0 + dot(ref_vec, normal), 1.0);
  675. ref_vec = scene_data.radiance_inverse_xform * ref_vec;
  676. #ifdef USE_RADIANCE_CUBEMAP_ARRAY
  677. float lod, blend;
  678. blend = modf(roughness * MAX_ROUGHNESS_LOD, lod);
  679. specular_light = texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(ref_vec, lod)).rgb;
  680. specular_light = mix(specular_light, texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(ref_vec, lod + 1)).rgb, blend);
  681. #else // USE_RADIANCE_CUBEMAP_ARRAY
  682. specular_light = textureLod(samplerCube(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), ref_vec, roughness * MAX_ROUGHNESS_LOD).rgb;
  683. #endif //USE_RADIANCE_CUBEMAP_ARRAY
  684. specular_light *= horizon * horizon;
  685. specular_light *= scene_data.ambient_light_color_energy.a;
  686. }
  687. #if defined(CUSTOM_RADIANCE_USED)
  688. specular_light = mix(specular_light, custom_radiance.rgb, custom_radiance.a);
  689. #endif // CUSTOM_RADIANCE_USED
  690. #ifndef USE_LIGHTMAP
  691. //lightmap overrides everything
  692. if (scene_data.use_ambient_light) {
  693. ambient_light = scene_data.ambient_light_color_energy.rgb;
  694. if (scene_data.use_ambient_cubemap) {
  695. vec3 ambient_dir = scene_data.radiance_inverse_xform * normal;
  696. #ifdef USE_RADIANCE_CUBEMAP_ARRAY
  697. vec3 cubemap_ambient = texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(ambient_dir, MAX_ROUGHNESS_LOD)).rgb;
  698. #else
  699. vec3 cubemap_ambient = textureLod(samplerCube(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), ambient_dir, MAX_ROUGHNESS_LOD).rgb;
  700. #endif //USE_RADIANCE_CUBEMAP_ARRAY
  701. ambient_light = mix(ambient_light, cubemap_ambient * scene_data.ambient_light_color_energy.a, scene_data.ambient_color_sky_mix);
  702. }
  703. }
  704. #endif // !USE_LIGHTMAP
  705. #if defined(CUSTOM_IRRADIANCE_USED)
  706. ambient_light = mix(specular_light, custom_irradiance.rgb, custom_irradiance.a);
  707. #endif // CUSTOM_IRRADIANCE_USED
  708. #ifdef LIGHT_CLEARCOAT_USED
  709. if (scene_data.use_reflection_cubemap) {
  710. vec3 n = normalize(normal_interp); // We want to use geometric normal, not normal_map
  711. float NoV = max(dot(n, view), 0.0001);
  712. vec3 ref_vec = reflect(-view, n);
  713. // The clear coat layer assumes an IOR of 1.5 (4% reflectance)
  714. float Fc = clearcoat * (0.04 + 0.96 * SchlickFresnel(NoV));
  715. float attenuation = 1.0 - Fc;
  716. ambient_light *= attenuation;
  717. specular_light *= attenuation;
  718. float horizon = min(1.0 + dot(ref_vec, normal), 1.0);
  719. ref_vec = scene_data.radiance_inverse_xform * ref_vec;
  720. float roughness_lod = mix(0.001, 0.1, clearcoat_roughness) * MAX_ROUGHNESS_LOD;
  721. #ifdef USE_RADIANCE_CUBEMAP_ARRAY
  722. float lod, blend;
  723. blend = modf(roughness_lod, lod);
  724. vec3 clearcoat_light = texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(ref_vec, lod)).rgb;
  725. clearcoat_light = mix(clearcoat_light, texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(ref_vec, lod + 1)).rgb, blend);
  726. #else
  727. vec3 clearcoat_light = textureLod(samplerCube(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), ref_vec, roughness_lod).rgb;
  728. #endif //USE_RADIANCE_CUBEMAP_ARRAY
  729. specular_light += clearcoat_light * horizon * horizon * Fc * scene_data.ambient_light_color_energy.a;
  730. }
  731. #endif
  732. #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  733. //radiance
  734. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  735. #ifdef USE_LIGHTMAP
  736. //lightmap
  737. if (bool(draw_call.flags & INSTANCE_FLAGS_USE_LIGHTMAP_CAPTURE)) { //has lightmap capture
  738. uint index = draw_call.gi_offset;
  739. vec3 wnormal = mat3(scene_data.inv_view_matrix) * normal;
  740. const float c1 = 0.429043;
  741. const float c2 = 0.511664;
  742. const float c3 = 0.743125;
  743. const float c4 = 0.886227;
  744. const float c5 = 0.247708;
  745. ambient_light += (c1 * lightmap_captures.data[index].sh[8].rgb * (wnormal.x * wnormal.x - wnormal.y * wnormal.y) +
  746. c3 * lightmap_captures.data[index].sh[6].rgb * wnormal.z * wnormal.z +
  747. c4 * lightmap_captures.data[index].sh[0].rgb -
  748. c5 * lightmap_captures.data[index].sh[6].rgb +
  749. 2.0 * c1 * lightmap_captures.data[index].sh[4].rgb * wnormal.x * wnormal.y +
  750. 2.0 * c1 * lightmap_captures.data[index].sh[7].rgb * wnormal.x * wnormal.z +
  751. 2.0 * c1 * lightmap_captures.data[index].sh[5].rgb * wnormal.y * wnormal.z +
  752. 2.0 * c2 * lightmap_captures.data[index].sh[3].rgb * wnormal.x +
  753. 2.0 * c2 * lightmap_captures.data[index].sh[1].rgb * wnormal.y +
  754. 2.0 * c2 * lightmap_captures.data[index].sh[2].rgb * wnormal.z);
  755. } else if (bool(draw_call.flags & INSTANCE_FLAGS_USE_LIGHTMAP)) { // has actual lightmap
  756. bool uses_sh = bool(draw_call.flags & INSTANCE_FLAGS_USE_SH_LIGHTMAP);
  757. uint ofs = draw_call.gi_offset & 0xFFFF;
  758. vec3 uvw;
  759. uvw.xy = uv2 * draw_call.lightmap_uv_scale.zw + draw_call.lightmap_uv_scale.xy;
  760. uvw.z = float((draw_call.gi_offset >> 16) & 0xFFFF);
  761. if (uses_sh) {
  762. uvw.z *= 4.0; //SH textures use 4 times more data
  763. vec3 lm_light_l0 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 0.0), 0.0).rgb;
  764. vec3 lm_light_l1n1 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 1.0), 0.0).rgb;
  765. vec3 lm_light_l1_0 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 2.0), 0.0).rgb;
  766. vec3 lm_light_l1p1 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 3.0), 0.0).rgb;
  767. uint idx = draw_call.gi_offset >> 20;
  768. vec3 n = normalize(lightmaps.data[idx].normal_xform * normal);
  769. ambient_light += lm_light_l0 * 0.282095f;
  770. ambient_light += lm_light_l1n1 * 0.32573 * n.y;
  771. ambient_light += lm_light_l1_0 * 0.32573 * n.z;
  772. ambient_light += lm_light_l1p1 * 0.32573 * n.x;
  773. if (metallic > 0.01) { // since the more direct bounced light is lost, we can kind of fake it with this trick
  774. vec3 r = reflect(normalize(-vertex), normal);
  775. specular_light += lm_light_l1n1 * 0.32573 * r.y;
  776. specular_light += lm_light_l1_0 * 0.32573 * r.z;
  777. specular_light += lm_light_l1p1 * 0.32573 * r.x;
  778. }
  779. } else {
  780. ambient_light += textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw, 0.0).rgb;
  781. }
  782. }
  783. // No GI nor non low end mode...
  784. #endif // USE_LIGHTMAP
  785. // skipping ssao, do we remove ssao totally?
  786. if (!sc_disable_reflection_probes) { //Reflection probes
  787. vec4 reflection_accum = vec4(0.0, 0.0, 0.0, 0.0);
  788. vec4 ambient_accum = vec4(0.0, 0.0, 0.0, 0.0);
  789. uint reflection_indices = draw_call.reflection_probes.x;
  790. for (uint i = 0; i < 8; i++) {
  791. uint reflection_index = reflection_indices & 0xFF;
  792. if (i == 4) {
  793. reflection_indices = draw_call.reflection_probes.y;
  794. } else {
  795. reflection_indices = reflection_indices >> 8;
  796. }
  797. if (reflection_index == 0xFF) {
  798. break;
  799. }
  800. #ifdef LIGHT_ANISOTROPY_USED
  801. // https://google.github.io/filament/Filament.html#lighting/imagebasedlights/anisotropy
  802. vec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent;
  803. vec3 anisotropic_tangent = cross(anisotropic_direction, view);
  804. vec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction);
  805. vec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0)));
  806. #else
  807. vec3 bent_normal = normal;
  808. #endif
  809. reflection_process(reflection_index, vertex, bent_normal, roughness, ambient_light, specular_light, ambient_accum, reflection_accum);
  810. }
  811. if (reflection_accum.a > 0.0) {
  812. specular_light = reflection_accum.rgb / reflection_accum.a;
  813. }
  814. #if !defined(USE_LIGHTMAP)
  815. if (ambient_accum.a > 0.0) {
  816. ambient_light = ambient_accum.rgb / ambient_accum.a;
  817. }
  818. #endif
  819. } //Reflection probes
  820. // finalize ambient light here
  821. ambient_light *= albedo.rgb;
  822. ambient_light *= ao;
  823. // convert ao to direct light ao
  824. ao = mix(1.0, ao, ao_light_affect);
  825. //this saves some VGPRs
  826. vec3 f0 = F0(metallic, specular, albedo);
  827. {
  828. #if defined(DIFFUSE_TOON)
  829. //simplify for toon, as
  830. specular_light *= specular * metallic * albedo * 2.0;
  831. #else
  832. // scales the specular reflections, needs to be computed before lighting happens,
  833. // but after environment, GI, and reflection probes are added
  834. // Environment brdf approximation (Lazarov 2013)
  835. // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
  836. const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
  837. const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);
  838. vec4 r = roughness * c0 + c1;
  839. float ndotv = clamp(dot(normal, view), 0.0, 1.0);
  840. float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y;
  841. vec2 env = vec2(-1.04, 1.04) * a004 + r.zw;
  842. specular_light *= env.x * f0 + env.y;
  843. #endif
  844. }
  845. #endif // !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  846. #if !defined(MODE_RENDER_DEPTH)
  847. //this saves some VGPRs
  848. uint orms = packUnorm4x8(vec4(ao, roughness, metallic, specular));
  849. #endif
  850. // LIGHTING
  851. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  852. if (!sc_disable_directional_lights) { //directional light
  853. #ifndef SHADOWS_DISABLED
  854. // Do shadow and lighting in two passes to reduce register pressure
  855. uint shadow0 = 0;
  856. uint shadow1 = 0;
  857. for (uint i = 0; i < 8; i++) {
  858. if (i >= scene_data.directional_light_count) {
  859. break;
  860. }
  861. if (!bool(directional_lights.data[i].mask & draw_call.layer_mask)) {
  862. continue; //not masked
  863. }
  864. float shadow = 1.0;
  865. // Directional light shadow code is basically the same as forward clustered at this point in time minus `LIGHT_TRANSMITTANCE_USED` support.
  866. // Not sure if there is a reason to change this seeing directional lights are part of our global data
  867. // Should think about whether we may want to move this code into an include file or function??
  868. #ifdef USE_SOFT_SHADOWS
  869. //version with soft shadows, more expensive
  870. if (directional_lights.data[i].shadow_enabled) {
  871. float depth_z = -vertex.z;
  872. vec4 pssm_coord;
  873. vec3 light_dir = directional_lights.data[i].direction;
  874. #define BIAS_FUNC(m_var, m_idx) \
  875. m_var.xyz += light_dir * directional_lights.data[i].shadow_bias[m_idx]; \
  876. vec3 normal_bias = normalize(normal_interp) * (1.0 - max(0.0, dot(light_dir, -normalize(normal_interp)))) * directional_lights.data[i].shadow_normal_bias[m_idx]; \
  877. normal_bias -= light_dir * dot(light_dir, normal_bias); \
  878. m_var.xyz += normal_bias;
  879. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  880. vec4 v = vec4(vertex, 1.0);
  881. BIAS_FUNC(v, 0)
  882. pssm_coord = (directional_lights.data[i].shadow_matrix1 * v);
  883. pssm_coord /= pssm_coord.w;
  884. if (directional_lights.data[i].softshadow_angle > 0) {
  885. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  886. float range_begin = directional_lights.data[i].shadow_range_begin.x;
  887. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  888. vec2 tex_scale = directional_lights.data[i].uv_scale1 * test_radius;
  889. shadow = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  890. } else {
  891. shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  892. }
  893. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  894. vec4 v = vec4(vertex, 1.0);
  895. BIAS_FUNC(v, 1)
  896. pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  897. pssm_coord /= pssm_coord.w;
  898. if (directional_lights.data[i].softshadow_angle > 0) {
  899. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  900. float range_begin = directional_lights.data[i].shadow_range_begin.y;
  901. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  902. vec2 tex_scale = directional_lights.data[i].uv_scale2 * test_radius;
  903. shadow = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  904. } else {
  905. shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  906. }
  907. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  908. vec4 v = vec4(vertex, 1.0);
  909. BIAS_FUNC(v, 2)
  910. pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  911. pssm_coord /= pssm_coord.w;
  912. if (directional_lights.data[i].softshadow_angle > 0) {
  913. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  914. float range_begin = directional_lights.data[i].shadow_range_begin.z;
  915. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  916. vec2 tex_scale = directional_lights.data[i].uv_scale3 * test_radius;
  917. shadow = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  918. } else {
  919. shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  920. }
  921. } else {
  922. vec4 v = vec4(vertex, 1.0);
  923. BIAS_FUNC(v, 3)
  924. pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  925. pssm_coord /= pssm_coord.w;
  926. if (directional_lights.data[i].softshadow_angle > 0) {
  927. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  928. float range_begin = directional_lights.data[i].shadow_range_begin.w;
  929. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  930. vec2 tex_scale = directional_lights.data[i].uv_scale4 * test_radius;
  931. shadow = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  932. } else {
  933. shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  934. }
  935. }
  936. if (directional_lights.data[i].blend_splits) {
  937. float pssm_blend;
  938. float shadow2;
  939. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  940. vec4 v = vec4(vertex, 1.0);
  941. BIAS_FUNC(v, 1)
  942. pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  943. pssm_coord /= pssm_coord.w;
  944. if (directional_lights.data[i].softshadow_angle > 0) {
  945. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  946. float range_begin = directional_lights.data[i].shadow_range_begin.y;
  947. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  948. vec2 tex_scale = directional_lights.data[i].uv_scale2 * test_radius;
  949. shadow2 = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  950. } else {
  951. shadow2 = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  952. }
  953. pssm_blend = smoothstep(0.0, directional_lights.data[i].shadow_split_offsets.x, depth_z);
  954. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  955. vec4 v = vec4(vertex, 1.0);
  956. BIAS_FUNC(v, 2)
  957. pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  958. pssm_coord /= pssm_coord.w;
  959. if (directional_lights.data[i].softshadow_angle > 0) {
  960. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  961. float range_begin = directional_lights.data[i].shadow_range_begin.z;
  962. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  963. vec2 tex_scale = directional_lights.data[i].uv_scale3 * test_radius;
  964. shadow2 = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  965. } else {
  966. shadow2 = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  967. }
  968. pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.x, directional_lights.data[i].shadow_split_offsets.y, depth_z);
  969. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  970. vec4 v = vec4(vertex, 1.0);
  971. BIAS_FUNC(v, 3)
  972. pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  973. pssm_coord /= pssm_coord.w;
  974. if (directional_lights.data[i].softshadow_angle > 0) {
  975. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  976. float range_begin = directional_lights.data[i].shadow_range_begin.w;
  977. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  978. vec2 tex_scale = directional_lights.data[i].uv_scale4 * test_radius;
  979. shadow2 = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  980. } else {
  981. shadow2 = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  982. }
  983. pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.y, directional_lights.data[i].shadow_split_offsets.z, depth_z);
  984. } else {
  985. pssm_blend = 0.0; //if no blend, same coord will be used (divide by z will result in same value, and already cached)
  986. }
  987. pssm_blend = sqrt(pssm_blend);
  988. shadow = mix(shadow, shadow2, pssm_blend);
  989. }
  990. shadow = mix(shadow, 1.0, smoothstep(directional_lights.data[i].fade_from, directional_lights.data[i].fade_to, vertex.z)); //done with negative values for performance
  991. #undef BIAS_FUNC
  992. }
  993. #else
  994. // Soft shadow disabled version
  995. if (directional_lights.data[i].shadow_enabled) {
  996. float depth_z = -vertex.z;
  997. vec4 pssm_coord;
  998. vec3 light_dir = directional_lights.data[i].direction;
  999. vec3 base_normal_bias = normalize(normal_interp) * (1.0 - max(0.0, dot(light_dir, -normalize(normal_interp))));
  1000. #define BIAS_FUNC(m_var, m_idx) \
  1001. m_var.xyz += light_dir * directional_lights.data[i].shadow_bias[m_idx]; \
  1002. vec3 normal_bias = base_normal_bias * directional_lights.data[i].shadow_normal_bias[m_idx]; \
  1003. normal_bias -= light_dir * dot(light_dir, normal_bias); \
  1004. m_var.xyz += normal_bias;
  1005. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  1006. vec4 v = vec4(vertex, 1.0);
  1007. BIAS_FUNC(v, 0)
  1008. pssm_coord = (directional_lights.data[i].shadow_matrix1 * v);
  1009. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  1010. vec4 v = vec4(vertex, 1.0);
  1011. BIAS_FUNC(v, 1)
  1012. pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  1013. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  1014. vec4 v = vec4(vertex, 1.0);
  1015. BIAS_FUNC(v, 2)
  1016. pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  1017. } else {
  1018. vec4 v = vec4(vertex, 1.0);
  1019. BIAS_FUNC(v, 3)
  1020. pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  1021. }
  1022. pssm_coord /= pssm_coord.w;
  1023. shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  1024. if (directional_lights.data[i].blend_splits) {
  1025. float pssm_blend;
  1026. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  1027. vec4 v = vec4(vertex, 1.0);
  1028. BIAS_FUNC(v, 1)
  1029. pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  1030. pssm_blend = smoothstep(0.0, directional_lights.data[i].shadow_split_offsets.x, depth_z);
  1031. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  1032. vec4 v = vec4(vertex, 1.0);
  1033. BIAS_FUNC(v, 2)
  1034. pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  1035. pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.x, directional_lights.data[i].shadow_split_offsets.y, depth_z);
  1036. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  1037. vec4 v = vec4(vertex, 1.0);
  1038. BIAS_FUNC(v, 3)
  1039. pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  1040. pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.y, directional_lights.data[i].shadow_split_offsets.z, depth_z);
  1041. } else {
  1042. pssm_blend = 0.0; //if no blend, same coord will be used (divide by z will result in same value, and already cached)
  1043. }
  1044. pssm_coord /= pssm_coord.w;
  1045. float shadow2 = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  1046. shadow = mix(shadow, shadow2, pssm_blend);
  1047. }
  1048. shadow = mix(shadow, 1.0, smoothstep(directional_lights.data[i].fade_from, directional_lights.data[i].fade_to, vertex.z)); //done with negative values for performance
  1049. #undef BIAS_FUNC
  1050. }
  1051. #endif
  1052. if (i < 4) {
  1053. shadow0 |= uint(clamp(shadow * 255.0, 0.0, 255.0)) << (i * 8);
  1054. } else {
  1055. shadow1 |= uint(clamp(shadow * 255.0, 0.0, 255.0)) << ((i - 4) * 8);
  1056. }
  1057. }
  1058. #endif // SHADOWS_DISABLED
  1059. for (uint i = 0; i < 8; i++) {
  1060. if (i >= scene_data.directional_light_count) {
  1061. break;
  1062. }
  1063. if (!bool(directional_lights.data[i].mask & draw_call.layer_mask)) {
  1064. continue; //not masked
  1065. }
  1066. // We're not doing light transmittence
  1067. float shadow = 1.0;
  1068. #ifndef SHADOWS_DISABLED
  1069. if (i < 4) {
  1070. shadow = float(shadow0 >> (i * 8) & 0xFF) / 255.0;
  1071. } else {
  1072. shadow = float(shadow1 >> ((i - 4) * 8) & 0xFF) / 255.0;
  1073. }
  1074. #endif
  1075. blur_shadow(shadow);
  1076. light_compute(normal, directional_lights.data[i].direction, normalize(view), 0.0, directional_lights.data[i].color * directional_lights.data[i].energy, shadow, f0, orms, 1.0, albedo, alpha,
  1077. #ifdef LIGHT_BACKLIGHT_USED
  1078. backlight,
  1079. #endif
  1080. /* not supported here
  1081. #ifdef LIGHT_TRANSMITTANCE_USED
  1082. transmittance_color,
  1083. transmittance_depth,
  1084. transmittance_boost,
  1085. transmittance_z,
  1086. #endif
  1087. */
  1088. #ifdef LIGHT_RIM_USED
  1089. rim, rim_tint,
  1090. #endif
  1091. #ifdef LIGHT_CLEARCOAT_USED
  1092. clearcoat, clearcoat_roughness, normalize(normal_interp),
  1093. #endif
  1094. #ifdef LIGHT_ANISOTROPY_USED
  1095. binormal, tangent, anisotropy,
  1096. #endif
  1097. #ifdef USE_SOFT_SHADOW
  1098. directional_lights.data[i].size,
  1099. #endif
  1100. diffuse_light,
  1101. specular_light);
  1102. }
  1103. } //directional light
  1104. if (!sc_disable_omni_lights) { //omni lights
  1105. uint light_indices = draw_call.omni_lights.x;
  1106. for (uint i = 0; i < 8; i++) {
  1107. uint light_index = light_indices & 0xFF;
  1108. if (i == 4) {
  1109. light_indices = draw_call.omni_lights.y;
  1110. } else {
  1111. light_indices = light_indices >> 8;
  1112. }
  1113. if (light_index == 0xFF) {
  1114. break;
  1115. }
  1116. float shadow = light_process_omni_shadow(light_index, vertex, normal);
  1117. shadow = blur_shadow(shadow);
  1118. light_process_omni(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, shadow, albedo, alpha,
  1119. #ifdef LIGHT_BACKLIGHT_USED
  1120. backlight,
  1121. #endif
  1122. /*
  1123. #ifdef LIGHT_TRANSMITTANCE_USED
  1124. transmittance_color,
  1125. transmittance_depth,
  1126. transmittance_boost,
  1127. #endif
  1128. */
  1129. #ifdef LIGHT_RIM_USED
  1130. rim,
  1131. rim_tint,
  1132. #endif
  1133. #ifdef LIGHT_CLEARCOAT_USED
  1134. clearcoat, clearcoat_roughness, normalize(normal_interp),
  1135. #endif
  1136. #ifdef LIGHT_ANISOTROPY_USED
  1137. tangent,
  1138. binormal, anisotropy,
  1139. #endif
  1140. diffuse_light, specular_light);
  1141. }
  1142. } //omni lights
  1143. if (!sc_disable_spot_lights) { //spot lights
  1144. uint light_indices = draw_call.spot_lights.x;
  1145. for (uint i = 0; i < 8; i++) {
  1146. uint light_index = light_indices & 0xFF;
  1147. if (i == 4) {
  1148. light_indices = draw_call.spot_lights.y;
  1149. } else {
  1150. light_indices = light_indices >> 8;
  1151. }
  1152. if (light_index == 0xFF) {
  1153. break;
  1154. }
  1155. float shadow = light_process_spot_shadow(light_index, vertex, normal);
  1156. shadow = blur_shadow(shadow);
  1157. light_process_spot(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, shadow, albedo, alpha,
  1158. #ifdef LIGHT_BACKLIGHT_USED
  1159. backlight,
  1160. #endif
  1161. /*
  1162. #ifdef LIGHT_TRANSMITTANCE_USED
  1163. transmittance_color,
  1164. transmittance_depth,
  1165. transmittance_boost,
  1166. #endif
  1167. */
  1168. #ifdef LIGHT_RIM_USED
  1169. rim,
  1170. rim_tint,
  1171. #endif
  1172. #ifdef LIGHT_CLEARCOAT_USED
  1173. clearcoat, clearcoat_roughness, normalize(normal_interp),
  1174. #endif
  1175. #ifdef LIGHT_ANISOTROPY_USED
  1176. tangent,
  1177. binormal, anisotropy,
  1178. #endif
  1179. diffuse_light, specular_light);
  1180. }
  1181. } //spot lights
  1182. #ifdef USE_SHADOW_TO_OPACITY
  1183. alpha = min(alpha, clamp(length(ambient_light), 0.0, 1.0));
  1184. #if defined(ALPHA_SCISSOR_USED)
  1185. if (alpha < alpha_scissor) {
  1186. discard;
  1187. }
  1188. #endif // ALPHA_SCISSOR_USED
  1189. #ifdef USE_OPAQUE_PREPASS
  1190. if (alpha < scene_data.opaque_prepass_threshold) {
  1191. discard;
  1192. }
  1193. #endif // USE_OPAQUE_PREPASS
  1194. #endif // USE_SHADOW_TO_OPACITY
  1195. #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  1196. #ifdef MODE_RENDER_DEPTH
  1197. #ifdef MODE_RENDER_MATERIAL
  1198. albedo_output_buffer.rgb = albedo;
  1199. albedo_output_buffer.a = alpha;
  1200. normal_output_buffer.rgb = normal * 0.5 + 0.5;
  1201. normal_output_buffer.a = 0.0;
  1202. depth_output_buffer.r = -vertex.z;
  1203. orm_output_buffer.r = ao;
  1204. orm_output_buffer.g = roughness;
  1205. orm_output_buffer.b = metallic;
  1206. orm_output_buffer.a = sss_strength;
  1207. emission_output_buffer.rgb = emission;
  1208. emission_output_buffer.a = 0.0;
  1209. #endif // MODE_RENDER_MATERIAL
  1210. #else // MODE_RENDER_DEPTH
  1211. // multiply by albedo
  1212. diffuse_light *= albedo; // ambient must be multiplied by albedo at the end
  1213. // apply direct light AO
  1214. ao = unpackUnorm4x8(orms).x;
  1215. specular_light *= ao;
  1216. diffuse_light *= ao;
  1217. // apply metallic
  1218. metallic = unpackUnorm4x8(orms).z;
  1219. diffuse_light *= 1.0 - metallic;
  1220. ambient_light *= 1.0 - metallic;
  1221. //restore fog
  1222. fog = vec4(unpackHalf2x16(fog_rg), unpackHalf2x16(fog_ba));
  1223. #ifdef MODE_MULTIPLE_RENDER_TARGETS
  1224. #ifdef MODE_UNSHADED
  1225. diffuse_buffer = vec4(albedo.rgb, 0.0);
  1226. specular_buffer = vec4(0.0);
  1227. #else // MODE_UNSHADED
  1228. #ifdef SSS_MODE_SKIN
  1229. sss_strength = -sss_strength;
  1230. #endif // SSS_MODE_SKIN
  1231. diffuse_buffer = vec4(emission + diffuse_light + ambient_light, sss_strength);
  1232. specular_buffer = vec4(specular_light, metallic);
  1233. #endif // MODE_UNSHADED
  1234. diffuse_buffer.rgb = mix(diffuse_buffer.rgb, fog.rgb, fog.a);
  1235. specular_buffer.rgb = mix(specular_buffer.rgb, vec3(0.0), fog.a);
  1236. #else //MODE_MULTIPLE_RENDER_TARGETS
  1237. #ifdef MODE_UNSHADED
  1238. frag_color = vec4(albedo, alpha);
  1239. #else // MODE_UNSHADED
  1240. frag_color = vec4(emission + ambient_light + diffuse_light + specular_light, alpha);
  1241. #endif // MODE_UNSHADED
  1242. // Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky.
  1243. frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a);
  1244. // On mobile we use a UNORM buffer with 10bpp which results in a range from 0.0 - 1.0 resulting in HDR breaking
  1245. // We divide by sc_luminance_multiplier to support a range from 0.0 - 2.0 both increasing precision on bright and darker images
  1246. frag_color.rgb = frag_color.rgb / sc_luminance_multiplier;
  1247. #endif //MODE_MULTIPLE_RENDER_TARGETS
  1248. #endif //MODE_RENDER_DEPTH
  1249. }