-
-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathObjectCloud.js
More file actions
60 lines (46 loc) · 1.24 KB
/
ObjectCloud.js
File metadata and controls
60 lines (46 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import {
BoxGeometry,
ConeGeometry,
Group,
Mesh,
MeshPhongMaterial,
OctahedronGeometry,
SphereGeometry
} from "three";
/**
* Creates an object cloud.
*
* @param {Number} [amount=100] - The amount of spheres.
* @param {Number} [range=10.0] - The spread range.
* @return {Group} The sphere cloud.
*/
export function create(amount = 30, range = 10.0) {
const group = new Group();
const PI2 = 2 * Math.PI;
const geometries = [
new BoxGeometry(1, 1, 1),
new ConeGeometry(1, 1, 16),
new OctahedronGeometry(),
new SphereGeometry(1, 16, 16)
];
for(let i = 0, j = 0, l = geometries.length; i < amount; ++i, j = (j + 1) % l) {
const material = new MeshPhongMaterial({
color: 0xffffff * Math.random()
});
const mesh = new Mesh(geometries[j], material);
mesh.rotation.set(Math.random() * PI2, Math.random() * PI2, Math.random() * PI2);
mesh.scale.multiplyScalar(Math.random() + 0.75);
const phi = Math.random() * PI2;
const cosTheta = Math.random() * 2.0 - 1.0;
const u = Math.random();
const theta = Math.acos(cosTheta);
const r = Math.cbrt(u) * range;
mesh.position.set(
r * Math.sin(theta) * Math.cos(phi),
r * Math.sin(theta) * Math.sin(phi),
r * Math.cos(theta)
);
group.add(mesh);
}
return group;
}