# Import necessary modules# 导入必要的模块import osfrom directshowbaseShowBase import ShowBase # 导入ShowBase类from panda3dcore import WindowProperties # 导入WindowProperties类from directshowbaseShowBase import
Import necessary modules
导入必要的模块
import os from direct.showbase.ShowBase import ShowBase # 导入ShowBase类 from panda3d.core import WindowProperties # 导入WindowProperties类 from direct.showbase.ShowBase import ShowBase # 导入ShowBase类 from panda3d.core import Vec3 # 导入Vec3类 from direct.task import Task # 导入Task类 from direct.actor.Actor import Actor # 导入Actor类 from panda3d.core import DirectionalLight # 导入DirectionalLight类 from panda3d.core import Point3 # 导入Point3类 from panda3d.core import WindowProperties # 导入WindowProperties类 from win32api import GetSystemMetrics import pyautogui
Create a class that inherits from ShowBase
创建一个继承自ShowBase的类
class MyApp(ShowBase): def init(self): ShowBase.init(self)
# 加载场景模型(场景树)
self.scene = self.loader.loadModel("models/environment") # 加载名为"models/environment"的模型
# 将render设为场景模型的父节点
self.scene.reparentTo(self.render) # 将场景模型的父节点设置为render
# 改变场景大小,并设置镜头初始位置
self.scene.setScale(0.25, 0.25, 0.25) # 缩小场景模型
self.scene.setPos(-8, 42, 0) # 设置场景模型的位置
# Disable the mouse cursor
# 禁用鼠标光标
#props = WindowProperties()
#props.setCursorHidden(True)
#self.win.requestProperties(props)
self.isDragging = False # 初始化isDragging为False
self.edge=False
self.camera_distance = 5 # 摄像机距离(x, y, z)点的距离
self.camera_target = Point3(0, 0, 0) # 摄像机的目标点
self.camera_theta = 0 # 摄像机的极角
self.camera_phi = 0 # 摄像机的方位角
self.update_camera() # 更新摄像机位置和朝向
# Set up the camera
# 设置相机
self.disableMouse() # 禁用鼠标
self.taskMgr.add(self.rotateCameraTask, "rotateCameraTask") # 添加rotateCameraTask任务
pyautogui.mouseUp(button='middle')
pyautogui.mouseUp(button='left')
pyautogui.mouseUp(button='right')
def stopDrag(self):
if self.isDragging == True:
try:
pyautogui.mouseUp(button='middle')
except:
pass
self.isDragging = False # 将isDragging设置为False
wp = WindowProperties() # 创建WindowProperties对象
#wp.setMouseMode(WindowProperties.M_relative)
wp.setCursorHidden(False) # 显示鼠标光标
self.win.requestProperties(wp) # 设置窗口属性
self.win.movePointer(0, self.win.getXSize() // 2, self.win.getYSize() // 2) # 将鼠标指针移动到窗口中心
else:
self.isDragging = True # 将isDragging设置为True
self.lastPosition = self.win.getPointer(0) # 获取鼠标指针的位置
#self.rotateCamera()
self.disableMouse() # 禁用鼠标
wp = WindowProperties() # 创建WindowProperties对象
#wp.setMouseMode(WindowProperties.M_relative)
wp.setCursorHidden(True) # 隐藏鼠标光标
#wp.setMouseEnclosed(True)
self.win.requestProperties(wp) # 设置窗口属性
#self.win.setMouseVisible(False)
pyautogui.mouseDown(button='middle')
def rotateCamera(self):
if self.isDragging:
currentPosition = self.win.getPointer(0) # 获取鼠标指针的位置
#print(self.camera.getHpr())
if currentPosition.getX() >= self.win.getXSize() - 2 or currentPosition.getX() <= 2:
print('碰到了窗口左或右边缘,,已将鼠标重新设置为窗口中心')
self.edge = True
elif currentPosition.getY() >= self.win.getYSize() - 2 or currentPosition.getY() <= 2:
print('碰到了窗口上或下边缘,已将鼠标重新设置为窗口中心')
self.edge = True
else:
self.edge=False
if self.edge:
self.win.movePointer(0, self.win.getXSize() // 2, self.win.getYSize() // 2)
self.lastPosition = self.win.getPointer(0) # 获取鼠标指针的位置
else:
deltaX = self.lastPosition.getX() - currentPosition.getX() # 计算鼠标指针在x轴上的移动距离
deltaY = self.lastPosition.getY() - currentPosition.getY() # 计算鼠标指针在y轴上的移动距离
self.camera_theta -= deltaY * 0.1 # 更新摄像机的极角
self.camera_phi += deltaX * 0.1 # 更新摄像机的方位角
if self.camera_theta < -85: # 避免摄像机翻转
self.camera_theta = -85
elif self.camera_theta > 85:
self.camera_theta = 85
self.update_camera() # 更新摄像机位置和朝向
self.lastPosition = currentPosition # 更新lastPosition的值
def rotateCameraTask(self, task):
self.rotateCamera() # 调用rotateCamera方法
return task.cont # 返回task.cont
def update_camera(self):
# 计算摄像机位置
x = self.camera_distance * math.sin(math.radians(self.camera_theta)) * math.cos(math.radians(self.camera_phi)) + self.camera_target.getX()
y = self.camera_distance * math.sin(math.radians(self.camera_theta)) * math.sin(math.radians(self.camera_phi)) + self.camera_target.getY()
z = self.camera_distance * math.cos(math.radians(self.camera_theta)) + self.camera_target.getZ()
# 计算摄像机朝向
self.camera.setPos(x, y, z)
self.camera.lookAt(self.camera_target)
创建MyApp类的实例并运行应用程序
app = MyApp() # 创建MyApp类的实例 app.run() # 运行应用程
原文地址: https://www.cveoy.top/t/topic/eIWW 著作权归作者所有。请勿转载和采集!