首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在PerspectiveCamera和PointerLockControls中使用指针锁

无法在PerspectiveCamera和PointerLockControls中使用指针锁
EN

Stack Overflow用户
提问于 2021-12-08 14:52:28
回答 1查看 539关注 0票数 0

我喜欢“反应”

我增加了PerspectiveCamera来改变相机的位置,但是现在PointerLockControls不再工作了,我不明白为什么和如何处理它。

谢谢

PerspectiveCamera用法

代码语言:javascript
复制
export const Player = (props) => {
  const axe = useRef()
  const [ref, api] = useSphere(() => ({ mass: 1, type: "Dynamic", position: [0, 10, 0], ...props }))
  const { forward, backward, left, right, jump } = usePlayerControls()
  const { camera } = useThree()
  const velocity = useRef([0, 0, 0])
  useEffect(() => api.velocity.subscribe((v) => (velocity.current = v)), [])
  useFrame((state) => {
    ref.current.getWorldPosition(camera.position)
    frontVector.set(0, 0, Number(backward) - Number(forward))
    sideVector.set(Number(left) - Number(right), 0, 0)
    direction.subVectors(frontVector, sideVector).normalize().multiplyScalar(SPEED).applyEuler(camera.rotation)
    speed.fromArray(velocity.current)
    axe.current.children[0].rotation.x = THREE.MathUtils.lerp(
      axe.current.children[0].rotation.x,
      Math.sin((speed.length() > 1) * state.clock.elapsedTime * 10) / 20,
      0.01,
    )
    axe.current.rotation.copy(camera.rotation)
    axe.current.position.copy(camera.position).add(camera.getWorldDirection(rotation).multiplyScalar(1))

    api.velocity.set(direction.x, velocity.current[1], direction.z)
    if (jump && Math.abs(velocity.current[1].toFixed(2)) < 0.05) api.velocity.set(velocity.current[0], 10, velocity.current[2])
  })
  return (
    <>
      <mesh ref={ref} />
      <group ref={axe} onPointerMissed={(e) => (axe.current.children[0].rotation.x = -0.5)}>
        <group position={[0, 7, 0]}>
          <PerspectiveCamera makeDefault={true} fov={75}>
            <Body position={[0, -4, 1]} />
          </PerspectiveCamera>
        </group>
      </group>
    </>
  )
}

PointerLockControls用法

代码语言:javascript
复制
export default function App() {
  return (
    <Canvas
      shadows
      gl={{ alpha: false }}
      camera={{ fov: 45 }}
      raycaster={{
        computeOffsets: (e) => ({ offsetX: e.target.width / 2, offsetY: e.target.height / 2 }),
      }}>
      <Sky sunPosition={[100, 20, 100]} />
      <ambientLight intensity={0.3} />
      <pointLight castShadow intensity={0.8} position={[100, 100, 100]} />
      <Physics gravity={[0, -30, 0]}>
        <Ground />
        <Player />
        <Reflector
          blur={[0, 0]} // Blur ground reflections (width, heigt), 0 skips blur
          mixBlur={0.1} // How much blur mixes with surface roughness
          mixStrength={0.25} // Strength of the reflections
          resolution={1024} // Off-buffer resolution, lower=faster, higher=better quality
          args={[10, 30]} // PlaneBufferGeometry arguments
          position={[0, 0, -20]}
          rotation={[0, 0, 2 * Math.PI]}
          mirror={0.5} // Mirror environment, 0 = texture colors, 1 = pick up env colors
          minDepthThreshold={0.25}
          maxDepthThreshold={1}
          depthScale={50}>
          {(Material, props) => <Material metalness={0} roughness={0} {...props} />}
        </Reflector>
      </Physics>
      <PointerLockControls />
    </Canvas>
  )
}

浏览器:Firefox94.0.2(64位),macOS蒙特雷

“依赖关系”:{“@pmndrs/品牌化”:"0.0.8“、”@drei 3/cannon“:"4.0.1”、“@react-3/drei”:"7.17.2“、”@react三/纤维“:"7.0.17”、“@type/3”:"0.133.1“、"react":”17.0.2“、"react-dom":"17.0.2",“反应-脚本”:"4.0.3",“单纯噪声”:"3.0.0",“三”:"0.133.1",“作站”:"3.6.1“},

https://codesandbox.io/s/fpv-player-ers63

EN

回答 1

Stack Overflow用户

发布于 2021-12-08 18:38:08

嗯,我发现在useFrame更新https://codesandbox.io/s/fpv-player-ers63中偏移相机就足够了。

代码语言:javascript
复制
export const Player = (props) => {
  const axe = useRef()
  const [ref, api] = useSphere(() => ({ args: [1], mass: 1, type: "Dynamic", position: [0, 10, 0], ...props }))
  const { forward, backward, left, right, jump } = usePlayerControls()
  const { camera } = useThree()
  const velocity = useRef([0, 0, 0])
  useEffect(() => api.velocity.subscribe((v) => (velocity.current = v)), [])
  useFrame((state) => {
    ref.current.getWorldPosition(camera.position)
    frontVector.set(0, 0, Number(backward) - Number(forward))
    sideVector.set(Number(left) - Number(right), 0, 0)
    direction.subVectors(frontVector, sideVector).normalize().multiplyScalar(SPEED).applyEuler(camera.rotation)
    speed.fromArray(velocity.current)
    axe.current.children[0].rotation.x = THREE.MathUtils.lerp(
      axe.current.children[0].rotation.x,
      Math.sin((speed.length() > 1) * state.clock.elapsedTime * 10) / 10,
      0.1,
    )
    axe.current.rotation.copy(camera.rotation)
    axe.current.position.copy(camera.position).add(camera.getWorldDirection(rotation).multiplyScalar(1))
    camera.position.setY(camera.position.y + 8)
    api.velocity.set(direction.x, velocity.current[1], direction.z)
    if (jump && Math.abs(velocity.current[1].toFixed(2)) < 0.05) api.velocity.set(velocity.current[0], 10, velocity.current[2])
  })
  return (
    <>
      <mesh ref={ref} />
      <group ref={axe} onPointerMissed={(e) => (axe.current.children[0].rotation.x = -0.5)}>
        <Body position={[0, 4, 0]} />
      </group>
    </>
  )
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70277007

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档