使用设备的方向绕原点旋转

时间:2016-01-26 20:16:09

标签: javascript three.js google-cardboard

我试图用three.js复制Google的Cardboard Demo“Exhibit”的功能。我直接从Chrome Experiments web page获取了起始示例,并在代码中删除了init方法中绘制一个简单的三角形金字塔:

function init() {
  renderer = new THREE.WebGLRenderer();
  element = renderer.domElement;
  container = document.getElementById('example');
  container.appendChild(element);

  effect = new THREE.StereoEffect(renderer);

  scene = new THREE.Scene();

  camera = new THREE.PerspectiveCamera(90, 1, 0.001, 700);
  camera.position.set(0, 0, 50);
  scene.add(camera);

  controls = new THREE.OrbitControls(camera, element);
  controls.rotateUp(Math.PI / 4);
  controls.noZoom = true;
  controls.noPan = true;

  var geometry = new THREE.CylinderGeometry( 0, 10, 30, 4, 1 );
  var material = new THREE.MeshPhongMaterial( { color:0xffffff, shading: THREE.FlatShading } );

  var mesh = new THREE.Mesh( geometry, material );
  mesh.updateMatrix();
  mesh.matrixAutoUpdate = false;
  scene.add( mesh );

  var 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 );

  function setOrientationControls(e) {
    if (!e.alpha) {
      return;
    }

    controls = new THREE.DeviceOrientationControls(camera);
    controls.connect();
    controls.update();

    element.addEventListener('click', fullscreen, false);

    window.removeEventListener('deviceorientation', setOrientationControls, true);
  }
  window.addEventListener('deviceorientation', setOrientationControls, true);

  window.addEventListener('resize', resize, false);
  setTimeout(resize, 1);
}

桌面上的OrbitControls方法运行正常:通过鼠标拖动,屏幕绕金字塔旋转。然而,在使用DeviceOrientationControls的移动设备上,此效果完全丢失,而相机在(0,0,0)处自由移动。我尝试将previous question suggested作为camera替换为scene,以便:{/ p>

controls = new THREE.DeviceOrientationControls(scene);

然而,这根本不起作用,旋转设备时没有任何动作。通过OrbitControls捕获的动议,我需要更改以复制DeviceOrientationControls行为?

2 个答案:

答案 0 :(得分:5)

要创建一个设备定位轨道控制器,就像您在此演示中看到的那样http://novak.us/labs/UmDemo/;它涉及修改现有的OrbitControls.js。

可以在github:https://github.com/snovak/three.js/commit/f6542ab3d95b1c746ab4d39ab5d3253720830dd3

上的此提交中看到文件更改

我一直想要做几个月的拉动请求。只是没有得到它。需要一堆清理。

你可以在这里下载我修改过的OrbitControls.js(我几个月没有合并,结果可能会有所不同):https://raw.githubusercontent.com/snovak/three.js/master/examples/js/controls/OrbitControls.js

以下是如何在自己的脚本中实现修改后的OrbitControl:

this.controls = new THREE.OrbitControls( camera, document.getElementById('screen') ) ;

            controls.tiltEnabled = true ;  // default is false.  You need to turn this on to control with the gyro sensor.

            controls.minPolarAngle = Math.PI * 0.4; // radians
            controls.maxPolarAngle = Math.PI * 0.6; // radians
            controls.noZoom = true ;

            // How far you can rotate on the horizontal axis, upper and lower limits.
            // If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
            controls.minAzimuthAngle = - Math.PI * 0.1; // radians
            controls.maxAzimuthAngle = Math.PI * 0.1; // radians

            this.UMLogo = scene.children[1];
            controls.target = UMLogo.position;

我希望能让你成为你想去的地方! : - )

答案 1 :(得分:1)

使用DeviceOrientationControls您必须在动画循环期间调用controls.update(),否则控件将不会根据设备信息更新其位置。