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_color_normal.frag 10KB

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