fs_sky_landscape.sc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. $input v_normal, v_texcoord0
  2. /*
  3. * Copyright 2017 Stanislav Pidhorskyi. All rights reserved.
  4. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  5. */
  6. #include "../common/common.sh"
  7. SAMPLER2D(s_texLightmap, 0);
  8. uniform vec4 u_sunDirection;
  9. uniform vec4 u_sunLuminance;
  10. uniform vec4 u_skyLuminance;
  11. uniform vec4 u_parameters;
  12. // https://www.shadertoy.com/view/4ssXRX
  13. // http://www.loopit.dk/banding_in_games.pdf
  14. // http://www.dspguide.com/ch2/6.htm
  15. //uniformly distributed, normalized rand, [0, 1)
  16. float nrand(in vec2 n)
  17. {
  18. return fract(sin(dot(n.xy, vec2(12.9898, 78.233)))* 43758.5453);
  19. }
  20. float n4rand_ss(in vec2 n)
  21. {
  22. float nrnd0 = nrand( n + 0.07*fract( u_parameters.w ) );
  23. float nrnd1 = nrand( n + 0.11*fract( u_parameters.w + 0.573953 ) );
  24. return 0.23*sqrt(-log(nrnd0+0.00001))*cos(2.0*3.141592*nrnd1)+0.5;
  25. }
  26. float toLinear(float _rgb)
  27. {
  28. return pow(abs(_rgb), 2.2);
  29. }
  30. void main()
  31. {
  32. vec3 normal = normalize(v_normal);
  33. float occulsion = toLinear(texture2D(s_texLightmap, v_texcoord0).r);
  34. vec3 skyDirection = vec3(0.0, 0.0, 1.0);
  35. float diffuseSun = max(0.0, dot(normal, normalize(u_sunDirection.xyz)));
  36. float diffuseSky = 1.0 + 0.5 * dot(normal, skyDirection);
  37. vec3 color = diffuseSun * u_sunLuminance.rgb + (diffuseSky * u_skyLuminance.rgb + 0.01) * occulsion;
  38. color *= 0.5;
  39. //color = mix(color, (u_skyLuminance + u_sunLuminance)*0.3, v_fogFactor);
  40. gl_FragColor.xyz = color * u_parameters.z;
  41. gl_FragColor.w = 1.0;
  42. float r = n4rand_ss(gl_FragCoord.xy) / 40.0;
  43. gl_FragColor.xyz = toGamma(gl_FragColor.xyz) + vec3(r, r, r);
  44. }