我目前正在使用AngularJS和Three.js尝试开发一个小型虚拟现实应用的示例。 我根据用户代理是否为移动设备定义了控件,这是一种不太可靠的方法,但只是一个例子。 在非移动设备上使用OrbitControls,在其他情况下使用DeviceOrientationControls。
var controls = new THREE.OrbitControls(camera, game.renderer.domElement);
controls.noPan = true;
controls.noZoom = true;
controls.target.set(
camera.position.x,
camera.position.y,
camera.position.z
);
// Really dodgy method of checking if we're on mobile or not
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
controls = new THREE.DeviceOrientationControls(camera, true);
controls.connect();
controls.update();
}
return controls;
我还创建了一些对象来实际显示。
this.camera = new THREE.PerspectiveCamera(90, window.innerWidth / window.innerHeight, 0.001, 1000);
this.camera.position.set(0, 15, 0);
this.textGeometry = new THREE.TextGeometry("Hello World", { size: 5, height: 1 });
this.textMesh = new THREE.Mesh(this.textGeometry, new THREE.MeshBasicMaterial({
color: 0xFF0000, opacity: 1
}));
this.textMesh.position.set(-20, 0, -20);
this.light = new THREE.SpotLight(0x999999, 3, 300);
this.light.position.set(50, 50, 50);
this.floorGeometry = new THREE.PlaneBufferGeometry(1000, 1000);
this.floorTexture = THREE.ImageUtils.loadTexture('img/textures/wood.jpg');
this.floorTexture.wrapS = THREE.RepeatWrapping;
this.floorTexture.wrapT = THREE.RepeatWrapping;
this.floorTexture.repeat = new THREE.Vector2(50, 50);
this.floorTexture.anisotropy = this.game.renderer.getMaxAnisotropy();
this.floorMaterial = new THREE.MeshPhongMaterial({
color: 0xffffff,
specular: 0xffffff,
shininess: 20,
shading: THREE.FlatShading,
map: this.floorTexture
});
this.floor = new THREE.Mesh(this.floorGeometry, this.floorMaterial);
this.floor.rotation.x = -Math.PI / 2;
this.scene.add(this.textMesh);
this.scene.add(this.light);
this.scene.add(this.floor);
this.scene.add(this.camera);
在Mac OS的Chrome、Mac OS的Safari和iPad上的Safari上(包括适当的设备方向控制),这个功能运行良好。
问题出现在Android的Chrome上运行应用程序时。场景中添加的聚光灯将始终指向与相机相同的方向。以下是Android上运行应用程序的屏幕截图:
在我尝试使用的所有其他浏览器上,光源位置正确地位于(50, 50, 50),并保持静态。在上面的例子中,光源被渲染在相机的中心,并且在移动或旋转相机时继续跟随相机。实际的方向控制工作正常。
这只发生在一个浏览器中的事实让我非常头疼,因为演示需要在Android的Chrome上运行。
谢谢。
更新:尝试了许多不同的解决方案,从不同的控制方法到不同类型的照明都无济于事。