Last active
May 21, 2017 20:21
-
-
Save Erkaman/66baeef959ef9211de779ca3d3f6d4e1 to your computer and use it in GitHub Desktop.
infinite staircase gif. From https://twitter.com/erkaman2/status/863473960486359041
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
The MIT License (MIT) | |
Copyright (c) 2017 Eric Arnebäck | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
// to build gif: | |
// install all needed npm packages, then run | |
// `node main.js` | |
// a folder of frame images will be created. | |
// now run something like: | |
// `ffmpeg -y -framerate 30 -i video-41a2fb4b7e31eb644492/frame%08d.jpg -vframes 50 -vf "vflip" -c:v libx264 -r 30 out.mp4 && gifgen -f 30 -o out.gif -s out.mp4` | |
const VIDEO_SIZE = 512 | |
//const canvas = document.body.appendChild(document.createElement('canvas')) | |
//canvas.width = VIDEO_SIZE | |
//canvas.height = VIDEO_SIZE | |
const FBO_SIZE = 4 * VIDEO_SIZE | |
// load all modules here: | |
//const regl = require('regl')({canvas: canvas}) | |
const regl = require('regl')(require('gl')(VIDEO_SIZE, VIDEO_SIZE, {preserveDrawingBuffer: true})) | |
var recorder = require('regl-recorder')(regl, 150) | |
const mat4 = require('gl-mat4') | |
const quat = require('gl-quat') | |
const vec3 = require('gl-vec3') | |
const fbo = regl.framebuffer({ | |
color: regl.texture({ | |
width: 1, | |
height: 1, | |
wrap: 'clamp', | |
min: 'nearest', | |
mag: 'nearest' | |
}), | |
depth: true | |
}) | |
fbo.resize(FBO_SIZE, FBO_SIZE) | |
// create box geometry. | |
var boxPosition = [ | |
// side faces | |
[-0.5, +0.5, +0.5], [+0.5, +0.5, +0.5], [+0.5, -0.5, +0.5], [-0.5, -0.5, +0.5], // positive z face. | |
[+0.5, +0.5, +0.5], [+0.5, +0.5, -0.5], [+0.5, -0.5, -0.5], [+0.5, -0.5, +0.5], // positive x face | |
[+0.5, +0.5, -0.5], [-0.5, +0.5, -0.5], [-0.5, -0.5, -0.5], [+0.5, -0.5, -0.5], // negative z face | |
[-0.5, +0.5, -0.5], [-0.5, +0.5, +0.5], [-0.5, -0.5, +0.5], [-0.5, -0.5, -0.5], // negative x face. | |
[-0.5, +0.5, -0.5], [+0.5, +0.5, -0.5], [+0.5, +0.5, +0.5], [-0.5, +0.5, +0.5], // top face | |
[-0.5, -0.5, -0.5], [-0.5, -0.5, +0.5], [+0.5, -0.5, +0.5], [+0.5, -0.5, -0.5] | |
] | |
const boxElements = [ | |
[2, 1, 0], [2, 0, 3], | |
[6, 5, 4], [6, 4, 7], | |
[10, 9, 8], [10, 8, 11], | |
[14, 13, 12], [14, 12, 15], | |
[18, 17, 16], [18, 16, 19], | |
[22, 21, 20], [22, 20, 23] | |
] | |
// all the normals of a single block. | |
var boxNormal = [ | |
// side faces | |
[0.0, 0.0, +1.0], [0.0, 0.0, +1.0], [0.0, 0.0, +1.0], [0.0, 0.0, +1.0], | |
[+1.0, 0.0, 0.0], [+1.0, 0.0, 0.0], [+1.0, 0.0, 0.0], [+1.0, 0.0, 0.0], | |
[0.0, 0.0, -1.0], [0.0, 0.0, -1.0], [0.0, 0.0, -1.0], [0.0, 0.0, -1.0], | |
[-1.0, 0.0, 0.0], [-1.0, 0.0, 0.0], [-1.0, 0.0, 0.0], [-1.0, 0.0, 0.0], | |
// top | |
[0.0, +1.0, 0.0], [0.0, +1.0, 0.0], [0.0, +1.0, 0.0], [0.0, +1.0, 0.0], | |
// bottom | |
[0.0, -1.0, 0.0], [0.0, -1.0, 0.0], [0.0, -1.0, 0.0], [0.0, -1.0, 0.0] | |
] | |
// all the normals of a single block. | |
var boxUv = [ | |
// side faces | |
[0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [0.0, 0.0], | |
[0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [0.0, 0.0], | |
[0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [0.0, 0.0], | |
[0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [0.0, 0.0], | |
[0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [0.0, 0.0], | |
[0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [0.0, 0.0] | |
] | |
var texData = [] | |
// make border texture. | |
var RES = 256 | |
for (var y = 0; y < RES; ++y) { | |
for (var x = 0; x < RES; ++x) { | |
var ind = 4 * (y * RES + x) | |
texData[ind + 0] = 255 | |
texData[ind + 1] = 255 | |
texData[ind + 2] = 255 | |
var a | |
var borderWidth = 0.1 | |
var uvx = x / RES | |
var uvy = y / RES | |
var k = Math.min(uvx, Math.min(uvy, Math.min(1.0 - uvx, 1.0 - uvy))) | |
if (k > borderWidth) { | |
a = 0 | |
} else { | |
a = (1.0 - k / borderWidth) * 255 | |
} | |
texData[ind + 3] = a | |
} | |
} | |
const globalScope = regl({ | |
vert: ` | |
precision mediump float; | |
attribute vec3 position; | |
attribute vec3 normal; | |
attribute vec2 uv; | |
varying vec2 vUv; | |
varying vec3 vPosition; | |
uniform mat4 model, normalMatrix, projection, view; | |
void main() { | |
vec4 worldSpacePos = model * vec4(position , 1); | |
vec4 p = projection * view * worldSpacePos; | |
vPosition = worldSpacePos.xyz; | |
vUv = uv; | |
gl_Position = p; | |
}`, | |
uniforms: { | |
tick: ({tick}) => { return tick }, | |
view: ({tick}) => { | |
var t = tick * 0.25 + 71.75 | |
/* | |
if(tick == 1) { | |
t = 72.0 | |
} | |
*/ | |
// record exactly 50 frames. | |
// start and end: | |
// please take that tick=1 from start, into acount. | |
// t = 72.0 | |
// t = 72 + 25.0 * 1.0 | |
// console.log(t) | |
var st = 1.4 | |
var eye = [20 * st, 10 * st, 10] | |
var origin = [0, 3, 10] | |
var v = 0.1 | |
var dir = [-1.0, +1.0, +0.0] | |
vec3.scaleAndAdd(eye, eye, dir, v * t) | |
vec3.scaleAndAdd(origin, origin, dir, v * t) | |
return mat4.lookAt([], | |
// [30 * Math.cos(t), 10.5, 30 * Math.sin(t)], | |
eye, | |
origin, | |
[0, 1, 0]) | |
}, | |
projection: ({viewportWidth, viewportHeight}) => | |
mat4.perspective([], | |
Math.PI / 4, | |
viewportWidth / viewportHeight, | |
0.01, | |
1000) | |
} | |
}) | |
// draw to gbuffer. | |
const drawBox = regl({ | |
frag: ` | |
precision mediump float; | |
varying vec3 vPosition; | |
uniform vec3 color; | |
uniform float tick; | |
uniform float seed; | |
uniform sampler2D tex; | |
varying vec2 vUv; | |
float rand(vec2 co){ | |
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); | |
} | |
vec3 c0 = vec3(1.0, 0.0, 0.0); | |
vec3 c1 = vec3(0.0, 1.0, 0.0); | |
vec3 c2 = vec3(0.0, 0.0, 1.0); | |
vec3 c3 = vec3(1.0, 0.0, 1.0); | |
vec3 c4 = vec3(0.0, 1.0, 1.0); | |
vec3 c5 = vec3(1.0, 1.0, 0.0); | |
#define N 3 | |
vec3 arr[N]; | |
vec3 colarr(int i) { | |
if(i == 0) { | |
return c3; | |
} else if(i == 1) { | |
return c4; | |
}else if(i == 2) { | |
return c5; | |
}/*else if(i == 3) { | |
return c3; | |
}else if(i == 4) { | |
return c4; | |
} else { | |
return c5; | |
}*/ | |
} | |
vec3 getCol() { | |
// divide by 50 so that colors loop properly, in order to make a seamless gif. | |
float i = (seed + tick/ 50.0); | |
int a = int(mod((floor(i)), float(N))); | |
int b = int(mod((ceil(i)), float(N))); | |
float l = fract(i); | |
vec3 ac = colarr(a); | |
vec3 bc = colarr(b); | |
return 0.8 * mix(ac, bc, | |
3.0 * l * l - 2.0 * l * l * l | |
); | |
} | |
void main () { | |
vec3 c; | |
c = texture2D(tex, vUv.xy).rgb * texture2D(tex, vUv.xy).a * getCol(); | |
gl_FragColor = vec4( | |
c | |
, 1.0); | |
}`, | |
uniforms: { | |
tex: regl.texture({ | |
width: RES, | |
height: RES, | |
min: 'linear mipmap linear', | |
mag: 'linear', | |
wrap: 'repeat', | |
data: texData | |
}) | |
}, | |
framebuffer: fbo | |
}) | |
const downsample = regl({ | |
frag: ` | |
precision mediump float; | |
varying vec2 uv; | |
uniform sampler2D tex; | |
uniform float wRcp, hRcp; | |
#define D 1.0 / float(${FBO_SIZE}) | |
void main() { | |
vec3 avg = vec3(0.0); | |
float W = 1.0 / 16.0; | |
for (int x = 0; x < 4; x++) { | |
for (int y = 0; y < 4; y++) { | |
avg += W * texture2D(tex, uv + vec2(float(x) * D, float(y) * D)).xyz; | |
} | |
} | |
gl_FragColor = | |
vec4(avg, 1.0); | |
}`, | |
vert: ` | |
precision mediump float; | |
attribute vec2 position; | |
varying vec2 uv; | |
void main() { | |
uv = 0.5 * (position + 1.0); | |
gl_Position = vec4(position, 0, 1); | |
}`, | |
attributes: { | |
position: [ -4, -4, 4, -4, 0, 4 ] | |
}, | |
uniforms: { | |
tex: ({count}) => fbo | |
}, | |
depth: { enable: false }, | |
count: 3 | |
}) | |
function Mesh (elements, position, normal, uv) { | |
this.elements = elements | |
this.position = position | |
this.normal = normal | |
this.uv = uv | |
} | |
// get single matrix from quaternion, translation and scale. | |
function fromRotationTranslationScale2 (out, q, v, s) { | |
mat4.identity(out) | |
var quatMat = mat4.create() | |
mat4.fromQuat(quatMat, q) | |
mat4.translate(out, out, v) | |
mat4.multiply(out, out, quatMat) | |
mat4.scale(out, out, s) | |
return out | |
} | |
Mesh.prototype.draw = regl({ | |
context: { | |
model: (_, props, batchId) => { | |
var m = mat4.identity([]) | |
var rot = quat.create() | |
quat.rotateX(rot, rot, props.rotation[0]) | |
quat.rotateY(rot, rot, props.rotation[1]) | |
quat.rotateZ(rot, rot, props.rotation[2]) | |
fromRotationTranslationScale2(m, rot, props.translate, [props.scale, props.scale, props.scale]) | |
return m | |
} | |
}, | |
uniforms: { | |
model: regl.context('model'), | |
normalMatrix: (context) => { | |
return mat4.transpose([], mat4.invert([], context.model)) | |
}, | |
color: regl.prop('color'), | |
seed: regl.prop('seed') | |
}, | |
attributes: { | |
position: regl.this('position'), | |
normal: regl.this('normal'), | |
uv: regl.this('uv') | |
}, | |
elements: regl.this('elements'), | |
cull: { | |
enable: true | |
} | |
}) | |
var boxMesh = new Mesh(boxElements, boxPosition, boxNormal, boxUv) | |
regl.frame(({tick, viewportWidth, viewportHeight}) => { | |
regl.clear({ | |
color: [0, 0, 0, 255], | |
depth: 1 | |
}) | |
globalScope(() => { | |
drawBox(() => { | |
regl.clear({ | |
color: [0, 0, 0, 255], | |
depth: 1 | |
}) | |
var S = 2.5 | |
function addBox (x, y, z, s) { | |
boxMesh.draw({scale: S, translate: [x, y, z], rotation: [0.0, 0.0, 0], seed: s}) | |
} | |
for (var x = 0; x < 60; x++) { | |
for (var z = 0; z < 20 / S; z++) { | |
addBox(0 + x * -S, 0.5 * S + x * S, 0.5 * S + z * S, x) | |
} | |
} | |
}) | |
downsample() | |
}) | |
recorder.frame(viewportWidth, viewportHeight) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment