-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube-sin-rotation.html
299 lines (225 loc) · 9.71 KB
/
cube-sin-rotation.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="shortcut icon" href="img/favicon.ico">
<title>Cube Sin Rotation</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 cubeGeometry, cubeMaterial, cube;
var cube2Geometry, cube2Material, cube2;
var cube3Geometry, cube3Material, cube3;
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(8, 8, 8);
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('#29261d');
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 box
sceneCube();
sceneCube2();
sceneCube3();
// Create group
sceneGroup(localGroup, [cube, cube2, cube3]);
window.addEventListener('mousemove', function() { lightCameraSync(cameraLight) }, false);
// INIT Helper
// --------------------------------------
// Grid
helperGrid();
// Lights Helper
// helperLight();
}
// Rendering the scene
function sceneAnimation() {
requestAnimationFrame(sceneAnimation);
framesCount++;
// Animation
var speed = 0.01;
var orbitRadius1 = new THREE.Vector3(-2, -2, -2);
var orbitRadius2 = new THREE.Vector3(2, 2, 2);
var orbitRadius3 = new THREE.Vector3(2, 2, 2);
orbitAround2D(cube, 'z', speed, orbitRadius1);
orbitAround2D(cube2, 'z', speed, orbitRadius2);
orbitAround2D(cube3, 'x', speed, orbitRadius3);
renderer.render(scene, camera);
}
function addLights(ambientColor = 0xffa030, directionalColor = 0xffffff) {
ambientLight = new THREE.AmbientLight(ambientColor, 0.1);
scene.add(ambientLight);
pointLight = new THREE.PointLight(ambientColor, 0.9, 0, 1);
pointLight.position.set(3, 4, 2);
scene.add(pointLight);
cameraLight = new THREE.PointLight(ambientColor, 0.6, 0, 1);
cameraLight.position.set(8, 8, 8);
cameraLight.rotation.set(-45, 35, 30);
scene.add(cameraLight);
// hemisphereLight = new THREE.HemisphereLight(0xffffff, 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 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;
}
function addControls() {
controls = new OrbitControls(camera, renderer.domElement);
controls.screenSpacePanning = true;
controls.minDistance = 2;
controls.maxDistance = 60;
controls.target.set(0, 0, 0);
controls.update();
}
function sceneCube() {
cubeGeometry = new THREE.BoxBufferGeometry();
cubeMaterial = new THREE.MeshStandardMaterial( { color: 0xC7C7C7 } );
cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.castShadow = true;
cube.receiveShadow = false;
cube.position.y = -1.5;
scene.add(cube);
}
function sceneCube2() {
cube2Geometry = new THREE.BoxBufferGeometry();
cube2Material = new THREE.MeshStandardMaterial( { color: 0xC7C7C7 } );
cube2 = new THREE.Mesh(cube2Geometry, cube2Material);
cube2.castShadow = true;
cube2.receiveShadow = false;
cube2.position.y = 1.5;
cube2.scale.set(0.5, 0.5, 0.5);
scene.add(cube2);
}
function sceneCube3() {
cube3Geometry = new THREE.BoxBufferGeometry();
cube3Material = new THREE.MeshStandardMaterial( { color: 0xC7C7C7 } );
cube3 = new THREE.Mesh(cube3Geometry, cube3Material);
cube3.castShadow = true;
cube3.receiveShadow = false;
cube3.position.z = 1.5;
cube3.scale.set(0.25, 0.25, 0.25);
scene.add(cube3);
}
function sceneGroup(group, objs) {
group = localGroup = new THREE.Group();
objs.forEach(function(obj) {
group.add(obj);
});
scene.add(group);
}
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);
}
</script>
</body>
</html>