Panda3D: 导入必要的模块并创建基本场景
导入必要的模块
from direct.showbase.ShowBase import ShowBase # 导入ShowBase类
from panda3d.core import WindowProperties # 导入WindowProperties类
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
# 创建一个继承自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) # 设置场景模型的位置
# 禁用鼠标光标
#props = WindowProperties()
#props.setCursorHidden(True)
#self.win.requestProperties(props)
self.isDragging = False # 初始化isDragging为False
self.edge=False
# 设置相机
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, targetPos):
# 获取鼠标指针在窗口中心位置时摄像机的位置和朝向
camPos = self.camera.getPos()
camHpr = self.camera.getHpr()
# 计算摄像机的新位置和朝向
deltaX = self.lastPosition.getX() - currentPosition.getX()
deltaY = self.lastPosition.getY() - currentPosition.getY()
camX = x + juli * math.sin(math.radians(camHpr.getX() + deltaX * 0.3)) * math.cos(math.radians(camHpr.getY() + deltaY * 0.3))
camY = y - juli * math.cos(math.radians(camHpr.getX() + deltaX * 0.3)) * math.cos(math.radians(camHpr.getY() + deltaY * 0.3))
camZ = z + juli * math.sin(math.radians(camHpr.getY() + deltaY * 0.3))
camH = camHpr.getX() + deltaX * 0.3
camP = camHpr.getY() + deltaY * 0.3
# 设置摄像机的新位置和朝向
self.camera.setPos(camX, camY, camZ)
self.camera.setHpr(camH, camP, 0)
self.camera.lookAt(Point3(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:
targetPos = Vec3(0, 0, 0) # 设置目标点的位置为场景中心
self.rotateCamera(0, 0, 0, 50, currentPosition, targetPos) # 将目标点的位置作为参数传递给rotateCamera函数
# 更新lastPosition的值
self.lastPosition = 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/nXU7 著作权归作者所有。请勿转载和采集!