xxxxxxxxxx
67
let n = 100;
let positions = new Float32Array(n * 2);
let velocities = new Float32Array(n * 2);
const gpu = new GPU();
function setup() {
createCanvas(400, 400);
for(let i = 0; i < n * 2; i ++)
{
let posX = random(0,width);
let posY = random(0,height);
positions[i] = posX;
positions[i+1] = posY;
velocities[i] = random(-1.1);
velocities[i+1] = random(-1,1);
}
frameRate(600);
}
function draw() {
for(let i = 0; i < n * 2; i += 2)
{
circle(positions[i], positions[i+1], 2);
}
newpositions = moveGPU(positions, n);
print(frameRate());
//print(newpositions[50]); // out[particle id][axis]
//noLoop();
}
const moveGPU = gpu.createKernel(function(particles, n) {
//let startOffset = this.thread.x%2;
//let particlex = particles[this.thread.x - startOffset];
//let particley = particles[this.thread.x - startOffset + 1];
//let c = [0,0];
//for(let i = 0; i < n ; i++){
//c[0] += pos[i][0];
//c[1] += pos[i][1];
//c += pos[i];
//}
//return c /= n;
return particles[this.thread.x];
}).setOutput([n]);
function moveCPU(pos, n)
{
let centers = [];
for(let i = 0; i < n ; i++){
let center = [0,0];
for(let j = 0; j < n ; j++){
center += pos[j];
}
center /= n
centers.push(center);
}
return centers;
}