-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpuzzle.js
217 lines (197 loc) · 5.46 KB
/
puzzle.js
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
'use strict';
function _create_geometry(box) {
let x0 = box[0][0];
let x1 = box[0][1];
let y0 = box[1][0];
let y1 = box[1][1];
let z0 = box[2][0];
let z1 = box[2][1];
let geometry = new THREE.BufferGeometry();
const vertices = new Float32Array([
x0, y0, z0,
x0, y0, z1,
x0, y1, z1,
x0, y0, z0,
x0, y1, z1,
x0, y1, z0,
x0, y0, z0,
x1, y0, z0,
x1, y0, z1,
x0, y0, z0,
x1, y0, z1,
x0, y0, z1,
x0, y0, z0,
x0, y1, z0,
x1, y1, z0,
x0, y0, z0,
x1, y1, z0,
x1, y0, z0,
x1, y0, z0,
x1, y1, z0,
x1, y1, z1,
x1, y0, z0,
x1, y1, z1,
x1, y0, z1,
x0, y0, z1,
x1, y0, z1,
x1, y1, z1,
x0, y0, z1,
x1, y1, z1,
x0, y1, z1,
x0, y1, z0,
x0, y1, z1,
x1, y1, z1,
x0, y1, z0,
x1, y1, z1,
x1, y1, z0,
]);
geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
return geometry;
}
function gen_puzzle_from_data(data) {
let black_material = new THREE.MeshBasicMaterial({color: 0x707070});
let color_materials = [];
let hilit_materials = [];
let quaternion_materials = [];
for (let i = 0; i < data.colors.length; ++i) {
let color = data.colors[i];
color_materials[i] = new THREE.MeshBasicMaterial({color: new THREE.Color(0.8*color[0], 0.8*color[1], 0.8*color[2])});
hilit_materials[i] = new THREE.MeshBasicMaterial({color: new THREE.Color(color[0], color[1], color[2])});
}
let panel_meshes = [];
let root = new THREE.Object3D();
let show_quaternions = true;
root.scale.x = 1/data.size;
root.scale.y = 1/data.size;
root.scale.z = 1/data.size;
for (let i = 0; i < data.panels.length; ++i) {
let panel = data.panels[i];
let geometry = _create_geometry(panel.box);
let mesh = new THREE.Mesh(geometry, black_material);
root.add(mesh);
panel_meshes[i] = mesh;
if (panel.color < 0) {
quaternion_materials[i] = black_material;
} else {
let cubie_center = new THREE.Vector3(
panel.cubie_center[0],
panel.cubie_center[1],
panel.cubie_center[2],
);
quaternion_materials[i] = new THREE.ShaderMaterial(
{
uniforms: {
cubie_center: { value: cubie_center },
quaternion: {value: new THREE.Vector4(0, 1, 0, 0)},
highlight: { value: 0 },
},
vertexShader: VERTEX_SHADER,
fragmentShader: FRAGMENT_SHADER,
}
);
}
}
let panel_colors = [];
let panel_quaternions = [];
for (let i = 0; i < data.panels.length; ++i) {
panel_colors[i] = data.panels[i].color;
panel_quaternions[i] = 0;
}
return {
get_move_count: function() { return data.moves.length; },
get_root: function() { return root; },
opposite_move: function(move_id) {
if (move_id == -1) return -1;
return data.move_opposites[move_id];
},
apply_move: function(move_id) {
let move = data.moves[move_id];
let perm = move.panel_perm;
let old_panel_colors = panel_colors.slice();
let old_panel_quaternions = panel_quaternions.slice();
for (let i = 0; i < data.panels.length; ++i) {
panel_colors[perm[i]] = old_panel_colors[i];
if (move.affected_panels[i] > 0) {
panel_quaternions[perm[i]] = data.quaternion_table[old_panel_quaternions[i]][move.quaternion];
}
}
},
update: function(move_id, amount, highlighted_panels, camera_quaternion) {
if (move_id < 0) {
move_id = 0;
amount = 0;
}
let move = data.moves[move_id];
let axis = new THREE.Vector3(move.axis[0], move.axis[1], move.axis[2]);
let rq = new THREE.Quaternion().setFromAxisAngle(axis, amount * Math.PI * 2 / move.fraction);
let irq = new THREE.Quaternion().setFromAxisAngle(axis, -amount * Math.PI * 2 / move.fraction);
for (let i = 0; i < data.panels.length; ++i) {
if (move.affected_panels[i]) {
panel_meshes[i].quaternion.copy(irq);
} else {
panel_meshes[i].quaternion.identity();
}
}
for (let i = 0; i < data.panels.length; ++i) {
let c = panel_colors[i];
if (c < 0) {
// Stay gray
} else if (show_quaternions) {
// Quaternion colors
panel_meshes[i].material = quaternion_materials[i];
let quaternion_id = panel_quaternions[i];
let pq = new THREE.Quaternion(
data.quaternions[quaternion_id].x,
data.quaternions[quaternion_id].y,
data.quaternions[quaternion_id].z,
data.quaternions[quaternion_id].w,
);
if (move.affected_panels[i]) {
pq.multiply(rq);
}
pq.multiplyQuaternions(camera_quaternion, pq);
quaternion_materials[i].uniforms.quaternion.value = new THREE.Vector4(
pq.x,
pq.y,
pq.z,
pq.w,
);
quaternion_materials[i].uniforms.highlight.value = highlighted_panels.includes(i) ? 1 : 0;
} else {
// Regular colors
if (highlighted_panels.includes(i)) {
panel_meshes[i].material = hilit_materials[c];
} else {
panel_meshes[i].material = color_materials[c];
}
}
}
},
get_panel: function(raycaster) {
let result_array = raycaster.intersectObject(root, true);
if (result_array.length == 0) {
return -1;
}
for (let i = 0; i < data.panels.length; ++i) {
if (panel_meshes[i] == result_array[0].object) {
if (data.panels[i].color < 0) return -1;
return i;
}
}
return -1;
},
try_rotate: function(old_panel, new_panel) {
if (old_panel < 0 || new_panel < 0) return -1;
return data.move_table[old_panel][new_panel];
},
set_show_quaternions: function(yes) {
show_quaternions = !!yes;
},
reset: function() {
for (let i = 0; i < data.panels.length; ++i) {
panel_colors[i] = data.panels[i].color;
panel_quaternions[i] = 0;
}
},
}
};