【问题标题】:movement of the rotated camera relative to space旋转相机相对于空间的运动
【发布时间】:2021-07-28 17:56:56
【问题描述】:

我正在使用 react-3。 我想将相机绑定到玩家的位置并且我想旋转相机,但是相机沿着它的坐标系移动(旋转),我希望它的位置在世界坐标系中改变。我该怎么做?相机位置也取决于 fov。

export const Player = React.memo((props) => {

  const {camera} = useThree()
  const mesh = useRef()
  const [fov, setFov] = useState(100)

  const distance = 5;

  useFrame(() => {
    camera.near = (1 / camera.fov) * 10
    camera.far=10000
    let init_depht_s = Math.tan(75 / 2.0 * Math.PI / 180.0) * 2.0;
    let current_depht_s = Math.tan(camera.fov / 2.0 * Math.PI / 180.0) * 2.0;
    camera.position.set(
      (mesh.current.position.x + ((mesh.current.position.x - distance)*-1)) * init_depht_s / current_depht_s,
      (mesh.current.position.y + ((mesh.current.position.y - distance)*-1)) * init_depht_s / current_depht_s,
      (mesh.current.position.z + distance) * init_depht_s / current_depht_s
    )
    camera.fov = fov
    
    camera.lookAt(mesh.current.position)

    camera.updateMatrixWorld()
    camera.updateProjectionMatrix()
  })

  return <mesh
    {...props}
    ref={mesh}
    scale={1}
    rotation={new Euler((0).toRad(), (0).toRad(), (0).toRad())}
  >
    <boxGeometry args={[1, 1, 1]}/>
    <meshStandardMaterial color={`hsl(0,100%,50%)`}/>
  </mesh>
})

【问题讨论】:

    标签: three.js react-three-fiber


    【解决方案1】:

    看来它解决了我的问题

    ...
    const newLoc = new Vector3(
    mesh.current.position.x + distance * init_depht_s / current_depht_s,
    mesh.current.position.y + distance * init_depht_s / current_depht_s,
    mesh.current.position.z + distance * init_depht_s / current_depht_s
    )
    camera.position.copy(newLoc)
    camera.lookAt(mesh.current.position)
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多