如何使一个球体由随机粒子在three.js

How to make a sphere made out of random particles in three.js

本文关键字:粒子 three js 随机 何使一      更新时间:2023-09-26

我需要一些帮助,使球体随机粒子在three.js。

我知道如何用粒子做出不同的形状,但不知道如何随机地制造它们。

这是我目前拥有的,

// 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 );

谢谢

在注释后编辑

这是一个技巧:它生成两个随机角度,然后使用它们从极(球)坐标转换为具有固定距离(等于球体半径)的笛卡尔坐标

        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);

(工作)