Javscript - Three.js 禁用移动设备上的平移?

发布于 2025-01-11 22:58:54 字数 837 浏览 0 评论 0原文

所以我正在使用 Three.js 制作一个 3D 项目。在我的笔记本电脑上一切都按预期工作。我使用 OrbitControls 来允许相机移动,但我禁用了右键平移,因为我希望相机仅旋转。当切换到移动设备(iPhone)时,我可以使用两根手指移动相机(而不是旋转)。有没有办法在移动设备上禁用此行为? 我的 OrbitControls:

this.controls = new OrbitControls(this.camera, this.renderer.domElement)
this.controls.enableDamping = true
this.controls.maxPolarAngle = Math.PI * 0.45
this.controls.mouseButtons = {
  LEFT: THREE.MOUSE.ROTATE,
  MIDDLE: THREE.MOUSE.DOLLY,
  RIGHT: ''
}

更新功能:

_RAF() {
    requestAnimationFrame(() => {
      this.water.material.uniforms[ 'time' ].value += 1.0 / 60.0
      this.controls.maxDistance = 10000.0
      this.controls.minDistance = 10.0
      this.controls.update()
      this.camera.updateProjectionMatrix()
      this.renderer.render(this.scene, this.camera)
      this._RAF()
    })
  }

So I'm making a 3d project with three.js. Everything works as expected on my laptop. I'm using OrbitControls to allow camera movement, but i have disabled right click panning, because I want the camera just to rotate. When switching to a mobile device (iphone), using two fingers I'm able to move the camera (not rotate). Is there a way to disable this behaviour on mobile devices?
My OrbitControls:

this.controls = new OrbitControls(this.camera, this.renderer.domElement)
this.controls.enableDamping = true
this.controls.maxPolarAngle = Math.PI * 0.45
this.controls.mouseButtons = {
  LEFT: THREE.MOUSE.ROTATE,
  MIDDLE: THREE.MOUSE.DOLLY,
  RIGHT: ''
}

Update function:

_RAF() {
    requestAnimationFrame(() => {
      this.water.material.uniforms[ 'time' ].value += 1.0 / 60.0
      this.controls.maxDistance = 10000.0
      this.controls.minDistance = 10.0
      this.controls.update()
      this.camera.updateProjectionMatrix()
      this.renderer.render(this.scene, this.camera)
      this._RAF()
    })
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤芳又自赏 2025-01-18 22:58:54

您似乎没有禁用平移,而只是更改控件使用的鼠标操作。

要禁用/启用平移,您应该使用 enablePan

现在,您只想在移动设备中禁用平移,因此我们可以选择一个断点来有条件地启用/禁用平移:

this.controls.updatePanning = () => {
  const minWidth = 780 // Your min breakpoint for enabling pan.
  if (window.innerWidth >= minWidth ) {
    this.controls.enablePan = true
  } else {
    this.controls.enablePan = false
  }
}

然后,在您的更新循环中,只需调用上述函数:

_RAF() {
    requestAnimationFrame(() => {
      this.water.material.uniforms[ 'time' ].value += 1.0 / 60.0
      this.controls.maxDistance = 10000.0
      this.controls.minDistance = 10.0
      this.controls.updatePanning()
      this.controls.update()
      this.camera.updateProjectionMatrix()
      this.renderer.render(this.scene, this.camera)
      this._RAF()
    })
  }

您可以这样做在几个方面。例如:您只能执行此函数一次,而不是每一帧。

It seems that you're not disabling panning, but just changing the mouse action used by controls.

In order to disable/enable panning, you should use enablePan.

Now, you only want to disable panning in mobile, so we could pick a break-point for conditionally enable/disable panning:

this.controls.updatePanning = () => {
  const minWidth = 780 // Your min breakpoint for enabling pan.
  if (window.innerWidth >= minWidth ) {
    this.controls.enablePan = true
  } else {
    this.controls.enablePan = false
  }
}

Then, in your update loop, just invoke the above function:

_RAF() {
    requestAnimationFrame(() => {
      this.water.material.uniforms[ 'time' ].value += 1.0 / 60.0
      this.controls.maxDistance = 10000.0
      this.controls.minDistance = 10.0
      this.controls.updatePanning()
      this.controls.update()
      this.camera.updateProjectionMatrix()
      this.renderer.render(this.scene, this.camera)
      this._RAF()
    })
  }

You could approach this in several ways. For example: You could only execute this function once and not every frame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文