【问题标题】:Three.js camera rotation issueThree.js 相机旋转问题
【发布时间】:2013-07-15 19:36:42
【问题描述】:

我想在查看(0, 0, 0) 点的同时,在 y-z 平面上围绕 x 轴旋转相机。原来lookAt 函数的行为很奇怪。旋转 180° 后,几何图形意外跳到另一侧。您能否解释一下为什么会发生这种情况,以及如何避免这种情况?

您可以在 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() 和朋友)。
  • 您确定这是云台锁定问题吗?问题是我不知道lookAt 函数的机制。为什么团队不在lookAt 函数中使用四元数?

标签: coffeescript three.js webgl


【解决方案1】:

您正在围绕 Y-Z 平面中的 X 轴旋转相机。当相机经过“北极”和“南极”时,它会翻转以保持正面朝上。相机的上矢量默认为(0, 1, 0)。

将相机的 x 位置设置为 100,这样它的行为对您来说是正确的。将一些轴添加到您的演示中以作为参考框架。

这不是图书馆的错。看看Camera.lookAt() 源代码。

如果您想通过四元数设置相机方向,您可以这样做。

three.js r.59

【讨论】:

  • 我阅读了源代码。谢谢,它有帮助。 three.js 的文档误导了我的想法。
  • 我给了x位置的偏移量,但是出现了同样的问题jsfiddle.net/ysmood/dryEa
  • 为了帮助您了解正在发生的事情,请将相机偏移设置为 200,然后是 50,然后是 20。一切正常 - 相机正在避免倒置旋转。如果您不在乎相机是否倒置,请使用TrackballControls 来控制您的相机。 threejs.org/examples/misc_controls_trackball.html
猜你喜欢
  • 2021-08-23
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 2012-02-15
  • 1970-01-01
  • 1970-01-01
  • 2013-09-20
相关资源
最近更新 更多