Skip to content

Instantly share code, notes, and snippets.

@ufna
Last active July 22, 2022 03:30
Show Gist options
  • Save ufna/e204d04ddd2a6eff5068fbcbc5426271 to your computer and use it in GitHub Desktop.
Save ufna/e204d04ddd2a6eff5068fbcbc5426271 to your computer and use it in GitHub Desktop.
Clouds by iq adoptation for Unreal Engine 4
float3 sundir = normalize( float3(-1.0,0.0,-1.0) );
struct Functions
{
float noise( in float3 x )
{
float3 p = floor(x);
float3 f = frac(x);
f = f*f*(3.0-2.0*f);
float2 uv = (p.xy+float2(37.0,239.0)*p.z) + f.xy;
uv.y = 1 - uv.y; // flip channel
float2 rg = iChannel0.SampleLevel(iChannel0Sampler, (uv+0.5)/256.0, 0.0).yx;
return -1.0+2.0*lerp( rg.x, rg.y, f.z );
}
float map5( in float3 p )
{
float3 q = p - float3(0.0,0.1,1.0)*iTime;
float f = 0.0;
f = 0.50000*noise( q ); q = q*2.02;
f += 0.25000*noise( q ); q = q*2.03;
f += 0.12500*noise( q ); q = q*2.01;
f += 0.06250*noise( q ); q = q*2.02;
f += 0.03125*noise( q );
return clamp( 1.5 - p.y - 2.0 + 1.75*f, 0.0, 1.0 );
}
float map4( in float3 p )
{
float3 q = p - float3(0.0,0.1,1.0)*iTime;
float f = 0.0;
f = 0.50000*noise( q ); q = q*2.02;
f += 0.25000*noise( q ); q = q*2.03;
f += 0.12500*noise( q ); q = q*2.01;
f += 0.06250*noise( q );
return clamp( 1.5 - p.y - 2.0 + 1.75*f, 0.0, 1.0 );
}
float map3( in float3 p )
{
float3 q = p - float3(0.0,0.1,1.0)*iTime;
float f = 0.0;
f = 0.50000*noise( q ); q = q*2.02;
f += 0.25000*noise( q ); q = q*2.03;
f += 0.12500*noise( q );
return clamp( 1.5 - p.y - 2.0 + 1.75*f, 0.0, 1.0 );
}
float map2( in float3 p )
{
float3 q = p - float3(0.0,0.1,1.0)*iTime;
float f = 0.0;
f = 0.50000*noise( q ); q = q*2.02;
f += 0.25000*noise( q );;
return clamp( 1.5 - p.y - 2.0 + 1.75*f, 0.0, 1.0 );
}
float4 integrate( in float4 sum, in float dif, in float den, in float3 bgcol, in float t )
{
// lighting
float3 lin = float3(0.65,0.7,0.75)*1.4 + float3(1.0, 0.6, 0.3)*dif;
float4 col = float4( lerp( float3(1.0,0.95,0.8), float3(0.25,0.3,0.35), den ), den );
col.xyz *= lin;
col.xyz = lerp( col.xyz, bgcol, 1.0-exp(-0.003*t*t) );
col.w *= 0.4;
// front to back blending
col.rgb *= col.a;
return sum + col*(1.0-sum.a);
}
#define MARCH(STEPS, MAPLOD)for(int i=0; i<STEPS; i++){ float3 pos = ro + t*rd; if( pos.y<-3.0 || pos.y>2.0 || sum.a>0.99 ) break; float den = MAPLOD( pos ); if( den>0.01 ) { float dif = clamp((den - MAPLOD(pos+0.3*sundir))/0.6, 0.0, 1.0 ); float3 lin = float3(0.65,0.7,0.75)*1.4 + float3(1.0,0.6,0.3)*dif; float4 col = float4( lerp( float3(1.0,0.95,0.8), float3(0.25,0.3,0.35), den ), den ); col.xyz *= lin; col.xyz = lerp( col.xyz, bgcol, 1.0-exp(-0.003*t*t) ); col.w *= 0.4; col.rgb *= col.a; sum += col*(1.0-sum.a); } t += max(0.05,0.02*t);}
float4 raymarch( in float3 ro, in float3 rd, in float3 bgcol, in int2 px )
{
float4 sum = float4(0.0, 0.0, 0.0, 0.0);
float t = 0.0;//0.05*texelFetch( iChannel0, px&255, 0 ).x;
MARCH(30,map5);
MARCH(30,map4);
MARCH(30,map3);
MARCH(30,map2);
return clamp( sum, float4(0.0,0.0,0.0,0.0), float4(1.0, 1.0, 1.0, 1.0) );
}
float3x3 setCamera( in float3 ro, in float3 ta, float cr )
{
float3 cw = normalize(ta-ro);
float3 cp = float3(sin(cr), cos(cr),0.0);
float3 cu = normalize( cross(cw,cp) );
float3 cv = normalize( cross(cu,cw) );
return float3x3( cu, cv, cw );
}
float4 render( in float3 ro, in float3 rd, in int2 px )
{
// background sky
float sun = clamp( dot(sundir,rd), 0.0, 1.0 );
float3 col = float3(0.6,0.71,0.75) - rd.y*0.2*float3(1.0,0.5,1.0) + 0.15*0.5;
col += 0.2*float3(1.0,.6,0.1)*pow( sun, 8.0 );
// clouds
float4 res = raymarch( ro, rd, col, px );
col = col*(1.0-res.w) + res.xyz;
// sun glare
col += 0.2*float3(1.0,0.4,0.2)*pow( sun, 3.0 );
return float4( col, 1.0 );
}
};
Functions func;
float2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
float2 m = float2(0.0,0.0);// = (iMouse.xy/iResolution.xy;
// camera
float3 ro = 4.0*normalize(float3(sin(3.0*m.x), 0.4*m.y, cos(3.0*m.x)));
float3 ta = float3(0.0, -1.0, 0.0);
float3x3 ca = func.setCamera( ro, ta, 0.0 );
// ray
float3 rd = mul(ca, normalize( float3(p.xy,1.5)));
return func.render( ro, rd, int2(fragCoord-0.5) );
@ufna
Copy link
Author

ufna commented Jan 29, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment