Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
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.
*/
const VIDEO_WIDTH = 540
const VIDEO_HEIGHT = 450
const canvas = document.body.appendChild(document.createElement('canvas'))
canvas.width = VIDEO_WIDTH
canvas.height = VIDEO_HEIGHT
const FBO_WIDTH = 4 * VIDEO_WIDTH
const FBO_HEIGHT = 4 * VIDEO_HEIGHT
// load all modules here:
const regl = require('regl')({canvas: canvas})
//const regl = require('regl')(require('gl')(VIDEO_WIDTH, VIDEO_HEIGHT, {preserveDrawingBuffer: true}))
//var recorder = require('regl-recorder')(regl, 156)
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_WIDTH, FBO_HEIGHT)
// 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 cube texture.
var RES = 256
for (var y = 0; y < RES; ++y) {
for (var x = 0; x < RES; ++x) {
var ind = 4 * (y * RES + x)
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)))
var r, g, b
// yellow
var yr = 220
var yg = 220
var yb = 60
// blue
var br = 120
var bg = 120
var bb = 250
if (k > borderWidth) {
r = yr
g = yg
b = yb
} else {
r = br
g = bg
b = bb
}
texData[ind + 0] = r
texData[ind + 1] = g
texData[ind + 2] = b
texData[ind + 3] = 255
}
}
const globalScope = regl({
uniforms: {
tick: ({tick}) => { return tick },
view: ({tick}) => {
var s = 1.2
var eye = [40.0*s, 30.0*s, 10*s]
var origin = [0, 0, 0]
return mat4.lookAt([],
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 float tick;
uniform sampler2D tex;
varying vec2 vUv;
varying vec3 vNormal;
void main () {
vec3 c = texture2D(tex, vUv.xy).rgb;
vec3 l = normalize(vec3(-0.3, 1.0, 0.3));
vec3 n = vNormal;
gl_FragColor = vec4(
c * 0.5 + c * 0.5 * dot(n, l)
, 1.0);
}`,
uniforms: {
tex: regl.texture({
width: RES,
height: RES,
min: 'linear mipmap linear',
mag: 'linear',
wrap: 'repeat',
data: texData
})
},
vert: `
precision mediump float;
attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;
varying vec2 vUv;
varying vec3 vPosition;
varying vec3 vNormal;
uniform mat4 model, normalMatrix, projection, view;
void main() {
vec4 worldSpacePos = model * vec4(position , 1);
vec4 p = projection * view * worldSpacePos;
vPosition = worldSpacePos.xyz;
vNormal = normalize((model * vec4(normal, 0.0)).xyz);
vUv = uv;
gl_Position = p;
}`,
framebuffer: fbo
})
const downsample = regl({
frag: `
precision mediump float;
varying vec2 uv;
uniform sampler2D tex;
uniform float wRcp, hRcp;
#define DX 1.0 / float(${FBO_WIDTH})
#define DY 1.0 / float(${FBO_HEIGHT})
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) * DX, float(y) * DY)).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))
}
},
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)
// this array keeps track of the rotation angles for the cubes.
var N = 12
var phis = []
for(var i = 0; i < 2*N*2*N; i++) {
phis[i] = 0.0
}
function getPhi(x, z) {
return phis[2*N*x + z]
}
function setPhi(x, z, phi) {
phis[2*N*x + z] = phi
}
var counter = 0.0
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 = 8.5
// rotation step
// var M =
var pi = Math.PI *0.5
for(var x = -N; x <= +N; x++) {
for(var z = -N; z <= +N; z++) {
// get rotation angle for cube.
var phi = getPhi(x+N,z+N)
var start = false
var a = x+N
var b = z+N
// 1st flip these cubes.
if(a%4 == 0 && b%4 == 0 ) {
start = true
}
// 2nd flip these cubes.
// and so on for the rest.
if(a%3 == 0 && b%3 == 0 && counter > 15) {
start = true
}
if(a%2 == 0 && b%2 == 0 && counter > 30) {
start = true
}
if((a+b)%2==0 && counter > 5*(-x+N)) {
start = true
}
if((a+b)%2==1 && counter > 7*(-x+N)) {
start = true
}
if(start) {
if(phi >= pi*1.0) {
// already rotated enough, so clamp.
} else {
phi += (pi * 1.0) / 70
}
}
boxMesh.draw({scale: S, translate: [0 + x * -S, 0.0, z * S], rotation: [0.0, 0.0, -phi]})
// keep track of rotation angle for cube.
setPhi(x+N,z+N, phi)
}
}
})
// SSAA downsample
downsample()
})
counter += 1
if(counter > 311) {
// start animation from beginning.
counter = 0
// RESET PHIs
for(var i = 0; i < 2*N*2*N; i++) {
phis[i] = 0.0
}
}
// recorder.frame(viewportWidth, viewportHeight)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment