xxxxxxxxxx
114
let img;
let ring;
function getSquareTexture( w, ratio, rows_per_side )
{
let total = 1;
let c = 2 * ratio;
for( let idx = 0; idx < rows_per_side; ++idx ) {
total += c;
c *= ratio;
}
let a = w / total;
const ret = createGraphics( w, w, P2D );
ret.background( 255, 220, 0 );
ret.fill( 0 );
ret.noStroke();
function row( y, th ) {
const dia = th * 0.95;
const count = int(w / dia);
const sp = (w-count*dia) / count;
for( let x = dia/2 + sp/2; x <= w; x += (dia+sp) ) {
ret.ellipse( x, y, dia, dia );
}
}
row( w/2, a );
let d = a/2;
for( let idx = 0; idx < rows_per_side; ++idx ) {
a *= ratio;
d += a/2;
row( w/2 - d, a );
row( w/2 + d, a );
d += a/2;
}
return ret;
}
function setup() {
createCanvas(512, 512, WEBGL );
img = getSquareTexture( 800, 0.6, 4 );
ring = getModel();
}
function getRing( ang, maj, min )
{
const ret = [];
const v = [cos(ang), sin(ang), 0];
const w = [0,0,1];
const o = [maj*v[0], maj*v[1], maj*v[2]];
for( let idx = 0; idx < 12; ++idx ) {
const theta = TWO_PI * idx / 12;
const c = min*cos( theta );
const s = min*sin( theta );
ret.push( [o[0]+c*v[0]+s*w[0], o[1]+c*v[1]+s*w[1], o[2]+c*v[2]+s*w[2]] );
}
return ret;
}
function getModel()
{
const ret = [];
let b1 = 0;
for( let idx = 0; idx < 48; ++idx ) {
const r1 = getRing( TWO_PI*idx/48, 160, 50 );
const r2 = getRing( TWO_PI*(idx+1)/48, 160, 50 );
r1.push( r1[0] );
r2.push( r2[0] );
let b2 = 0;
for( let jdx = 1; jdx < r1.length; ++jdx ) {
ret.push( [r1[jdx-1][0], r1[jdx-1][1], r1[jdx-1][2], b1, b2] );
ret.push( [r2[jdx-1][0], r2[jdx-1][1], r2[jdx-1][2], b1 + 0.5, b2] );
ret.push( [r2[jdx][0], r2[jdx][1], r2[jdx][2], b1 + 0.5, b2 + 0.5] );
ret.push( [r1[jdx][0], r1[jdx][1], r1[jdx][2], b1, b2 + 0.5] );
b2 = 0.5 - b2;
}
b1 = 0.5 - b1;
}
return ret;
}
function draw() {
background( 0 );
let ang = TWO_PI * (frameCount%200) / 200.0;
camera( 150, 0, 0, 149.8, 1, 0, cos(ang), 0, sin(ang) );
texture( img );
textureMode( NORMAL );
rotateZ( -ang );
for( let idx = 0; idx < ring.length; idx += 4 ) {
beginShape();
vertex( ring[idx] );
vertex( ring[idx+1] );
vertex( ring[idx+2] );
vertex( ring[idx+3] );
endShape();
}
// noLoop();
}
function keyPressed()
{
if( key == 'g' ) {
saveGif("output", 200, { units: "frames", delay: 0 });
}
}