Three.js:使用鼠标旋转立方体

时间:2016-09-11 23:33:53

标签: 3d three.js rotation mouseevent

我使用threejs渲染一个立方体。目前我实现了具有不同纹理的旋转立方体。现在我正在尝试使用鼠标旋转立方体。在我的情况下,立方体正在旋转:

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

    render();
    requestAnimationFrame( animate );
}

现在我有两个选择:

  1. 我在移动并按住鼠标的同时旋转相机
  2. 我旋转立方体,握住/移动鼠标。
  3. 我的目标也是两个州。一个是立方体自动旋转,另一个是立方体通过拖动鼠标旋转。 我已经尝试过OrbitControler,但它没有成功。有没有简单的解决方案只使用鼠标浮动旋转立方体?

    我的代码:

    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 个答案:

答案 0 :(得分:1)

查看代码: 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>

您需要将camera.position.z设置为大于零。这就是你不能使用OrbitControls的原因。