Skip to content

Instantly share code, notes, and snippets.

@ufna
Created January 24, 2020 05:57
Show Gist options
  • Save ufna/7367f96b88f50d1696f77a8935369f2a to your computer and use it in GitHub Desktop.
Save ufna/7367f96b88f50d1696f77a8935369f2a to your computer and use it in GitHub Desktop.
Texture variation for unreal engine 4
#define USEHASH
struct Functions
{
float4 hash4( float2 p ) { return frac(sin(float4( 1.0+dot(p,float2(37.0,17.0)),
2.0+dot(p,float2(11.0,47.0)),
3.0+dot(p,float2(41.0,29.0)),
4.0+dot(p,float2(23.0,31.0))))*103.0); }
float4 textureNoTile(Texture2D iChannel0, SamplerState iChannel0Sampler, in float2 uv, Texture2D iChannel1, SamplerState iChannel1Sampler)
{
float2 iuv = floor( uv );
float2 fuv = frac( uv );
#ifdef USEHASH
// generate per-tile transform (needs GL_NEAREST_MIPMAP_LINEARto work right)
float4 ofa = Texture2DSample( iChannel1, iChannel1Sampler, (iuv + float2(0.5,0.5))/256.0 );
float4 ofb = Texture2DSample( iChannel1, iChannel1Sampler, (iuv + float2(1.5,0.5))/256.0 );
float4 ofc = Texture2DSample( iChannel1, iChannel1Sampler, (iuv + float2(0.5,1.5))/256.0 );
float4 ofd = Texture2DSample( iChannel1, iChannel1Sampler, (iuv + float2(1.5,1.5))/256.0 );
#else
// generate per-tile transform
float4 ofa = hash4( iuv + float2(0.0,0.0) );
float4 ofb = hash4( iuv + float2(1.0,0.0) );
float4 ofc = hash4( iuv + float2(0.0,1.0) );
float4 ofd = hash4( iuv + float2(1.0,1.0) );
#endif
float2 ddx = DDX( uv );
float2 ddy = DDY( uv );
// transform per-tile uvs
ofa.zw = sign(ofa.zw-0.5);
ofb.zw = sign(ofb.zw-0.5);
ofc.zw = sign(ofc.zw-0.5);
ofd.zw = sign(ofd.zw-0.5);
// uv's, and derivarives (for correct mipmapping)
float2 uva = uv*ofa.zw + ofa.xy; float2 ddxa = ddx*ofa.zw; float2 ddya = ddy*ofa.zw;
float2 uvb = uv*ofb.zw + ofb.xy; float2 ddxb = ddx*ofb.zw; float2 ddyb = ddy*ofb.zw;
float2 uvc = uv*ofc.zw + ofc.xy; float2 ddxc = ddx*ofc.zw; float2 ddyc = ddy*ofc.zw;
float2 uvd = uv*ofd.zw + ofd.xy; float2 ddxd = ddx*ofd.zw; float2 ddyd = ddy*ofd.zw;
// fetch and blend
float2 b = smoothstep(0.25,0.75,fuv);
return lerp( lerp( iChannel0.SampleGrad( iChannel0Sampler, uva, ddxa, ddya ),
iChannel0.SampleGrad( iChannel0Sampler, uvb, ddxb, ddyb ), b.x ),
lerp( iChannel0.SampleGrad( iChannel0Sampler, uvc, ddxc, ddyc ),
iChannel0.SampleGrad( iChannel0Sampler, uvd, ddxd, ddyd ), b.x), b.y );
}
};
Functions func;
//float2 uv = fragCoord / iResolution.x;
float f = smoothstep( 0.4, 0.6, sin(iTime ) );
float s = smoothstep( 0.4, 0.6, sin(iTime*0.5) );
uv = 16 * uv;//(4.0 + 16.0*s)*uv + iTime*0.1;
float3 cola = func.textureNoTile( iChannel0, iChannel0Sampler, uv, iChannel1, iChannel1Sampler).xyz;
float3 colb = Texture2DSample( iChannel0, iChannel0Sampler, uv ).xyz;
float3 col = lerp( cola, colb, f );
return float4( cola, 1.0 );
@ufna
Copy link
Author

ufna commented Jan 24, 2020

image

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