<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Three.js Rubik’s Cube</title>
<style>
body { margin: 0; overflow: hidden; background: #202020;}
canvas { display: block;}
#interface {
position: absolute; top: 10px; left: 10px;
background: rgba(255,255,255,0.8); padding: 10px; border-radius: 4px;
}
#interface button { margin: 2px; }
</style>
</head>
<body>
<div id="interface">
<div>
<button onclick="doMove('U')">U</button>
<button onclick="doMove('U\'')">U′</button>
<button onclick="doMove('D')">D</button>
<button onclick="doMove('D\'')">D′</button>
<button onclick="doMove('L')">L</button>
<button onclick="doMove('L\'')">L′</button>
<button onclick="doMove('R')">R</button>
<button onclick="doMove('R\'')">R′</button>
<button onclick="doMove('F')">F</button>
<button onclick="doMove('F\'')">F′</button>
<button onclick="doMove('B')">B</button>
<button onclick="doMove('B\'')">B′</button>
</div>
<div>
<button onclick="scramble()">Scramble</button>
<button onclick="unscramble()">Unscramble</button>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<script>
let scene, camera, renderer, controls;
let cubeGroup = new THREE.Group();
let cubies = [];
let solvedState = [];
let scramblingMoves = [];
let animating = false;
let moveQueue = [];
init();
animate();
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.set(6,6,6);
renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
let light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(5,10,7);
scene.add(light);
scene.add(new THREE.AmbientLight(0xffffff, 0.4));
createCube();
scene.add(cubeGroup);
window.addEventListener('resize', onWindowResize, false);
}
function createCube() {
const colors = {
U: 0xffffff,
D: 0xffff00,
L: 0xff8000,
R: 0xff0000,
F: 0x00ff00,
B: 0x0000ff
};
const geometry = new THREE.BoxGeometry(0.95,0.95,0.95);
for(let x=-1;x<=1;x++){
for(let y=-1;y<=1;y++){
for(let z=-1;z<=1;z++){
const materials = [];
for(let i=0;i<6;i++){
let color = 0x000000;
if (i===0 && y===1) color=colors.U;
if (i===1 && y===-1) color=colors.D;
if (i===2 && x===-1) color=colors.L;
if (i===3 && x===1) color=colors.R;
if (i===4 && z===1) color=colors.F;
if (i===5 && z===-1) color=colors.B;
materials.push(new THREE.MeshLambertMaterial({color}));
}
const mesh = new THREE.Mesh(geometry, materials);
mesh.position.set(x, y, z);
cubeGroup.add(mesh);
cubies.push(mesh);
}
}
}
// Save solved state
solvedState = cubies.map(c => ({pos: c.position.clone(), rot: c.rotation.clone()}));
}
function onWindowResize(){
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate(){
requestAnimationFrame(animate);
controls.update();
if(moveQueue.length>0 && !animating){
const move = moveQueue.shift();
performMove(move);
}
renderer.render(scene,camera);
}
function doMove(move){
moveQueue.push(move);
}
function rotateFace(axis, index, clockwise, callback){
animating=true;
const group = new THREE.Group();
let epsilon = 0.1;
cubies.forEach(c=>{
if(Math.abs(c.position[axis]-index)<epsilon){
group.add(c);
}
});
cubeGroup.add(group);
let angle=0;
let target = Math.PI/2*(clockwise?1:-1);
function step(){
let delta = 0.1*(clockwise?1:-1);
angle+=delta;
group.rotation[axis==='x'?'x':axis==='y'?'y':'z']+=delta;
if(Math.abs(angle)>=Math.abs(target)){
group.rotation[axis==='x'?'x':axis==='y'?'y':'z']-=angle-target;
while(group.children.length>0){
let obj=group.children[0];
obj.applyMatrix4(group.matrix);
cubeGroup.add(obj);
}
cubeGroup.remove(group);
animating=false;
if(callback) callback();
return;
}
requestAnimationFrame(step);
}
requestAnimationFrame(step);
}
function performMove(move, cb){
let clockwise=true, axis, index;
switch(move){
case 'U': axis='y'; index=1; clockwise=true; break;
case 'U\'': axis='y'; index=1; clockwise=false; break;
case 'D': axis='y'; index=-1; clockwise=false; break;
case 'D\'': axis='y'; index=-1; clockwise=true; break;
case 'L': axis='x'; index=-1; clockwise=false; break;
case 'L\'': axis='x'; index=-1; clockwise=true; break;
case 'R': axis='x'; index=1; clockwise=true; break;
case 'R\'': axis='x'; index=1; clockwise=false; break;
case 'F': axis='z'; index=1; clockwise=true; break;
case 'F\'': axis='z'; index=1; clockwise=false; break;
case 'B': axis='z'; index=-1; clockwise=false; break;
case 'B\'': axis='z'; index=-1; clockwise=true; break;
}
rotateFace(axis,index,clockwise,cb);
}
function scramble(){
scramblingMoves=[];
let moveset=['U','U\'','D','D\'','L','L\'','R','R\'','F','F\'','B','B\''];
for(let i=0;i<20;i++){
let m=moveset[Math.floor(Math.random()*moveset.length)];
scramblingMoves.push(m);
moveQueue.push(m);
}
}
function inverseMove(move){
if(move.endsWith("'")) return move.slice(0,-1);
else return move+"'";
}
function unscramble(){
let inverseMoves = scramblingMoves.slice().reverse().map(inverseMove);
let i=0;
let interval=setInterval(()=>{
if(i>=inverseMoves.length){ clearInterval(interval); return;}
doMove(inverseMoves[i]);
i++;
},500);
}
function isSolved(){
let tol=0.001;
for(let i=0;i<cubies.length;i++){
let c=cubies[i];
let s=solvedState[i];
if(c.position.distanceTo(s.pos)>tol) return false;
if(Math.abs(c.rotation.x-s.rot.x)>tol) return false;
if(Math.abs(c.rotation.y-s.rot.y)>tol) return false;
if(Math.abs(c.rotation.z-s.rot.z)>tol) return false;
}
return true;
}
</script>
</body>
</html>