Shuvit game master repo. http://shuvit.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wind_pixel_lighting_normal.frag 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. varying vec2 texCoord;
  2. varying vec3 vNormal;
  3. varying vec3 eyePos;
  4. varying vec4 tangent;
  5. uniform float emission;
  6. uniform vec3 ambientColor;
  7. uniform vec3 specularColor;
  8. uniform float hardness;
  9. uniform float diffuseIntensity;
  10. uniform sampler2D diffuse1;
  11. uniform sampler2D normalmap;
  12. uniform float normalFactor;
  13. uniform vec2 scale1;
  14. uniform float mistEnable;
  15. uniform float mistStart;
  16. uniform float mistDistance;
  17. uniform float mistIntensity;
  18. uniform float mistType;
  19. uniform vec3 mistColor;
  20. #define maxLights 16
  21. uniform int lightCount;
  22. uniform int lightType[maxLights];
  23. uniform vec3 lightColor[maxLights];
  24. uniform vec3 lightPosition[maxLights];
  25. uniform vec3 lightOrientation[maxLights];
  26. uniform float lightDistance[maxLights];
  27. uniform float lightSpotsize[maxLights];
  28. uniform float lightSpotblend[maxLights];
  29. #define SPOT 0
  30. #define SUN 1
  31. #define POINT 2
  32. #define NORMAL 2
  33. #define HEMI 3
  34. #define M_PI 3.14159265358979323846
  35. // ########################################Functions########################################
  36. float exp_blender(float f)
  37. {
  38. return pow(2.71828182846, f);
  39. }
  40. float linearrgb_to_srgb(float c)
  41. {
  42. if(c < 0.0031308)
  43. return (c < 0.0) ? 0.0: c * 12.92;
  44. else
  45. return 1.055 * pow(c, 1.0/2.4) - 0.055;
  46. }
  47. float srgb_to_linearrgb(float c)
  48. {
  49. if(c < 0.04045)
  50. return (c < 0.0) ? 0.0: c * (1.0 / 12.92);
  51. else
  52. return pow((c + 0.055)*(1.0/1.055), 2.4);
  53. }
  54. vec3 linearrgb_to_srgb(vec3 col_from)
  55. {
  56. vec3 col_to;
  57. col_to.r = linearrgb_to_srgb(col_from.r);
  58. col_to.g = linearrgb_to_srgb(col_from.g);
  59. col_to.b = linearrgb_to_srgb(col_from.b);
  60. return col_to;
  61. }
  62. vec3 srgb_to_linearrgb(vec3 col_from)
  63. {
  64. vec3 col_to;
  65. col_to.r = srgb_to_linearrgb(col_from.r);
  66. col_to.g = srgb_to_linearrgb(col_from.g);
  67. col_to.b = srgb_to_linearrgb(col_from.b);
  68. return col_to;
  69. }
  70. float shade_hemi_spec(vec3 N, vec3 H, float hard)
  71. {
  72. float NdotH = dot(N, H) * 0.5 + 0.5;
  73. float specfac = pow(max(NdotH, 0.0), hard);
  74. return specfac;
  75. }
  76. float shade_phong_spec(vec3 N, vec3 H, vec3 L, vec3 E, float hard)
  77. {
  78. float rslt = max(dot(H, N), 0.0);
  79. float specfac = pow(rslt, hard);
  80. return specfac;
  81. }
  82. float shade_cooktorr_spec(vec3 H, vec3 N, vec3 E)
  83. {
  84. float specfac;
  85. float NdotH = dot(N, H);
  86. if(NdotH < 0.0)
  87. {
  88. specfac = 0.0;
  89. }
  90. else
  91. {
  92. float maxNdotE = max(dot(N, E), 0.0);
  93. specfac = pow(NdotH, hardness);
  94. specfac = specfac / (0.1 + maxNdotE);
  95. }
  96. return specfac;
  97. }
  98. float shade_blinn_spec(vec3 N, vec3 H, vec3 L, vec3 E, float refrac, float spec_power)
  99. {
  100. float specfac;
  101. if(refrac < 1.0) {
  102. specfac = 0.0;
  103. }
  104. else if(spec_power == 0.0) {
  105. specfac = 0.0;
  106. }
  107. else {
  108. if(spec_power<100.0)
  109. spec_power= sqrt(1.0/spec_power);
  110. else
  111. spec_power= 10.0/spec_power;
  112. float NdotH = dot(N, H);
  113. if(NdotH < 0.0) {
  114. specfac = 0.0;
  115. }
  116. else {
  117. float maxNdotE = max(dot(N, E), 0.01);
  118. float NdotL = dot(N, L);
  119. if(NdotL <= 0.01) {
  120. specfac = 0.0;
  121. }
  122. else {
  123. float maxEdotH = max(dot(E, H), 0.01);
  124. float a = 1.0;
  125. float b = (2.0 * NdotH * maxNdotE) / maxEdotH;
  126. float c = (2.0 * NdotH * NdotL) / maxEdotH;
  127. float g = 0.0;
  128. if(a < b && a < c) g = a;
  129. else if(b < a && b < c) g = b;
  130. else if(c < a && c < b) g = c;
  131. float p = sqrt(((refrac * refrac)+(maxEdotH*maxEdotH)-1.0));
  132. float f = (((p-maxEdotH)*(p-maxEdotH))/((p+maxEdotH)*(p+maxEdotH)))*(1.0+((((maxEdotH*(p+maxEdotH))-1.0)*((maxEdotH*(p+maxEdotH))-1.0))/(((maxEdotH*(p-maxEdotH))+1.0)*((maxEdotH*(p-maxEdotH))+1.0))));
  133. float ang = acos(NdotH);
  134. specfac = max(f*g*exp_blender((-(ang*ang)/(2.0*spec_power*spec_power))), 0.0);
  135. }
  136. }
  137. }
  138. return specfac;
  139. }
  140. float shade_wardiso_spec(vec3 N, vec3 H, vec3 E, vec3 L, float rms)
  141. {
  142. float maxNdotH = max(dot(N, H), 0.001);
  143. float maxNdotE = max(dot(N, E), 0.001);
  144. float maxNdotL = max(dot(N, L), 0.001);
  145. float angle = tan(acos(maxNdotH));
  146. float alpha = max(rms, 0.001);
  147. float specfac= maxNdotL * (1.0/(4.0 * M_PI * alpha * alpha)) * (exp_blender(-(angle * angle)/(alpha * alpha))/(sqrt(maxNdotE * maxNdotL)));
  148. return specfac;
  149. }
  150. float shade_toon_spec(vec3 N, vec3 H, float size, float tsmooth)
  151. {
  152. float specfac;
  153. float rslt;
  154. float NdotH = dot(N, H);
  155. float ang = acos(NdotH);
  156. if(ang < size) rslt = 1.0;
  157. else if(ang >= (size + tsmooth) || tsmooth == 0.0) rslt = 0.0;
  158. else rslt = 1.0 - ((ang - size)/tsmooth);
  159. specfac = rslt;
  160. return specfac;
  161. }
  162. vec3 shade_mist_blend(vec3 co, float enable, float miststa, float mistdist, float misttype, float intensity, vec3 col1, vec3 col2)
  163. {
  164. float mist = 0.0;
  165. if(enable == 1.0)
  166. {
  167. float fac, zcor;
  168. zcor = (gl_ProjectionMatrix[3][3] == 0.0)? length(co): -co[2];
  169. fac = clamp((zcor - miststa) / mistdist, 0.0, 1.0);
  170. if(misttype == 0.0) fac *= fac;
  171. else if(misttype == 1.0);
  172. else fac = sqrt(fac);
  173. mist = 1.0 - (1.0 - fac) * (1.0 - intensity);
  174. }
  175. float mixFac = clamp(mist, 0.0, 1.0);
  176. vec3 result = mix(col1, col2, mixFac);
  177. return result;
  178. }
  179. vec3 mtex_nspace_tangent(vec4 tangent, vec3 normal, vec3 texNormal)
  180. {
  181. vec3 binormal = tangent.w * cross(normal, tangent.xyz);
  182. return (normalize(texNormal.x * tangent.xyz + texNormal.y * binormal + texNormal.z * normal));
  183. }
  184. vec3 mtex_normal(vec2 texCoord, sampler2D image)
  185. {
  186. // The invert of the red channel is to make
  187. // the normal map compliant with the outside world.
  188. // It needs to be done because in Blender
  189. // the normal used points inward.
  190. // Should this ever change this negate must be removed.
  191. vec4 color = texture2D(image, texCoord);
  192. return (2.0 * (vec3(-color.r, color.g, color.b) - vec3(-0.5, 0.5, 0.5)));
  193. }
  194. vec3 mtex_blend_normal(float normalFactor, vec3 normal, vec3 newnormal)
  195. {
  196. return normalize(((1.0 - normalFactor) * normal + normalFactor * newnormal));
  197. }
  198. vec3 getLighting(vec3 normal, vec3 eyePos, vec3 color)
  199. //Calculates lighting
  200. {
  201. vec3 N = normal;
  202. vec3 E = normalize(eyePos);
  203. vec3 diffuse = vec3(0, 0, 0);
  204. vec3 specular = vec3(0, 0, 0);
  205. /* handle perspective/orthographic */
  206. E = (gl_ProjectionMatrix[3][3] == 0.0) ? E : vec3(0.0, 0.0, -1.0);
  207. //workaround for for loop unrolling. Change type from int to float.
  208. for (float c = 0.0; c < float(lightCount); c++)
  209. {
  210. int i = int(c);
  211. float attenuation = 1.0;
  212. vec3 lightVec = vec3(0.0, 0.0, 1.0);
  213. if (lightType[i] == SPOT || lightType[i] == POINT)
  214. {
  215. lightVec = eyePos - lightPosition[i];
  216. float lightDist = length(lightVec);
  217. // The invsquare attenuation calculation in Blender is not correct
  218. // I use Blenders attenuation calculation to get the same attenuation
  219. attenuation = lightDistance[i] / (lightDistance[i] + lightDist * lightDist); //attenuation = visifac in Blender shader code
  220. }
  221. else
  222. {
  223. lightVec = lightOrientation[i];
  224. }
  225. vec3 L = normalize(lightVec); //L = lv in Blender shader code
  226. vec3 H = normalize(L+E);
  227. if (lightType[i] == SPOT)
  228. {
  229. float inpr = 0.0;
  230. vec2 scale = vec2(1.0, 1.0);
  231. float visifac = attenuation;
  232. inpr = dot(L, lightOrientation[i]);
  233. float t = lightSpotsize[i]; //spot size
  234. if(inpr <= t)
  235. {
  236. attenuation = 0.0;
  237. }
  238. else
  239. {
  240. /* soft area */
  241. if(lightSpotblend[i] != 0.0)
  242. inpr *= smoothstep(0.0, 1.0, (inpr - t) / lightSpotblend[i]);
  243. attenuation = attenuation * inpr;
  244. }
  245. }
  246. float NdotL = dot(N, L);
  247. //float NdotH = dot(N, H);
  248. //float NdotE = dot(N, E);
  249. float ks;
  250. float kd;
  251. if (lightType[i] == HEMI)
  252. {
  253. NdotL = NdotL* 0.5 + 0.5;
  254. //NdotH = NdotH * 0.5 + 0.5;
  255. //ks = pow(max(NdotH, 0.0), hardness);
  256. ks = shade_hemi_spec(N, H, hardness);
  257. }
  258. else
  259. {
  260. //Phong specular shading
  261. ks = shade_phong_spec(N, H, L, E, hardness);
  262. //CookTorr specular shading
  263. //ks = shade_cooktorr_spec(H, N, E);
  264. //Toon specular shading
  265. /*
  266. float size = 0.5;
  267. float smooth = 0.1;
  268. ks = shade_toon_spec(N, H, size, smooth);
  269. */
  270. //Wardiso specular shading
  271. /*
  272. float slope = 0.1;
  273. ks = shade_wardiso_spec(N, H, E, L, slope);
  274. */
  275. //Blinn specular shading
  276. /*
  277. float refrac = 4.0;
  278. ks = shade_blinn_spec(N, H, L, E, refrac, hardness);
  279. */
  280. }
  281. kd = max(NdotL, 0.0) * attenuation;
  282. diffuse += kd * color * lightColor[i];
  283. ks = ks * attenuation;
  284. specular += ks * specularColor * lightColor[i];
  285. }
  286. return diffuse + specular;
  287. }
  288. // ########################################Main-Function########################################
  289. void main()
  290. {
  291. //vec3 normal = normalize(-vNormal); //activated Backface Culling (or when geometry shader will used)
  292. vec3 normal = (gl_FrontFacing)? normalize(-vNormal) : normalize(vNormal); //deactivated Backface Culling
  293. vec2 ofs = (1.0 - scale1) / 2.0;
  294. vec4 dif1 = texture2D(diffuse1, texCoord*scale1+ofs);
  295. dif1.rgb = srgb_to_linearrgb(dif1.rgb);
  296. vec3 texNormal = mtex_normal(texCoord*scale1+ofs, normalmap);
  297. vec3 tangentNormal = mtex_nspace_tangent(tangent, normal, texNormal);
  298. normal = mtex_blend_normal(normalFactor, normal, tangentNormal);
  299. vec3 lighting = getLighting(normal, eyePos, dif1.rgb);
  300. vec4 color = vec4((emission * dif1.rgb) + ambientColor + lighting, dif1.a);
  301. color.rgb = shade_mist_blend(eyePos, mistEnable, mistStart, mistDistance, mistType, mistIntensity, color.rgb, mistColor);
  302. color.rgb = linearrgb_to_srgb(color.rgb);
  303. gl_FragColor = color;
  304. }