scene.glsl 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151
  1. /* clang-format off */
  2. [vertex]
  3. #ifdef USE_GLES_OVER_GL
  4. #define lowp
  5. #define mediump
  6. #define highp
  7. #else
  8. precision highp float;
  9. precision highp int;
  10. #endif
  11. #include "stdlib.glsl"
  12. #define SHADER_IS_SRGB true
  13. #define M_PI 3.14159265359
  14. //
  15. // attributes
  16. //
  17. attribute highp vec4 vertex_attrib; // attrib:0
  18. /* clang-format on */
  19. attribute vec3 normal_attrib; // attrib:1
  20. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP)
  21. attribute vec4 tangent_attrib; // attrib:2
  22. #endif
  23. #if defined(ENABLE_COLOR_INTERP)
  24. attribute vec4 color_attrib; // attrib:3
  25. #endif
  26. #if defined(ENABLE_UV_INTERP)
  27. attribute vec2 uv_attrib; // attrib:4
  28. #endif
  29. #if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP)
  30. attribute vec2 uv2_attrib; // attrib:5
  31. #endif
  32. #ifdef USE_SKELETON
  33. #ifdef USE_SKELETON_SOFTWARE
  34. attribute highp vec4 bone_transform_row_0; // attrib:13
  35. attribute highp vec4 bone_transform_row_1; // attrib:14
  36. attribute highp vec4 bone_transform_row_2; // attrib:15
  37. #else
  38. attribute vec4 bone_ids; // attrib:6
  39. attribute highp vec4 bone_weights; // attrib:7
  40. uniform highp sampler2D bone_transforms; // texunit:-1
  41. uniform ivec2 skeleton_texture_size;
  42. #endif
  43. uniform highp mat4 skeleton_transform;
  44. uniform highp mat4 skeleton_transform_inverse;
  45. uniform bool skeleton_in_world_coords;
  46. #endif
  47. #ifdef USE_INSTANCING
  48. attribute highp vec4 instance_xform_row_0; // attrib:8
  49. attribute highp vec4 instance_xform_row_1; // attrib:9
  50. attribute highp vec4 instance_xform_row_2; // attrib:10
  51. attribute highp vec4 instance_color; // attrib:11
  52. attribute highp vec4 instance_custom_data; // attrib:12
  53. #endif
  54. //
  55. // uniforms
  56. //
  57. uniform highp mat4 camera_matrix;
  58. uniform highp mat4 camera_inverse_matrix;
  59. uniform highp mat4 projection_matrix;
  60. uniform highp mat4 projection_inverse_matrix;
  61. uniform highp mat4 world_transform;
  62. uniform highp float time;
  63. uniform highp vec2 viewport_size;
  64. #ifdef RENDER_DEPTH
  65. uniform float light_bias;
  66. uniform float light_normal_bias;
  67. #endif
  68. //
  69. // varyings
  70. //
  71. #if defined(RENDER_DEPTH) && defined(USE_RGBA_SHADOWS)
  72. varying highp vec4 position_interp;
  73. #endif
  74. varying highp vec3 vertex_interp;
  75. varying vec3 normal_interp;
  76. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP)
  77. varying vec3 tangent_interp;
  78. varying vec3 binormal_interp;
  79. #endif
  80. #if defined(ENABLE_COLOR_INTERP)
  81. varying vec4 color_interp;
  82. #endif
  83. #if defined(ENABLE_UV_INTERP)
  84. varying vec2 uv_interp;
  85. #endif
  86. #if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP)
  87. varying vec2 uv2_interp;
  88. #endif
  89. /* clang-format off */
  90. VERTEX_SHADER_GLOBALS
  91. /* clang-format on */
  92. #ifdef RENDER_DEPTH_DUAL_PARABOLOID
  93. varying highp float dp_clip;
  94. uniform highp float shadow_dual_paraboloid_render_zfar;
  95. uniform highp float shadow_dual_paraboloid_render_side;
  96. #endif
  97. #if defined(USE_SHADOW) && defined(USE_LIGHTING)
  98. uniform highp mat4 light_shadow_matrix;
  99. varying highp vec4 shadow_coord;
  100. #if defined(LIGHT_USE_PSSM2) || defined(LIGHT_USE_PSSM4)
  101. uniform highp mat4 light_shadow_matrix2;
  102. varying highp vec4 shadow_coord2;
  103. #endif
  104. #if defined(LIGHT_USE_PSSM4)
  105. uniform highp mat4 light_shadow_matrix3;
  106. uniform highp mat4 light_shadow_matrix4;
  107. varying highp vec4 shadow_coord3;
  108. varying highp vec4 shadow_coord4;
  109. #endif
  110. #endif
  111. #if defined(USE_VERTEX_LIGHTING) && defined(USE_LIGHTING)
  112. varying highp vec3 diffuse_interp;
  113. varying highp vec3 specular_interp;
  114. // general for all lights
  115. uniform highp vec4 light_color;
  116. uniform highp vec4 shadow_color;
  117. uniform highp float light_specular;
  118. // directional
  119. uniform highp vec3 light_direction;
  120. // omni
  121. uniform highp vec3 light_position;
  122. uniform highp float light_range;
  123. uniform highp float light_attenuation;
  124. // spot
  125. uniform highp float light_spot_attenuation;
  126. uniform highp float light_spot_range;
  127. uniform highp float light_spot_angle;
  128. void light_compute(
  129. vec3 N,
  130. vec3 L,
  131. vec3 V,
  132. vec3 light_color,
  133. vec3 attenuation,
  134. float roughness) {
  135. //this makes lights behave closer to linear, but then addition of lights looks bad
  136. //better left disabled
  137. //#define SRGB_APPROX(m_var) m_var = pow(m_var,0.4545454545);
  138. /*
  139. #define SRGB_APPROX(m_var) {\
  140. float S1 = sqrt(m_var);\
  141. float S2 = sqrt(S1);\
  142. float S3 = sqrt(S2);\
  143. m_var = 0.662002687 * S1 + 0.684122060 * S2 - 0.323583601 * S3 - 0.0225411470 * m_var;\
  144. }
  145. */
  146. #define SRGB_APPROX(m_var)
  147. float NdotL = dot(N, L);
  148. float cNdotL = max(NdotL, 0.0); // clamped NdotL
  149. float NdotV = dot(N, V);
  150. float cNdotV = max(NdotV, 0.0);
  151. #if defined(DIFFUSE_OREN_NAYAR)
  152. vec3 diffuse_brdf_NL;
  153. #else
  154. float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance
  155. #endif
  156. #if defined(DIFFUSE_LAMBERT_WRAP)
  157. // energy conserving lambert wrap shader
  158. diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness)));
  159. #elif defined(DIFFUSE_OREN_NAYAR)
  160. {
  161. // see http://mimosa-pudica.net/improved-oren-nayar.html
  162. float LdotV = dot(L, V);
  163. float s = LdotV - NdotL * NdotV;
  164. float t = mix(1.0, max(NdotL, NdotV), step(0.0, s));
  165. float sigma2 = roughness * roughness; // TODO: this needs checking
  166. vec3 A = 1.0 + sigma2 * (-0.5 / (sigma2 + 0.33) + 0.17 * diffuse_color / (sigma2 + 0.13));
  167. float B = 0.45 * sigma2 / (sigma2 + 0.09);
  168. diffuse_brdf_NL = cNdotL * (A + vec3(B) * s / t) * (1.0 / M_PI);
  169. }
  170. #else
  171. // lambert by default for everything else
  172. diffuse_brdf_NL = cNdotL * (1.0 / M_PI);
  173. #endif
  174. SRGB_APPROX(diffuse_brdf_NL)
  175. diffuse_interp += light_color * diffuse_brdf_NL * attenuation;
  176. if (roughness > 0.0) {
  177. // D
  178. float specular_brdf_NL = 0.0;
  179. #if !defined(SPECULAR_DISABLED)
  180. //normalized blinn always unless disabled
  181. vec3 H = normalize(V + L);
  182. float cNdotH = max(dot(N, H), 0.0);
  183. float cVdotH = max(dot(V, H), 0.0);
  184. float cLdotH = max(dot(L, H), 0.0);
  185. float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25;
  186. float blinn = pow(cNdotH, shininess);
  187. blinn *= (shininess + 8.0) * (1.0 / (8.0 * M_PI));
  188. specular_brdf_NL = (blinn) / max(4.0 * cNdotV * cNdotL, 0.75);
  189. #endif
  190. SRGB_APPROX(specular_brdf_NL)
  191. specular_interp += specular_brdf_NL * light_color * attenuation;
  192. }
  193. }
  194. #endif
  195. #ifdef USE_VERTEX_LIGHTING
  196. #ifdef USE_REFLECTION_PROBE1
  197. uniform highp mat4 refprobe1_local_matrix;
  198. varying mediump vec4 refprobe1_reflection_normal_blend;
  199. uniform highp vec3 refprobe1_box_extents;
  200. #ifndef USE_LIGHTMAP
  201. varying mediump vec3 refprobe1_ambient_normal;
  202. #endif
  203. #endif //reflection probe1
  204. #ifdef USE_REFLECTION_PROBE2
  205. uniform highp mat4 refprobe2_local_matrix;
  206. varying mediump vec4 refprobe2_reflection_normal_blend;
  207. uniform highp vec3 refprobe2_box_extents;
  208. #ifndef USE_LIGHTMAP
  209. varying mediump vec3 refprobe2_ambient_normal;
  210. #endif
  211. #endif //reflection probe2
  212. #endif //vertex lighting for refprobes
  213. #if defined(FOG_DEPTH_ENABLED) || defined(FOG_HEIGHT_ENABLED)
  214. varying vec4 fog_interp;
  215. uniform mediump vec4 fog_color_base;
  216. #ifdef LIGHT_MODE_DIRECTIONAL
  217. uniform mediump vec4 fog_sun_color_amount;
  218. #endif
  219. uniform bool fog_transmit_enabled;
  220. uniform mediump float fog_transmit_curve;
  221. #ifdef FOG_DEPTH_ENABLED
  222. uniform highp float fog_depth_begin;
  223. uniform mediump float fog_depth_curve;
  224. uniform mediump float fog_max_distance;
  225. #endif
  226. #ifdef FOG_HEIGHT_ENABLED
  227. uniform highp float fog_height_min;
  228. uniform highp float fog_height_max;
  229. uniform mediump float fog_height_curve;
  230. #endif
  231. #endif //fog
  232. void main() {
  233. highp vec4 vertex = vertex_attrib;
  234. mat4 world_matrix = world_transform;
  235. #ifdef USE_INSTANCING
  236. {
  237. highp mat4 m = mat4(
  238. instance_xform_row_0,
  239. instance_xform_row_1,
  240. instance_xform_row_2,
  241. vec4(0.0, 0.0, 0.0, 1.0));
  242. world_matrix = world_matrix * transpose(m);
  243. }
  244. #endif
  245. vec3 normal = normal_attrib;
  246. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP)
  247. vec3 tangent = tangent_attrib.xyz;
  248. float binormalf = tangent_attrib.a;
  249. vec3 binormal = normalize(cross(normal, tangent) * binormalf);
  250. #endif
  251. #if defined(ENABLE_COLOR_INTERP)
  252. color_interp = color_attrib;
  253. #ifdef USE_INSTANCING
  254. color_interp *= instance_color;
  255. #endif
  256. #endif
  257. #if defined(ENABLE_UV_INTERP)
  258. uv_interp = uv_attrib;
  259. #endif
  260. #if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP)
  261. uv2_interp = uv2_attrib;
  262. #endif
  263. #if defined(OVERRIDE_POSITION)
  264. highp vec4 position;
  265. #endif
  266. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  267. vertex = world_matrix * vertex;
  268. normal = normalize((world_matrix * vec4(normal, 0.0)).xyz);
  269. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP)
  270. tangent = normalize((world_matrix * vec4(tangent, 0.0)).xyz);
  271. binormal = normalize((world_matrix * vec4(binormal, 0.0)).xyz);
  272. #endif
  273. #endif
  274. #ifdef USE_SKELETON
  275. highp mat4 bone_transform = mat4(0.0);
  276. #ifdef USE_SKELETON_SOFTWARE
  277. // passing the transform as attributes
  278. bone_transform[0] = vec4(bone_transform_row_0.x, bone_transform_row_1.x, bone_transform_row_2.x, 0.0);
  279. bone_transform[1] = vec4(bone_transform_row_0.y, bone_transform_row_1.y, bone_transform_row_2.y, 0.0);
  280. bone_transform[2] = vec4(bone_transform_row_0.z, bone_transform_row_1.z, bone_transform_row_2.z, 0.0);
  281. bone_transform[3] = vec4(bone_transform_row_0.w, bone_transform_row_1.w, bone_transform_row_2.w, 1.0);
  282. #else
  283. // look up transform from the "pose texture"
  284. {
  285. for (int i = 0; i < 4; i++) {
  286. ivec2 tex_ofs = ivec2(int(bone_ids[i]) * 3, 0);
  287. highp mat4 b = mat4(
  288. texel2DFetch(bone_transforms, skeleton_texture_size, tex_ofs + ivec2(0, 0)),
  289. texel2DFetch(bone_transforms, skeleton_texture_size, tex_ofs + ivec2(1, 0)),
  290. texel2DFetch(bone_transforms, skeleton_texture_size, tex_ofs + ivec2(2, 0)),
  291. vec4(0.0, 0.0, 0.0, 1.0));
  292. bone_transform += transpose(b) * bone_weights[i];
  293. }
  294. }
  295. #endif
  296. if (skeleton_in_world_coords) {
  297. bone_transform = skeleton_transform * (bone_transform * skeleton_transform_inverse);
  298. world_matrix = bone_transform * world_matrix;
  299. } else {
  300. world_matrix = world_matrix * bone_transform;
  301. }
  302. #endif
  303. #ifdef USE_INSTANCING
  304. vec4 instance_custom = instance_custom_data;
  305. #else
  306. vec4 instance_custom = vec4(0.0);
  307. #endif
  308. mat4 modelview = camera_inverse_matrix * world_matrix;
  309. float roughness = 1.0;
  310. #define world_transform world_matrix
  311. {
  312. /* clang-format off */
  313. VERTEX_SHADER_CODE
  314. /* clang-format on */
  315. }
  316. vec4 outvec = vertex;
  317. // use local coordinates
  318. #if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
  319. vertex = modelview * vertex;
  320. normal = normalize((modelview * vec4(normal, 0.0)).xyz);
  321. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP)
  322. tangent = normalize((modelview * vec4(tangent, 0.0)).xyz);
  323. binormal = normalize((modelview * vec4(binormal, 0.0)).xyz);
  324. #endif
  325. #endif
  326. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  327. vertex = camera_inverse_matrix * vertex;
  328. normal = normalize((camera_inverse_matrix * vec4(normal, 0.0)).xyz);
  329. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP)
  330. tangent = normalize((camera_inverse_matrix * vec4(tangent, 0.0)).xyz);
  331. binormal = normalize((camera_inverse_matrix * vec4(binormal, 0.0)).xyz);
  332. #endif
  333. #endif
  334. vertex_interp = vertex.xyz;
  335. normal_interp = normal;
  336. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP)
  337. tangent_interp = tangent;
  338. binormal_interp = binormal;
  339. #endif
  340. #ifdef RENDER_DEPTH
  341. #ifdef RENDER_DEPTH_DUAL_PARABOLOID
  342. vertex_interp.z *= shadow_dual_paraboloid_render_side;
  343. normal_interp.z *= shadow_dual_paraboloid_render_side;
  344. dp_clip = vertex_interp.z; //this attempts to avoid noise caused by objects sent to the other parabolloid side due to bias
  345. //for dual paraboloid shadow mapping, this is the fastest but least correct way, as it curves straight edges
  346. highp vec3 vtx = vertex_interp + normalize(vertex_interp) * light_bias;
  347. highp float distance = length(vtx);
  348. vtx = normalize(vtx);
  349. vtx.xy /= 1.0 - vtx.z;
  350. vtx.z = (distance / shadow_dual_paraboloid_render_zfar);
  351. vtx.z = vtx.z * 2.0 - 1.0;
  352. vertex_interp = vtx;
  353. #else
  354. float z_ofs = light_bias;
  355. z_ofs += (1.0 - abs(normal_interp.z)) * light_normal_bias;
  356. vertex_interp.z -= z_ofs;
  357. #endif //dual parabolloid
  358. #endif //depth
  359. //vertex lighting
  360. #if defined(USE_VERTEX_LIGHTING) && defined(USE_LIGHTING)
  361. //vertex shaded version of lighting (more limited)
  362. vec3 L;
  363. vec3 light_att;
  364. #ifdef LIGHT_MODE_OMNI
  365. vec3 light_vec = light_position - vertex_interp;
  366. float light_length = length(light_vec);
  367. float normalized_distance = light_length / light_range;
  368. if (normalized_distance < 1.0) {
  369. float omni_attenuation = pow(1.0 - normalized_distance, light_attenuation);
  370. vec3 attenuation = vec3(omni_attenuation);
  371. light_att = vec3(omni_attenuation);
  372. } else {
  373. light_att = vec3(0.0);
  374. }
  375. L = normalize(light_vec);
  376. #endif
  377. #ifdef LIGHT_MODE_SPOT
  378. vec3 light_rel_vec = light_position - vertex_interp;
  379. float light_length = length(light_rel_vec);
  380. float normalized_distance = light_length / light_range;
  381. if (normalized_distance < 1.0) {
  382. float spot_attenuation = pow(1.0 - normalized_distance, light_attenuation);
  383. vec3 spot_dir = light_direction;
  384. float spot_cutoff = light_spot_angle;
  385. float angle = dot(-normalize(light_rel_vec), spot_dir);
  386. if (angle > spot_cutoff) {
  387. float scos = max(angle, spot_cutoff);
  388. float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_cutoff));
  389. spot_attenuation *= 1.0 - pow(spot_rim, light_spot_attenuation);
  390. light_att = vec3(spot_attenuation);
  391. } else {
  392. light_att = vec3(0.0);
  393. }
  394. } else {
  395. light_att = vec3(0.0);
  396. }
  397. L = normalize(light_rel_vec);
  398. #endif
  399. #ifdef LIGHT_MODE_DIRECTIONAL
  400. vec3 light_vec = -light_direction;
  401. light_att = vec3(1.0); //no base attenuation
  402. L = normalize(light_vec);
  403. #endif
  404. diffuse_interp = vec3(0.0);
  405. specular_interp = vec3(0.0);
  406. light_compute(normal_interp, L, -normalize(vertex_interp), light_color.rgb, light_att, roughness);
  407. #endif
  408. //shadows (for both vertex and fragment)
  409. #if defined(USE_SHADOW) && defined(USE_LIGHTING)
  410. vec4 vi4 = vec4(vertex_interp, 1.0);
  411. shadow_coord = light_shadow_matrix * vi4;
  412. #if defined(LIGHT_USE_PSSM2) || defined(LIGHT_USE_PSSM4)
  413. shadow_coord2 = light_shadow_matrix2 * vi4;
  414. #endif
  415. #if defined(LIGHT_USE_PSSM4)
  416. shadow_coord3 = light_shadow_matrix3 * vi4;
  417. shadow_coord4 = light_shadow_matrix4 * vi4;
  418. #endif
  419. #endif //use shadow and use lighting
  420. #ifdef USE_VERTEX_LIGHTING
  421. #ifdef USE_REFLECTION_PROBE1
  422. {
  423. vec3 ref_normal = normalize(reflect(vertex_interp, normal_interp));
  424. vec3 local_pos = (refprobe1_local_matrix * vec4(vertex_interp, 1.0)).xyz;
  425. vec3 inner_pos = abs(local_pos / refprobe1_box_extents);
  426. float blend = max(inner_pos.x, max(inner_pos.y, inner_pos.z));
  427. {
  428. vec3 local_ref_vec = (refprobe1_local_matrix * vec4(ref_normal, 0.0)).xyz;
  429. refprobe1_reflection_normal_blend.xyz = local_ref_vec;
  430. refprobe1_reflection_normal_blend.a = blend;
  431. }
  432. #ifndef USE_LIGHTMAP
  433. refprobe1_ambient_normal = (refprobe1_local_matrix * vec4(normal_interp, 0.0)).xyz;
  434. #endif
  435. }
  436. #endif //USE_REFLECTION_PROBE1
  437. #ifdef USE_REFLECTION_PROBE2
  438. {
  439. vec3 ref_normal = normalize(reflect(vertex_interp, normal_interp));
  440. vec3 local_pos = (refprobe2_local_matrix * vec4(vertex_interp, 1.0)).xyz;
  441. vec3 inner_pos = abs(local_pos / refprobe2_box_extents);
  442. float blend = max(inner_pos.x, max(inner_pos.y, inner_pos.z));
  443. {
  444. vec3 local_ref_vec = (refprobe2_local_matrix * vec4(ref_normal, 0.0)).xyz;
  445. refprobe2_reflection_normal_blend.xyz = local_ref_vec;
  446. refprobe2_reflection_normal_blend.a = blend;
  447. }
  448. #ifndef USE_LIGHTMAP
  449. refprobe2_ambient_normal = (refprobe2_local_matrix * vec4(normal_interp, 0.0)).xyz;
  450. #endif
  451. }
  452. #endif //USE_REFLECTION_PROBE2
  453. #if defined(FOG_DEPTH_ENABLED) || defined(FOG_HEIGHT_ENABLED)
  454. float fog_amount = 0.0;
  455. #ifdef LIGHT_MODE_DIRECTIONAL
  456. vec3 fog_color = mix(fog_color_base.rgb, fog_sun_color_amount.rgb, fog_sun_color_amount.a * pow(max(dot(normalize(vertex_interp), light_direction), 0.0), 8.0));
  457. #else
  458. vec3 fog_color = fog_color_base.rgb;
  459. #endif
  460. #ifdef FOG_DEPTH_ENABLED
  461. {
  462. float fog_z = smoothstep(fog_depth_begin, fog_max_distance, length(vertex));
  463. fog_amount = pow(fog_z, fog_depth_curve) * fog_color_base.a;
  464. }
  465. #endif
  466. #ifdef FOG_HEIGHT_ENABLED
  467. {
  468. float y = (camera_matrix * vec4(vertex_interp, 1.0)).y;
  469. fog_amount = max(fog_amount, pow(smoothstep(fog_height_min, fog_height_max, y), fog_height_curve));
  470. }
  471. #endif
  472. fog_interp = vec4(fog_color, fog_amount);
  473. #endif //fog
  474. #endif //use vertex lighting
  475. #if defined(OVERRIDE_POSITION)
  476. gl_Position = position;
  477. #else
  478. gl_Position = projection_matrix * vec4(vertex_interp, 1.0);
  479. #endif
  480. #if defined(RENDER_DEPTH) && defined(USE_RGBA_SHADOWS)
  481. position_interp = gl_Position;
  482. #endif
  483. }
  484. /* clang-format off */
  485. [fragment]
  486. #ifndef USE_GLES_OVER_GL
  487. #ifdef GL_EXT_shader_texture_lod
  488. #extension GL_EXT_shader_texture_lod : enable
  489. #define texture2DLod(img, coord, lod) texture2DLodEXT(img, coord, lod)
  490. #define textureCubeLod(img, coord, lod) textureCubeLodEXT(img, coord, lod)
  491. #endif
  492. #endif
  493. #ifdef GL_ARB_shader_texture_lod
  494. #extension GL_ARB_shader_texture_lod : enable
  495. #endif
  496. #if !defined(GL_EXT_shader_texture_lod) && !defined(GL_ARB_shader_texture_lod)
  497. #define texture2DLod(img, coord, lod) texture2D(img, coord, lod)
  498. #define textureCubeLod(img, coord, lod) textureCube(img, coord, lod)
  499. #endif
  500. #ifdef USE_GLES_OVER_GL
  501. #define lowp
  502. #define mediump
  503. #define highp
  504. #else
  505. #if defined(USE_HIGHP_PRECISION)
  506. precision highp float;
  507. precision highp int;
  508. #else
  509. precision mediump float;
  510. precision mediump int;
  511. #endif
  512. #endif
  513. #include "stdlib.glsl"
  514. #define M_PI 3.14159265359
  515. #define SHADER_IS_SRGB true
  516. //
  517. // uniforms
  518. //
  519. uniform highp mat4 camera_matrix;
  520. /* clang-format on */
  521. uniform highp mat4 camera_inverse_matrix;
  522. uniform highp mat4 projection_matrix;
  523. uniform highp mat4 projection_inverse_matrix;
  524. uniform highp mat4 world_transform;
  525. uniform highp float time;
  526. uniform highp vec2 viewport_size;
  527. #if defined(SCREEN_UV_USED)
  528. uniform vec2 screen_pixel_size;
  529. #endif
  530. // I think supporting this in GLES2 is difficult
  531. // uniform highp sampler2D depth_buffer;
  532. #if defined(SCREEN_TEXTURE_USED)
  533. uniform highp sampler2D screen_texture; //texunit:-4
  534. #endif
  535. #if defined(DEPTH_TEXTURE_USED)
  536. uniform highp sampler2D depth_texture; //texunit:-4
  537. #endif
  538. #ifdef USE_REFLECTION_PROBE1
  539. #ifdef USE_VERTEX_LIGHTING
  540. varying mediump vec4 refprobe1_reflection_normal_blend;
  541. #ifndef USE_LIGHTMAP
  542. varying mediump vec3 refprobe1_ambient_normal;
  543. #endif
  544. #else
  545. uniform bool refprobe1_use_box_project;
  546. uniform highp vec3 refprobe1_box_extents;
  547. uniform vec3 refprobe1_box_offset;
  548. uniform highp mat4 refprobe1_local_matrix;
  549. #endif //use vertex lighting
  550. uniform bool refprobe1_exterior;
  551. uniform highp samplerCube reflection_probe1; //texunit:-5
  552. uniform float refprobe1_intensity;
  553. uniform vec4 refprobe1_ambient;
  554. #endif //USE_REFLECTION_PROBE1
  555. #ifdef USE_REFLECTION_PROBE2
  556. #ifdef USE_VERTEX_LIGHTING
  557. varying mediump vec4 refprobe2_reflection_normal_blend;
  558. #ifndef USE_LIGHTMAP
  559. varying mediump vec3 refprobe2_ambient_normal;
  560. #endif
  561. #else
  562. uniform bool refprobe2_use_box_project;
  563. uniform highp vec3 refprobe2_box_extents;
  564. uniform vec3 refprobe2_box_offset;
  565. uniform highp mat4 refprobe2_local_matrix;
  566. #endif //use vertex lighting
  567. uniform bool refprobe2_exterior;
  568. uniform highp samplerCube reflection_probe2; //texunit:-6
  569. uniform float refprobe2_intensity;
  570. uniform vec4 refprobe2_ambient;
  571. #endif //USE_REFLECTION_PROBE2
  572. #define RADIANCE_MAX_LOD 6.0
  573. #if defined(USE_REFLECTION_PROBE1) || defined(USE_REFLECTION_PROBE2)
  574. void reflection_process(samplerCube reflection_map,
  575. #ifdef USE_VERTEX_LIGHTING
  576. vec3 ref_normal,
  577. #ifndef USE_LIGHTMAP
  578. vec3 amb_normal,
  579. #endif
  580. float ref_blend,
  581. #else //no vertex lighting
  582. vec3 normal, vec3 vertex,
  583. mat4 local_matrix,
  584. bool use_box_project, vec3 box_extents, vec3 box_offset,
  585. #endif //vertex lighting
  586. bool exterior, float intensity, vec4 ref_ambient, float roughness, vec3 ambient, vec3 skybox, inout highp vec4 reflection_accum, inout highp vec4 ambient_accum) {
  587. vec4 reflection;
  588. #ifdef USE_VERTEX_LIGHTING
  589. reflection.rgb = textureCubeLod(reflection_map, ref_normal, roughness * RADIANCE_MAX_LOD).rgb;
  590. float blend = ref_blend; //crappier blend formula for vertex
  591. blend *= blend;
  592. blend = max(0.0, 1.0 - blend);
  593. #else //fragment lighting
  594. vec3 local_pos = (local_matrix * vec4(vertex, 1.0)).xyz;
  595. if (any(greaterThan(abs(local_pos), box_extents))) { //out of the reflection box
  596. return;
  597. }
  598. vec3 inner_pos = abs(local_pos / box_extents);
  599. float blend = max(inner_pos.x, max(inner_pos.y, inner_pos.z));
  600. blend = mix(length(inner_pos), blend, blend);
  601. blend *= blend;
  602. blend = max(0.0, 1.0 - blend);
  603. //reflect and make local
  604. vec3 ref_normal = normalize(reflect(vertex, normal));
  605. ref_normal = (local_matrix * vec4(ref_normal, 0.0)).xyz;
  606. if (use_box_project) { //box project
  607. vec3 nrdir = normalize(ref_normal);
  608. vec3 rbmax = (box_extents - local_pos) / nrdir;
  609. vec3 rbmin = (-box_extents - local_pos) / nrdir;
  610. vec3 rbminmax = mix(rbmin, rbmax, vec3(greaterThan(nrdir, vec3(0.0, 0.0, 0.0))));
  611. float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z);
  612. vec3 posonbox = local_pos + nrdir * fa;
  613. ref_normal = posonbox - box_offset.xyz;
  614. }
  615. reflection.rgb = textureCubeLod(reflection_map, ref_normal, roughness * RADIANCE_MAX_LOD).rgb;
  616. #endif
  617. if (exterior) {
  618. reflection.rgb = mix(skybox, reflection.rgb, blend);
  619. }
  620. reflection.rgb *= intensity;
  621. reflection.a = blend;
  622. reflection.rgb *= blend;
  623. reflection_accum += reflection;
  624. #ifndef USE_LIGHTMAP
  625. vec4 ambient_out;
  626. #ifndef USE_VERTEX_LIGHTING
  627. vec3 amb_normal = (local_matrix * vec4(normal, 0.0)).xyz;
  628. #endif
  629. ambient_out.rgb = textureCubeLod(reflection_map, amb_normal, RADIANCE_MAX_LOD).rgb;
  630. ambient_out.rgb = mix(ref_ambient.rgb, ambient_out.rgb, ref_ambient.a);
  631. if (exterior) {
  632. ambient_out.rgb = mix(ambient, ambient_out.rgb, blend);
  633. }
  634. ambient_out.a = blend;
  635. ambient_out.rgb *= blend;
  636. ambient_accum += ambient_out;
  637. #endif
  638. }
  639. #endif //use refprobe 1 or 2
  640. #ifdef USE_LIGHTMAP
  641. uniform mediump sampler2D lightmap; //texunit:-4
  642. uniform mediump float lightmap_energy;
  643. #endif
  644. #ifdef USE_LIGHTMAP_CAPTURE
  645. uniform mediump vec4[12] lightmap_captures;
  646. uniform bool lightmap_capture_sky;
  647. #endif
  648. #ifdef USE_RADIANCE_MAP
  649. uniform samplerCube radiance_map; // texunit:-2
  650. uniform mat4 radiance_inverse_xform;
  651. #endif
  652. uniform float bg_energy;
  653. uniform float ambient_sky_contribution;
  654. uniform vec4 ambient_color;
  655. uniform float ambient_energy;
  656. #ifdef USE_LIGHTING
  657. uniform highp vec4 shadow_color;
  658. #ifdef USE_VERTEX_LIGHTING
  659. //get from vertex
  660. varying highp vec3 diffuse_interp;
  661. varying highp vec3 specular_interp;
  662. uniform highp vec3 light_direction; //may be used by fog, so leave here
  663. #else
  664. //done in fragment
  665. // general for all lights
  666. uniform highp vec4 light_color;
  667. uniform highp float light_specular;
  668. // directional
  669. uniform highp vec3 light_direction;
  670. // omni
  671. uniform highp vec3 light_position;
  672. uniform highp float light_attenuation;
  673. // spot
  674. uniform highp float light_spot_attenuation;
  675. uniform highp float light_spot_range;
  676. uniform highp float light_spot_angle;
  677. #endif
  678. //this is needed outside above if because dual paraboloid wants it
  679. uniform highp float light_range;
  680. #ifdef USE_SHADOW
  681. uniform highp vec2 shadow_pixel_size;
  682. #if defined(LIGHT_MODE_OMNI) || defined(LIGHT_MODE_SPOT)
  683. uniform highp sampler2D light_shadow_atlas; //texunit:-3
  684. #endif
  685. #ifdef LIGHT_MODE_DIRECTIONAL
  686. uniform highp sampler2D light_directional_shadow; // texunit:-3
  687. uniform highp vec4 light_split_offsets;
  688. #endif
  689. varying highp vec4 shadow_coord;
  690. #if defined(LIGHT_USE_PSSM2) || defined(LIGHT_USE_PSSM4)
  691. varying highp vec4 shadow_coord2;
  692. #endif
  693. #if defined(LIGHT_USE_PSSM4)
  694. varying highp vec4 shadow_coord3;
  695. varying highp vec4 shadow_coord4;
  696. #endif
  697. uniform vec4 light_clamp;
  698. #endif // light shadow
  699. // directional shadow
  700. #endif
  701. //
  702. // varyings
  703. //
  704. #if defined(RENDER_DEPTH) && defined(USE_RGBA_SHADOWS)
  705. varying highp vec4 position_interp;
  706. #endif
  707. varying highp vec3 vertex_interp;
  708. varying vec3 normal_interp;
  709. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP)
  710. varying vec3 tangent_interp;
  711. varying vec3 binormal_interp;
  712. #endif
  713. #if defined(ENABLE_COLOR_INTERP)
  714. varying vec4 color_interp;
  715. #endif
  716. #if defined(ENABLE_UV_INTERP)
  717. varying vec2 uv_interp;
  718. #endif
  719. #if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP)
  720. varying vec2 uv2_interp;
  721. #endif
  722. varying vec3 view_interp;
  723. vec3 F0(float metallic, float specular, vec3 albedo) {
  724. float dielectric = 0.16 * specular * specular;
  725. // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials;
  726. // see https://google.github.io/filament/Filament.md.html
  727. return mix(vec3(dielectric), albedo, vec3(metallic));
  728. }
  729. /* clang-format off */
  730. FRAGMENT_SHADER_GLOBALS
  731. /* clang-format on */
  732. #ifdef RENDER_DEPTH_DUAL_PARABOLOID
  733. varying highp float dp_clip;
  734. #endif
  735. #ifdef USE_LIGHTING
  736. // 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.
  737. // We're dividing this factor off because the overall term we'll end up looks like
  738. // (see, for example, the first unnumbered equation in B. Burley, "Physically Based Shading at Disney", SIGGRAPH 2012):
  739. //
  740. // F(L.V) D(N.H) G(N.L) G(N.V) / (4 N.L N.V)
  741. //
  742. // We're basically regouping this as
  743. //
  744. // F(L.V) D(N.H) [G(N.L)/(2 N.L)] [G(N.V) / (2 N.V)]
  745. //
  746. // and thus, this function implements the [G(N.m)/(2 N.m)] part with m = L or V.
  747. //
  748. // The contents of the D and G (G1) functions (GGX) are taken from
  749. // E. Heitz, "Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs", J. Comp. Graph. Tech. 3 (2) (2014).
  750. // Eqns 71-72 and 85-86 (see also Eqns 43 and 80).
  751. /*
  752. float G_GGX_2cos(float cos_theta_m, float alpha) {
  753. // Schlick's approximation
  754. // C. Schlick, "An Inexpensive BRDF Model for Physically-based Rendering", Computer Graphics Forum. 13 (3): 233 (1994)
  755. // Eq. (19), although see Heitz (2014) the about the problems with his derivation.
  756. // It nevertheless approximates GGX well with k = alpha/2.
  757. float k = 0.5 * alpha;
  758. return 0.5 / (cos_theta_m * (1.0 - k) + k);
  759. // float cos2 = cos_theta_m * cos_theta_m;
  760. // float sin2 = (1.0 - cos2);
  761. // return 1.0 / (cos_theta_m + sqrt(cos2 + alpha * alpha * sin2));
  762. }
  763. */
  764. // This approximates G_GGX_2cos(cos_theta_l, alpha) * G_GGX_2cos(cos_theta_v, alpha)
  765. // See Filament docs, Specular G section.
  766. float V_GGX(float cos_theta_l, float cos_theta_v, float alpha) {
  767. return 0.5 / mix(2.0 * cos_theta_l * cos_theta_v, cos_theta_l + cos_theta_v, alpha);
  768. }
  769. float D_GGX(float cos_theta_m, float alpha) {
  770. float alpha2 = alpha * alpha;
  771. float d = 1.0 + (alpha2 - 1.0) * cos_theta_m * cos_theta_m;
  772. return alpha2 / (M_PI * d * d);
  773. }
  774. /*
  775. float G_GGX_anisotropic_2cos(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) {
  776. float cos2 = cos_theta_m * cos_theta_m;
  777. float sin2 = (1.0 - cos2);
  778. float s_x = alpha_x * cos_phi;
  779. float s_y = alpha_y * sin_phi;
  780. return 1.0 / max(cos_theta_m + sqrt(cos2 + (s_x * s_x + s_y * s_y) * sin2), 0.001);
  781. }
  782. */
  783. // This approximates G_GGX_anisotropic_2cos(cos_theta_l, ...) * G_GGX_anisotropic_2cos(cos_theta_v, ...)
  784. // See Filament docs, Anisotropic specular BRDF section.
  785. float V_GGX_anisotropic(float alpha_x, float alpha_y, float TdotV, float TdotL, float BdotV, float BdotL, float NdotV, float NdotL) {
  786. float Lambda_V = NdotL * length(vec3(alpha_x * TdotV, alpha_y * BdotV, NdotV));
  787. float Lambda_L = NdotV * length(vec3(alpha_x * TdotL, alpha_y * BdotL, NdotL));
  788. return 0.5 / (Lambda_V + Lambda_L);
  789. }
  790. float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi, float NdotH) {
  791. float alpha2 = alpha_x * alpha_y;
  792. highp vec3 v = vec3(alpha_y * cos_phi, alpha_x * sin_phi, alpha2 * NdotH);
  793. highp float v2 = dot(v, v);
  794. float w2 = alpha2 / v2;
  795. float D = alpha2 * w2 * w2 * (1.0 / M_PI);
  796. return D;
  797. /* float cos2 = cos_theta_m * cos_theta_m;
  798. float sin2 = (1.0 - cos2);
  799. float r_x = cos_phi / alpha_x;
  800. float r_y = sin_phi / alpha_y;
  801. float d = cos2 + sin2 * (r_x * r_x + r_y * r_y);
  802. return 1.0 / max(M_PI * alpha_x * alpha_y * d * d, 0.001); */
  803. }
  804. float SchlickFresnel(float u) {
  805. float m = 1.0 - u;
  806. float m2 = m * m;
  807. return m2 * m2 * m; // pow(m,5)
  808. }
  809. float GTR1(float NdotH, float a) {
  810. if (a >= 1.0) return 1.0 / M_PI;
  811. float a2 = a * a;
  812. float t = 1.0 + (a2 - 1.0) * NdotH * NdotH;
  813. return (a2 - 1.0) / (M_PI * log(a2) * t);
  814. }
  815. void light_compute(
  816. vec3 N,
  817. vec3 L,
  818. vec3 V,
  819. vec3 B,
  820. vec3 T,
  821. vec3 light_color,
  822. vec3 attenuation,
  823. vec3 diffuse_color,
  824. vec3 transmission,
  825. float specular_blob_intensity,
  826. float roughness,
  827. float metallic,
  828. float specular,
  829. float rim,
  830. float rim_tint,
  831. float clearcoat,
  832. float clearcoat_gloss,
  833. float anisotropy,
  834. inout vec3 diffuse_light,
  835. inout vec3 specular_light) {
  836. //this makes lights behave closer to linear, but then addition of lights looks bad
  837. //better left disabled
  838. //#define SRGB_APPROX(m_var) m_var = pow(m_var,0.4545454545);
  839. /*
  840. #define SRGB_APPROX(m_var) {\
  841. float S1 = sqrt(m_var);\
  842. float S2 = sqrt(S1);\
  843. float S3 = sqrt(S2);\
  844. m_var = 0.662002687 * S1 + 0.684122060 * S2 - 0.323583601 * S3 - 0.0225411470 * m_var;\
  845. }
  846. */
  847. #define SRGB_APPROX(m_var)
  848. #if defined(USE_LIGHT_SHADER_CODE)
  849. // light is written by the light shader
  850. vec3 normal = N;
  851. vec3 albedo = diffuse_color;
  852. vec3 light = L;
  853. vec3 view = V;
  854. /* clang-format off */
  855. LIGHT_SHADER_CODE
  856. /* clang-format on */
  857. #else
  858. float NdotL = dot(N, L);
  859. float cNdotL = max(NdotL, 0.0); // clamped NdotL
  860. float NdotV = dot(N, V);
  861. float cNdotV = max(abs(NdotV), 1e-6);
  862. #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_BLINN) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_USE_CLEARCOAT)
  863. vec3 H = normalize(V + L);
  864. #endif
  865. #if defined(SPECULAR_BLINN) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_USE_CLEARCOAT)
  866. float cNdotH = max(dot(N, H), 0.0);
  867. #endif
  868. #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_USE_CLEARCOAT)
  869. float cLdotH = max(dot(L, H), 0.0);
  870. #endif
  871. if (metallic < 1.0) {
  872. #if defined(DIFFUSE_OREN_NAYAR)
  873. vec3 diffuse_brdf_NL;
  874. #else
  875. float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance
  876. #endif
  877. #if defined(DIFFUSE_LAMBERT_WRAP)
  878. // energy conserving lambert wrap shader
  879. diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness)));
  880. #elif defined(DIFFUSE_OREN_NAYAR)
  881. {
  882. // see http://mimosa-pudica.net/improved-oren-nayar.html
  883. float LdotV = dot(L, V);
  884. float s = LdotV - NdotL * NdotV;
  885. float t = mix(1.0, max(NdotL, NdotV), step(0.0, s));
  886. float sigma2 = roughness * roughness; // TODO: this needs checking
  887. vec3 A = 1.0 + sigma2 * (-0.5 / (sigma2 + 0.33) + 0.17 * diffuse_color / (sigma2 + 0.13));
  888. float B = 0.45 * sigma2 / (sigma2 + 0.09);
  889. diffuse_brdf_NL = cNdotL * (A + vec3(B) * s / t) * (1.0 / M_PI);
  890. }
  891. #elif defined(DIFFUSE_TOON)
  892. diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL);
  893. #elif defined(DIFFUSE_BURLEY)
  894. {
  895. float FD90_minus_1 = 2.0 * cLdotH * cLdotH * roughness - 0.5;
  896. float FdV = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotV);
  897. float FdL = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotL);
  898. diffuse_brdf_NL = (1.0 / M_PI) * FdV * FdL * cNdotL;
  899. /*
  900. float energyBias = mix(roughness, 0.0, 0.5);
  901. float energyFactor = mix(roughness, 1.0, 1.0 / 1.51);
  902. float fd90 = energyBias + 2.0 * VoH * VoH * roughness;
  903. float f0 = 1.0;
  904. float lightScatter = f0 + (fd90 - f0) * pow(1.0 - cNdotL, 5.0);
  905. float viewScatter = f0 + (fd90 - f0) * pow(1.0 - cNdotV, 5.0);
  906. diffuse_brdf_NL = lightScatter * viewScatter * energyFactor;
  907. */
  908. }
  909. #else
  910. // lambert
  911. diffuse_brdf_NL = cNdotL * (1.0 / M_PI);
  912. #endif
  913. SRGB_APPROX(diffuse_brdf_NL)
  914. diffuse_light += light_color * diffuse_color * diffuse_brdf_NL * attenuation;
  915. #if defined(TRANSMISSION_USED)
  916. diffuse_light += light_color * diffuse_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * transmission * attenuation;
  917. #endif
  918. #if defined(LIGHT_USE_RIM)
  919. float rim_light = pow(max(0.0, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0));
  920. diffuse_light += rim_light * rim * mix(vec3(1.0), diffuse_color, rim_tint) * light_color;
  921. #endif
  922. }
  923. if (roughness > 0.0) {
  924. #if defined(SPECULAR_SCHLICK_GGX)
  925. vec3 specular_brdf_NL = vec3(0.0);
  926. #else
  927. float specular_brdf_NL = 0.0;
  928. #endif
  929. #if defined(SPECULAR_BLINN)
  930. //normalized blinn
  931. float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25;
  932. float blinn = pow(cNdotH, shininess);
  933. blinn *= (shininess + 8.0) * (1.0 / (8.0 * M_PI));
  934. specular_brdf_NL = (blinn) / max(4.0 * cNdotV * cNdotL, 0.75);
  935. #elif defined(SPECULAR_PHONG)
  936. vec3 R = normalize(-reflect(L, N));
  937. float cRdotV = max(0.0, dot(R, V));
  938. float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25;
  939. float phong = pow(cRdotV, shininess);
  940. phong *= (shininess + 8.0) * (1.0 / (8.0 * M_PI));
  941. specular_brdf_NL = (phong) / max(4.0 * cNdotV * cNdotL, 0.75);
  942. #elif defined(SPECULAR_TOON)
  943. vec3 R = normalize(-reflect(L, N));
  944. float RdotV = dot(R, V);
  945. float mid = 1.0 - roughness;
  946. mid *= mid;
  947. specular_brdf_NL = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid;
  948. #elif defined(SPECULAR_DISABLED)
  949. // none..
  950. #elif defined(SPECULAR_SCHLICK_GGX)
  951. // shlick+ggx as default
  952. #if defined(LIGHT_USE_ANISOTROPY)
  953. float alpha = roughness * roughness;
  954. float aspect = sqrt(1.0 - anisotropy * 0.9);
  955. float ax = alpha / aspect;
  956. float ay = alpha * aspect;
  957. float XdotH = dot(T, H);
  958. float YdotH = dot(B, H);
  959. float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH, cNdotH);
  960. //float G = G_GGX_anisotropic_2cos(cNdotL, ax, ay, XdotH, YdotH) * G_GGX_anisotropic_2cos(cNdotV, ax, ay, XdotH, YdotH);
  961. float G = V_GGX_anisotropic(ax, ay, dot(T, V), dot(T, L), dot(B, V), dot(B, L), cNdotV, cNdotL);
  962. #else
  963. float alpha = roughness * roughness;
  964. float D = D_GGX(cNdotH, alpha);
  965. //float G = G_GGX_2cos(cNdotL, alpha) * G_GGX_2cos(cNdotV, alpha);
  966. float G = V_GGX(cNdotL, cNdotV, alpha);
  967. #endif
  968. // F
  969. vec3 f0 = F0(metallic, specular, diffuse_color);
  970. float cLdotH5 = SchlickFresnel(cLdotH);
  971. vec3 F = mix(vec3(cLdotH5), vec3(1.0), f0);
  972. specular_brdf_NL = cNdotL * D * F * G;
  973. #endif
  974. SRGB_APPROX(specular_brdf_NL)
  975. specular_light += specular_brdf_NL * light_color * specular_blob_intensity * attenuation;
  976. #if defined(LIGHT_USE_CLEARCOAT)
  977. #if !defined(SPECULAR_SCHLICK_GGX)
  978. float cLdotH5 = SchlickFresnel(cLdotH);
  979. #endif
  980. float Dr = GTR1(cNdotH, mix(.1, .001, clearcoat_gloss));
  981. float Fr = mix(.04, 1.0, cLdotH5);
  982. //float Gr = G_GGX_2cos(cNdotL, .25) * G_GGX_2cos(cNdotV, .25);
  983. float Gr = V_GGX(cNdotL, cNdotV, 0.25);
  984. float clearcoat_specular_brdf_NL = 0.25 * clearcoat * Gr * Fr * Dr * cNdotL;
  985. specular_light += clearcoat_specular_brdf_NL * light_color * specular_blob_intensity * attenuation;
  986. #endif
  987. }
  988. #endif //defined(USE_LIGHT_SHADER_CODE)
  989. }
  990. #endif
  991. // shadows
  992. #ifdef USE_SHADOW
  993. #ifdef USE_RGBA_SHADOWS
  994. #define SHADOW_DEPTH(m_val) dot(m_val, vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0))
  995. #else
  996. #define SHADOW_DEPTH(m_val) (m_val).r
  997. #endif
  998. #define SAMPLE_SHADOW_TEXEL(p_shadow, p_pos, p_depth) step(p_depth, SHADOW_DEPTH(texture2D(p_shadow, p_pos)))
  999. #define SAMPLE_SHADOW_TEXEL_PROJ(p_shadow, p_pos) step(p_pos.z, SHADOW_DEPTH(texture2DProj(p_shadow, p_pos)))
  1000. float sample_shadow(highp sampler2D shadow, highp vec4 spos) {
  1001. #ifdef SHADOW_MODE_PCF_13
  1002. spos.xyz /= spos.w;
  1003. vec2 pos = spos.xy;
  1004. float depth = spos.z;
  1005. float avg = SAMPLE_SHADOW_TEXEL(shadow, pos, depth);
  1006. avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(shadow_pixel_size.x, 0.0), depth);
  1007. avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(-shadow_pixel_size.x, 0.0), depth);
  1008. avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, shadow_pixel_size.y), depth);
  1009. avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, -shadow_pixel_size.y), depth);
  1010. avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(shadow_pixel_size.x, shadow_pixel_size.y), depth);
  1011. avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(-shadow_pixel_size.x, shadow_pixel_size.y), depth);
  1012. avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(shadow_pixel_size.x, -shadow_pixel_size.y), depth);
  1013. avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(-shadow_pixel_size.x, -shadow_pixel_size.y), depth);
  1014. avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(shadow_pixel_size.x * 2.0, 0.0), depth);
  1015. avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(-shadow_pixel_size.x * 2.0, 0.0), depth);
  1016. avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, shadow_pixel_size.y * 2.0), depth);
  1017. avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, -shadow_pixel_size.y * 2.0), depth);
  1018. return avg * (1.0 / 13.0);
  1019. #endif
  1020. #ifdef SHADOW_MODE_PCF_5
  1021. spos.xyz /= spos.w;
  1022. vec2 pos = spos.xy;
  1023. float depth = spos.z;
  1024. float avg = SAMPLE_SHADOW_TEXEL(shadow, pos, depth);
  1025. avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(shadow_pixel_size.x, 0.0), depth);
  1026. avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(-shadow_pixel_size.x, 0.0), depth);
  1027. avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, shadow_pixel_size.y), depth);
  1028. avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, -shadow_pixel_size.y), depth);
  1029. return avg * (1.0 / 5.0);
  1030. #endif
  1031. #if !defined(SHADOW_MODE_PCF_5) || !defined(SHADOW_MODE_PCF_13)
  1032. return SAMPLE_SHADOW_TEXEL_PROJ(shadow, spos);
  1033. #endif
  1034. }
  1035. #endif
  1036. #if defined(FOG_DEPTH_ENABLED) || defined(FOG_HEIGHT_ENABLED)
  1037. #if defined(USE_VERTEX_LIGHTING)
  1038. varying vec4 fog_interp;
  1039. #else
  1040. uniform mediump vec4 fog_color_base;
  1041. #ifdef LIGHT_MODE_DIRECTIONAL
  1042. uniform mediump vec4 fog_sun_color_amount;
  1043. #endif
  1044. uniform bool fog_transmit_enabled;
  1045. uniform mediump float fog_transmit_curve;
  1046. #ifdef FOG_DEPTH_ENABLED
  1047. uniform highp float fog_depth_begin;
  1048. uniform mediump float fog_depth_curve;
  1049. uniform mediump float fog_max_distance;
  1050. #endif
  1051. #ifdef FOG_HEIGHT_ENABLED
  1052. uniform highp float fog_height_min;
  1053. uniform highp float fog_height_max;
  1054. uniform mediump float fog_height_curve;
  1055. #endif
  1056. #endif //vertex lit
  1057. #endif //fog
  1058. void main() {
  1059. #ifdef RENDER_DEPTH_DUAL_PARABOLOID
  1060. if (dp_clip > 0.0)
  1061. discard;
  1062. #endif
  1063. highp vec3 vertex = vertex_interp;
  1064. vec3 view = -normalize(vertex_interp);
  1065. vec3 albedo = vec3(1.0);
  1066. vec3 transmission = vec3(0.0);
  1067. float metallic = 0.0;
  1068. float specular = 0.5;
  1069. vec3 emission = vec3(0.0);
  1070. float roughness = 1.0;
  1071. float rim = 0.0;
  1072. float rim_tint = 0.0;
  1073. float clearcoat = 0.0;
  1074. float clearcoat_gloss = 0.0;
  1075. float anisotropy = 0.0;
  1076. vec2 anisotropy_flow = vec2(1.0, 0.0);
  1077. float sss_strength = 0.0; //unused
  1078. float alpha = 1.0;
  1079. float side = 1.0;
  1080. float specular_blob_intensity = 1.0;
  1081. #if defined(SPECULAR_TOON)
  1082. specular_blob_intensity *= specular * 2.0;
  1083. #endif
  1084. #if defined(ENABLE_AO)
  1085. float ao = 1.0;
  1086. float ao_light_affect = 0.0;
  1087. #endif
  1088. #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP)
  1089. vec3 binormal = normalize(binormal_interp) * side;
  1090. vec3 tangent = normalize(tangent_interp) * side;
  1091. #else
  1092. vec3 binormal = vec3(0.0);
  1093. vec3 tangent = vec3(0.0);
  1094. #endif
  1095. vec3 normal = normalize(normal_interp) * side;
  1096. #if defined(ENABLE_NORMALMAP)
  1097. vec3 normalmap = vec3(0.5);
  1098. #endif
  1099. float normaldepth = 1.0;
  1100. #if defined(ALPHA_SCISSOR_USED)
  1101. float alpha_scissor = 0.5;
  1102. #endif
  1103. #if defined(SCREEN_UV_USED)
  1104. vec2 screen_uv = gl_FragCoord.xy * screen_pixel_size;
  1105. #endif
  1106. {
  1107. /* clang-format off */
  1108. FRAGMENT_SHADER_CODE
  1109. /* clang-format on */
  1110. }
  1111. #if defined(ENABLE_NORMALMAP)
  1112. normalmap.xy = normalmap.xy * 2.0 - 1.0;
  1113. normalmap.z = sqrt(max(0.0, 1.0 - dot(normalmap.xy, normalmap.xy)));
  1114. normal = normalize(mix(normal_interp, tangent * normalmap.x + binormal * normalmap.y + normal * normalmap.z, normaldepth)) * side;
  1115. //normal = normalmap;
  1116. #endif
  1117. normal = normalize(normal);
  1118. vec3 N = normal;
  1119. vec3 specular_light = vec3(0.0, 0.0, 0.0);
  1120. vec3 diffuse_light = vec3(0.0, 0.0, 0.0);
  1121. vec3 ambient_light = vec3(0.0, 0.0, 0.0);
  1122. vec3 eye_position = view;
  1123. #if defined(ALPHA_SCISSOR_USED)
  1124. if (alpha < alpha_scissor) {
  1125. discard;
  1126. }
  1127. #endif
  1128. #ifdef USE_DEPTH_PREPASS
  1129. if (alpha < 0.99) {
  1130. discard;
  1131. }
  1132. #endif
  1133. #ifdef BASE_PASS
  1134. //none
  1135. #ifdef USE_RADIANCE_MAP
  1136. vec3 ref_vec = reflect(-eye_position, N);
  1137. ref_vec = normalize((radiance_inverse_xform * vec4(ref_vec, 0.0)).xyz);
  1138. ref_vec.z *= -1.0;
  1139. specular_light = textureCubeLod(radiance_map, ref_vec, roughness * RADIANCE_MAX_LOD).xyz * bg_energy;
  1140. {
  1141. vec3 ambient_dir = normalize((radiance_inverse_xform * vec4(normal, 0.0)).xyz);
  1142. vec3 env_ambient = textureCubeLod(radiance_map, ambient_dir, RADIANCE_MAX_LOD).xyz * bg_energy;
  1143. ambient_light = mix(ambient_color.rgb, env_ambient, ambient_sky_contribution);
  1144. }
  1145. #else
  1146. ambient_light = ambient_color.rgb;
  1147. #endif
  1148. ambient_light *= ambient_energy;
  1149. #if defined(USE_REFLECTION_PROBE1) || defined(USE_REFLECTION_PROBE2)
  1150. vec4 ambient_accum = vec4(0.0);
  1151. vec4 reflection_accum = vec4(0.0);
  1152. #ifdef USE_REFLECTION_PROBE1
  1153. reflection_process(reflection_probe1,
  1154. #ifdef USE_VERTEX_LIGHTING
  1155. refprobe1_reflection_normal_blend.rgb,
  1156. #ifndef USE_LIGHTMAP
  1157. refprobe1_ambient_normal,
  1158. #endif
  1159. refprobe1_reflection_normal_blend.a,
  1160. #else
  1161. normal_interp, vertex_interp, refprobe1_local_matrix,
  1162. refprobe1_use_box_project, refprobe1_box_extents, refprobe1_box_offset,
  1163. #endif
  1164. refprobe1_exterior, refprobe1_intensity, refprobe1_ambient, roughness,
  1165. ambient_light, specular_light, reflection_accum, ambient_accum);
  1166. #endif // USE_REFLECTION_PROBE1
  1167. #ifdef USE_REFLECTION_PROBE2
  1168. reflection_process(reflection_probe2,
  1169. #ifdef USE_VERTEX_LIGHTING
  1170. refprobe2_reflection_normal_blend.rgb,
  1171. #ifndef USE_LIGHTMAP
  1172. refprobe2_ambient_normal,
  1173. #endif
  1174. refprobe2_reflection_normal_blend.a,
  1175. #else
  1176. normal_interp, vertex_interp, refprobe2_local_matrix,
  1177. refprobe2_use_box_project, refprobe2_box_extents, refprobe2_box_offset,
  1178. #endif
  1179. refprobe2_exterior, refprobe2_intensity, refprobe2_ambient, roughness,
  1180. ambient_light, specular_light, reflection_accum, ambient_accum);
  1181. #endif // USE_REFLECTION_PROBE2
  1182. if (reflection_accum.a > 0.0) {
  1183. specular_light = reflection_accum.rgb / reflection_accum.a;
  1184. }
  1185. #ifndef USE_LIGHTMAP
  1186. if (ambient_accum.a > 0.0) {
  1187. ambient_light = ambient_accum.rgb / ambient_accum.a;
  1188. }
  1189. #endif
  1190. #endif // defined(USE_REFLECTION_PROBE1) || defined(USE_REFLECTION_PROBE2)
  1191. #ifdef USE_LIGHTMAP
  1192. //ambient light will come entirely from lightmap is lightmap is used
  1193. ambient_light = texture2D(lightmap, uv2_interp).rgb * lightmap_energy;
  1194. #endif
  1195. #ifdef USE_LIGHTMAP_CAPTURE
  1196. {
  1197. vec3 cone_dirs[12] = vec3[](
  1198. vec3(0.0, 0.0, 1.0),
  1199. vec3(0.866025, 0.0, 0.5),
  1200. vec3(0.267617, 0.823639, 0.5),
  1201. vec3(-0.700629, 0.509037, 0.5),
  1202. vec3(-0.700629, -0.509037, 0.5),
  1203. vec3(0.267617, -0.823639, 0.5),
  1204. vec3(0.0, 0.0, -1.0),
  1205. vec3(0.866025, 0.0, -0.5),
  1206. vec3(0.267617, 0.823639, -0.5),
  1207. vec3(-0.700629, 0.509037, -0.5),
  1208. vec3(-0.700629, -0.509037, -0.5),
  1209. vec3(0.267617, -0.823639, -0.5));
  1210. vec3 local_normal = normalize(camera_matrix * vec4(normal, 0.0)).xyz;
  1211. vec4 captured = vec4(0.0);
  1212. float sum = 0.0;
  1213. for (int i = 0; i < 12; i++) {
  1214. float amount = max(0.0, dot(local_normal, cone_dirs[i])); //not correct, but creates a nice wrap around effect
  1215. captured += lightmap_captures[i] * amount;
  1216. sum += amount;
  1217. }
  1218. captured /= sum;
  1219. if (lightmap_capture_sky) {
  1220. ambient_light = mix(ambient_light, captured.rgb, captured.a);
  1221. } else {
  1222. ambient_light = captured.rgb;
  1223. }
  1224. }
  1225. #endif
  1226. #endif //BASE PASS
  1227. //
  1228. // Lighting
  1229. //
  1230. #ifdef USE_LIGHTING
  1231. #ifndef USE_VERTEX_LIGHTING
  1232. vec3 L;
  1233. #endif
  1234. vec3 light_att = vec3(1.0);
  1235. #ifdef LIGHT_MODE_OMNI
  1236. #ifndef USE_VERTEX_LIGHTING
  1237. vec3 light_vec = light_position - vertex;
  1238. float light_length = length(light_vec);
  1239. float normalized_distance = light_length / light_range;
  1240. if (normalized_distance < 1.0) {
  1241. float omni_attenuation = pow(1.0 - normalized_distance, light_attenuation);
  1242. light_att = vec3(omni_attenuation);
  1243. } else {
  1244. light_att = vec3(0.0);
  1245. }
  1246. L = normalize(light_vec);
  1247. #endif
  1248. #ifdef USE_SHADOW
  1249. {
  1250. highp vec4 splane = shadow_coord;
  1251. float shadow_len = length(splane.xyz);
  1252. splane.xyz = normalize(splane.xyz);
  1253. vec4 clamp_rect = light_clamp;
  1254. if (splane.z >= 0.0) {
  1255. splane.z += 1.0;
  1256. clamp_rect.y += clamp_rect.w;
  1257. } else {
  1258. splane.z = 1.0 - splane.z;
  1259. }
  1260. splane.xy /= splane.z;
  1261. splane.xy = splane.xy * 0.5 + 0.5;
  1262. splane.z = shadow_len / light_range;
  1263. splane.xy = clamp_rect.xy + splane.xy * clamp_rect.zw;
  1264. splane.w = 1.0;
  1265. float shadow = sample_shadow(light_shadow_atlas, splane);
  1266. light_att *= mix(shadow_color.rgb, vec3(1.0), shadow);
  1267. }
  1268. #endif
  1269. #endif //type omni
  1270. #ifdef LIGHT_MODE_DIRECTIONAL
  1271. #ifndef USE_VERTEX_LIGHTING
  1272. vec3 light_vec = -light_direction;
  1273. L = normalize(light_vec);
  1274. #endif
  1275. float depth_z = -vertex.z;
  1276. #ifdef USE_SHADOW
  1277. #ifdef USE_VERTEX_LIGHTING
  1278. //compute shadows in a mobile friendly way
  1279. #ifdef LIGHT_USE_PSSM4
  1280. //take advantage of prefetch
  1281. float shadow1 = sample_shadow(light_directional_shadow, shadow_coord);
  1282. float shadow2 = sample_shadow(light_directional_shadow, shadow_coord2);
  1283. float shadow3 = sample_shadow(light_directional_shadow, shadow_coord3);
  1284. float shadow4 = sample_shadow(light_directional_shadow, shadow_coord4);
  1285. if (depth_z < light_split_offsets.w) {
  1286. float pssm_fade = 0.0;
  1287. float shadow_att = 1.0;
  1288. #ifdef LIGHT_USE_PSSM_BLEND
  1289. float shadow_att2 = 1.0;
  1290. float pssm_blend = 0.0;
  1291. bool use_blend = true;
  1292. #endif
  1293. if (depth_z < light_split_offsets.y) {
  1294. if (depth_z < light_split_offsets.x) {
  1295. shadow_att = shadow1;
  1296. #ifdef LIGHT_USE_PSSM_BLEND
  1297. shadow_att2 = shadow2;
  1298. pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z);
  1299. #endif
  1300. } else {
  1301. shadow_att = shadow2;
  1302. #ifdef LIGHT_USE_PSSM_BLEND
  1303. shadow_att2 = shadow3;
  1304. pssm_blend = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z);
  1305. #endif
  1306. }
  1307. } else {
  1308. if (depth_z < light_split_offsets.z) {
  1309. shadow_att = shadow3;
  1310. #if defined(LIGHT_USE_PSSM_BLEND)
  1311. shadow_att2 = shadow4;
  1312. pssm_blend = smoothstep(light_split_offsets.y, light_split_offsets.z, depth_z);
  1313. #endif
  1314. } else {
  1315. shadow_att = shadow4;
  1316. pssm_fade = smoothstep(light_split_offsets.z, light_split_offsets.w, depth_z);
  1317. #if defined(LIGHT_USE_PSSM_BLEND)
  1318. use_blend = false;
  1319. #endif
  1320. }
  1321. }
  1322. #if defined(LIGHT_USE_PSSM_BLEND)
  1323. if (use_blend) {
  1324. shadow_att = mix(shadow_att, shadow_att2, pssm_blend);
  1325. }
  1326. #endif
  1327. light_att *= mix(shadow_color.rgb, vec3(1.0), shadow_att);
  1328. }
  1329. #endif //LIGHT_USE_PSSM4
  1330. #ifdef LIGHT_USE_PSSM2
  1331. //take advantage of prefetch
  1332. float shadow1 = sample_shadow(light_directional_shadow, shadow_coord);
  1333. float shadow2 = sample_shadow(light_directional_shadow, shadow_coord2);
  1334. if (depth_z < light_split_offsets.y) {
  1335. float shadow_att = 1.0;
  1336. float pssm_fade = 0.0;
  1337. #ifdef LIGHT_USE_PSSM_BLEND
  1338. float shadow_att2 = 1.0;
  1339. float pssm_blend = 0.0;
  1340. bool use_blend = true;
  1341. #endif
  1342. if (depth_z < light_split_offsets.x) {
  1343. float pssm_fade = 0.0;
  1344. shadow_att = shadow1;
  1345. #ifdef LIGHT_USE_PSSM_BLEND
  1346. shadow_att2 = shadow2;
  1347. pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z);
  1348. #endif
  1349. } else {
  1350. shadow_att = shadow2;
  1351. pssm_fade = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z);
  1352. #ifdef LIGHT_USE_PSSM_BLEND
  1353. use_blend = false;
  1354. #endif
  1355. }
  1356. #ifdef LIGHT_USE_PSSM_BLEND
  1357. if (use_blend) {
  1358. shadow_att = mix(shadow_att, shadow_att2, pssm_blend);
  1359. }
  1360. #endif
  1361. light_att *= mix(shadow_color.rgb, vec3(1.0), shadow_att);
  1362. }
  1363. #endif //LIGHT_USE_PSSM2
  1364. #if !defined(LIGHT_USE_PSSM4) && !defined(LIGHT_USE_PSSM2)
  1365. light_att *= mix(shadow_color.rgb, vec3(1.0), sample_shadow(light_directional_shadow, shadow_coord));
  1366. #endif //orthogonal
  1367. #else //fragment version of pssm
  1368. {
  1369. #ifdef LIGHT_USE_PSSM4
  1370. if (depth_z < light_split_offsets.w) {
  1371. #elif defined(LIGHT_USE_PSSM2)
  1372. if (depth_z < light_split_offsets.y) {
  1373. #else
  1374. if (depth_z < light_split_offsets.x) {
  1375. #endif //pssm2
  1376. highp vec4 pssm_coord;
  1377. float pssm_fade = 0.0;
  1378. #ifdef LIGHT_USE_PSSM_BLEND
  1379. float pssm_blend;
  1380. highp vec4 pssm_coord2;
  1381. bool use_blend = true;
  1382. #endif
  1383. #ifdef LIGHT_USE_PSSM4
  1384. if (depth_z < light_split_offsets.y) {
  1385. if (depth_z < light_split_offsets.x) {
  1386. pssm_coord = shadow_coord;
  1387. #ifdef LIGHT_USE_PSSM_BLEND
  1388. pssm_coord2 = shadow_coord2;
  1389. pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z);
  1390. #endif
  1391. } else {
  1392. pssm_coord = shadow_coord2;
  1393. #ifdef LIGHT_USE_PSSM_BLEND
  1394. pssm_coord2 = shadow_coord3;
  1395. pssm_blend = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z);
  1396. #endif
  1397. }
  1398. } else {
  1399. if (depth_z < light_split_offsets.z) {
  1400. pssm_coord = shadow_coord3;
  1401. #if defined(LIGHT_USE_PSSM_BLEND)
  1402. pssm_coord2 = shadow_coord4;
  1403. pssm_blend = smoothstep(light_split_offsets.y, light_split_offsets.z, depth_z);
  1404. #endif
  1405. } else {
  1406. pssm_coord = shadow_coord4;
  1407. pssm_fade = smoothstep(light_split_offsets.z, light_split_offsets.w, depth_z);
  1408. #if defined(LIGHT_USE_PSSM_BLEND)
  1409. use_blend = false;
  1410. #endif
  1411. }
  1412. }
  1413. #endif // LIGHT_USE_PSSM4
  1414. #ifdef LIGHT_USE_PSSM2
  1415. if (depth_z < light_split_offsets.x) {
  1416. pssm_coord = shadow_coord;
  1417. #ifdef LIGHT_USE_PSSM_BLEND
  1418. pssm_coord2 = shadow_coord2;
  1419. pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z);
  1420. #endif
  1421. } else {
  1422. pssm_coord = shadow_coord2;
  1423. pssm_fade = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z);
  1424. #ifdef LIGHT_USE_PSSM_BLEND
  1425. use_blend = false;
  1426. #endif
  1427. }
  1428. #endif // LIGHT_USE_PSSM2
  1429. #if !defined(LIGHT_USE_PSSM4) && !defined(LIGHT_USE_PSSM2)
  1430. {
  1431. pssm_coord = shadow_coord;
  1432. }
  1433. #endif
  1434. float shadow = sample_shadow(light_directional_shadow, pssm_coord);
  1435. #ifdef LIGHT_USE_PSSM_BLEND
  1436. if (use_blend) {
  1437. shadow = mix(shadow, sample_shadow(light_directional_shadow, pssm_coord2), pssm_blend);
  1438. }
  1439. #endif
  1440. light_att *= mix(shadow_color.rgb, vec3(1.0), shadow);
  1441. }
  1442. }
  1443. #endif //use vertex lighting
  1444. #endif //use shadow
  1445. #endif
  1446. #ifdef LIGHT_MODE_SPOT
  1447. light_att = vec3(1.0);
  1448. #ifndef USE_VERTEX_LIGHTING
  1449. vec3 light_rel_vec = light_position - vertex;
  1450. float light_length = length(light_rel_vec);
  1451. float normalized_distance = light_length / light_range;
  1452. if (normalized_distance < 1.0) {
  1453. float spot_attenuation = pow(1.0 - normalized_distance, light_attenuation);
  1454. vec3 spot_dir = light_direction;
  1455. float spot_cutoff = light_spot_angle;
  1456. float angle = dot(-normalize(light_rel_vec), spot_dir);
  1457. if (angle > spot_cutoff) {
  1458. float scos = max(angle, spot_cutoff);
  1459. float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_cutoff));
  1460. spot_attenuation *= 1.0 - pow(spot_rim, light_spot_attenuation);
  1461. light_att = vec3(spot_attenuation);
  1462. } else {
  1463. light_att = vec3(0.0);
  1464. }
  1465. } else {
  1466. light_att = vec3(0.0);
  1467. }
  1468. L = normalize(light_rel_vec);
  1469. #endif
  1470. #ifdef USE_SHADOW
  1471. {
  1472. highp vec4 splane = shadow_coord;
  1473. float shadow = sample_shadow(light_shadow_atlas, splane);
  1474. light_att *= mix(shadow_color.rgb, vec3(1.0), shadow);
  1475. }
  1476. #endif
  1477. #endif // LIGHT_MODE_SPOT
  1478. #ifdef USE_VERTEX_LIGHTING
  1479. //vertex lighting
  1480. specular_light += specular_interp * specular_blob_intensity * light_att;
  1481. diffuse_light += diffuse_interp * albedo * light_att;
  1482. #else
  1483. //fragment lighting
  1484. light_compute(
  1485. normal,
  1486. L,
  1487. eye_position,
  1488. binormal,
  1489. tangent,
  1490. light_color.xyz,
  1491. light_att,
  1492. albedo,
  1493. transmission,
  1494. specular_blob_intensity * light_specular,
  1495. roughness,
  1496. metallic,
  1497. specular,
  1498. rim,
  1499. rim_tint,
  1500. clearcoat,
  1501. clearcoat_gloss,
  1502. anisotropy,
  1503. diffuse_light,
  1504. specular_light);
  1505. #endif //vertex lighting
  1506. #endif //USE_LIGHTING
  1507. //compute and merge
  1508. #ifndef RENDER_DEPTH
  1509. #ifdef SHADELESS
  1510. gl_FragColor = vec4(albedo, alpha);
  1511. #else
  1512. ambient_light *= albedo;
  1513. #if defined(ENABLE_AO)
  1514. ambient_light *= ao;
  1515. ao_light_affect = mix(1.0, ao, ao_light_affect);
  1516. specular_light *= ao_light_affect;
  1517. diffuse_light *= ao_light_affect;
  1518. #endif
  1519. diffuse_light *= 1.0 - metallic;
  1520. ambient_light *= 1.0 - metallic;
  1521. // environment BRDF approximation
  1522. {
  1523. #if defined(DIFFUSE_TOON)
  1524. //simplify for toon, as
  1525. specular_light *= specular * metallic * albedo * 2.0;
  1526. #else
  1527. //TODO: this curve is not really designed for gammaspace, should be adjusted
  1528. const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
  1529. const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);
  1530. vec4 r = roughness * c0 + c1;
  1531. float ndotv = clamp(dot(normal, eye_position), 0.0, 1.0);
  1532. float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y;
  1533. vec2 env = vec2(-1.04, 1.04) * a004 + r.zw;
  1534. vec3 f0 = F0(metallic, specular, albedo);
  1535. specular_light *= env.x * f0 + env.y;
  1536. #endif
  1537. }
  1538. gl_FragColor = vec4(ambient_light + diffuse_light + specular_light, alpha);
  1539. //add emission if in base pass
  1540. #ifdef BASE_PASS
  1541. gl_FragColor.rgb += emission;
  1542. #endif
  1543. // gl_FragColor = vec4(normal, 1.0);
  1544. #endif //unshaded
  1545. //apply fog
  1546. #if defined(FOG_DEPTH_ENABLED) || defined(FOG_HEIGHT_ENABLED)
  1547. #if defined(USE_VERTEX_LIGHTING)
  1548. #if defined(BASE_PASS)
  1549. gl_FragColor.rgb = mix(gl_FragColor.rgb, fog_interp.rgb, fog_interp.a);
  1550. #else
  1551. gl_FragColor.rgb *= (1.0 - fog_interp.a);
  1552. #endif // BASE_PASS
  1553. #else //pixel based fog
  1554. float fog_amount = 0.0;
  1555. #ifdef LIGHT_MODE_DIRECTIONAL
  1556. vec3 fog_color = mix(fog_color_base.rgb, fog_sun_color_amount.rgb, fog_sun_color_amount.a * pow(max(dot(eye_position, light_direction), 0.0), 8.0));
  1557. #else
  1558. vec3 fog_color = fog_color_base.rgb;
  1559. #endif
  1560. #ifdef FOG_DEPTH_ENABLED
  1561. {
  1562. float fog_z = smoothstep(fog_depth_begin, fog_max_distance, length(vertex));
  1563. fog_amount = pow(fog_z, fog_depth_curve) * fog_color_base.a;
  1564. if (fog_transmit_enabled) {
  1565. vec3 total_light = gl_FragColor.rgb;
  1566. float transmit = pow(fog_z, fog_transmit_curve);
  1567. fog_color = mix(max(total_light, fog_color), fog_color, transmit);
  1568. }
  1569. }
  1570. #endif
  1571. #ifdef FOG_HEIGHT_ENABLED
  1572. {
  1573. float y = (camera_matrix * vec4(vertex, 1.0)).y;
  1574. fog_amount = max(fog_amount, pow(smoothstep(fog_height_min, fog_height_max, y), fog_height_curve));
  1575. }
  1576. #endif
  1577. #if defined(BASE_PASS)
  1578. gl_FragColor.rgb = mix(gl_FragColor.rgb, fog_color, fog_amount);
  1579. #else
  1580. gl_FragColor.rgb *= (1.0 - fog_amount);
  1581. #endif // BASE_PASS
  1582. #endif //use vertex lit
  1583. #endif // defined(FOG_DEPTH_ENABLED) || defined(FOG_HEIGHT_ENABLED)
  1584. #else // not RENDER_DEPTH
  1585. //depth render
  1586. #ifdef USE_RGBA_SHADOWS
  1587. highp float depth = ((position_interp.z / position_interp.w) + 1.0) * 0.5 + 0.0; // bias
  1588. highp vec4 comp = fract(depth * vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0));
  1589. comp -= comp.xxyz * vec4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);
  1590. gl_FragColor = comp;
  1591. #endif
  1592. #endif
  1593. }