Skip to content

Instantly share code, notes, and snippets.

@hb3p8
Created Feb 26, 2020
Embed
What would you like to do?
// based on shader by Nikos Papadopoulos, 4rknova / 2013 (https://www.shadertoy.com/view/llGSzw)
// Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
#define ENABLE_LIGHTING
#define ENABLE_SPECULAR
#define OFFSET_X 1
#define OFFSET_Y 1
#define DEPTH 5.5
vec3 texsample(const int x, const int y, in vec2 fragCoord)
{
vec2 uv = fragCoord.xy / iResolution.xy * iChannelResolution[0].xy;
uv = (uv + vec2(x, y)) / iChannelResolution[0].xy;
return texture(iChannel0, uv).xyz;
}
float luminance(vec3 c)
{
return dot(c, vec3(.2126, .7152, .0722));
}
float hash1( uint n )
{
// integer hash copied from Hugo Elias
n = (n << 13U) ^ n;
n = n * (n * n * 15731U + 789221U) + 1376312589U;
return float( n & uvec3(0x7fffffffU))/float(0x7fffffff);
}
float rand (in vec2 point)
{
uvec2 p = uvec2(point);
return hash1( p.x + 1920U*p.y + (1920U*1080U) ); //*uint(iFrame)
}
vec3 normal(in vec2 fragCoord)
{
float exponent = 100.0;
float scale = 0.4;
float u = rand(vec2(65.0, 87.0) + fragCoord * scale);
float v = rand(fragCoord * scale);
float phi = u * 2.0 * 3.141592;
float cos_theta = pow(v, 1.0 / (exponent + 1.0));
float sintheta = sqrt(1.0 - cos_theta*cos_theta);
return vec3(cos(phi) * sintheta, sin(phi) * sintheta, cos_theta);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord)
{
vec3 n = normal(fragCoord);
#ifdef ENABLE_LIGHTING
vec3 lp = vec3(iMouse.xy / iResolution.xy * iChannelResolution[0].xy, 200.);
vec3 sp = vec3(fragCoord.xy / iResolution.xy * iChannelResolution[0].xy, 0.);
vec3 c = vec3(0.9) * dot(n, normalize(lp - sp));
#ifdef ENABLE_SPECULAR
float e = 64.;
vec3 ep = vec3(iChannelResolution[0].x * .5, (iChannelResolution[0].y) * .5, 500.);
c += pow(clamp(dot(normalize(reflect(lp - sp, n)),
normalize(sp - ep)), 0., 1.), e);
#endif /* ENABLE_SPECULAR */
#else
vec3 c = n * 0.5 + 0.5;
#endif /* ENABLE_LIGHTING */
fragColor = vec4(c, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment