-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirrorCube.html
308 lines (243 loc) · 10.9 KB
/
mirrorCube.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
<html>
<head>
<style>
</style>
<title>Monte Carlo</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<script src="lib/three.min.js"></script>
<script src="lib/lil-gui.umd.min.js"></script>
<script src="OrbitControls.js"></script>
</head>
<body>
<script>
var renderer, scene, camera, light, stats, t, animate;
function vaxInit()
{
renderer = new THREE.WebGLRenderer( {antialias: true} );
document.body.appendChild( renderer.domElement );
document.body.style.margin = 0;
document.body.style.overflow = 'hidden';
scene = new THREE.Scene();
scene.background = new THREE.Color(0x9B59B6);
camera = new THREE.PerspectiveCamera( 60, 1, 20, 1000 );
camera.lookAt(new THREE.Vector3(0,0,0));
light = new THREE.PointLight();
light.position.set(0,150,300);
scene.add( light );
window.addEventListener( 'resize', onWindowResize, false );
onWindowResize();
renderer.setAnimationLoop( frame );
}
function onWindowResize( event )
{
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight, true );
}
var oldTime = 0;
var accTime = 0;
function frame( time )
{
// защита от прекалено голяма скорост, при по-бързи компютри някои анимации
// ще са прекалено бързи, затова изкуствено се забавя до 60 fps
accTime += time-oldTime;
oldTime = time;
if( accTime < 1000/60 ) return;
accTime = 0;
if( animate ) animate( time/1000 );
renderer.render( scene, camera );
}
vaxInit();
camera.position.set(0,50,200); // Set position like this
camera.lookAt(new THREE.Vector3(0,0,0)); // Set look at coordinate like this
var cube = new THREE.Group();
const mid = 19;
const halfMid = mid / 2;
const gap = 1;
const map = {
'x': { 0 : 13, 1 : 19, 2 : 25},
'y': { 0 : 29, 1 : 19, 2 : 9},
'z': { 0 : 17, 1 : 19, 2 : 21},
}
for(let x = 0; x < 3; x++){
for(let y = 0; y < 3; y++){
for(let z = 0; z < 3; z++){
var xs = map['x'][x];
var ys = map['y'][y];
var zs = map['z'][z];
var boxGeometry = new THREE.BoxGeometry(xs, ys, zs);
var material = new THREE.MeshPhongMaterial({color : 'gray', shininess : 200});
var mesh = new THREE.Mesh(boxGeometry, material);
mesh.position.set((xs/2 + halfMid + gap) * (x - 1), (ys/2 + halfMid + gap) * (y - 1), (zs/2 + halfMid + gap) * (z - 1));
mesh.virtual = new THREE.Vector3(x-1, y-1, z-1);
cube.add(mesh);
}
}
}
function selectSide(coord, value){
var side = [];
cube.traverse(function(element) {
if(element.virtual && element.virtual.toArray()[coord] == value){
side.push(element);
}
});
return side;
}
function getNormalVectorOfSticker(sticker){
var v = new THREE.Vector3(0, 0, 1);
v.applyEuler(sticker.rotation);
v.applyEuler(sticker.parent.rotation);
return v.round();
}
const coords = [0, 1, 2, 0, 1, 2];
const vals = [1, 1, 1, -1, -1, -1];
// const colors = ["red", "yellow", "blue", "orange", "white", "green"];
var stickers = [];
for(let i = 0; i < 6; i++){
let axis = coords[i];
let val = vals[i];
let side = selectSide(axis, val);
for(const cubie of side){
var sticker = null;
var stickerGeometry = null;
var material = new THREE.MeshBasicMaterial();
if(axis == 2) {
let w = cubie.geometry.parameters.width - 1;
let h = cubie.geometry.parameters.height - 1;
stickerGeometry = new THREE.PlaneGeometry(w, h);
sticker = new THREE.Mesh(stickerGeometry, material);
sticker.position.setComponent(axis, val * (cubie.geometry.parameters.depth/2 + 0.1));
if(val == -1)
sticker.rotation.set(-Math.PI, 0, 0);
}
else if(axis == 1) {
let w = cubie.geometry.parameters.width - 1;
let h = cubie.geometry.parameters.depth - 1;
stickerGeometry = new THREE.PlaneGeometry(w, h);
sticker = new THREE.Mesh(stickerGeometry, material);
sticker.position.setComponent(axis, val * (cubie.geometry.parameters.height/2 + 0.1));
sticker.rotation.set(val * -Math.PI/2, 0, 0);
}
else if(axis == 0){
let w = cubie.geometry.parameters.depth - 1;
let h = cubie.geometry.parameters.height - 1;
stickerGeometry = new THREE.PlaneGeometry(w, h);
sticker = new THREE.Mesh(stickerGeometry, material);
sticker.position.setComponent(axis, val * (cubie.geometry.parameters.width/2 + 0.1));
sticker.rotation.set(0, val * Math.PI/2, 0);
}
sticker.isSticker = true;
stickers.push(sticker);
cubie.add(sticker);
}
}
function colorStickers(){
// -1 == -x, -2 == -y, -3 == -z
const axisToColor = {
'-1' : "red",
'-2' : "yellow",
'-3' : "blue",
'1' : "orange",
'2' : "white",
'3' : "green"
};
for(const sticker of stickers){
const normal = getNormalVectorOfSticker(sticker);
const arr = [0, 0, 0];
normal.toArray(arr);
var axisStr = null;
for(let i = 0; i < arr.length; i++){
if(arr[i] != 0){
axisStr = arr[i] * (i + 1);
break;
}
}
sticker.material.color.set(axisToColor[axisStr]);
}
}
class Move {
constructor(coord, value, angle) {
this.coord = coord;
this.value = value;
this.angle = angle;
}
initMove(){
var arr = [0, 0, 0];
arr[this.coord] = 1;
this.axis = new THREE.Vector3().fromArray(arr);
this.side = selectSide(this.coord, this.value);
this.total = 0;
for(const el of this.side){
el.virtual.applyAxisAngle(this.axis, this.angle);
el.virtual.round();
}
}
rotateSide(d_angle){
if(this.angle < 0){
d_angle *= -1;
}
var end = false;
if(Math.abs(this.total + d_angle) > Math.abs(this.angle)){
end = true;
d_angle = this.angle - this.total;
}
for(const el of this.side){
const v = new THREE.Vector3(el.position.x, el.position.y, el.position.z);
v.applyAxisAngle(this.axis, d_angle);
el.position.set(v.x, v.y, v.z);
el.rotateOnWorldAxis(this.axis, d_angle);
}
this.total += d_angle;
return end;
}
}
//randomly Shuffle The cube and return a stack of inverse moves(functions)
function randomShuffle(){
var moves = [];
for(let i = 0; i < 20; i++) {
var axis = THREE.MathUtils.randInt(0, 2);
var val = THREE.MathUtils.randInt(0, 2);
var move = new Move(axis, val, Math.PI / 2);
move.initMove();
move.rotateSide(Math.PI / 2);
moves.push(new Move(axis, val, -Math.PI / 2));
}
return moves;
}
var moves = randomShuffle();
colorStickers();
var cnt = 0;
scene.add(cube);
var oldT = 0;
var end = true;
var move = null;
var rotateFunction = null;
var clock = new THREE.Clock();
var oldT = 0;
cube.rotation.set(0, Math.PI, 0);
const controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.enabled = true;
function animate( t ) {
var delta = t - oldT;
oldT = t;
delta *= 5;
// cube.rotation.set(t/2, t, 0);
if(t < 5){
return;
}
// delta = clock.getDelta() * 10;
controls.update();
if(end){
if(moves.length != 0){
move = moves.pop();
move.initMove();
end = move.rotateSide(delta);
}
}else{
end = move.rotateSide(delta);
}
}
</script>
</body>
</html>