scene.glsl 55 KB

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