Skip to content

Instantly share code, notes, and snippets.

@scriptum
Last active December 26, 2015 04:19
Show Gist options
  • Save scriptum/7091627 to your computer and use it in GitHub Desktop.
Save scriptum/7091627 to your computer and use it in GitHub Desktop.
Extremely packed 1k demo
from OpenGL.GL import*;from OpenGL.GLUT import*;from OpenGL.GL.shaders import*;from time import*;t=time();glutInit();glutInitDisplayMode(2);glutInitWindowSize(640,480);glutCreateWindow('');glOrtho(0,1,0,1,0,1);glMatrixMode(5888);p=compileProgram(compileShader('<SHADER>',35632));
def d():glUseProgram(p);glUniform1f(0,time()-t);glBegin(7);glVertex2i(0,1);glVertex2i(1,1);glVertex2i(1,0);glVertex2i(0,0);glEnd();glutSwapBuffers()
glutIdleFunc(d);glutMainLoop()
TMPSHADER=.shader.tmp
TMPDEMO=.demo.tmp
tr -d "\n\t" < shader.frag > $TMPSHADER
perl -ne 's/<SHADER>/`cat '$TMPSHADER'`/e;print' demo.py > .pydemo.tmp
gzip < .pydemo.tmp > $TMPDEMO
cat unpacker.sh $TMPDEMO > demo
chmod +x demo
wc -c demo
rm -f $TMPDEMO $TMPSHADER
from OpenGL.GL import*
from OpenGL.GLUT import*
from OpenGL.GL.shaders import*
from time import*
t=time()
glutInit()
glutInitDisplayMode(GLUT_DOUBLE)
glutInitWindowSize(640,480)
glutCreateWindow("")
glOrtho(0,1,0,1,0,1)
glMatrixMode(GL_MODELVIEW)
p=compileProgram(compileShader('''<YOUR SHADER HERE>''',GL_FRAGMENT_SHADER))
def d():
glUseProgram(p)
glUniform1f(0,time()-t)
glBegin(GL_QUADS)
glVertex2f(0,1)
glVertex2f(1,1)
glVertex2f(1,0)
glVertex2f(0,0)
glEnd()
glutSwapBuffers()
glutIdleFunc(d)
glutMainLoop()
main()
uniform float t;
const int Iterations=14;
const float detail=.00002;
const float Scale=2.;
vec3 lightdir=normalize(vec3(0.,-0.3,-1.));
vec3 ambdir=normalize(vec3(0.,-1.,0.5));
float ot=0.;
float det=0.;
float hitfloor;
float de(vec3 pos) {
hitfloor=0.;
vec3 p=pos;
p.xz=abs(.5-mod(pos.xz,1.))+.01;
float DEfactor=1.;
ot=1000.;
for (int i=0; i<Iterations; i++) {
p = abs(p)-vec3(0.,2.,0.);
float r2 = dot(p, p);
ot = min(ot,abs(length(p)));
float sc=Scale/clamp(r2,0.4,1.);
p*=sc;
DEfactor*=sc;
p = p - vec3(0.5,1.,0.5);
}
float fl=pos.y-3.013;
float d=min(fl,length(p)/DEfactor-.0005);
d=min(d,-pos.y+3.9);
if (abs(d-fl)<.0001) hitfloor=1.;
return d;
}
vec3 normal(vec3 p) {
vec3 e = vec3(0.0,det,0.0);
return normalize(vec3(
de(p+e.yxx)-de(p-e.yxx),
de(p+e.xyx)-de(p-e.xyx),
de(p+e.xxy)-de(p-e.xxy)
)
);
}
float shadow(vec3 pos, vec3 sdir) {
float totalDist =2.0*det, sh=1.;
for (int steps=0; steps<30; steps++) {
if (totalDist<1.) {
vec3 p = pos - totalDist * sdir;
float dist = de(p)*1.5;
if (dist < detail) sh=0.;
totalDist += max(0.05,dist);
}
}
return max(0.,sh);
}
float calcAO( const vec3 pos, const vec3 nor ) {
float aodet=detail*80.;
float totao = 0.0;
float sca = 10.0;
for( int aoi=0; aoi<5; aoi++ ) {
float hr = aodet + aodet*float(aoi*aoi);
vec3 aopos = nor * hr + pos;
float dd = de( aopos );
totao += -(dd-hr)*sca;
sca *= 0.75;
}
return clamp( 1.0 - 5.0*totao, 0.0, 1.0 );
}
float kset(vec3 p) {
p=abs(.5-fract(p*20.));
float es, l=es=0.;
for (int i=0;i<13;i++) {
float pl=l;
l=length(p);
p=abs(p)/dot(p,p)-.5;
es+=exp(-1./abs(l-pl));
}
return es;
}
vec3 light(in vec3 p, in vec3 dir) {
float hf=hitfloor;
vec3 n=normal(p);
float sh=min(1.,shadow(p, lightdir)+hf);
//float sh=1.;
float ao=calcAO(p,n);
float diff=max(0.,dot(lightdir,-n))*sh*1.3;
float amb=max(0.4,dot(normalize(ambdir),-n))*.55;
vec3 r = reflect(lightdir,n);
float spec=pow(max(0.,dot(dir,-r))*sh,10.)*(.5+ao*.5);
float k=kset(p)*.18;
vec3 col=mix(vec3(k*1.1,k*k*1.3,k*k*k),vec3(k),.45)*2.;
col=col*ao*(amb*vec3(.9,.85,1.)+diff*vec3(1.,.9,.9))+spec*vec3(1,.9,.5)*.7;
return col;
}
vec3 raymarch(in vec3 from, in vec3 dir)
{
vec2 lig=vec2(sin(t*2.)*.6,cos(t)*.25-.25);
float fog,glow,d=1., totdist=glow=fog=0.;
vec3 p, col=vec3(0.);
float ref=0.;
float steps;
for (int i=0; i<130; i++) {
if (d>det && totdist<3.5) {
p=from+totdist*dir;
d=de(p);
det=detail*(1.+totdist*55.);
totdist+=d;
glow+=max(0.,.02-d)*exp(-totdist);
steps++;
}
}
//glow/=steps;
float l=pow(max(0.,dot(normalize(-dir),normalize(lightdir))),10.);
vec3 backg=vec3(.8,.85,1.)*.25*(2.-l)+vec3(1.,.9,.65)*l*.4;
float hf=hitfloor;
if (d<det) {
col=light(p-det*dir*1.5, dir);
if (hf>0.5) col*=vec3(1.,.85,.8)*.6;
col*=min(1.2,.5+totdist*totdist*1.5);
col = mix(col, backg, 1.0-exp(-1.3*pow(totdist,1.3)));
} else {
col=backg;
}
col+=glow*vec3(1.,.9,.8)*.34;
col+=vec3(1,.8,.6)*pow(l,3.)*.5;
return col;
}
void main(void)
{
vec2 uv = gl_FragCoord.xy / vec2(640.,480.)*2.-1.;
float y=cos(t*.1)+1.;
uv+=vec2(sin(t*.3),cos(t*0.15)+.3)*.15*(.5+y)*min(1.,t*.1)*1.5;
uv.y-=.1;
vec3 from=vec3(0.0,3.04+y*.1,-2.+t*.05);
vec3 dir=normalize(vec3(uv*.85,1.));
vec3 color=raymarch(from,dir);
color*=vec3(1.,.94,.87);
color=pow(color,vec3(1.2));
color=mix(vec3(length(color)),color,.85)*.95;
color+=vec3(1,.85,.7)*pow(max(0.,.3-length(uv-vec2(0.,.03)))/.3,1.5)*.65;
gl_FragColor = vec4(color,1.);
}
uniform float t;
vec3 L=-normalize(vec3(0,1,3));
float H;
float de(vec3 y){
H=0;
vec3 p=y;
p.xz=abs(.5-mod(y.xz,1))+.01;
float f=1,o=1000,l,d,r,s;
for(int i=0;i<14;i++){
p=abs(p)-vec3(0,2,0);
r=dot(p,p);
o=min(o,abs(length(p)));
s=2/clamp(r,0.5,1);
p*=s;
f*=s;
p-=vec3(.5,1,.5);
}
l=y.y-3.013,d=min(l,length(p)/f-5e-4);
d=min(d,-y.y+4);
if(abs(d-l)<1e-4)H=1.;
return d;
}
void main()
{
vec2 u=gl_FragCoord.xy/vec2(640,480)*2-1;
float y=cos(t*.1)+1.,w,d=1.,b=w=0.,m=0.,l,x,s;
u+=vec2(sin(t*.3),cos(t*0.2));
u.y-=.1;
vec3 f=vec3(0,3.04+y*.1,-2+t*.05),o=normalize(vec3(u*.8,1)),p,c,j,n;
for(int i=0;i<130;i++){
if(d>m&&b<3.5){
p=f+b*o;
d=de(p);
m=2e-5*(1+b*50);
b+=d;
w+=max(0,.02-d)*exp(-b);
}
}
l=pow(max(0,dot(-o,L)),20);
j=.6+l*.1;
if(d<m){
p=p-m*o;
f=vec3(0,m,0);
n=normalize(vec3(de(p+f.yxx)-de(p-f.yxx),de(p+f.xyx)-de(p-f.xyx),de(p+f.xxy)-de(p-f.xxy)));
y=2*m;
d=1;
for(int i=0;i<30;i++){
if(y<1){
m=de(p-y*L)*1.5;
if(m<2e-5)d=0;
y+=max(.05,m);
}
}
d=min(1,max(0,d)+H);
x=0;
s=10;
for(int i=0;i<5;i++){
y=2e-5*(1+float(i*i))*80;
x+=(y-de(n*y+p))*s;
s*=0.8;
}
p=abs(.5-fract(p*20));
y=m=0;
for(int i=0;i<10;i++){
s=y;
y=length(p);
p=abs(p)/dot(p,p)-.5;
m+=exp(-1/abs(y-s));
}
s=clamp(1-5*x,0,1);
x=m*.2;
c=mix(vec3(x,x*x,x*x*x),vec3(x),.5);
c*=max(.5,dot(normalize(vec3(0,-2,1)),-n))+max(0,dot(L,-n))*d;
c+=max(0,dot(o,-reflect(L,n)))*d;
c*=min(1,.5+b*b)*s;
c=mix(c,j,1-exp(-b));
}else c=j;
c+=(w+l)*.5;
gl_FragColor.xyz=c*vec3(1,.9,.8);
}
tail -n+2 $0|zcat|python;exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment