# Import necessary modules# 导入必要的模块from directshowbaseShowBase import ShowBase # 导入ShowBase类from panda3dcore import WindowProperties # 导入WindowProperties类from directshowbaseShowBase import ShowBase
Import necessary modules
导入必要的模块
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 import math,sys from direct.gui.DirectGui import DirectButton
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
# Set up the camera
# 设置相机
self.disableMouse() # 禁用鼠标
self.camera.setPos(0, -10, 0) # 设置相机的位置
self.camera.lookAt(0, 0, 0) # 设置相机的朝向
#self.accept("mouse3", self.startDrag) # 监听鼠标左键按下事件
self.accept("mouse3", self.rightDrag) # 监听鼠标右键按下事件
self.isDragging = False # 初始化isDragging为False
self.lastPosition = None # 初始化lastPosition为None
self.taskMgr.add(self.rotateCameraTask, "rotateCameraTask") # 添加rotateCameraTask任务
self.lastPosition = self.win.getPointer(0) # 获取鼠标指针的位置
pyautogui.mouseUp(button='middle')
pyautogui.mouseUp(button='left')
pyautogui.mouseUp(button='right')
# 创建一个2D按钮
self.button = DirectButton(text="sb!", scale=0.1, command=self.buttonClicked)
self.button.setPos(0, 0, -0.5)
def buttonClicked(self):
print('点到我了')
sys.exit(0)
def rightDrag(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, x, y, z, juli, currentPosition):
# 设置相机的位置
self.camera_pos = Vec3(x, y, z)
# 计算鼠标指针在x轴上的移动距离
deltaX = self.lastPosition.getX() - currentPosition.getX()
# 计算鼠标指针在y轴上的移动距离
deltaY = self.lastPosition.getY() - currentPosition.getY()
# 计算相机相对原点的位置
cameraPos = self.camera.getPos() - self.camera_pos
# 计算相机距离原点的距离
cameraDist = cameraPos.length()
# 计算相机的方位角和俯仰角
azimuth = math.atan2(cameraPos.getY(), cameraPos.getX())
elevation = math.atan2(cameraPos.getZ(), math.sqrt(cameraPos.getX() ** 2 + cameraPos.getY() ** 2))
# 根据鼠标移动更新方位角和俯仰角
azimuth -= deltaX * 0.01
elevation += deltaY * 0.01
# 根据更新后的方位角和俯仰角计算新的相机位置
cameraPos.setX(cameraDist * math.cos(elevation) * math.cos(azimuth))
cameraPos.setY(cameraDist * math.cos(elevation) * math.sin(azimuth))
cameraPos.setZ(cameraDist * math.sin(elevation))
# 设置相机新的位置
self.camera.setPos(cameraPos + self.camera_pos)
# 计算相机当前位置到目标点的向量
targetPos = Point3(self.camera_pos) # 将x,y,z替换为目标点的坐标
lookAtVec = targetPos - self.camera.getPos()
# 计算相机与目标点之间的距离
distance = lookAtVec.length()
# 计算相机的俯仰角
new_elevation = math.atan2(cameraPos.getZ(), math.sqrt(cameraPos.getX() ** 2 + cameraPos.getY() ** 2))
# 如果相机的俯仰角小于90度,就将相机的位置设置为新的位置
if new_elevation < math.pi / 2:
cameraPos = targetPos - lookAtVec.normalized() * juli
self.camera.setPos(cameraPos)
# 使相机对准目标点
self.camera.lookAt(targetPos)
# 更新lastPosition的值
self.lastPosition = currentPosition
def SetCamera(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:
self.rotateCamera(0,0,0,50,currentPosition)
def rotateCameraTask(self, task):
self.SetCamera() # 调用rotateCamera方法
return task.cont # 返回task.cont
创建MyApp类的实例并运行应用程序
app = MyApp() # 创建MyApp类的实例 app.run() # 运行应用程
原文地址: https://www.cveoy.top/t/topic/ewOa 著作权归作者所有。请勿转载和采集!