我想在y-z平面上围绕x轴旋转相机,同时查看(0, 0, 0)
点。事实证明lookAt
函数行为怪异。当旋转180°后,几何图形意外跳到另一侧。你能解释为什么发生这种情况,以及如何避免它?Three.js相机旋转问题
你可以看到的jsfiddle的现场演示:http://jsfiddle.net/ysmood/dryEa/
class Stage
constructor: ->
window.requestAnimationFrame =
window.requestAnimationFrame or
window.webkitRequestAnimationFrame or
window.mozRequestAnimationFrame
@init_scene()
@make_meshes()
init_scene: ->
@scene = new THREE.Scene
# Renderer
width = window.innerWidth;
height = window.innerHeight;
@renderer = new THREE.WebGLRenderer({
canvas: document.querySelector('.scene')
})
@renderer.setSize(width, height)
# Camera
@camera = new THREE.PerspectiveCamera(
45, # fov
width/height, # aspect
1, # near
1000 # far
)
@scene.add(@camera)
make_meshes: ->
size = 20
num = 1
geo = new THREE.CylinderGeometry(0, size, size)
material = new THREE.MeshNormalMaterial()
mesh = new THREE.Mesh(geo, material)
mesh.rotation.z = Math.PI/2
@scene.add(mesh)
draw: =>
angle = Date.now() * 0.001
radius = 100
@camera.position.set(
0,
radius * Math.cos(angle),
radius * Math.sin(angle)
)
@camera.lookAt(new THREE.Vector3())
@renderer.render(@scene, @camera)
requestAnimationFrame(@draw)
stage = new Stage
stage.draw()
您可能碰到了Gimbal Lock问题(您可以在网上搜索)。解决方法是使用四元数旋转(也可研究)。请注意,最新的Three.js,r59现在默认使用四元数,并且在Object3D中有新的旋转方法可以帮助(rotateX(),rotateAroundAxis()和朋友)。 – yaku
你确定这是一个万向节锁定问题吗?问题是我不知道'lookAt'函数的机制。为什么团队不在'lookAt'函数中使用Quaternion? –