0

I need some help making a sphere out of random particles in three.js.

I know how to make different shapes out of particles, but not how to make them randomly.

This is what I currently have,

// point cloud geometry
var geometry = new THREE.SphereGeometry(100, 100, 100);

material = new THREE.PointCloudMaterial({
    size: 1,
    transparent: true,
    opacity: 0.5,
    color: 0xffffff
});

// point cloud
var pointCloud = new THREE.PointCloud(geometry, material);

//add to scene
scene.add( pointCloud );

thanks

WestLangley
  • 102,557
  • 10
  • 276
  • 276
user5134207
  • 1
  • 1
  • 3
  • See http://stackoverflow.com/questions/31289577/three-js-position-particles-evenly-on-objects-faces-rather-than-verticies – WestLangley Jul 20 '15 at 16:33

1 Answers1

0

EDIT after the comments

This does the trick: it generates two random angles, then uses them to translate from polar (spherical) coordinates to cartesian coordinates with a fixed distance (that is equal to the radius of the sphere)

        var distance = 100;    
        var geometry = new THREE.Geometry();

        for (var i = 0; i < 1000; i++) {

            var vertex = new THREE.Vector3();

            var theta = THREE.Math.randFloatSpread(360); 
            var phi = THREE.Math.randFloatSpread(360); 

            vertex.x = distance * Math.sin(theta) * Math.cos(phi);
            vertex.y = distance * Math.sin(theta) * Math.sin(phi);
            vertex.z = distance * Math.cos(theta);

            geometry.vertices.push(vertex);
        }
        var particles = new THREE.PointCloud(geometry, new THREE.PointCloudMaterial({color: 0xffffff}));
        particles.boundingSphere = 50;

        scene.add(particles);

(Working example)

Evil Toad
  • 3,053
  • 2
  • 19
  • 28
  • Sorry, maybe I misunderstood the question... your problem is not the "random" part alone, is it? Do you want particles randomly scattered across a spherical surface? or inside the volume of the sphere? – Evil Toad Jul 20 '15 at 10:03
  • Mostly the random part. I want them to be across the surface, so that it is a hollow sphere – user5134207 Jul 20 '15 at 10:59
  • user5134207, did you try it? – Evil Toad Jul 29 '15 at 13:23
  • How can I convert it for an sphere with volume? – Giancarlo Ventura Granados Dec 08 '15 at 23:43
  • @Giancarlo Ventura Granados make also the distance random in any cycle of the loop with something like `var distance = THREE.Math.randFloatSpread(100)`. Then you may want to adjust the distribution to make the particle look like they are uniformely distributed, but this [example works as a basis](http://jsfiddle.net/eviltoad/sf6hdpar/). – Evil Toad Dec 09 '15 at 15:47