Advertisement
Guest User

Untitled

a guest
Nov 20th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. uniform int iUseTexture0;
  2. uniform int iUseTexture1;
  3. uniform int iUseTexture2;
  4. uniform int iUseTexture3;
  5.  
  6. uniform sampler2D iTexture0;
  7. uniform sampler2D iTexture1;
  8. uniform sampler2D iTexture2;
  9. uniform sampler2D iTexture3;
  10.  
  11. uniform vec4 iClipPlane;
  12. uniform int iUseClipPlane;
  13. uniform int iUseLighting;
  14. uniform int iMultiplyAmbient;
  15.  
  16. //fog-related uniforms
  17. uniform int iFogEnabled;
  18. uniform int iFogMode; //0 - linear, 1 - exp, 2 - exp2
  19. uniform float iFogDensity;
  20. uniform float iFogStart;
  21. uniform float iFogEnd;
  22. uniform vec3 iFogCol;
  23.  
  24. varying vec3 iPositionWorld;
  25. varying vec3 iPositionCamera;
  26. varying vec3 iNormalCamera;
  27.  
  28. uniform float resolution;
  29. uniform float radius;
  30. uniform vec2 dir;
  31.  
  32. uniform float screenWidth = 1920;
  33. uniform float screenHeight = 1080;
  34.  
  35.  
  36.  
  37.  
  38.  
  39. void main()
  40. {
  41.  
  42. //clipping plane part of shader
  43. if (iUseClipPlane == 1 && dot(iPositionWorld, iClipPlane.xyz) < iClipPlane.w) {
  44. discard;
  45. }
  46.  
  47. vec3 N = normalize(iNormalCamera);
  48. vec3 v = normalize(iPositionCamera);
  49. vec3 R = reflect(-v,N);
  50.  
  51. vec4 ambient = vec4(0,0,0,0);
  52. vec4 diffuse = vec4(0,0,0,0);
  53. vec4 specular = vec4(0,0,0,0);
  54.  
  55. //ambient component
  56. if (iUseTexture1 == 1) {
  57. ambient = texture2D(iTexture1, gl_TexCoord[1].xy);
  58. }
  59. ambient.w = 1.0;
  60.  
  61. //diffuse component
  62. if (iUseTexture0 == 1) {
  63. diffuse = texture2D(iTexture0, gl_TexCoord[0].xy);
  64. }
  65. else {
  66. diffuse = gl_FrontMaterial.diffuse;
  67. }
  68.  
  69. if(ambient.w < 0.01 || diffuse.w < 0.01) {
  70. discard;
  71. }
  72. if (iUseLighting == 1) {
  73. diffuse.xyz *= abs(dot(N, v));
  74. }
  75.  
  76. //specular component - note disabled if "lighting" is off
  77. if (iUseLighting == 1) {
  78.  
  79. //specular component
  80. if (iUseTexture2 == 1) {
  81. specular = texture2D(iTexture2, gl_TexCoord[2].xy);
  82. }
  83. else {
  84. specular = gl_FrontMaterial.specular;
  85. }
  86.  
  87. //specular *= clamp(pow(max(dot(R,v),0.0), gl_FrontMaterial.shininess), 0.0, 1.0);
  88. //specular *= pow(max(dot(R,v),0.0), max(gl_FrontMaterial.shininess, 1.0));
  89. specular *= pow(max(dot(R,v),0.0), max(gl_FrontMaterial.shininess, 1.0));
  90. if (iUseTexture3 == 1) {
  91. specular *= texture2D(iTexture3, gl_TexCoord[3].xy);
  92. }
  93.  
  94. }
  95. specular.w = 1.0;
  96.  
  97. //global scaling via colour
  98. gl_FragColor = gl_Color;
  99. if (iMultiplyAmbient == 0) {
  100. gl_FragColor.xyz *= (ambient + diffuse + specular).xyz;
  101. }
  102. else {
  103. if (iUseTexture1 == 1) {
  104. //gl_FragColor.xyz *= ((vec4(1,1,1,1) * 0.2 + ambient * 0.8) * diffuse + specular).xyz;
  105. gl_FragColor.xyz *= (0.1 + ambient * diffuse * 1.25).xyz;
  106. }
  107. else {
  108. gl_FragColor.xyz *= (diffuse + specular).xyz;
  109. //gl_FragColor.xyz *= (diffuse).xyz;
  110. }
  111.  
  112. }
  113.  
  114. if (iUseTexture0 == 1) {
  115. gl_FragColor.w = diffuse.w;
  116. }
  117.  
  118. //apply fog
  119. if (iFogEnabled == 1) {
  120.  
  121. //compute length from eye to fragment
  122. float c = length(iPositionCamera);
  123.  
  124. //compute blend value
  125. float f;
  126. if (iFogMode == 0) {
  127. f = (iFogEnd - c) / (iFogEnd - iFogStart);
  128. }
  129. else if (iFogMode == 1) {
  130. f = exp(-iFogDensity * c);
  131. }
  132. else if (iFogMode == 2) {
  133. f = exp(-(iFogDensity * c)*(iFogDensity * c));
  134. }
  135. f = clamp(f, 0, 1);
  136.  
  137. //blend with fragment's post-texturing colour using the appropriate blending factor
  138. gl_FragColor.xyz = gl_FragColor.xyz * f + iFogCol * (1.0 - f);
  139.  
  140. }
  141.  
  142. // Converting (x,y,z) to range [0,1]
  143. float x = gl_FragCoord.x/screenWidth;
  144. float y = gl_FragCoord.y/screenHeight;
  145. float z = gl_FragCoord.z; // Already in range [0,1]
  146.  
  147. // Converting from range [0,1] to NDC [-1,1]
  148. float ndcx = x * 2.0 - 1.0;
  149. float ndcy = y * 2.0 - 1.0;
  150. float ndcz = z * 2.0 - 1.0;
  151. vec3 ndc = vec3(ndcx, ndcy, ndcz);
  152.  
  153. vec4 blurSample = 0.0;
  154. vec4 tmpPix;
  155. vec4 offPix;
  156. vec2 uv = vec2(ndcx, ndcy);
  157. for(int i=-4;i<5;i++)
  158. {
  159. tmpPix = texture2D(iTexture0,uv + vec2( i*0.005,0 ));
  160. offPix = -0.3+tmpPix;
  161. offPix = offPix * 15;
  162. if( (offPix.r+offPix.g+offPix.b)>0 )
  163. {
  164. blurSample = blurSample + offPix;
  165. }
  166. }
  167.  
  168. for(int i=-4;i<5;i++)
  169. {
  170. tmpPix = texture2D(iTexture0,uv + vec2( 0,i*0.005 ));
  171. offPix = -0.3+tmpPix;
  172. offPix = offPix * 15;
  173. if( (offPix.r+offPix.g+offPix.b)>0 )
  174. {
  175. blurSample += offPix;
  176. }
  177.  
  178. }
  179.  
  180. blurSample = blurSample / 64;
  181. gl_FragColor.xyz = blurSample*1.2;
  182.  
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement