Advertisement
Guest User

Untitled

a guest
Jan 12th, 2022
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.57 KB | None | 0 0
  1. #if OPENGL
  2.     #define SV_POSITION POSITION
  3.     #define VS_SHADERMODEL vs_3_0
  4.     #define PS_SHADERMODEL ps_3_0
  5. #else
  6.     #define VS_SHADERMODEL vs_4_0_level_9_1
  7.     #define PS_SHADERMODEL ps_4_0_level_9_1
  8. #endif
  9.  
  10. // ms elapsed
  11. float time;
  12. // final alpha multiplier
  13. float alpha;
  14. float2 center;
  15.  
  16. #define saturate(a) clamp(a, 0., 1.);
  17.  
  18. #define EPS 0.0001
  19.  
  20. #define SAMPLE_COUNT 40
  21.  
  22. #define SHADOW_LENGTH 3.6
  23. #define SHADOW_ITERATIONS 4
  24.  
  25. #define DENSITY_INTENSITY .55
  26. #define AMBIENT_INTENSITY 24.
  27.  
  28. static const float3 ABSORPTION_INTENSITY = float3(.5, .8, .7) * .25;
  29.  
  30. static const float3 sunDirection = normalize(float3(.5, .8, .5));
  31. static const float3 lightColor = float3(.3, .3, .2);
  32.    
  33. static const float3 ambientLightDir = normalize(float3(0., 1., 0.));
  34. static const float3 ambientLightColor = float3(.9, .7, .3);
  35.  
  36. static const float3 cloudColor = float3(.92, .92, .92);    
  37.    
  38. static const float3x3 m = float3x3( 0.00,  0.80,  0.60,
  39.                          -0.80,  0.36, -0.48,
  40.                          -0.60, -0.48,  0.64);
  41.  
  42. float hash(float n)
  43. {
  44.     return frac(sin(n) * 43758.5453);
  45. }
  46.  
  47. float noise(in float3 x)
  48. {
  49.     float3 p = floor(x);
  50.     float3 f = frac(x);
  51.    
  52.     f = f * f * (3.0 - 2.0 * f);
  53.    
  54.     float n = p.x + p.y * 57.0 + 113.0 * p.z;
  55.    
  56.     float res = lerp(lerp(lerp(hash(n +   0.0), hash(n +   1.0), f.x),
  57.                           lerp(hash(n +  57.0), hash(n +  58.0), f.x), f.y),
  58.                      lerp(lerp(hash(n + 113.0), hash(n + 114.0), f.x),
  59.                           lerp(hash(n + 170.0), hash(n + 171.0), f.x), f.y), f.z);
  60.     return res;
  61. }
  62.  
  63. float fbm(float3 p)
  64. {
  65.     float f;
  66.     p = mul(m, p) * 1.5;
  67.     f  = .5 * noise(p);
  68.     p = mul(m, p) * 2.;
  69.     f += .15 * noise(p);
  70.     //p = m * p * .2;
  71.     //f += 0.15 * noise(p);
  72.     return f;
  73. }
  74.  
  75. float sdPlane(float3 p, float4 n) {
  76.   return dot(p,n.xyz) + n.w;
  77. }
  78.  
  79. float scene(float3 p) {
  80.     float iTime = time / 1000.0;
  81.    return 1. - sdPlane(p, float4(0., 1., 0., 1.)) + fbm(p * .7 + iTime) * 2.8;
  82. }
  83.  
  84. float3x3 camera(float3 ro, float3 ta)
  85. {
  86.     float3 forward = normalize(ta - ro);
  87.     float3 side = normalize(cross(forward, float3(0., 1., 0.)));
  88.     float3 up = normalize(cross(side, forward));
  89.     return float3x3(side, up, forward);
  90. }
  91.  
  92. float4 rayMarchFog(float3 p, float3 dir)
  93. {    
  94.    float zStep = 16. / float(SAMPLE_COUNT);
  95.    
  96.    float transmittance = 1.;
  97.    
  98.    float3 color = float3(0.0, 0.0, 0.0);    
  99.    
  100.    float densityScale = DENSITY_INTENSITY * zStep;
  101.    float shadowSize = SHADOW_LENGTH / float(SHADOW_ITERATIONS);
  102.    float3 shadowScale = ABSORPTION_INTENSITY * shadowSize;
  103.    float3 shadowStep = sunDirection * shadowSize;    
  104.    
  105.    for(int i = 0; i < SAMPLE_COUNT; i++)
  106.    {
  107.         float density = scene(p);
  108.        
  109.         if(density > EPS)
  110.         {
  111.             density = saturate(density * densityScale);
  112.            
  113.             float3 shadowPosition = p;
  114.             float shadowDensity = 0.;
  115.             for(int si = 0; si < SHADOW_ITERATIONS; si++)
  116.             {
  117.                 float sp = scene(shadowPosition);
  118.                 shadowDensity += sp;
  119.                 shadowPosition += shadowStep;
  120.             }
  121.             float3 attenuation = exp(-shadowDensity * shadowScale);
  122.             float3 attenuatedLight = lightColor * attenuation;
  123.             color += cloudColor * attenuatedLight * transmittance * density;
  124.  
  125.             shadowDensity = 0.;
  126.             shadowPosition = p + ambientLightDir * .05;
  127.             shadowDensity += scene(p) * .05;
  128.             shadowPosition = p + ambientLightDir * .1;
  129.             shadowDensity += scene(p) * .05;
  130.             shadowPosition = p + ambientLightDir * .2;
  131.             shadowDensity += scene(p) * .1;
  132.             attenuation = exp(-shadowDensity * AMBIENT_INTENSITY);
  133.             attenuatedLight = float3(ambientLightColor * attenuation);
  134.             color += cloudColor * attenuatedLight * transmittance * density;
  135.            
  136.             transmittance *= 1. - density;            
  137.         }
  138.  
  139.         if(transmittance < EPS) {
  140.             break;
  141.         }
  142.        
  143.       p += dir * zStep;
  144.    }
  145.    
  146.    return float4(color, 1. - transmittance);
  147. }
  148.  
  149. float4 main(float2 coord : TEXCOORD) : COLOR
  150. {
  151.     float iTime = time / 1000.0;
  152.     float2 iResolution = float2(1920, 1080);
  153.     float2 fragCoord = float2(coord.x * iResolution.x, (1 - coord.y) * iResolution.y);
  154.     float2 iMouse = center;//float2(960, -400);
  155.  
  156.     float2 uv = (fragCoord.xy - iResolution.xy * .5) / min(iResolution.x, iResolution.y);
  157.     float2 mouse = (iMouse.xy - iResolution.xy * .5) / min(iResolution.x, iResolution.y);
  158.    
  159.     float rot = sin(iTime * .8) * .06;
  160.     uv = mul(float2x2(float2(cos(rot), -sin(rot)), float2(sin(rot), cos(rot))), uv);
  161.  
  162.     float fov = 1.2;
  163.     float3 f = float3(0.0, 0.0, iTime);
  164.     float3 lookAt = float3(
  165.         cos(iTime * .4) * .4 + 0.0,
  166.         sin(iTime * .5) * .18 + 1.6,
  167.         0.0
  168.     ) + f + float3(mouse, 0.0) * 1.;
  169.     float3 cameraPos = float3(
  170.         0.0,
  171.         2.35,
  172.         2.
  173.     ) + f;
  174.  
  175.     // raymarch
  176.     float3 rayOrigin = cameraPos;
  177.     float3 rayDirection = mul(camera(rayOrigin, lookAt), normalize(float3(uv, fov)));
  178.  
  179.     float4 color = float4(0.0, 0.0, 0.0, 0.0);
  180.    
  181.     float4 res = rayMarchFog(rayOrigin, rayDirection);
  182.     color += res;
  183.    
  184.     float avg = (color.r + color.g + color.b) / 3;
  185.     color.rgb = avg.xxx;
  186.     color.b *= 1.6;
  187.     color.a *= alpha;
  188.    
  189.     return color;
  190. }
  191.  
  192. technique Technique1
  193. {
  194.     pass Pass1
  195.     {
  196.         PixelShader = compile PS_SHADERMODEL main();
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement