-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticle-sphere-v3.html
313 lines (236 loc) · 10.2 KB
/
particle-sphere-v3.html
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="shortcut icon" href="img/favicon.ico">
<title>Particle Sphere v3</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<div id="canvas-container"></div>
<script src="js/three.js"></script>
<script type="module">
// import * as THREE from './js/three.js';
import { OrbitControls } from './js/OrbitControls.js';
var container;
var camera, scene, renderer, controls;
var framesCount = 0;
var lightHelper, ambientLight, pointLight, directionalLight, hemisphereLight, cameraLight;
var wireframe;
var localGroup;
var particles = [],
particlesSlice = [],
particle, geometry, material;
// Math variables
const deg = Math.PI / 180; // one degree
init();
sceneAnimation();
// Initiating Scene
function init() {
// INIT Scene
// --------------------------------------
scene = new THREE.Scene();
// INIT Camera
// --------------------------------------
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(6, 6, 6);
camera.rotation.set(-45, 35, 30);
// INIT Renderer
// --------------------------------------
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor('#0f0f1f');
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container = document.getElementById('canvas-container');
container.appendChild( renderer.domElement );
// INIT Options
// --------------------------------------
// Lights
addLights();
// Controls
addControls();
// Create particles
sceneParticles();
groupSlices();
// Create fog
// scene.fog = new THREE.FogExp2(0x0f0f1f, 0.065);
// Create group
sceneGroup(localGroup, particlesSlice);
// Update renderer and camera when resizing
window.addEventListener('resize', onWindowResize, false);
// INIT Helper
// --------------------------------------
// Grid
helperGrid();
// Lights Helper
// helperLight();
// Sphere Helper
// helperSphere();
}
// Rendering the scene
function sceneAnimation() {
requestAnimationFrame(sceneAnimation);
framesCount++;
// Sphere Animation
// localGroup.rotation.y = Math.cos(framesCount * 0.01);
// localGroup.rotation.z = Math.sin(framesCount * 0.01);
renderer.render(scene, camera);
}
function addLights(ambientColor = 0xffa030, directionalColor = 0xffffff) {
// ambientLight = new THREE.AmbientLight(ambientColor, 0.1);
// scene.add(ambientLight);
hemisphereLight = new THREE.HemisphereLight(0xffff0f, 0xffffff, 1);
scene.add(hemisphereLight);
directionalLight = new THREE.DirectionalLight(directionalColor, 0.75, 100);
directionalLight.position.set(0, 4, 0);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
scene.add(directionalLight);
}
function addControls() {
controls = new OrbitControls(camera, renderer.domElement);
controls.screenSpacePanning = true;
controls.minDistance = 0;
controls.maxDistance = 80;
controls.target.set(0, 0, 0);
controls.update();
}
function sceneParticles(size = 0.01, length = 64, r = 2) {
geometry = new THREE.SphereBufferGeometry(size, 16, 16);
material = new THREE.MeshBasicMaterial( { color: "hsl(21, 100%, 64%)" } );
let i = 0, ix, iy;
const theta = (180 / length) * deg; // [0 - 180]
const varphi = (360 / length) * deg; // [-180 - 180]
// Two Dimensions (x & y)
for (let ix = 0; ix < length; ++ix) {
for (let iy = 0; iy < length; ++iy) {
particle = particles[i++] = new THREE.Mesh(geometry, material);
particle.position.x = r * Math.sin(theta * iy) * Math.cos((varphi * ix) - (theta * length));
particle.position.y = r * Math.sin(theta * iy) * Math.sin((varphi * ix) - (theta * length));
particle.position.z = r * Math.cos(theta * iy);
scene.add(particle);
}
}
}
function groupSlices(length = 64) {
let i = 0, ix, iy;
// Two Dimensions (x & y)
for (let ix = 0; ix < length; ++ix) {
particlesSlice[ix] = new THREE.Group();
// Third Dimension (z)
for (let iy = 0; iy < length; ++iy) {
i++;
particlesSlice[ix].add(particles[i-1]);
}
}
}
function sceneGroup(group, objs) {
group = localGroup = new THREE.Group();
objs.forEach(function(obj) {
group.add(obj);
});
scene.add(group);
}
function equalDistribution(min, length, i) {
return ((min / (length / 2)) * (i + 1)) - (min + (1 / (length)));
}
function sin(x, speed = 0.001, radius = 1, center = 0) {
return (Math.sin((Math.PI / 2) * (x * speed)) * radius) + center;
}
function cos(x, speed = 0.001, radius = 1, center = 0) {
return (Math.cos((Math.PI / 2) * (x * speed)) * radius) + center;
}
function orbitAround2D(obj, ax = 'y', speed = 0.01, radius, center) {
radius = (radius === undefined) ? new THREE.Vector3(2, 2, 2) : radius; // Orbit radius
center = (center === undefined) ? new THREE.Vector3(0, 0, 0) : center; // Orbit center
// Orbit around x
if (ax.includes('x')) {
obj.position.x = center.x;
obj.position.y = cos(framesCount, speed, radius.y, center.y);
obj.position.z = sin(framesCount, speed, radius.z, center.z);
obj.rotation.x = framesCount * speed * (Math.PI / 2);
}
// Orbit around y
if (ax.includes('y')) {
obj.position.y = center.y;
obj.position.z = cos(framesCount, speed, radius.z, center.z);
obj.position.x = sin(framesCount, speed, radius.x, center.x);
obj.rotation.y = framesCount * speed * (Math.PI / 2);
}
// Orbit around z
if (ax.includes('z')) {
obj.position.z = center.z;
obj.position.x = cos(framesCount, speed, radius.x, center.x);
obj.position.y = sin(framesCount, speed, radius.y, center.y);
obj.rotation.z = framesCount * speed * (Math.PI / 2);
}
}
// Helpers
function helperGrid() {
var grid = new THREE.GridHelper(0, 0);
scene.add(grid);
}
function helperWireframe(obj, geometry) {
var wireframeGeometry = new THREE.WireframeGeometry(geometry);
wireframe = new THREE.LineSegments(wireframeGeometry);
wireframe.material.depthTest = false;
wireframe.material.opacity = 0.5;
wireframe.material.transparent = true;
// Apply all properties from obj to wireframe
wireframe.scale.x = obj.scale.x;
wireframe.scale.y = obj.scale.y;
wireframe.scale.z = obj.scale.z;
wireframe.position.x = obj.position.x;
wireframe.position.y = obj.position.y;
wireframe.position.z = obj.position.z;
wireframe.rotation.x = obj.rotation.x;
wireframe.rotation.y = obj.rotation.y;
wireframe.rotation.z = obj.rotation.z;
scene.add(wireframe);
}
function helperLight(light = directionalLight, size = 1, color = '#ffffff') {
if (light.type === 'DirectionalLight') {
lightHelper = new THREE.DirectionalLightHelper(light, size, color);
}
if (light.type === 'PointLight') {
lightHelper = new THREE.PointLightHelper(light, size, color);
}
if (light.type === 'SpotLight') {
lightHelper = new THREE.SpotLightHelper(light, size, color);
}
if (light.type === 'HemisphereLight') {
lightHelper = new THREE.HemisphereLightHelper(light, size, color);
}
scene.add(lightHelper);
}
function helperSphere(r = 2) {
let sphereGeometry = new THREE.SphereBufferGeometry(r, 64, 64);
let sphereMaterial = new THREE.MeshBasicMaterial( { color: "hsl(0, 100%, 80)" } );
let sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
sphere.material.transparent = true;
sphere.material.opacity = 0.3;
scene.add(sphere);
}
// Camera
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function lightCameraSync(light = cameraLight) {
light.position.x = camera.position.x;
light.position.y = camera.position.y;
light.position.z = camera.position.z;
light.rotation.x = camera.rotation.x;
light.rotation.y = camera.rotation.y;
light.rotation.z = camera.rotation.z;
}
</script>
</body>
</html>