sky.comp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #version 450
  2. layout (local_size_x = 16, local_size_y = 16) in;
  3. layout(rgba8,set = 0, binding = 0) uniform image2D image;
  4. //push constants block
  5. layout( push_constant ) uniform constants
  6. {
  7. vec4 data1;
  8. vec4 data2;
  9. vec4 data3;
  10. vec4 data4;
  11. } PushConstants;
  12. // License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  13. // Return random noise in the range [0.0, 1.0], as a function of x.
  14. float Noise2d( in vec2 x )
  15. {
  16. float xhash = cos( x.x * 37.0 );
  17. float yhash = cos( x.y * 57.0 );
  18. return fract( 415.92653 * ( xhash + yhash ) );
  19. }
  20. // Convert Noise2d() into a "star field" by stomping everthing below fThreshhold to zero.
  21. float NoisyStarField( in vec2 vSamplePos, float fThreshhold )
  22. {
  23. float StarVal = Noise2d( vSamplePos );
  24. if ( StarVal >= fThreshhold )
  25. StarVal = pow( (StarVal - fThreshhold)/(1.0 - fThreshhold), 6.0 );
  26. else
  27. StarVal = 0.0;
  28. return StarVal;
  29. }
  30. // Stabilize NoisyStarField() by only sampling at integer values.
  31. float StableStarField( in vec2 vSamplePos, float fThreshhold )
  32. {
  33. // Linear interpolation between four samples.
  34. // Note: This approach has some visual artifacts.
  35. // There must be a better way to "anti alias" the star field.
  36. float fractX = fract( vSamplePos.x );
  37. float fractY = fract( vSamplePos.y );
  38. vec2 floorSample = floor( vSamplePos );
  39. float v1 = NoisyStarField( floorSample, fThreshhold );
  40. float v2 = NoisyStarField( floorSample + vec2( 0.0, 1.0 ), fThreshhold );
  41. float v3 = NoisyStarField( floorSample + vec2( 1.0, 0.0 ), fThreshhold );
  42. float v4 = NoisyStarField( floorSample + vec2( 1.0, 1.0 ), fThreshhold );
  43. float StarVal = v1 * ( 1.0 - fractX ) * ( 1.0 - fractY )
  44. + v2 * ( 1.0 - fractX ) * fractY
  45. + v3 * fractX * ( 1.0 - fractY )
  46. + v4 * fractX * fractY;
  47. return StarVal;
  48. }
  49. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  50. {
  51. vec2 iResolution = imageSize(image);
  52. // Sky Background Color
  53. vec3 vColor = vec3( 0.1, 0.2, 0.4 ) * fragCoord.y / iResolution.y;
  54. // Note: Choose fThreshhold in the range [0.99, 0.9999].
  55. // Higher values (i.e., closer to one) yield a sparser starfield.
  56. float StarFieldThreshhold = 0.97;
  57. // Stars with a slow crawl.
  58. float xRate = 0.2;
  59. float yRate = -0.06;
  60. vec2 vSamplePos = fragCoord.xy + vec2( xRate * float( 1 ), yRate * float( 1 ) );
  61. float StarVal = StableStarField( vSamplePos, StarFieldThreshhold );
  62. vColor += vec3( StarVal );
  63. fragColor = vec4(vColor, 1.0);
  64. }
  65. void main()
  66. {
  67. vec4 value = vec4(0.0, 0.0, 0.0, 1.0);
  68. ivec2 texelCoord = ivec2(gl_GlobalInvocationID.xy);
  69. ivec2 size = imageSize(image);
  70. if(texelCoord.x < size.x && texelCoord.y < size.y)
  71. {
  72. vec4 color;
  73. mainImage(color,texelCoord);
  74. imageStore(image, texelCoord, color);
  75. }
  76. }