繁体   English   中英

使用 LibGDX 的 3d 错觉的偏心相机投影

[英]Off-center camera projection for 3d illusion using LibGDX

我正在尝试在 Android 上使用 LibGDX 来实现这种 3d 弹出屏幕的效果(给定链接中描述的相机过程):

https://www.anxious-bored.com/blog/

但是当眼睛四处移动时,我得到的结果是拉伸的物体。

我使用陀螺仪模拟眼睛 position 以获取设备旋转并假设与设备屏幕的距离固定。 我还结合了 ARCore 用于眼动追踪,但这也产生了相同的结果。 这是我所看到的 GIF:

https://i.stack.imgur.com/YOf6n.gif

有人知道我做错了什么吗? 这是我在 Kotlin 中使用的相关代码。

private fun updateCamera() {
        val camera = // The Perspective camera instance

        // Move the camera to the eye position, but keep the same camera direction
        camera.view.setToLookAt(camera.direction, camera.up).mul(GDXHelperInstances.matrix4_1.setToTranslation(GDXHelperInstances.vector3_1.set(camera.position).scl(-1f)))

        // Get the size of the screen in meters
        val deviceHalfHeight = camera.viewportHeight / Gdx.graphics.ppcY * 0.01f
        val deviceHalfWidth = camera.viewportWidth / Gdx.graphics.ppcX * 0.01f

        // Get the device position and plane relative to the camera
        val pos = Vector3(Vector3.Zero).mul(camera.view)
        val plane = Plane(GDXHelperInstances.vector3_1.set(camera.direction).scl(-1f), Vector3.Zero)

        // Calculate the bounds of the viewport in virtual space (the device screen dimensions in meters with center at Vector3.Zero) relative to the camera (view space)
        val left = pos.x - deviceHalfWidth
        val right = pos.x + deviceHalfWidth
        val bottom = pos.y - deviceHalfHeight
        val top = pos.y + deviceHalfHeight
        val nearScale = camera.near / plane.distance(camera.position).absoluteValue

        // Off-axis projection
        camera.projection.setToProjection(left*nearScale, right*nearScale, bottom*nearScale, top*nearScale, camera.near, camera.far)

        // Calculate new asymmetrical frustum
        camera.combined.set(camera.projection)
        Matrix4.mul(camera.combined.`val`, camera.view.`val`)
        camera.invProjectionView.set(camera.combined)
        Matrix4.inv(camera.invProjectionView.`val`)
        camera.frustum.update(camera.invProjectionView)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM