0
votes

I am using threejs to render a cube. Currently I achieved a rotating cube with different textures. Now I am trying to make the cube to rotate using the mouse. In my case now, the cube is rotating:

function animate() {
    mesh.rotation.x += .015;
    mesh.rotation.y += .015;
    mesh.rotation.y += .015;

    render();
    requestAnimationFrame( animate );
}

Now I have two options:

  1. I rotate the camera while moving and holding the mouse
  2. I rotate the cube whilte holding/moving the mouse.

I am also aiming to have two states. One, in which the cube is rotating automatically and one, in which the cube rotates by draging the mouse. I tried already OrbitControler, but it didn't work out. Is there any simple solution to just have a floating rotating cube using the mouse?

My code:

THREE.JS

var camera;
var scene;
var renderer;
var mesh;
var geometry;
var material1;
var material2;
var material3;
var material4;
var material5;
var material6;
var materials;
var meshFaceMaterial;



init();
animate();

function init() {

    scene = new THREE.Scene();
    camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1000);

    var light = new THREE.DirectionalLight( 0xffffff );
    light.position.set( 0, 1, 1 ).normalize();
    scene.add(light);

    geometry = new THREE.CubeGeometry( 10, 10, 10);
    material1 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub1.jpg') } );
    material2 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub2.jpg') } );
    material3 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub3.jpg') } );
    material4 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub4.jpg') } );
    material5 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub5.jpg') } );
    material6 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub6.jpg') } );

    materials = [material1, material3, material5, material6, material4, material2];

    meshFaceMaterial = new THREE.MeshFaceMaterial( materials );

    mesh = new THREE.Mesh(geometry, meshFaceMaterial );
    mesh.position.z = -50;
    scene.add( mesh );

    renderer = new THREE.WebGLRenderer();
    renderer.setSize( window.innerWidth, window.innerHeight );
    document.getElementById('render').appendChild( renderer.domElement );   
    window.addEventListener( 'resize', onWindowResize, false );

    render();
}

function animate() {
    mesh.rotation.x += .015;
    mesh.rotation.y += .015;
    mesh.rotation.y += .015;

    render();
    requestAnimationFrame( animate );
}

function render() {
    renderer.render( scene, camera );
}

function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize( window.innerWidth, window.innerHeight );
    render();
}
1

1 Answers

2
votes

Look at the code: https://codepen.io/kranzy/pen/pEjGqV

<script src="https://dl.dropboxusercontent.com/u/3587259/Code/Threejs/three.min.js"></script>
<script src="https://dl.dropboxusercontent.com/u/3587259/Code/Threejs/OrbitControls.js"></script>

<div id="container"></div> 
<script>
var container, stats;

var camera, controls, scene, renderer;

var cross;

init();
animate();

function init() {

  camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  camera.position.z = 10;

  controls = new THREE.OrbitControls( camera );
  controls.addEventListener( 'change', render );

  scene = new THREE.Scene();
  // world

    geometry = new THREE.CubeGeometry( 10, 10, 10);
  /*
    material1 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub1.jpg') } );
    material2 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub2.jpg') } );
    material3 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub3.jpg') } );
    material4 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub4.jpg') } );
    material5 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub5.jpg') } );
    material6 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub6.jpg') } );

    materials = [material1, material3, material5, material6, material4, material2];
*/
    meshFaceMaterial = /*new THREE.MeshFaceMaterial( materials )*/ new THREE.MeshPhongMaterial({color: 0xff00ff});

    mesh = new THREE.Mesh(geometry, meshFaceMaterial );
    mesh.position.z = 0;
    scene.add( mesh );


  // lights

  light = new THREE.DirectionalLight( 0xffffff );
  light.position.set( 1, 1, 1 );
  scene.add( light );

  light = new THREE.DirectionalLight( 0x002288 );
  light.position.set( -1, -1, -1 );
  scene.add( light );

  light = new THREE.AmbientLight( 0x222222 );
  scene.add( light );


  // renderer

  renderer = new THREE.WebGLRenderer( { antialias: false } );
  renderer.setSize( window.innerWidth, window.innerHeight );

  container = document.getElementById( 'container' );
  container.appendChild( renderer.domElement );

  window.addEventListener( 'resize', onWindowResize, false );

}

function onWindowResize() {

  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();

  renderer.setSize( window.innerWidth, window.innerHeight );

  render();

}

function animate() {

  requestAnimationFrame( animate );
  controls.update();

}

function render() {
  renderer.render( scene, camera );
}
</script>

You need to set camera.position.z larger than zero. That's why you can't use OrbitControls.