xxxxxxxxxx
213
let serial;
let portName = '/dev/tty.usbmodem13201';
let inData;
function setup() {
createCanvas(400, 400, WEBGL);
serial = new p5.SerialPort(); // make a new instance of the serialport library
serial.on('list', printList); // set a callback function for the serialport list event
serial.on('connected', serverConnected); // callback for connecting to the server
serial.on('open', portOpen); // callback for the port opening
serial.on('data', serialEvent); // callback for when new data arrives
serial.on('error', serialError); // callback for errors
serial.on('close', portClose); // callback for the port closing
serial.list(); // list the serial ports
serial.open(portName); // open a serial port
}
// get the list of ports:
function printList(portList) {
// portList is an array of serial port names
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
console.log(i + portList[i]);
}
}
function serverConnected() {
console.log('connected to server.');
}
function portOpen() {
console.log('the serial port opened.')
}
function serialEvent() {
inData = serial.read();
}
function serialError(err) {
console.log('Something went wrong with the serial port. ' + err);
}
function portClose() {
console.log('The serial port closed.');
}
let shape = 0;
let rand = 0;
let z = 5;
let t = 45;
zt = 45;
let speed = .0005;
let rf = 0; let rf_color = 0;
let gf = 0; let gf_color = 0;
let bf = 0; let bf_color = 0;
let rs = 255; let rs_color = 255;
let gs = 255; let gs_color = 255;
let bs = 255; let bs_color = 255;
let bcr = 220; let bcr_color = 220;
let bcg = 220; let bcg_color = 220;
let bcb = 220; let bcb_color = 220;
let s = 2; let s_width = 2;
let num_shapes = 1;
let slow_down = 0;
let texture = 1;
let rotateZfast = false;
function draw() {
background(bcr_color, bcg_color, bcb_color);
//console.log("sensor value: " + inData);
rectMode(CENTER);
fill(rf_color, gf_color, bf_color);
strokeWeight(s_width/num_shapes);
stroke(rs_color, gs_color, bs_color);
if (texture%2 == 0) {
normalMaterial()
} else if (texture%3 == 0) {
ambientLight(60, 60, 60);
pointLight(255, 255, 255, t*5, t, t);
pointLight(255, 255, 255, t/2, t/3, width - width/.2);
specularMaterial(250);
shininess(120);
}
translate(-1 * (width/2), -1 * (height/2));
for (var y = 1; y <= num_shapes; y++) {
for (var x = 1; x <= num_shapes; x++) {
push();
translate(width/(num_shapes+1) * x + (rand * random(-5, 5)) , width/(num_shapes+1) * y + (rand * random(-5, 5)), 10 * z);
rotateZ(t-.2)
rotateX(t +.2)
rotateY(t +.4)
if (rotateZfast) {
rotateX(zt+1)
}
let boxScale = 100 - 20 * num_shapes + 10;
if(shape%4 == 0) {
box(boxScale); //* sin(t/10));
} else if (shape%3 == 0) {
sphere(boxScale/1.5)
} else if (shape%2 == 0) {
torus(boxScale/1.5, boxScale/4);
} else {
cone(boxScale/2, boxScale*1.5)
}
pop();
}
}
t += speed;
if(keyIsDown(87) || inData == 1) {
t += speed * random(.1, 300)
}
if(keyIsDown(69) || inData == 2) {
slow_down++;
if(slow_down%10 == 0) {
shape += 1;
}
}
if(keyIsDown(65) || inData == 3) {
rf += .1;
rf_color += map(sin(rf), -1, 1, 0, 255)
gf += .1;
gf_color = map(cos(gf), -1, 1, 0, 255)
bf += .2;
bf_color = map(cos(bf), -1, 1, 0, 255)
}
if(keyIsDown(83) || inData == 4) {
rs -= .05;
rs_color += map(sin(rs), -1, 1, 0, 255)
gs += .1;
gs_color = map(sin(gs), -1, 1, 0, 255)
bs -= .1;
bs_color = map(sin(bs), -1, 1, 0, 255)
s += .1
s_width = map(sin(s), -1, 1, .1, 5)
}
if(keyIsDown(68) || inData == 5) {
bcr += .01;
bcr_color += map(sin(bcr), -1, 1, 200, 255)
bcg += .02;
bcg_color = map(cos(bcg), -1, 1, 0, 255)
bcb += .03;
bcb_color = map(sin(bcb), -1, 1, 0, 100)
}
if(keyIsDown(81) || inData == 6) {
slow_down++;
if(slow_down%10 == 0) {
num_shapes++;
if (num_shapes == 6) {
num_shapes = 1;
}
}
}
if(keyIsDown(90) || inData == 7) {
slow_down++;
if(slow_down%10 == 0) {
texture++;
}
}
if(keyIsDown(88) || inData == 8) {
rotateZfast = true
zt += .5;
} else {
rotateZfast = false
}
if(keyIsDown(67) || inData == 9) {
rand = random(.1, 5)
} else {
rand = 0;
}
}