Python 3D 鼠标控制相机旋转应用程序
Python程序:实现鼠标控制相机旋转的 3D 应用程序
from panda3d.core import WindowProperties
from direct.showbase.ShowBase import ShowBase
class MyApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
# 隐藏鼠标光标
props = WindowProperties()
props.setCursorHidden(True)
self.win.requestProperties(props)
# 设置鼠标控制
self.accept('mouse1', self.toggleMouseControl)
self.mouseControlEnabled = False
def toggleMouseControl(self):
self.mouseControlEnabled = not self.mouseControlEnabled
props = WindowProperties()
props.setPointerHidden(self.mouseControlEnabled)
self.win.requestProperties(props)
def update(self):
if self.mouseControlEnabled:
# 获取鼠标移动的相对位置
md = self.win.getPointer(0)
x = md.getX()
y = md.getY()
# 计算相机的旋转角度
self.camera.setH(self.camera.getH() - x * 0.2)
self.camera.setP(self.camera.getP() - y * 0.2)
# 继续更新场景
return task.cont
app = MyApp()
app.run()
原文地址: https://www.cveoy.top/t/topic/pgVX 著作权归作者所有。请勿转载和采集!