-
Notifications
You must be signed in to change notification settings - Fork 0
/
WireframeSphere.astro
131 lines (113 loc) · 2.92 KB
/
WireframeSphere.astro
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<canvas class="background-sphere" id="canvas"></canvas>
<script>
/**
* Example: Displays a rotating wireframe sphere
**/
import ThreeJSWrapper from "three-js-wrapper";
import { ThreeJSEntity } from "three-js-wrapper";
//wireframe sphere
class WireframeSphere extends ThreeJSEntity {
create() {
const {
x = 0,
y = 0,
z = 0,
radius = 10,
segments = 16,
rings = 16,
color = 0xcc0000,
rotation_axis = "x",
} = this.params;
let material = new this.THREE.MeshBasicMaterial({
wireframe: true,
color: color,
});
let geo = new this.THREE.SphereGeometry(radius, segments, rings);
let obj3d = new this.THREE.Mesh(geo, material);
obj3d.position.z = z;
obj3d.position.y = y;
obj3d.position.x = x;
obj3d.rotation.x = 90;
this.params.rotation_axis = rotation_axis;
return obj3d;
}
update(event) {
event.target.rotation[this.params.rotation_axis] -= 0.2 / 1000;
}
}
let canvas = document.getElementById("canvas");
let wrapper = new ThreeJSWrapper(canvas);
//create entitites
//randomly position spheres
function rand_coord() {
let x = Math.random() * 18 + 1;
return (x *= Math.floor(Math.random() * 2) == 1 ? 1 : -1);
}
//check that no spheres are overlapping
//if they are, reposition them
function check_overlap(objects) {
let overlap = false;
for (let i = 0; i < objects.length; i++) {
for (let j = 0; j < objects.length; j++) {
if (i != j) {
let dist = objects[i].object3d.position.distanceTo(
objects[j].object3d.position
);
if (dist < 18) {
objects[i].object3d.position.y = rand_coord();
objects[i].object3d.position.x = rand_coord();
objects[i].object3d.position.z = z;
overlap = true;
}
}
}
}
if (overlap) {
check_overlap(objects);
}
}
let z = -50;
let spheres = [
new WireframeSphere({
rotation_axis: "y",
z: z,
y: rand_coord(),
x: rand_coord(),
color: 0x663333,
}),
new WireframeSphere({
rotation_axis: "x",
z: z,
y: rand_coord(),
x: rand_coord(),
color: 0x663333,
}),
new WireframeSphere({
rotation_axis: "z",
z: z,
y: rand_coord(),
x: rand_coord(),
color: 0x663333,
}),
new WireframeSphere({
rotation_axis: "x",
z: z,
y: rand_coord(),
x: rand_coord(),
color: 0x663333,
}),
];
check_overlap(spheres);
//add enitities
for (let i = 0; i < spheres.length; i++) {
wrapper.addEntity(spheres[i]);
}
//position controls
wrapper.controls.target.set(0, 0, -50);
wrapper.controls.enableZoom = false;
//make transparent
wrapper.renderer.setClearAlpha(0);
wrapper.camera.fov = 32;
//start wrapper
wrapper.start();
</script>