How fill a sphere with random particles in three.js

I read the excelent question about random populate the surface of a sphere with particles: How to make a sphere made out of random particles in three.js. How can I populate the total volume of an sphere with random generated particles? I try it:

var particleCount = 1800,
  particles = new THREE.Geometry(),

  pMaterial = new THREE.PointCloudMaterial({
    color: 0xFFFFFF,
    size: 20,
    map: THREE.ImageUtils.loadTexture(
      "images/particle.png"
    ),
    blending: THREE.AdditiveBlending,
    transparent: true
  });

for (var t = 0; t < particleCount; t++) {
  var angle3 = Math.random() * Math.PI * 2;
  var radius3 = Math.random() * 350 + 1;
  var pX1 = Math.cos(angle3) * radius3,
    pY1 = Math.random() * 70 - 35,
    pZ1 = Math.sin(angle3) * radius3,
    skparticle11 = new THREE.Vector3(pX1, pY1, pZ1);
  particles.vertices.push(skparticle11);
}

var particleSystem = new THREE.PointCloud(
  particles,
  pMaterial);

// add it to the scene
scene.add(particleSystem);

But I'm only get an disk. How to make an sphere filled with particles?

Upvotes: 1

Views: 1933

Answers (1)

Derte Trdelnik
Derte Trdelnik

Reputation: 2706

one angle is not enough, that is why you get a disk

create a random normal vector

var randomDirection = new THREE.Vector3(Math.random()-0.5,Math.random()-0.5,Math.random()-0.5).normalize();

create random distance as you did before

var radius3 = Math.random() * 350 + 1;

a random point in a sphere around origin will be

var randomParticle = randomDirection.multiplyScalar(radius3);

for a better distribution use some better generator than Math.random

Upvotes: 5

Related Questions