Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Last active December 23, 2019 19:42
Show Gist options
  • Save KrabCode/a95d8a1ab759bcd03fa558fd67098606 to your computer and use it in GitHub Desktop.
Save KrabCode/a95d8a1ab759bcd03fa558fd67098606 to your computer and use it in GitHub Desktop.
PGraphics tex;
PImage src;
float t;
float size = 1920/8;
float w = size*2;
float h = sqrt(3)*size;
float xstep = w*(3/4f);
float ystep = h;
int hexCountX;
int hexCountY;
float equilateralTriangleHeight = sqrt(3)/2;
int doubleClickInterval = 60;
int lastPressed = -doubleClickInterval*2;
int frameStartedRec = -1;
int recordingDuration = 3000;
void setup() {
fullScreen(P2D);
hexCountX = floor(width/xstep+2);
hexCountY = floor(height/ystep+2);
tex = createGraphics(floor(size), floor(size), P2D);
reset();
}
void draw() {
t = map(frameCount, frameStartedRec, frameStartedRec+recordingDuration, 0, TWO_PI);
background(0);
updateTexture();
drawHexagonGrid();
if (frameStartedRec > 0 && frameCount < frameStartedRec+recordingDuration) {
saveFrame("gif/####.jpg");
}
drawTextureForDebugging();
}
void keyPressed() {
if (key == 'k') {
frameStartedRec = frameCount+1;
}
if (key == 'r') {
reset();
}
}
void mousePressed() {
if (lastPressed + doubleClickInterval > frameCount) {
lastPressed = -doubleClickInterval*2;
reset();
} else {
lastPressed = frameCount;
}
}
void reset() {
/*
PImage large = loadImage("img_1.png");
int s = floor(size);
src = large.get(large.width/2-s, large.height-s*2,s*2,s*2);
*/
src = loadImage("https://picsum.photos/" + floor(size*2) + ".jpg");
}
void drawHexagonGrid() {
for (float xi = -hexCountX/2; xi <= hexCountX/2; xi++) {
for (float yi = -hexCountY/2; yi <= hexCountY/2; yi ++) {
float x = width/2 + xi*xstep;
float y = height/2 + yi*ystep-ystep/2f;
if (xi % 2 ==0) {
y += ystep/2f;
}
pushMatrix();
translate(x, y);
drawHexagon();
popMatrix();
}
}
}
void drawHexagon() {
for (int triangleIndex = 0; triangleIndex <= 6; triangleIndex++) {
float angle0 = map(triangleIndex, 0, 6, 0, TWO_PI);
float angle1 = map(triangleIndex+1, 0, 6, 0, TWO_PI);
float x0 = size*cos(angle0);
float y0 = size*sin(angle0);
float x1 = size*cos(angle1);
float y1 = size*sin(angle1);
beginShape();
noStroke();
//strokeWeight(2);
//stroke(255);
textureMode(NORMAL);
texture(tex);
vertex(0, 0, 0.5, 1-equilateralTriangleHeight);
if (triangleIndex % 2 == 0) { // mirror the texture every second triangle
vertex(x0, y0, 0, 1);
vertex(x1, y1, 1, 1);
} else {
vertex(x0, y0, 1, 1);
vertex(x1, y1, 0, 1);
}
endShape();
}
}
void updateTexture() {
float w = tex.width;
float h = tex.height;
tex.beginDraw();
tex.colorMode(HSB, 1, 1, 1, 1);
tex.background(0);
tex.imageMode(CENTER);
tex.translate(w/2, h/2);
tex.rotate(t);
// tex.tint(.3);
tex.image(src, 0, 0);
tex.endDraw();
}
void drawTextureForDebugging() {
pushMatrix();
translate(tex.width*.6, tex.height*.6);
rectMode(CENTER);
noStroke();
fill(0);
rect(0, 0, tex.width*1.2, tex.height*1.2);
imageMode(CENTER);
image(tex, 0, 0);
noFill();
stroke(255, 0, 0);
triangle(0, -(tex.height*equilateralTriangleHeight)/2, -tex.width/2, tex.height/2, tex.width/2, tex.height/2);
popMatrix();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment