scene.glsl 74 KB

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