I am trying to make a rubiks cube simulator with three.js so my goal now is to rotate the bottom and then rotate the middle layer. After I rotate the bottom layer and switch to the middle layer the bottom layer goes back in (the position after the rotation is not saved) and the middle layer snappes instantly.
This is my code:
import * as THREE from 'three'
import { TrackballControls } from 'three/examples/jsm/controls/TrackballControls';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer({canvas: document.querySelector("canvas.webgl")});
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
const geometry = new THREE.BoxGeometry(1, 1, 1);
const materials = [
new THREE.MeshBasicMaterial({color: 'white'}),
new THREE.MeshBasicMaterial({color: 'yellow'}),
new THREE.MeshBasicMaterial({color: 'red'}),
new THREE.MeshBasicMaterial({color: 'darkorange'}),
new THREE.MeshBasicMaterial({color: 'blue'}),
new THREE.MeshBasicMaterial({color: 'green'}),
];
for(let i = 0; i < geometry.groups.length; i++)
geometry.groups[i].materialIndex = i;
const cube = [];
for(let y = -1; y <= 1; y++){
for(let x = -1; x <= 1; x++){
for(let z = -1; z <= 1; z++){
const mesh = new THREE.Mesh(geometry, materials);
mesh.position.x = x;
mesh.position.y = y;
mesh.position.z = z;
cube.push(mesh);
scene.add(mesh);
}
}
}
const controls = new TrackballControls( camera, renderer.domElement );
controls.noPan = true;
controls.noZoom = true;
controls.rotateSpeed = 3;
camera.position.z = 7.5;
controls.update();
const face = new THREE.Group();
// get the bottom layer
for(let i = 0; i < 9; i++){
scene.remove(cube[i]);
face.add(cube[i]);
}
scene.add(face);
let moveCount = 0;
function animate() {
requestAnimationFrame( animate );
controls.update();
if(moveCount < 1){
if(face.rotation.y < THREE.MathUtils.degToRad(90)){
face.rotation.y += THREE.MathUtils.degToRad(2);
}
else{
changeFace();
moveCount++;
}
}
renderer.render( scene, camera );
}
function changeFace() {
scene.add(...face.children);
face.remove(...face.children);
for(let i = 9; i < 18; i++){
scene.remove(cube[i]);
face.add(cube[i]);
}
};
animate();