Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.40 KB | None | 0 0
  1. #import "Common/ShaderLib/Parallax.glsllib"
  2. //#extension GL_NV_shadow_samplers_cube : enable
  3.  
  4. #ifdef USE_REFLECTION
  5. // Because here it causes errors
  6. #import "Common/ShaderLib/Optics.glsllib"
  7. #endif
  8.  
  9. // -----------< SHADOW >----------------
  10.  
  11. #import "Common/ShaderLib/Shadows15.glsllib"
  12. #if defined(PSSM) || defined(FADE)
  13. in float shadowPosition;
  14. #endif
  15.  
  16. in vec4 projCoord0;
  17. in vec4 projCoord1;
  18. in vec4 projCoord2;
  19. in vec4 projCoord3;
  20.  
  21. uniform vec3 m_LightPos;
  22. uniform float m_LightRadius;
  23. //varying float initMaxss;
  24. //uniform float m_LightRadius;
  25. #ifdef POINTLIGHT
  26. in vec4 projCoord4;
  27. in vec4 projCoord5;
  28. in vec4 worldPos;
  29. #else
  30. #ifndef PSSM
  31. in float lightDot;
  32. #endif
  33. #endif
  34.  
  35. #ifdef FADE
  36. uniform vec2 m_FadeInfo;
  37. #endif
  38.  
  39. // -----------< SHADOW >----------------
  40.  
  41. #define ATTENUATION
  42. //#define HQ_ATTENUATION
  43.  
  44. varying vec2 texCoord;
  45. #ifdef SEPARATE_TEXCOORD
  46. varying vec2 texCoord2;
  47. #endif
  48.  
  49. //varying vec3 AmbientSum;
  50. varying vec4 DiffuseSum;
  51. varying vec3 SpecularSum;
  52.  
  53. #ifndef VERTEX_LIGHTING
  54. // uniform vec4 g_LightDirection;
  55. //varying vec3 vPosition;
  56. varying vec3 vViewDir;
  57. varying vec4 vLightDir;
  58. varying vec3 lightVec;
  59. #else
  60. varying vec2 vertexLightValues;
  61. #endif
  62.  
  63. #ifdef DIFFUSEMAP
  64. uniform sampler2D m_DiffuseMap;
  65. #endif
  66.  
  67. #ifdef SPECULARMAP
  68. uniform sampler2D m_SpecularMap;
  69. #endif
  70.  
  71. #ifdef PARALLAXMAP
  72. uniform sampler2D m_ParallaxMap;
  73. #endif
  74. #if (defined(PARALLAXMAP) || (defined(NORMALMAP_PARALLAX) && defined(NORMALMAP))) && !defined(VERTEX_LIGHTING)
  75. uniform float m_ParallaxHeight;
  76. #endif
  77.  
  78. #ifdef LIGHTMAP
  79. uniform sampler2D m_LightMap;
  80. #endif
  81.  
  82. #ifdef NORMALMAP
  83. uniform sampler2D m_NormalMap;
  84. #else
  85. varying vec3 vNormal;
  86. #endif
  87.  
  88. #ifdef ALPHAMAP
  89. uniform sampler2D m_AlphaMap;
  90. #endif
  91.  
  92. #ifdef COLORRAMP
  93. uniform sampler2D m_ColorRamp;
  94. #endif
  95.  
  96. uniform float m_AlphaDiscardThreshold;
  97.  
  98. #ifndef VERTEX_LIGHTING
  99. uniform float m_Shininess;
  100.  
  101. #ifdef HQ_ATTENUATION
  102. uniform vec4 g_LightPosition;
  103. #endif
  104.  
  105. #ifdef USE_REFLECTION
  106. uniform float m_ReflectionPower;
  107. uniform float m_ReflectionIntensity;
  108. varying vec4 refVec;
  109.  
  110. uniform ENVMAP m_EnvMap;
  111. #endif
  112.  
  113. // -----------< SHADOW >----------------
  114.  
  115. float shadow(){
  116.  
  117. float shadow = 1.0;
  118. #ifdef POINTLIGHT
  119. shadow = getPointLightShadows(worldPos, m_LightPos, //.xyz,
  120. m_ShadowMap0,m_ShadowMap1,m_ShadowMap2,m_ShadowMap3,m_ShadowMap4,m_ShadowMap5,
  121. projCoord0, projCoord1, projCoord2, projCoord3, projCoord4, projCoord5);
  122. #else
  123. #ifdef PSSM
  124. shadow = getDirectionalLightShadows(m_Splits, shadowPosition,
  125. m_ShadowMap0,m_ShadowMap1,m_ShadowMap2,m_ShadowMap3,
  126. projCoord0, projCoord1, projCoord2, projCoord3);
  127. #else
  128. //spotlight
  129. if(lightDot < 0){
  130. return 1.0;
  131. }
  132. shadow = getSpotLightShadows(m_ShadowMap0,projCoord0);
  133. #endif
  134. #endif
  135.  
  136. #ifdef FADE
  137. shadow = max(0.0,mix(shadow,1.0,(shadowPosition - m_FadeInfo.x) * m_FadeInfo.y));
  138. #endif
  139.  
  140. return shadow * m_ShadowIntensity + (1.0 - m_ShadowIntensity);
  141.  
  142. }
  143. // -----------< SHADOW >----------------
  144.  
  145. float tangDot(in vec3 v1, in vec3 v2){
  146. float d = dot(v1,v2);
  147. #ifdef V_TANGENT
  148. d = 1.0 - d*d;
  149. return step(0.0, d) * sqrt(d);
  150. #else
  151. return d;
  152. #endif
  153. }
  154.  
  155. float lightComputeDiffuse(in vec3 norm, in vec3 lightdir, in vec3 viewdir){
  156. #ifdef MINNAERT
  157. float NdotL = max(0.0, dot(norm, lightdir));
  158. float NdotV = max(0.0, dot(norm, viewdir));
  159. return NdotL * pow(max(NdotL * NdotV, 0.1), -1.0) * 0.5;
  160. #else
  161. //return max(0.0, dot(norm, lightdir));
  162.  
  163. // Poor modification used also in my own Lighting.frag
  164. float val = 1.0 - max(0.0, dot(norm, lightdir));
  165. if (val == 1.0) return 0.0;
  166. val = ((1.0 - (vLightDir.w * val)) + vLightDir.w * 0.3) * 0.85;
  167. val = max(0.0, val);
  168. return val;
  169. #endif
  170. }
  171.  
  172. float lightComputeSpecular(in vec3 norm, in vec3 viewdir, in vec3 lightdir, in float shiny){
  173. // NOTE: check for shiny <= 1 removed since shininess is now
  174. // 1.0 by default (uses matdefs default vals)
  175. #ifdef LOW_QUALITY
  176. // Blinn-Phong
  177. // Note: preferably, H should be computed in the vertex shader
  178. vec3 H = (viewdir + lightdir) * vec3(0.5);
  179. return pow(max(tangDot(H, norm), 0.0), shiny);
  180. #elif defined(WARDISO)
  181. // Isotropic Ward
  182. vec3 halfVec = normalize(viewdir + lightdir);
  183. float NdotH = max(0.001, tangDot(norm, halfVec));
  184. float NdotV = max(0.001, tangDot(norm, viewdir));
  185. float NdotL = max(0.001, tangDot(norm, lightdir));
  186. float a = tan(acos(NdotH));
  187. float p = max(shiny/128.0, 0.001);
  188. return NdotL * (1.0 / (4.0*3.14159265*p*p)) * (exp(-(a*a)/(p*p)) / (sqrt(NdotV * NdotL)));
  189. #else
  190. // Standard Phong
  191. vec3 R = reflect(-lightdir, norm);
  192. return pow(max(tangDot(R, viewdir), 0.0), shiny);
  193. #endif
  194. }
  195.  
  196. vec2 computeLighting(in vec3 wvNorm, in vec3 wvViewDir, in vec3 wvLightDir){
  197. float diffuseFactor = lightComputeDiffuse(wvNorm, wvLightDir, wvViewDir);
  198. float specularFactor = lightComputeSpecular(wvNorm, wvViewDir, wvLightDir, m_Shininess);
  199.  
  200. #ifdef HQ_ATTENUATION
  201. float att = clamp(1.0 - g_LightPosition.w * length(lightVec), 0.0, 1.0);
  202. #else
  203. float att = vLightDir.w;
  204. #endif
  205.  
  206. if (m_Shininess <= 1.0) {
  207. specularFactor = 0.0; // should be one instruction on most cards ..
  208. }
  209.  
  210. specularFactor *= diffuseFactor;
  211.  
  212. return vec2(diffuseFactor, specularFactor) * vec2(att);
  213. }
  214. #endif
  215.  
  216. void main(){
  217.  
  218. // Without that the shadows are endless and there is a strange lighting effect
  219. // far far away, at the edge of view cam's far frustum
  220. vec3 lightVector = m_LightPos - worldPos.xyz; //vLightPos.xyz - worldPos.xyz;
  221. float dist = length(lightVector);
  222. float lrh = (1.0 / m_LightRadius); // / 2;
  223. float initMaxss = (lrh - dist) / lrh;
  224.  
  225. //if (dist > lrh && lrh > 0)
  226. //{
  227. // dist -= lrh;
  228. // initMaxss = 1.0 - (dist / lrh);
  229. //}
  230. if (initMaxss <= 0) return; // initMaxss is used at the end too (gl_FragColor.rgb)
  231.  
  232.  
  233. vec2 newTexCoord;
  234.  
  235. #if (defined(PARALLAXMAP) || (defined(NORMALMAP_PARALLAX) && defined(NORMALMAP))) && !defined(VERTEX_LIGHTING)
  236.  
  237. #ifdef STEEP_PARALLAX
  238. #ifdef NORMALMAP_PARALLAX
  239. //parallax map is stored in the alpha channel of the normal map
  240. newTexCoord = steepParallaxOffset(m_NormalMap, vViewDir, texCoord, m_ParallaxHeight);
  241. #else
  242. //parallax map is a texture
  243. newTexCoord = steepParallaxOffset(m_ParallaxMap, vViewDir, texCoord, m_ParallaxHeight);
  244. #endif
  245. #else
  246. #ifdef NORMALMAP_PARALLAX
  247. //parallax map is stored in the alpha channel of the normal map
  248. newTexCoord = classicParallaxOffset(m_NormalMap, vViewDir, texCoord, m_ParallaxHeight);
  249. #else
  250. //parallax map is a texture
  251. newTexCoord = classicParallaxOffset(m_ParallaxMap, vViewDir, texCoord, m_ParallaxHeight);
  252. #endif
  253. #endif
  254. #else
  255. newTexCoord = texCoord;
  256. #endif
  257.  
  258. #ifdef DIFFUSEMAP
  259. vec4 diffuseColor = texture2D(m_DiffuseMap, newTexCoord);
  260. #else
  261. vec4 diffuseColor = vec4(1.0);
  262. #endif
  263.  
  264. float alpha = DiffuseSum.a * diffuseColor.a;
  265. #ifdef ALPHAMAP
  266. alpha = alpha * texture2D(m_AlphaMap, newTexCoord).r;
  267. #endif
  268. if(alpha < m_AlphaDiscardThreshold){
  269. discard;
  270. }
  271. /*
  272. #ifndef VERTEX_LIGHTING
  273. float spotFallOff = 1.0;
  274.  
  275. #if __VERSION__ >= 110
  276. // allow use of control flow
  277. if(g_LightDirection.w != 0.0){
  278. #endif
  279.  
  280. vec3 L = normalize(lightVec.xyz);
  281. vec3 spotdir = normalize(g_LightDirection.xyz);
  282. float curAngleCos = dot(-L, spotdir);
  283. float innerAngleCos = floor(g_LightDirection.w) * 0.001;
  284. float outerAngleCos = fract(g_LightDirection.w);
  285. float innerMinusOuter = innerAngleCos - outerAngleCos;
  286. spotFallOff = (curAngleCos - outerAngleCos) / innerMinusOuter;
  287.  
  288. #if __VERSION__ >= 110
  289. if(spotFallOff <= 0.0){
  290. gl_FragColor.rgb = AmbientSum * diffuseColor.rgb;
  291. gl_FragColor.a = alpha;
  292. return;
  293. }else{
  294. spotFallOff = clamp(spotFallOff, 0.0, 1.0);
  295. }
  296. }
  297. #else
  298. spotFallOff = clamp(spotFallOff, step(g_LightDirection.w, 0.001), 1.0);
  299. #endif
  300. #endif
  301. */
  302.  
  303. // ***********************
  304. // Read from textures
  305. // ***********************
  306. #if defined(NORMALMAP) && !defined(VERTEX_LIGHTING)
  307. vec4 normalHeight = texture2D(m_NormalMap, newTexCoord);
  308. //Note the -2.0 and -1.0. We invert the green channel of the normal map,
  309. //as it's complient with normal maps generated with blender.
  310. //see http://hub.jmonkeyengine.org/forum/topic/parallax-mapping-fundamental-bug/#post-256898
  311. //for more explanation.
  312. vec3 normal = normalize((normalHeight.xyz * vec3(2.0,-2.0,2.0) - vec3(1.0,-1.0,1.0)));
  313. #ifdef LATC
  314. normal.z = sqrt(1.0 - (normal.x * normal.x) - (normal.y * normal.y));
  315. #endif
  316. #elif !defined(VERTEX_LIGHTING)
  317. vec3 normal = vNormal;
  318. #if !defined(LOW_QUALITY) && !defined(V_TANGENT)
  319. normal = normalize(normal);
  320. #endif
  321. #endif
  322.  
  323. #ifdef SPECULARMAP
  324. vec4 specularColor = texture2D(m_SpecularMap, newTexCoord);
  325. #else
  326. vec4 specularColor = vec4(1.0);
  327. #endif
  328.  
  329. #ifdef LIGHTMAP
  330. vec3 lightMapColor;
  331. #ifdef SEPARATE_TEXCOORD
  332. lightMapColor = texture2D(m_LightMap, texCoord2).rgb;
  333. #else
  334. lightMapColor = texture2D(m_LightMap, texCoord).rgb;
  335. #endif
  336. specularColor.rgb *= lightMapColor;
  337. diffuseColor.rgb *= lightMapColor;
  338. #endif
  339.  
  340. #ifdef VERTEX_LIGHTING
  341. vec2 light = vertexLightValues.xy;
  342. #ifdef COLORRAMP
  343. light.x = texture2D(m_ColorRamp, vec2(light.x, 0.0)).r;
  344. light.y = texture2D(m_ColorRamp, vec2(light.y, 0.0)).r;
  345. #endif
  346.  
  347. // -----------< SHADOW >----------------
  348.  
  349.  
  350. float shadow = shadow();
  351. light.x = min(shadow,light.x);
  352. light.y = min(shadow,light.y);
  353.  
  354. // -----------< SHADOW >----------------
  355. gl_FragColor.rgb = //AmbientSum * diffuseColor.rgb +
  356. DiffuseSum.rgb * diffuseColor.rgb * vec3(light.x) +
  357. SpecularSum * specularColor.rgb * vec3(light.y);
  358. #else
  359. vec4 lightDir = vLightDir;
  360. lightDir.xyz = normalize(lightDir.xyz);
  361. //lightDir.x = abs(lightDir.x);
  362. //lightDir.y = abs(lightDir.y);
  363. //lightDir.z = abs(lightDir.z);
  364. lightDir = abs(lightDir);
  365. vec3 viewDir = normalize(vViewDir);
  366.  
  367. vec2 light = computeLighting(normal, viewDir, lightDir.xyz);// * spotFallOff;
  368. #ifdef COLORRAMP
  369. diffuseColor.rgb *= texture2D(m_ColorRamp, vec2(light.x, 0.0)).rgb;
  370. specularColor.rgb *= texture2D(m_ColorRamp, vec2(light.y, 0.0)).rgb;
  371. #endif
  372.  
  373. // Workaround, since it is not possible to modify varying variables
  374. vec4 SpecularSum2 = vec4(SpecularSum, 1.0);
  375. #ifdef USE_REFLECTION
  376. vec4 refColor = Optics_GetEnvColor(m_EnvMap, refVec.xyz);
  377.  
  378. // Interpolate light specularity toward reflection color
  379. // Multiply result by specular map
  380. specularColor = mix(SpecularSum2 * light.y, refColor, refVec.w) * specularColor;
  381.  
  382. SpecularSum2 = vec4(1.0);
  383. light.y = 1.0;
  384. #endif
  385.  
  386. // -----------< SHADOW >----------------
  387.  
  388. float shadow = shadow();
  389. light.x = min(shadow,light.x);
  390. light.y = min(shadow,light.y);
  391.  
  392. // -----------< SHADOW >----------------
  393.  
  394. gl_FragColor.rgb = DiffuseSum.rgb * diffuseColor.rgb * vec3(light.x * initMaxss) +
  395. SpecularSum2.rgb * specularColor.rgb * vec3(light.y * initMaxss);
  396. #endif
  397. //gl_FragColor.xyz = vec3(shadow());
  398. gl_FragColor.a = alpha;
  399. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement