xxxxxxxxxx
86
let capture;
let R = 0, G = 0, B = 0;
function setup() {
createCanvas(640, 480, WEBGL);
capture = createCapture(VIDEO);
capture.size(width, height);
capture.hide();
frameRate(30);
}
function draw() {
background(0);
orbitControl();
let centerColor = capture.get(width/2, height/2);
R = lerp(R, red(centerColor), 0.1);
G = lerp(G, green(centerColor), 0.1);
B = lerp(B, blue(centerColor), 0.1);
// 비디오 표시
push();
translate(-width/2, -height/2, 0);
image(capture, 0, 0, width/4, height/4);
pop();
// 현재 색상 표시
push();
translate(-width/2 + width/8, -height/2 + height/8, 0);
fill(R, G, B);
noStroke();
circle(0, 0, 20);
pop();
drawLEDs();
}
function drawLEDs() {
drawColorLEDs(R, -width/4, height/6, color(255, 0, 0));
drawColorLEDs(G, 0, height/6, color(0, 255, 0));
drawColorLEDs(B, width/4, height/6, color(0, 0, 255));
}
function drawColorLEDs(colorValue, x, y, ledColor) {
let ledCount = 15;
let ledSize = 15;
let spacing = 25;
let rows = 2;
let cols = 5;
let totalWidth = cols * spacing;
let totalHeight = rows * spacing;
let litLEDs = Math.round((colorValue / 255) * ledCount);
push();
translate(x, y, 0);
for (let i = 0; i < ledCount; i++) {
let row = Math.floor(i / cols);
let col = i % cols;
let ledX = -totalWidth / 2 + col * spacing;
let ledY = -totalHeight / 2 + row * spacing;
push();
translate(ledX, ledY, 0);
if (i < litLEDs) {
fill(ledColor);
emissiveMaterial(ledColor);
} else {
fill(50);
emissiveMaterial(50);
}
noStroke();
sphere(ledSize / 2);
pop();
}
pop();
}