Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. #version 120
  2.  
  3. /* SHADOWRES:1024 */
  4. /* SHADOWHPL:64 */
  5.  
  6. #define SHADOWRES 2048
  7. #define SHADOWHPL 256
  8.  
  9. /* DRAWBUFFERS:3 */
  10.  
  11. // ----------
  12.  
  13. uniform sampler2D gcolor;
  14. uniform sampler2D gnormal;
  15. uniform sampler2D gaux1; // specular map
  16. uniform sampler2D gaux2; // weather
  17. uniform sampler2D colortex1; // ambient light
  18. uniform sampler2D shadow;
  19. uniform sampler2D watershadow;
  20. uniform sampler2D depthtex0;
  21. uniform sampler2D depthtex1;
  22. uniform sampler2D shadowcolor;
  23.  
  24. varying vec4 texcoord;
  25. varying vec3 lightVector;
  26. //varying vec3 specMultiplier;
  27.  
  28. uniform mat4 gbufferProjectionInverse;
  29. uniform mat4 gbufferModelViewInverse;
  30. uniform mat4 shadowProjection;
  31. uniform mat4 shadowModelView;
  32.  
  33. uniform int fogMode;
  34. uniform int worldTime;
  35. uniform vec3 sunPosition;
  36. uniform vec3 moonPosition;
  37. uniform float sunAngle;
  38. uniform float rainStrength;
  39.  
  40. varying vec3 heldLightSpecMultiplier;
  41. varying float heldLightMagnitude;
  42.  
  43. const int FOGMODE_LINEAR = 9729;
  44. const int FOGMODE_EXP = 2048;
  45. const float PI = 3.1415927;
  46.  
  47. vec4 fcolor = texture2D(gcolor, texcoord.st);
  48. vec4 fnormal = texture2D(gnormal, texcoord.st);
  49. vec4 faux1 = texture2D(gaux1, texcoord.st);
  50. vec4 faux2 = texture2D(gaux2, texcoord.st);
  51. vec4 fambient = texture2D(colortex1, texcoord.st);
  52. float depth0 = texture2D(depthtex0, texcoord.st).x;
  53. float depth1 = texture2D(depthtex1, texcoord.st).x;
  54.  
  55. void calcShadowShading(in vec3 prjpos, out vec3 nfpos, out vec3 shading)
  56. {
  57. vec4 fragposition = gbufferProjectionInverse * vec4(prjpos.x * 2.0 - 1.0, prjpos.y * 2.0 - 1.0, prjpos.z * 2.0 - 1.0, 1.0);
  58. fragposition /= fragposition.w;
  59. nfpos = normalize(fragposition.xyz);
  60.  
  61. float distance = sqrt(fragposition.x * fragposition.x + fragposition.y * fragposition.y + fragposition.z * fragposition.z);
  62.  
  63. shading = vec3(1.0);
  64.  
  65. vec4 shadowColorSample = vec4(1.0);
  66.  
  67. if (distance < SHADOWHPL && distance > 0.1) {
  68. // shadows
  69. vec4 worldposition = gbufferModelViewInverse * fragposition;
  70.  
  71. float xzDistanceSquared = worldposition.x * worldposition.x + worldposition.z * worldposition.z;
  72. float yDistanceSquared = worldposition.y * worldposition.y;
  73.  
  74. if (yDistanceSquared < (SHADOWHPL*SHADOWHPL*0.75)) {
  75. worldposition = shadowModelView * worldposition;
  76. float comparedepth = -worldposition.z;
  77. worldposition = shadowProjection * worldposition;
  78. worldposition /= worldposition.w;
  79.  
  80. worldposition.st = worldposition.st * 0.5 + 0.5;
  81.  
  82. if (comparedepth > 0.0 && worldposition.s < 1.0 && worldposition.s > 0.0 && worldposition.t < 1.0 && worldposition.t > 0.0){
  83. float shadowMult = clamp(1.0 - xzDistanceSquared / (SHADOWHPL*SHADOWHPL*1.0), 0.0, 1.0) * clamp(1.0 - yDistanceSquared / (SHADOWHPL*SHADOWHPL*1.0), 0.0, 1.0);
  84. float sampleDistance = 0.25 / SHADOWRES;
  85.  
  86. vec2 shadowPos = worldposition.st;
  87. float shadowSample = texture2D(shadow, shadowPos).r;
  88. float waterShadowSample = texture2D(watershadow, shadowPos).r;
  89.  
  90. float shadowDepth = 0.05 + shadowSample * (256.0 - 0.05);
  91. float waterShadowDepth = 0.05 + waterShadowSample * (256.0 - 0.05);
  92.  
  93. float shading1 = 1.0 - shadowMult * (clamp(comparedepth - shadowDepth - 0.05, 0.0, 1.0)) * clamp(1.0 - rainStrength,0.1,1.0);
  94. float shading2 = 1.0 - shadowMult * (clamp(comparedepth - waterShadowDepth - 0.05, 0.0, 1.0)) * clamp(1.0 - rainStrength,0.1,1.0);
  95.  
  96. vec4 shadowColorSample = texture2D(shadowcolor, shadowPos);
  97. shading = shadowColorSample.rgb*(shading1-shading2) + shading2;
  98. }
  99. }
  100. }
  101. }
  102.  
  103. void main() {
  104. //gl_FragData[0] = texture2D(gcolor, texcoord.st);
  105. //gl_FragData[2] = texture2D(gnormal, texcoord.st);
  106. //gl_FragData[4] = texture2D(gaux1, texcoord.st);
  107. float depth = depth1;
  108. vec3 normal = fnormal.xyz * 2.0 - 1.0;
  109. vec3 dcolor = fcolor.rgb;
  110. vec3 ambient = fambient.rgb;
  111. vec4 comp;
  112.  
  113. if (normal==vec3(0.0) || normal==vec3(-1.0))
  114. {
  115. comp = vec4(dcolor * ambient,1.0);
  116. }
  117. else
  118. {
  119. vec3 tsnfpos;
  120. vec3 tsshading;
  121. calcShadowShading(vec3(texcoord.st,depth0), tsnfpos, tsshading);
  122.  
  123. vec3 specularColor = faux1.rgb;
  124. float sunmoonLevel = sin(sunAngle*(2*PI));
  125. if (sunmoonLevel < 0) // night
  126. {
  127. sunmoonLevel *= (-0.5);
  128. }
  129. vec3 sunmoonColor = vec3(sunmoonLevel*0.5+0.15,sunmoonLevel*0.5+0.10,sunmoonLevel*0.5+0.05);
  130.  
  131. float s;
  132.  
  133. normal = normalize(normal);
  134. s = max(dot(reflect(tsnfpos, normal), lightVector), 0.0);
  135. comp = vec4(min(
  136. dcolor * (ambient + max(dot(normal, lightVector),0.0) * sunmoonColor * tsshading)
  137. + specularColor * s * s * s * sunmoonColor * tsshading
  138. , 1.0), 1.0);
  139.  
  140. if (depth1 > depth0) {
  141. vec3 uwnfpos;
  142. vec3 uwshading;
  143. calcShadowShading(vec3(texcoord.st,depth1), uwnfpos, uwshading);
  144. //s = max(dot(reflect(uwnfpos, normal), lightVector), 0.0);
  145. comp = comp * 0.5 + vec4(min(
  146. dcolor * (ambient + ambient * sunmoonColor * uwshading)
  147. // + specularColor * s * s * s * sunmoonColor * uwshading
  148. , 1.0), 1.0) * 0.5;
  149. }
  150.  
  151. //gl_FragData[3] = vec4(max(dot(normal, lightVector),0.0));
  152.  
  153. //if (heldLightMagnitude > 0.0) {
  154. // if (distance < heldLightMagnitude && distance > 0.1) {
  155. // float intensity = 1.0 - min(distance / heldLightMagnitude, 1.0);
  156. // s = max(dot(bump, -npos), 0.0);
  157. // gl_FragData[3].rgb = min(gl_FragData[3].rgb + intensity * specularColor * s * s * s * heldLightSpecMultiplier, 1.0);
  158. // }
  159. //}
  160.  
  161. //gl_FragData[3].rgb *= shading;
  162. }
  163.  
  164. comp.rgb = mix(comp.rgb, gl_Fog.color.rgb, 1.0-fambient.a);
  165.  
  166. // rain
  167. comp.rgb = mix(comp.rgb, faux2.rgb, faux2.a);
  168.  
  169. #if 0
  170. float fogDepth = abs(depth);
  171. if (fogMode == FOGMODE_EXP) {
  172. comp.rgb = mix(gl_FragData[3].rgb, gl_Fog.color.rgb, 1.0 - clamp(exp(-gl_Fog.density * fogDepth), 0.0, 1.0));
  173. } else if (fogMode == FOGMODE_LINEAR) {
  174. comp.rgb = mix(gl_FragData[3].rgb, gl_Fog.color.rgb, clamp((fogDepth - gl_Fog.start) * gl_Fog.scale, 0.0, 1.0));
  175. } else {
  176. comp.rgb = mix(gl_FragData[3].rgb, gl_Fog.color.rgb, 0.5);
  177. }
  178. #endif
  179.  
  180. gl_FragData[0] = comp;
  181.  
  182. #if 0
  183. if (texcoord.s < 0.18)
  184. {
  185. if (texcoord.t < 0.32)
  186. {
  187. gl_FragData[0] = vec4(
  188. texcoord.t*(1.0/0.32),
  189. texture2D(shadow, texcoord.st*vec2(1.0/0.18,1.0/0.32)).r,
  190. texture2D(watershadow,texcoord.st*vec2(1.0/0.18,1.0/0.32)).r,
  191. 1.0);
  192. }
  193. else if (texcoord.t < 0.64)
  194. {
  195. gl_FragData[0] = texture2D(shadowcolor,(texcoord.st-vec2(0.0,0.32))*vec2(1.0/0.18,1.0/0.32));
  196. }
  197. }
  198. #endif
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement