A sphere populated with random points whose scale is controlled by noise.. Link to editor and source files.
let numPts = 600;
let pts = [];
let zcount = 0;
let noiseScale=0.05;
///camera varibles
let oldx;
let oldy;
let rotx = 0;
let roty = 0;
let zcam = -600;
function setup(){
createCanvas(800, 800, WEBGL);
oldx = mouseX;
oldy = mouseY;
stroke(40, 166);
strokeWeight(4.0);
for(let i = 0; i < numPts; i++){
append(pts,new spherePt(530));
}
}
function draw(){
background(0);
cam();
for(let i = 0; i < pts.length; i++){
let pt = pts[i];
pt.display();
}
rotx += 0.002;
zcount += 0.1;
}
class spherePt {
constructor(rad) {
this.r = rad;
this.rotd = random(-0.01,0.01);
this.raxis = createVector(random(1),random(1),random(1));
let a=0, b=0, c=0, d=0, k=99;
while (k >= 1.0) {
a = random (-1.0, 1.0);
b = random (-1.0, 1.0);
c = random (-1.0, 1.0);
d = random (-1.0, 1.0);
k = a*a +b*b +c*c +d*d;
}
k = k / this.r;
this.p = createVector(2*(b*d+a*c)/k,2*(c*d-a*b)/k,(a*a + d*d - b*b - c*c) / k);
this.c = color(random(255),random(255),random(255));
}
display(){
push();
noStroke();
fill(this.c);
translate(this.p.x,this.p.y,this.p.z);
let sc = noise(this.p.x*noiseScale,this.p.y*noiseScale,(this.p.z+zcount) * noiseScale);
sc = map(sc, 0.3, 0.6, 0, 1);
sc = sc* 60;
sphere(sc,32,32);
pop();
//this.p = rotateAround(this.p,createVector(0,0,1),this.rotd);
}
}
function rotateAround(vect, axis, angle) {
// Make sure our axis is a unit vector
axis = p5.Vector.normalize(axis);
return p5.Vector.add(
p5.Vector.mult(vect, cos(angle)),
p5.Vector.add(
p5.Vector.mult(
p5.Vector.cross(axis, vect),
sin(angle)
),
p5.Vector.mult(
p5.Vector.mult(
axis,
p5.Vector.dot(axis, vect)
),
(1 - cos(angle))
)
)
);
}
///camera functions
function cam() {
let newx = mouseX;
let newy = mouseY;
translate(0, 0,zcam);
rotateY(rotx); rotateX(roty);
if (mouseIsPressed == true) {
rotx = rotx + (oldx-newx)/50.0;
roty = roty + (oldy-newy)/50.0;
}
oldx = newx;
oldy = newy;
}
function mouseWheel(event) {
let e = event.delta;
zcam = zcam - e*1;
}