lighting_nodes.glslf 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. #ifndef LIGHTING_NODES_GLSLF
  2. #define LIGHTING_NODES_GLSLF
  3. // #import u_light_factors
  4. // #import u_light_color_intensities
  5. // #import u_light_positions
  6. // #import u_light_directions
  7. // #import v_shade_tang
  8. #include <std.glsl>
  9. #node LIGHTING_BEGIN
  10. #node_out vec3 E
  11. #node_out vec3 A
  12. #node_out vec3 D
  13. #node_out optional vec3 S
  14. #node_out optional vec3 normal
  15. #node_out optional vec2 diffuse_params
  16. #node_out optional vec2 specular_params
  17. #node_out optional vec4 shadow_factor
  18. #node_out optional float translucency_color
  19. #node_out optional vec4 translucency_params
  20. E = nin_E;
  21. A = nin_A;
  22. D = nin_D;
  23. #node_if USE_OUT_S
  24. S = nin_S;
  25. #node_endif
  26. #node_if USE_OUT_normal
  27. normal = nin_normal;
  28. #node_endif
  29. #node_if USE_OUT_specular_params
  30. specular_params = nin_specular_params;
  31. #node_endif
  32. #node_if USE_OUT_diffuse_params
  33. diffuse_params = nin_diffuse_params;
  34. #node_endif
  35. #node_if USE_OUT_shadow_factor
  36. shadow_factor = nin_shadow_factor;
  37. #node_endif
  38. #node_if USE_OUT_translucency_color
  39. translucency_color = nin_translucency_color;
  40. #node_endif
  41. #node_if USE_OUT_translucency_params
  42. translucency_params = nin_translucency_params;
  43. #node_endif
  44. #endnode
  45. // lighting_ambient function
  46. #node LIGHTING_AMBIENT
  47. #node_in vec3 E
  48. #node_in vec3 A
  49. #node_in vec3 D
  50. #node_out vec4 color_out
  51. #node_out vec3 specular_out
  52. color_out = vec4(E + D * A, _0_0);
  53. specular_out = vec3(_0_0);
  54. #endnode
  55. #node LIGHTING_LAMP
  56. #node_var LAMP_TYPE HEMI
  57. #node_var LAMP_IND 0
  58. #node_var LAMP_SPOT_SIZE 0.8
  59. #node_var LAMP_SPOT_BLEND 0.03
  60. #node_var LAMP_LIGHT_DIST 30.0
  61. #node_var LAMP_SHADOW_MAP_IND -1
  62. #node_var LAMP_USE_SPHERE 0
  63. #node_in vec4 shadow_factor
  64. #node_out vec3 ldir
  65. #node_out vec2 lfac
  66. #node_out vec3 lcolorint
  67. #node_out float norm_fac
  68. // unpack light_factors
  69. lfac = vec2(u_light_positions[LAMP_IND].w, u_light_color_intensities[LAMP_IND].w);
  70. # node_if LAMP_TYPE == HEMI
  71. norm_fac = _0_5;
  72. # node_else
  73. norm_fac = _0_0;
  74. # node_endif
  75. // 0.0 - full shadow, 1.0 - no shadow
  76. lcolorint = u_light_color_intensities[LAMP_IND].xyz;
  77. # node_if LAMP_SHADOW_MAP_IND != -1
  78. lcolorint *= shadow_factor[LAMP_SHADOW_MAP_IND];
  79. # node_endif
  80. # node_if LAMP_TYPE == SPOT || LAMP_TYPE == POINT
  81. vec3 lpos = u_light_positions[LAMP_IND].xyz;
  82. ldir = lpos - nin_pos_world;
  83. // calc attenuation, falloff_type = "INVERSE_SQUARE"
  84. float dist = length(ldir);
  85. float vis_factor = LAMP_LIGHT_DIST / (LAMP_LIGHT_DIST + dist * dist);
  86. ldir = normalize(ldir);
  87. # node_if LAMP_TYPE == SPOT
  88. // spot shape like in Blender,
  89. // source/blender/gpu/shaders/gpu_shader_material.glsl
  90. vec3 ldirect = u_light_directions[LAMP_IND];
  91. float spot_factor = dot(ldir, ldirect);
  92. spot_factor *= smoothstep(_0_0, _1_0,
  93. (spot_factor - LAMP_SPOT_SIZE) / LAMP_SPOT_BLEND);
  94. vis_factor *= spot_factor;
  95. # node_endif
  96. # node_if LAMP_USE_SPHERE
  97. vis_factor *= max(LAMP_LIGHT_DIST - dist, _0_0) / LAMP_LIGHT_DIST;
  98. # node_endif
  99. lcolorint *= vis_factor;
  100. # node_else // LAMP_TYPE == SPOT || LAMP_TYPE == POINT
  101. ldir = u_light_directions[LAMP_IND];
  102. # node_endif // LAMP_TYPE == SPOT || LAMP_TYPE == POINT
  103. #endnode
  104. #node DIFFUSE_FRESNEL
  105. #node_var MAT_USE_TBN_SHADING 0
  106. #node_in vec3 ldir
  107. #node_in vec2 lfac
  108. #node_in vec3 normal
  109. #node_in float norm_fac
  110. #node_in vec2 dif_params
  111. #node_out float lfactor
  112. # node_if MAT_USE_TBN_SHADING
  113. vec3 crss = cross(ldir, v_shade_tang.xyz);
  114. normal = cross(crss, v_shade_tang.xyz);
  115. normal = -normalize(normal);
  116. # node_endif
  117. lfactor = _0_0;
  118. if (lfac.r != _0_0) {
  119. float dot_nl = (_1_0 - norm_fac) * dot(normal, ldir) + norm_fac;
  120. if (dif_params[0] == _0_0) {
  121. lfactor = _1_0;
  122. } else {
  123. float t = _1_0 + abs(dot_nl);
  124. t = dif_params[1] + (_1_0 - dif_params[1]) * pow(t, dif_params[0]);
  125. lfactor = clamp(t, _0_0, _1_0);
  126. }
  127. lfactor = max(lfactor, _0_0);
  128. }
  129. #endnode
  130. #node DIFFUSE_LAMBERT
  131. #node_var MAT_USE_TBN_SHADING 0
  132. #node_in vec3 ldir
  133. #node_in vec2 lfac
  134. #node_in vec3 normal
  135. #node_in float norm_fac
  136. #node_out float lfactor
  137. # node_if MAT_USE_TBN_SHADING
  138. vec3 crss = cross(ldir, v_shade_tang.xyz);
  139. normal = cross(crss, v_shade_tang.xyz);
  140. normal = -normalize(normal);
  141. # node_endif
  142. lfactor = _0_0;
  143. if (lfac.r != _0_0) {
  144. float dot_nl = (_1_0 - norm_fac) * dot(normal, ldir) + norm_fac;
  145. lfactor = max(dot_nl, _0_0);
  146. }
  147. #endnode
  148. #node DIFFUSE_OREN_NAYAR
  149. #node_var MAT_USE_TBN_SHADING 0
  150. #node_in vec3 ldir
  151. #node_in vec2 lfac
  152. #node_in vec3 normal
  153. #node_in float norm_fac
  154. #node_in vec2 dif_params
  155. #node_out float lfactor
  156. # node_if MAT_USE_TBN_SHADING
  157. vec3 crss = cross(ldir, v_shade_tang.xyz);
  158. normal = cross(crss, v_shade_tang.xyz);
  159. normal = -normalize(normal);
  160. # node_endif
  161. lfactor = _0_0;
  162. if (lfac.r != _0_0) {
  163. float dot_nl = (_1_0 - norm_fac) * dot(normal, ldir) + norm_fac;
  164. if (dif_params[0] > _0_0) {
  165. float nv = max(dot(normal, nin_eye_dir), _0_0);
  166. float sigma_sq = dif_params[0] * dif_params[0];
  167. float A = _1_0 - _0_5 * (sigma_sq / (sigma_sq + 0.33));
  168. vec3 l_diff = ldir - dot_nl*normal;
  169. vec3 e_diff = nin_eye_dir - nv*normal;
  170. // handle normalize() and acos() values which may result to
  171. // "undefined behavior"
  172. // (noticeable for "mediump" precision, nin_eye_dir.g some mobile devies)
  173. if (length(l_diff) == _0_0 || length(e_diff) == _0_0 ||
  174. abs(dot_nl) > _1_0 || abs(nv) > _1_0)
  175. // HACK: undefined result of normalize() for this vectors
  176. // remove t-multiplier for zero-length vectors
  177. lfactor = dot_nl * A;
  178. else {
  179. float Lit_A = acos(dot_nl);
  180. float View_A = acos(nv);
  181. vec3 Lit_B = normalize(l_diff);
  182. vec3 View_B = normalize(e_diff);
  183. float a, b;
  184. a = max(Lit_A, View_A);
  185. b = min(Lit_A, View_A);
  186. b *= 0.95;
  187. float t = max(dot(Lit_B, View_B), _0_0);
  188. float B = 0.45 * (sigma_sq / (sigma_sq + 0.09));
  189. lfactor = dot_nl * (A + (B * t * sin(a) * tan(b)));
  190. }
  191. } else
  192. lfactor = dot_nl;
  193. lfactor = max(lfactor, _0_0);
  194. }
  195. #endnode
  196. #node DIFFUSE_MINNAERT
  197. #node_var MAT_USE_TBN_SHADING 0
  198. #node_in vec3 ldir
  199. #node_in vec2 lfac
  200. #node_in vec3 normal
  201. #node_in float norm_fac
  202. #node_in vec2 dif_params
  203. #node_out float lfactor
  204. # node_if MAT_USE_TBN_SHADING
  205. vec3 crss = cross(ldir, v_shade_tang.xyz);
  206. normal = cross(crss, v_shade_tang.xyz);
  207. normal = -normalize(normal);
  208. # node_endif
  209. lfactor = _0_0;
  210. if (lfac.r != _0_0) {
  211. float dot_nl = (_1_0 - norm_fac) * dot(normal, ldir) + norm_fac;
  212. float nv = max(dot(normal, nin_eye_dir), _0_0);
  213. if (dif_params[0] <= _1_0)
  214. lfactor = dot_nl * pow(max(nv * dot_nl, 0.1), dif_params[0] - _1_0);
  215. else
  216. lfactor = dot_nl * pow(1.0001 - nv, dif_params[0] - _1_0);
  217. lfactor = max(lfactor, _0_0);
  218. }
  219. #endnode
  220. #node DIFFUSE_TOON
  221. #node_var MAT_USE_TBN_SHADING 0
  222. #node_in vec3 ldir
  223. #node_in vec2 lfac
  224. #node_in vec3 normal
  225. #node_in float norm_fac
  226. #node_in vec2 dif_params
  227. #node_out float lfactor
  228. # node_if MAT_USE_TBN_SHADING
  229. vec3 crss = cross(ldir, v_shade_tang.xyz);
  230. normal = cross(crss, v_shade_tang.xyz);
  231. normal = -normalize(normal);
  232. # node_endif
  233. lfactor = _0_0;
  234. if (lfac.r != _0_0) {
  235. float dot_nl = (_1_0 - norm_fac) * dot(normal, ldir) + norm_fac;
  236. float ang = acos(dot_nl);
  237. if (ang < dif_params[0])
  238. lfactor = _1_0;
  239. else if (ang > (dif_params[0] + dif_params[1]) || dif_params[1] == _0_0)
  240. lfactor = _0_0;
  241. else
  242. lfactor = _1_0 - ((ang - dif_params[0])/dif_params[1]);
  243. lfactor = max(lfactor, _0_0);
  244. }
  245. #endnode
  246. #node SPECULAR_PHONG
  247. #node_var MAT_USE_TBN_SHADING 0
  248. #node_in vec3 ldir
  249. #node_in vec2 lfac
  250. #node_in vec3 normal
  251. #node_in float norm_fac
  252. #node_in vec2 sp_params
  253. #node_out float sfactor
  254. sfactor = _0_0;
  255. if (lfac.g != _0_0) {
  256. vec3 halfway = normalize(ldir + nin_eye_dir);
  257. # node_if MAT_USE_TBN_SHADING
  258. if (norm_fac == _0_0) {
  259. sfactor = dot(v_shade_tang.xyz, halfway);
  260. sfactor = sqrt(_1_0 - sfactor * sfactor);
  261. }
  262. # node_else
  263. sfactor = (_1_0 - norm_fac) * max(dot(normal, halfway),
  264. _0_0) + norm_fac;
  265. # node_endif
  266. sfactor = pow(sfactor, sp_params[0]);
  267. }
  268. #endnode
  269. #node SPECULAR_COOKTORR
  270. #node_var MAT_USE_TBN_SHADING 0
  271. #node_in vec3 ldir
  272. #node_in vec2 lfac
  273. #node_in vec3 normal
  274. #node_in float norm_fac
  275. #node_in vec2 sp_params
  276. #node_out float sfactor
  277. sfactor = _0_0;
  278. if (lfac.g != _0_0) {
  279. vec3 halfway = normalize(ldir + nin_eye_dir);
  280. # node_if MAT_USE_TBN_SHADING
  281. if (norm_fac == _0_0) {
  282. sfactor = dot(v_shade_tang.xyz, halfway);
  283. sfactor = sqrt(_1_0 - sfactor * sfactor);
  284. }
  285. # node_else
  286. sfactor = max(dot(normal, halfway), _0_0);
  287. sfactor = (_1_0 - norm_fac) * sfactor + norm_fac;
  288. # node_endif
  289. # node_if MAT_USE_TBN_SHADING
  290. float nv = max(dot(v_shade_tang.xyz, nin_eye_dir), _0_0);
  291. nv = sqrt(_1_0 - nv * nv);
  292. # node_else
  293. float nv = max(dot(normal, nin_eye_dir), _0_0);
  294. # node_endif
  295. sfactor = pow(sfactor, sp_params[0]);
  296. sfactor = sfactor / (0.1 + nv);
  297. }
  298. #endnode
  299. #node SPECULAR_WARDISO
  300. #node_var MAT_USE_TBN_SHADING 0
  301. #node_in vec3 ldir
  302. #node_in vec2 lfac
  303. #node_in vec3 normal
  304. #node_in float norm_fac
  305. #node_in vec2 sp_params
  306. #node_out float sfactor
  307. sfactor = _0_0;
  308. if (lfac.g != _0_0) {
  309. vec3 halfway = normalize(ldir + nin_eye_dir);
  310. # node_if MAT_USE_TBN_SHADING
  311. float nh = _0_0;
  312. float nv = _0_0;
  313. float nl = _0_0;
  314. if (norm_fac == _0_0) {
  315. nh = dot(v_shade_tang.xyz, halfway);
  316. nv = dot(v_shade_tang.xyz, nin_eye_dir);
  317. nl = dot(v_shade_tang.xyz, ldir);
  318. nh = sqrt(_1_0 - nh * nh);
  319. nv = sqrt(_1_0 - nv * nv);
  320. nl = sqrt(_1_0 - nl * nl);
  321. }
  322. # node_else
  323. float nh = max(dot(normal, halfway), 0.01);
  324. // NOTE: 0.01 for mobile devices
  325. float nv = max(dot(normal, nin_eye_dir), 0.01);
  326. float nl = max(dot(normal, ldir), 0.01);
  327. # node_endif
  328. float angle = tan(acos(nh));
  329. float alpha = max(sp_params[0], 0.01);
  330. sfactor = nl * (_1_0/(4.0*M_PI*alpha*alpha))
  331. * (exp(-(angle * angle) / (alpha * alpha)) /(sqrt(nv * nl)));
  332. }
  333. #endnode
  334. #node SPECULAR_TOON
  335. #node_var MAT_USE_TBN_SHADING 0
  336. #node_in vec3 ldir
  337. #node_in vec2 lfac
  338. #node_in vec3 normal
  339. #node_in float norm_fac
  340. #node_in vec2 sp_params
  341. #node_out float sfactor
  342. sfactor = _0_0;
  343. if (lfac.g != _0_0) {
  344. vec3 h = normalize(ldir + nin_eye_dir);
  345. # node_if MAT_USE_TBN_SHADING
  346. float cosinus = dot(h, v_shade_tang.xyz);
  347. float angle = sp_params[0] + sp_params[1];
  348. if (norm_fac == _0_0)
  349. angle = acos(sqrt(_1_0 - cosinus * cosinus));
  350. # node_else
  351. float angle = acos(dot(h, normal));
  352. # node_endif
  353. if (angle < sp_params[0])
  354. sfactor = _1_0;
  355. else if (angle >= sp_params[0] + sp_params[1] || sp_params[1] == _0_0)
  356. sfactor = _0_0;
  357. else
  358. sfactor = _1_0 - (angle - sp_params[0]) / sp_params[1];
  359. }
  360. #endnode
  361. #node SPECULAR_BLINN
  362. #node_var MAT_USE_TBN_SHADING 0
  363. #node_in vec3 ldir
  364. #node_in vec2 lfac
  365. #node_in vec3 normal
  366. #node_in float norm_fac
  367. #node_in vec2 sp_params
  368. #node_out float sfactor
  369. float refrac = sp_params[0];
  370. float spec_power = sp_params[1];
  371. sfactor = _0_0;
  372. if (lfac.g != _0_0) {
  373. if (refrac < 1.0 || spec_power == _0_0)
  374. sfactor = _0_0;
  375. else {
  376. if (spec_power < 100.0)
  377. spec_power = sqrt(1.0 / spec_power);
  378. else
  379. spec_power = 10.0 / spec_power;
  380. vec3 halfway = normalize(nin_eye_dir + ldir);
  381. # node_if MAT_USE_TBN_SHADING
  382. float nh = 0.0;
  383. if (norm_fac == _0_0) {
  384. float dot_ht = dot(v_shade_tang.xyz, halfway);
  385. nh = sqrt(_1_0 - dot_ht * dot_ht);
  386. }
  387. # node_else
  388. float nh = (_1_0 - norm_fac) * max(dot(normal, halfway),
  389. _0_0) + norm_fac;
  390. # node_endif
  391. if (nh < _0_0)
  392. sfactor = _0_0;
  393. else {
  394. float nv = max(dot(normal, nin_eye_dir), 0.01);
  395. float nl = dot(normal, ldir);
  396. if (nl <= 0.01)
  397. sfactor = _0_0;
  398. else {
  399. float vh = max(dot(nin_eye_dir, halfway), 0.01);
  400. float a = _1_0;
  401. float b = (2.0 * nh * nv) / vh;
  402. float c = (2.0 * nh * nl) / vh;
  403. float g = min(min(a, b), c);
  404. float p = sqrt(pow(refrac, 2.0) + pow(vh, 2.0) - _1_0);
  405. float f = pow(p - vh, 2.0) / pow(p + vh, 2.0) * (_1_0
  406. + pow(vh * (p + vh) - _1_0, 2.0)/pow(vh * (p - vh)
  407. + _1_0, 2.0));
  408. float ang = acos(nh);
  409. sfactor = max(f * g * exp(-pow(ang, 2.0) / (2.0 * pow(spec_power, 2.0))),
  410. _0_0);
  411. }
  412. }
  413. }
  414. }
  415. #endnode
  416. #node LIGHTING_APPLY
  417. #node_in vec4 color_in
  418. #node_in vec3 specular_in
  419. #node_in float lfactor
  420. #node_in float sfactor
  421. #node_in vec3 ldir
  422. #node_in vec3 normal
  423. #node_in vec4 translucency_params
  424. #node_in vec3 D
  425. #node_in vec3 S
  426. #node_in vec3 lcolorint
  427. #node_in float translucency_color
  428. #node_out vec4 color_out
  429. #node_out vec3 specular_out
  430. specular_out = specular_in + lcolorint * S * sfactor;
  431. color_out = color_in + vec4(lcolorint * D * lfactor, sfactor);
  432. #endnode
  433. #node LIGHTING_END
  434. #node_in vec4 color
  435. #node_in vec3 specular
  436. nout_color = color.rgb;
  437. nout_specular = specular;
  438. #endnode
  439. #nodes_global
  440. void nodes_lighting(
  441. vec3 nin_E,
  442. vec3 nin_A,
  443. vec3 nin_D,
  444. vec3 nin_S,
  445. vec3 nin_pos_world,
  446. vec3 nin_normal,
  447. vec3 nin_eye_dir,
  448. vec2 nin_specular_params,
  449. vec2 nin_diffuse_params,
  450. vec4 nin_shadow_factor,
  451. float nin_translucency_color,
  452. vec4 nin_translucency_params,
  453. out vec3 nout_color,
  454. out vec3 nout_specular) {
  455. #nodes_main
  456. }
  457. #endif