scene.glsl 64 KB

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