Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>Three js</title>
- <link rel="stylesheet" href="style.css">
- <meta charset="utf-8">
- </meta>
- <script src="./three.js"></script>
- </head>
- <body>
- </body>
- </html>
- <script>
- let scene, camera, renderer, rubiks = [];
- let cameraMovement = false;
- var posX = 0, posY = 0, xDown, xUp, yDown, yUp;
- let ray = new THREE.Raycaster();
- let mouse = new THREE.Vector2();
- // set up the environment -
- // initiallize scene, camera, objects and renderer
- class Materials {
- constructor(right, left, top, bottom, front, back) {
- this.colors = [];
- this.colors.push(right);
- this.colors.push(left);
- this.colors.push(top);
- this.colors.push(bottom);
- this.colors.push(front);
- this.colors.push(back);
- }
- setRight(c){this.colors[0] = c;}
- setLeft(c){this.colors[1] = c;}
- setTop(c){this.colors[2] = c;}
- setBot(c){this.colors[3] = c;}
- setFront(c){this.colors[4] = c;}
- setBack(c){this.colors[5] = c;}
- }
- let rotateX = function(pos, sens){
- let min = ((pos-1)*(9));
- let max = pos*9;
- //let faceMoving = new THREE.Group();
- for(min; min<max; min++){
- rubiks[min].rotation.x += (Math.PI/2 * sens);
- //faceMoving.add(rubiks[min]);
- }
- //face.rotateOnAxis(new THREE.Vector3(1, 0, 0).normalize(), Math.PI/2*sens);
- //faceMoving.rotation.x += Math.PI/2*sens;
- //scene.add(faceMoving);
- }
- let rotateY = function(pos, sens){ // A AMELIORER (modulo ?)
- let change = [];
- //let faceMoving = new THREE.Group();
- if(pos == 1){
- change.push(0, 1, 2, 9, 10, 11, 18, 19, 20);
- } else if (pos == 2) {
- change.push(3, 4, 5, 12, 13, 14, 21, 22, 23);
- } else if (pos == 3) {
- change.push(6, 7, 8, 15, 16, 17, 24, 25, 26);
- }
- for(let i = 0; i<=rubiks.length-1; i++){
- if(change.includes(i)){
- rubiks[i].rotation.y += (Math.PI/2 * sens);
- //faceMoving.add(rubiks[i]);
- }
- }
- //console.log(faceMoving);
- //face.rotateOnAxis(new THREE.Vector3(0, 1, 0).normalize(), Math.PI/2*sens);
- //faceMoving.rotation.y += Math.PI/2*sens;
- //scene.add(faceMoving);
- }
- let rotateZ = function(pos, sens){
- for(let i = 0; i<rubiks.length; i++){
- if(i%3 == pos-1){
- rubiks[i].rotation.z += (Math.PI/2 * sens);
- }
- }
- }
- let collision = function(){
- let on = ray.intersectObjects(scene.children)[0].object;
- let pos = 1;
- let cube = 0;
- let rotaX, rotaY;
- rubiks.forEach(function(i, ind){
- if(i == on){
- cube = ind;
- }
- });
- switch(cube){
- case 0: rotaY = 1; rotaX = 1; rotaZ = 1; break;
- case 1: rotaY = 1; rotaX = 1; rotaZ = 2; break;
- case 2: rotaY = 1; rotaX = 1; rotaZ = 3; break;
- case 3: rotaY = 2; rotaX = 1; rotaZ = 1; break;
- case 4: rotaY = 2; rotaX = 1; rotaZ = 2; break;
- case 5: rotaY = 2; rotaX = 1; rotaZ = 3; break;
- case 6: rotaY = 3; rotaX = 1; rotaZ = 1; break;
- case 7: rotaY = 3; rotaX = 1; rotaZ = 2; break;
- case 8: rotaY = 3; rotaX = 1; rotaZ = 3; break;
- case 9: rotaY = 1; rotaX = 2; rotaZ = 1; break;
- case 10: rotaY = 1; rotaX = 2; rotaZ = 2; break;
- case 11: rotaY = 1; rotaX = 2; rotaZ = 3; break;
- case 12: rotaY = 2; rotaX = 2; rotaZ = 1; break;
- case 13: rotaY = 2; rotaX = 2; rotaZ = 2; break;
- case 14: rotaY = 2; rotaX = 2; rotaZ = 3; break;
- case 15: rotaY = 3; rotaX = 2; rotaZ = 1; break;
- case 16: rotaY = 3; rotaX = 2; rotaZ = 2; break;
- case 17: rotaY = 3; rotaX = 2; rotaZ = 3; break;
- case 18: rotaY = 1; rotaX = 3; rotaZ = 1; break;
- case 19: rotaY = 1; rotaX = 3; rotaZ = 2; break;
- case 20: rotaY = 1; rotaX = 3; rotaZ = 3; break;
- case 21: rotaY = 2; rotaX = 3; rotaZ = 1; break;
- case 22: rotaY = 2; rotaX = 3; rotaZ = 2; break;
- case 23: rotaY = 2; rotaX = 3; rotaZ = 3; break;
- case 24: rotaY = 3; rotaX = 3; rotaZ = 1; break;
- case 25: rotaY = 3; rotaX = 3; rotaZ = 2; break;
- case 26: rotaY = 3; rotaX = 3; rotaZ = 3; break;
- }
- if(Math.abs(xUp-xDown) > 10 || Math.abs(yUp-yDown) > 10){
- if(xDown > xUp && (Math.abs(xUp-xDown) > Math.abs(yUp-yDown))) {
- rotateY(rotaY, -1);
- console.log("Rota gauche");
- } else if(xDown < xUp && (Math.abs(xUp-xDown) > Math.abs(yUp-yDown))) {
- rotateY(rotaY, 1);
- console.log("Rota droite");
- } else if (yDown > yUp) {
- rotateX(rotaX, -1);
- console.log("Rota haut");
- } else if (yDown < yUp) {
- rotateX(rotaX, 1);
- console.log("Rota bas");
- }
- }
- }
- let animation = function(face, sens){
- switch(face){
- case 1:
- if(sens == 1){
- } else if (sens == -1){
- }
- break;
- case 2:
- break;
- case 3:
- break;
- case 4:
- break;
- case 5:
- break;
- case 6:
- break;
- case 7:
- break;
- case 8:
- break;
- case 9:
- break;
- }
- }
- let init = function() {
- // 1. create the scene
- // ....
- scene = new THREE.Scene();
- scene.background = new THREE.Color(0xababab);
- // 2. create an locate the camera
- // ....
- camera = new THREE.PerspectiveCamera(80, window.innerWidth / window.innerHeight, 0.1, 2000);
- camera.position.z = 10;
- // 3. create and locate the objects on the scene
- // ...
- let help = new THREE.AxesHelper(5);
- scene.add(help);
- let createCube = function(pos, mat) {
- let geometry = new THREE.BoxGeometry(1, 1, 1);
- rubiks.push(new THREE.Mesh(geometry, mat.colors));
- rubiks[rubiks.length-1].position.x = pos.x;
- rubiks[rubiks.length-1].position.y = pos.y;
- rubiks[rubiks.length-1].position.z = pos.z;
- scene.add(rubiks[rubiks.length-1]);
- }
- let createRubiks = function(){
- let mat = new Materials(
- new THREE.MeshBasicMaterial({color:0xff0000}),
- new THREE.MeshBasicMaterial({color:0xff8900}),
- new THREE.MeshBasicMaterial({color:0x0000ff}),
- new THREE.MeshBasicMaterial({color:0x0b9f10}),
- new THREE.MeshBasicMaterial({color:0xffffff}),
- new THREE.MeshBasicMaterial({color:0xffff00})
- );
- /*
- let reset = new Materials(
- new THREE.MeshBasicMaterial({color:0x000000}),
- new THREE.MeshBasicMaterial({color:0x000000}),
- new THREE.MeshBasicMaterial({color:0x000000}),
- new THREE.MeshBasicMaterial({color:0x000000}),
- new THREE.MeshBasicMaterial({color:0x000000}),
- new THREE.MeshBasicMaterial({color:0x000000})
- );;
- let black = new THREE.MeshBasicMaterial({color:0x000000});
- */
- for(let i = 0; i<3; i++){
- for(let j = 0; j<3; j++){
- for(let k = 0; k<3; k++){
- createCube(new THREE.Vector3(i*1.02, j*1.02, k*1.02), mat);
- }
- }
- }
- rubiks.forEach((i, ind) => i.name = ind);
- /* for(let i = 0; i<3; i++){
- if(i==0) {
- mat.setRight(black);
- } else if (i==1) {
- mat.setRight(black);
- mat.setLeft(black);
- } else if (i==2) {
- mat.setLeft(black);
- }
- for(let j = 0; j<3; j++){
- if(j==0){
- mat.setTop(black);
- } else if(j==1) {
- mat.setTop(black);
- mat.setBot(black);
- } else if(j==2) {
- mat.setBot(black);
- }
- for(let k = 0; k<3; k++){
- if(k==0){
- mat.setFront(black);
- } else if (k==1) {
- mat.setFront(black);
- mat.setBack(black);
- } else if (k==2) {
- mat.setBack(black);
- }
- createCube(new THREE.Vector3(i*1.2, j*1.2, k*1.2), mat);
- mat = reset;
- }
- }
- } */ // marche pas : tout noir
- }
- createRubiks();
- // 4. create the renderer
- // ...
- renderer = new THREE.WebGLRenderer();
- renderer.setSize(window.innerWidth, window.innerHeight);
- document.body.appendChild(renderer.domElement);
- document.addEventListener("mousemove", function(e){
- if(cameraMovement){
- if(e.clientX > posX) {
- camera.position.x -= 0.1;
- }
- else if(e.clientX < posX){
- camera.position.x += 0.1;
- }
- if(e.clientY > posY) {
- camera.position.y += 0.1;
- }
- else if(e.clientY < posY){
- camera.position.y -= 0.1;
- }
- }
- posX = e.clientX;
- posY = e.clientY;
- });
- document.addEventListener("mousedown", function(e){
- ray.setFromCamera(mouse, camera);
- if(ray.intersectObjects(scene.children).length == 0){
- cameraMovement = true;
- } else {
- xDown = e.clientX;
- yDown = e.clientY;
- }
- });
- document.addEventListener("mouseup", function(e){
- if(cameraMovement){
- cameraMovement = false;
- } else {
- xUp = e.clientX;
- yUp = e.clientY;
- collision();
- }
- });
- window.addEventListener("mousemove", function(){
- mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
- mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
- });
- };
- // main animation loop - calls every 50-60 ms.
- let mainLoop = function() {
- renderer.render(scene, camera);
- requestAnimationFrame(mainLoop);
- };
- ///////////////////////////////////////////////
- init();
- mainLoop();
- </script>
Advertisement
Add Comment
Please, Sign In to add comment