const dotRadius = 1;
const detail = 4;
let img;
let fpsDisplay;
let lastTime = 0;
let geom;
function preload() {
img = loadImage('https://www.paulwheeler.us/files/clooney.jpeg');
}
function setup() {
console.log('Initializing');
createCanvas(720, 400, WEBGL);
noStroke();
img.resize(width / 3, height / 2);
const dotGrid = function() {
const sliceCount = this.detailX + 1;
let dotNumber = 0;
for (let col = 0; col < img.width; col += 3) {
for (let row = 0; row < img.height; row += 3) {
let c = img.get(col, row);
let rgb_val = c[0] + c[1] + c[2];
let xOff = map(col, 0, img.width, -125, 125);
let yOff = map(row, 0, img.height, -125, 125);
let zOff = map(rgb_val, 0, 765, -50, 0);
for (let i = 0; i <= this.detailY; i++) {
const v = i / this.detailY;
const phi = PI * v - PI / 2;
const cosPhi = cos(phi);
const sinPhi = sin(phi);
for (let j = 0; j <= this.detailX; j++) {
const u = j / this.detailX;
const theta = 2 * PI * u;
const cosTheta = cos(theta);
const sinTheta = sin(theta);
const p = createVector(
xOff + dotRadius * cosPhi * sinTheta,
yOff + dotRadius * sinPhi,
zOff + dotRadius * cosPhi * cosTheta
);
this.vertices.push(p);
this.vertexNormals.push(p);
this.uvs.push(map(col, 0, img.width, 0, 1), map(row, 0, img.height, 0, 1));
}
}
let offset = dotNumber * (this.detailX + 1) * (this.detailY + 1);
let v1, v2, v3, v4;
for (let i = 0; i < this.detailY; i++) {
for (let j = 0; j < this.detailX; j++) {
v1 = i * sliceCount + j + offset;
v2 = i * sliceCount + j + 1 + offset;
v3 = (i + 1) * sliceCount + j + 1 + offset;
v4 = (i + 1) * sliceCount + j + offset;
this.faces.push([v1, v2, v4]);
this.faces.push([v4, v2, v3]);
}
}
dotNumber++;
}
}
console.log(`Dots: ${dotNumber}`);
console.log(`Vertices: ${this.vertices.length}`);
console.log(`Faces: ${this.faces.length}`);
};
geom = new p5.Geometry(detail, detail, dotGrid);
geom.gid = 'dot-grid';
fpsDisplay = createInput('0');
fpsDisplay.position(10, 10);
}
function draw() {
background(0);
orbitControl(2, 1, 0.1);
texture(img);
model(geom);
let t = millis();
fpsDisplay.value(`${1000 / (t - lastTime)}`);
lastTime = t;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>