-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject-work-portfolio-background-spheres-v2.html
253 lines (189 loc) · 8.11 KB
/
project-work-portfolio-background-spheres-v2.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="shortcut icon" href="img/favicon.ico">
<title>Project - Portfolio - Background Spheres</title>
<style>
body { margin: 0; }
canvas { display: block; }
canvas:focus { outline: none; }
</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 lightHelper, ambientLight, pointLight, pointLight2, cameraLight, pointerLight;
var localGroup;
var particles = [],
particle, geometry, material;
var framesCount = 0;
var globalColor = 0xFFFFFF;
let cameraPosition = new THREE.Vector3(0, 0, 0.5);
let cameraTarget = new THREE.Vector3(0, 0, -2);
// Math variables
const deg = Math.PI / 180; // one degree
const goldenAngle = Math.PI * (3 - Math.sqrt(5));
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(0, 0, 3); // From the top
camera.position.set(cameraPosition.x, cameraPosition.y, cameraPosition.z); // Tilted to the bottom
// INIT Renderer
// --------------------------------------
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor('#fff');
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container = document.getElementById('canvas-container');
container.appendChild( renderer.domElement );
// INIT Options
// --------------------------------------
// Lights
addLights();
// Controls
addControls();
// Grid
// helperGrid();
// helperLight(pointLight2, 1, '#f66');
// helperLight(pointLight, 1, '#f66');
// Create fog
scene.fog = new THREE.FogExp2(globalColor, 0.75);
// Create box
sceneParticles(0.007, 2048, 1.2)
sceneGroup(localGroup, particles);
window.addEventListener('resize', onWindowResize, false);
window.addEventListener('mousemove', function(e) { cameraPanning(e, 0.5); });
}
// Rendering the scene
function sceneAnimation() {
requestAnimationFrame(sceneAnimation);
framesCount++;
// Single Particles Animation
for (let i = 0; i < particles.length; ++i) {
// Scaling
particles[i].scale.set(
(Math.sin((framesCount + Math.sqrt(i)) * 0.015)),
(Math.sin((framesCount + Math.sqrt(i)) * 0.015)),
(Math.sin((framesCount + Math.sqrt(i)) * 0.015))
);
}
// Sphere Animation
localGroup.rotation.x += 0.0025;
localGroup.rotation.y += 0.002;
localGroup.rotation.z += 0.001;
renderer.render(scene, camera);
}
// Lights
function addLights(ambientColor = globalColor) {
ambientLight = new THREE.AmbientLight(ambientColor, 0.375);
scene.add(ambientLight);
pointLight = new THREE.PointLight(ambientColor, 0.5, 0, 70);
pointLight.position.set(0, -1, 1);
pointLight.castShadow = true;
pointLight.shadow.mapSize.width = 2048;
pointLight.shadow.mapSize.height = 2048;
scene.add(pointLight);
pointLight2 = new THREE.PointLight(ambientColor, 0.5, 0, 70);
pointLight2.position.set(0, 1, -1);
pointLight2.castShadow = true;
pointLight2.shadow.mapSize.width = 2048;
pointLight2.shadow.mapSize.height = 2048;
scene.add(pointLight2);
}
// Camera
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function cameraPanning(e, multiplier = 1) {
const screenX = window.innerWidth;
const screenY = window.innerHeight;
const mouseX = e.x;
const mouseY = e.y;
const offsetX = ((1 / screenX * mouseX) - 0.5) * 2 * multiplier;
const offsetY = ((1 / screenY * mouseY) - 0.5) * 2 * multiplier;
const panX = cameraPosition.x + offsetX;
const panY = cameraPosition.y + (offsetY * -1);
camera.position.set(panX, panY, cameraPosition.z);
camera.lookAt(cameraTarget.x, cameraTarget.y, cameraTarget.z);
}
function addControls() {
controls = new OrbitControls(camera, renderer.domElement);
controls.screenSpacePanning = false;
controls.minDistance = 0.5;
controls.maxDistance = 7.5;
controls.target.set(cameraTarget.x, cameraTarget.y, cameraTarget.z);
controls.update();
}
// Elements
function sceneParticles(size = 0.01, length = 2048, r = 1) {
geometry = new THREE.SphereBufferGeometry(size, 16, 16);
material = new THREE.MeshStandardMaterial( { color: 0xEEEEEE } );
for (let i = 0; i < length; ++i) {
// material = new THREE.MeshBasicMaterial( { color: "hsl(0, 0%, 50%)" } );
// Method: http://blog.marmakoide.org/?p=1
let theta = goldenAngle * i;
let z = equalDistribution(Math.pow(r, 2), length, i);
let radius = Math.sqrt(Math.pow(r, 2) - (z * z));
particle = particles[i] = new THREE.Mesh(geometry, material);
particle.position.x = radius * Math.sin(theta);
particle.position.y = radius * Math.cos(theta);
particle.position.z = z;
scene.add(particle);
}
}
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;
}
// Helpers
function helperGrid() {
var grid = new THREE.GridHelper(0, 0);
scene.add(grid);
}
function helperLight(light = directionalLight, size = 1, color = '#f55') {
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);
}
</script>
</body>
</html>