如何在three.js中用随机粒子制作一个球体 [英] How to make a sphere made out of random particles in three.js

查看:67
本文介绍了如何在three.js中用随机粒子制作一个球体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助,在 Three.js 中用随机粒子制作一个球体.

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.

这是我目前拥有的,

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

谢谢

推荐答案

评论后编辑

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

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

(工作示例)

这篇关于如何在three.js中用随机粒子制作一个球体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆