-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube.js
186 lines (149 loc) · 6.63 KB
/
cube.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
var lattices = [];
function showCube( moveText, titleText, colorInfo = {}, endSolved = true ) {
var parentElement = document.currentScript.parentElement;
var title = document.createElement ( "div" );
title.className = "cube-title";
title.id = parentElement.id + "-title";
title.innerHTML = titleText;
parentElement.appendChild ( title );
var renderer = new THREE.WebGLRenderer( { alpha: true, antialias: true } );
parentElement.appendChild ( renderer.domElement );
var cubeControls = createControls ( parentElement.id );
parentElement.appendChild ( cubeControls.parentElement );
var width = parentElement.clientWidth;
var height = parentElement.clientHeight - title.clientHeight - cubeControls.parentElement.clientHeight - 6; //TODO: Why?
renderer.setSize( width, height );
var scene = new THREE.Scene();
scene.updateMatrixWorld ( true );
var camera = new THREE.PerspectiveCamera ( 45, width / height, 0.1, 1000 );
camera.position.z = 13.5; //move camera back so we can see the cube
var cameraControls = new THREE.OrbitControls( camera, renderer.domElement );
//controls.addEventListener( 'change', render ); // add this only if there is no animation loop (requestAnimationFrame)
cameraControls.enableDamping = true;
cameraControls.dampingFactor = 0.75;
cameraControls.enableKeys = false;
camera.position.x = -8.381782047178634;
camera.position.y = 5.375814232166432;
camera.position.z = 9.115719996513208;
camera.rotation.x = -0.532833886365542;
camera.rotation.y = -0.6698555225205431;
camera.rotation.z = -0.35098692002078447;
var lights = [];
lights[0] = new THREE.DirectionalLight ( 0xFFFFFF, 0.25 );
lights[0].position.set ( 0, 1, 0 );
scene.add ( lights[0] );
lights[1] = new THREE.DirectionalLight ( 0xFFFFFF, 0.25 );
lights[1].position.set ( 1, 0, 0 );
scene.add ( lights[1] );
lights[2] = new THREE.DirectionalLight ( 0xFFFFFF, 0.25 );
lights[2].position.set ( -1, 0, 0 );
scene.add ( lights[2] );
lights[3] = new THREE.DirectionalLight ( 0xFFFFFF, 0.25 );
lights[3].position.set ( 0, 0, 1 );
scene.add ( lights[3] );
lights[4] = new THREE.DirectionalLight ( 0xFFFFFF, 0.25 );
lights[4].position.set ( 0, 0, -1 );
scene.add ( lights[4] );
lights[5] = new THREE.DirectionalLight ( 0xFFFFFF, 0.25 );
lights[5].position.set ( 0, -1, 0 );
scene.add ( lights[5] );
lights[6] = new THREE.AmbientLight( 0xAAAAAA );
scene.add( lights[6] );
var loader = new THREE.ObjectLoader();
var centerRY;
var lattice;
loader.load ( '/clover-cube-files/clover-cube.json', function ( parsedModels ) {
for ( var i in parsedModels.children ) {
parsedModels.children[i].material.shading = THREE.FlatShading;
parsedModels.children[i].material.shininess = 15;
parsedModels.children[i].material.reflectivity = 25;
applyStatesToMatrixDirectly ( parsedModels.children[i] );
}
lattice = new Lattice( parsedModels, moveText, colorInfo, endSolved, cubeControls );
lattices [ parentElement.id ] = lattice;
scene.add ( parsedModels );
});
var render = function() {
var rotationSpeed = Math.PI * 1/32;
requestAnimationFrame(render);
renderer.render(scene, camera);
if ( lattice == null ) return; //if we haven't loaded everything yet, just chill
if ( lattice.pendingRotations.length > 0 ) {
pendingRotation = lattice.pendingRotations [ 0 ];
var negator = pendingRotation.distance < 0 ? -1 : 1;
if ( Math.abs ( pendingRotation.distance ) > rotationSpeed ) {
for ( var k = 0; k < pendingRotation.blocks.length; k++ ) {
rotate ( pendingRotation.blocks [ k ], pendingRotation.axis, rotationSpeed * negator );
}
pendingRotation.distance -= rotationSpeed * negator;
} else {
for ( var k = 0; k < pendingRotation.blocks.length; k++ ) {
rotate ( pendingRotation.blocks [ k ], pendingRotation.axis, pendingRotation.distance );
}
lattice.pendingRotations.shift(); //remove the 0th element from the array, because we're done with it.
}
}
};
render();
}
function rotate ( block, axis, distance ) {
block.model.rotateOnAxis ( axis, distance );
applyStatesToMatrixDirectly ( block.model );
for ( var stickerIndex in block.stickers ) {
block.stickers[stickerIndex].rotateOnAxis ( axis, distance );
applyStatesToMatrixDirectly ( block.stickers[stickerIndex] );
}
}
function applyStatesToMatrixDirectly ( model ) {
model.updateMatrix();
model.geometry.applyMatrix( model.matrix );
model.position.set( 0, 0, 0 );
model.rotation.set( 0, 0, 0 );
model.scale.set( 1, 1, 1 )
model.updateMatrix();
}
function createControls ( elementId ) {
parentElement = document.createElement ( "div" );
parentElement.className = "cube-controls";
beginningButton = document.createElement ( "button" );
beginningButton.className = "beginning-button controls-button";
beginningButton.type ="button";
beginningButton.id = elementId + "-beginning-button";
beginningButton.innerHTML = "|<";
beginningButton.onclick = function() { lattices[elementId].toBeginning(); };
parentElement.appendChild ( beginningButton );
backOneButton = document.createElement ( "button" );
backOneButton.className = "back-one-button controls-button";
backOneButton.type ="button";
backOneButton.id = elementId + "-back-one-button";
backOneButton.innerHTML = "<";
backOneButton.onclick = function() { lattices[elementId].backOne(); };
parentElement.appendChild ( backOneButton );
moveIndex = document.createElement ( "div" );
moveIndex.className = "controls-move-index";
moveIndex.id = elementId + "-move-index";
moveIndex.innerHTML = " / ";
parentElement.appendChild ( moveIndex );
forwardOneButton = document.createElement ( "button" );
forwardOneButton.className = "forward-one-button controls-button";
forwardOneButton.type ="button";
forwardOneButton.id = elementId + "-forward-one-button";
forwardOneButton.innerHTML = ">";
forwardOneButton.onclick = function() { lattices[elementId].forwardOne(); };
parentElement.appendChild ( forwardOneButton );
endButton = document.createElement ( "button" );
endButton.className = "end-button controls-button";
endButton.type ="button";
endButton.id = elementId + "-end-button";
endButton.innerHTML = ">|";
endButton.onclick = function() { lattices[elementId].toEnd(); };
parentElement.appendChild ( endButton );
retMe = {};
retMe.parentElement = parentElement;
retMe.beginningButton = beginningButton;
retMe.backOneButton = backOneButton;
retMe.moveIndex = moveIndex;
retMe.forwardOneButton = forwardOneButton;
retMe.endButton = endButton;
return retMe;
}