Advertisement
Guest User

Untitled

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