我想在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? –